Based on another SO question titled "Problem with overriding “operator<<” in class derived from “ostream”" I can achieve my aims.
- As Johannes Schaub - litb suggested on the link above, I don't use shift operator as member function, I will use it as free functions. Therefore I have to define a new member of the class that stores whether the file was opened in binary mode or not.
- The move semantics seems to be necessary. Without that, the string, char and cstring outputs not working correctly (at least, working differently than expected :) ).
- UncleBens' answer with the tweaks looks to be good.
First, define the class:
class mixstream:public fstream
{
bool binmode;
public:
//constructors
explicit mixstream() {};
explicit mixstream ( const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out) :
fstream(filename, mode)
{
if (mode & ios_base::binary)
binmode = true;
else
binmode = false;
};
void open(const char *_Filename, ios_base::openmode _Mode = ios_base::in | ios_base::out, int _Prot = (int)ios_base::_Openprot)
{
fstream::open (_Filename, _Mode, _Prot);
if (_Mode & ios_base::binary)
binmode = true;
else
binmode = false;
}
bool get_binmode() const {return binmode;}
}
And then define the overloaded insertation and extractation operator:
template <class T> mixstream& operator<< (mixstream& stream, const T&& param)
{
if (stream.get_binmode())
stream.write((char *) ¶m, sizeof(param));
else
(fstream&)stream << param;
return stream;
}
template <class T> mixstream& operator>> (mixstream& stream, const T&& param)
{
if (stream.get_binmode())
read((char *) ¶m, sizeof(param));
else
ostream::operator>>param;
return *this;
}
So then I can use my new stream in a form:
int main(int argc, char *argv[]) {
mixstream bin_or_not;
if (true) //some condition
bin_or_not.open("file.dat",ios_base::out | ios_base::binary);
else
bin_or_not.open("file.dat",ios_base::out);
char testcs[] = "testsc";
string tests("tests");
int testn = 10;
bin_or_not << 10 << testn << "testcs" << testcs << tests;
return 0;
}
Comments and answwers are still welcomed as suggestions for improvement.