Streams
- Dettagli
- Categoria: TSL
- Pubblicato Venerdì, 23 Novembre 2012 00:29
- Scritto da Giampiero Gabbiani
- Visite: 2309
As shown in the Archives article, both the input and output archive wrap a reference to a corresponding c++ standard stream. The output archives use an underlying std::ostream, the input archives a std::istream.
Since the input archives derived from tsl::itext_archive are implemented through boost::spirit, the underlying std::istream should have the ios::skipws bit unset, if not, a std::runtime_error is thrown at the very first TSL usage. TSL also uses std::exception in all its code, so it is a good practice to enable them inside the standard c++ IO streams as well. Below the code in order to properly use a stream for [de]serialization under TSL:
ifstream stream; stream.exceptions(std::ifstream::failbit|std::ifstream::badbit); stream.open("file_name"); stream.unsetf(ios::skipws);
The most recent version of TSL introduces a template wrapper for c++ standard stream so that the previous code becomes the following:
tsl::ISWrapper stream("file_name");
Considering an output stream instead:
ofstream stream; stream.exceptions(std::ifstream::failbit|std::ifstream::badbit); stream.open("output_file");
with the usage of the OSWrapper the code becomes:
tsl::OSWrapper stream("output_file_name");