For binary reading and writing to files, the type must either have the following methods implemented:
-
bool readB(std::ifstream& _input_file)
-
void writeB(std::ofstream& _output_file) const
or have the following template functions specialized;
-
template bool slv::rw::readB(T& _value, std::ifstream& _input_file);
-
template void slv::rw::writeB(const T& _value, std::ofstream& _output_file);
Specializations for common types and containers are provided at this page.
class ClassA {
private:
float value1 = 20;
std::string value2 = "foo";
public:
ClassA() {}
bool readB(std::ifstream& _input_file) {
bool l_read = slv::rw::readB(value1, _input_file);
if (l_read) l_read = slv::rw::readB(value2, _input_file);
return l_read;
}
void writeB(std::ofstream& _output_file) const {
slv::rw::writeB(value1, _output_file);
slv::rw::writeB(value2, _output_file);
}
};
To write and read the instance:
ClassA classA;
SlvFileMgr::write_binary(classA, "classA");//write to file ./classA
SlvFileMgr::read_binary(classA, "classA");//read from file ./classA
The instance is written/read to the file. Then the file is closed. i.e. the file only contains classA
output.
More details can be found in sample007_0.cpp.
The read
and write
methods being public, they can be called to provide instance read/write independently from file opening/closing. See this example.