26 #ifndef DATATOOLS_IO_FACTORY_H 27 #define DATATOOLS_IO_FACTORY_H 45 #pragma clang diagnostic push 46 #pragma clang diagnostic ignored "-Wshadow" 49 #pragma GCC diagnostic push 50 #pragma GCC diagnostic ignored "-Wshadow" 52 #include <boost/iostreams/filtering_stream.hpp> 54 #pragma GCC diagnostic pop 57 #pragma clang diagnostic pop 88 #include <boost/serialization/string.hpp> 231 void init_read_archive();
233 void init_read(
const std::string & stream_name_);
235 void reset_read_archive();
239 void init_write_archive();
241 void init_write(
const std::string & stream_name_);
243 void reset_write_archive();
247 void init(
const std::string& stream_name_,
int mode_);
249 void ctor_defaults();
255 template <
typename Data>
260 "Not a writer factory!");
261 if (_otar_ptr_ !=
nullptr) {
262 this->store_text<Data>(*_otar_ptr_, data_);
263 }
else if (_oxar_ptr_ !=
nullptr) {
264 this->store_xml<Data>(*_oxar_ptr_, data_);
265 }
else if (_obar_ptr_ !=
nullptr) {
266 this->store_binary<Data>(*_obar_ptr_, data_);
270 template <
typename Data>
275 "Not a reader factory!");
294 "Input stream at EOF!");
297 "Input stream in fail status!");
300 "Input stream in bad status!");
304 if (_itar_ptr_ !=
nullptr) {
305 this->load_text<Data>(*_itar_ptr_, data_);
307 }
else if (_ixar_ptr_ !=
nullptr) {
308 this->load_xml<Data>(*_ixar_ptr_, data_);
310 }
else if (_ibar_ptr_ !=
nullptr) {
311 this->load_binary<Data>(*_ibar_ptr_, data_);
313 }
catch (boost::archive::archive_exception & x) {
315 }
catch (std::exception & x) {
319 "Cannot load data from archive: " 320 <<
"unexpected exception" <<
"!");
321 DT_THROW(std::runtime_error,
"Internal exception!");
325 if (_in_fs_->fail()) {
327 "Input stream is now in fail status!");
329 if (_in_fs_->eof()) {
331 "Input stream is now in EOF status!");
333 if (_in_fs_->bad()) {
335 "Input stream is now in bad status!");
340 template <
typename Data>
347 template <
typename Data>
354 virtual void tree_dump(std::ostream & out_ = std::clog,
355 const std::string & title_ =
"",
356 const std::string & indent_ =
"",
357 bool inherit_ =
false)
const;
359 void dump(std::ostream & out_)
const;
363 template <
typename Data>
364 void store_text(boost::archive::text_oarchive & archive_,
367 const Data & b = data_;
372 template <
typename Data>
373 void store_xml(boost::archive::xml_oarchive & archive_,
376 const Data & b = data_;
377 archive_ << boost::serialization::make_nvp(
"record", b);
381 template <
typename Data>
382 void store_binary(datatools::portable_oarchive & archive_,
385 const Data & b = data_;
390 template <
typename Data>
391 void load_text(boost::archive::text_iarchive & archive_,
398 template <
typename Data>
399 void load_xml(boost::archive::xml_iarchive & archive_,
403 archive_ >> boost::serialization::make_nvp(
"record", b);
407 template <
typename Data>
408 void load_binary(datatools::portable_iarchive & archive_,
420 unsigned int _mode_ = 0;
422 std::istream * _in_ =
nullptr;
423 std::ostream * _out_ =
nullptr;
425 std::ifstream * _fin_ =
nullptr;
426 std::ofstream * _fout_ =
nullptr;
428 boost::iostreams::filtering_istream * _in_fs_ =
nullptr;
429 boost::iostreams::filtering_ostream * _out_fs_ =
nullptr;
431 std::locale * _default_locale_ =
nullptr;
432 std::locale * _locale_ =
nullptr;
434 bool _read_archive_is_initialized_ =
false;
435 bool _write_archive_is_initialized_ =
false;
437 boost::archive::text_iarchive * _itar_ptr_ =
nullptr;
438 boost::archive::text_oarchive * _otar_ptr_ =
nullptr;
440 boost::archive::xml_iarchive * _ixar_ptr_ =
nullptr;
441 boost::archive::xml_oarchive * _oxar_ptr_ =
nullptr;
443 datatools::portable_iarchive * _ibar_ptr_ =
nullptr;
444 datatools::portable_oarchive * _obar_ptr_ =
nullptr;
461 io_reader(
const std::string & stream_name_,
480 io_writer(
const std::string & stream_name_,
489 static const bool using_multi_archives =
true;
490 static const bool using_multiple_archives = using_multi_archives;
491 static const bool using_single_archive =
false;
492 static const bool append_mode =
true;
493 static const bool no_append_mode =
false;
541 bool use_multiple_archives_ = using_single_archive);
544 data_reader(
const std::string & filename_,
int mode_);
583 void init(
const std::string & filename_,
584 bool use_multiple_archives_ = using_single_archive);
588 this->
init(filename_, using_multiple_archives);
594 this->
init(filename_, using_single_archive);
597 void init(
const std::string & filename_,
int mode_);
599 void dump(std::ostream & out_ = std::clog)
const;
601 template <
typename Data>
602 void load(
const std::string & tag_, Data & data_)
606 "No more record tag!");
610 <<
" != " << tag_ <<
"')!");
613 this->read_next_tag();
617 template <
typename Data>
618 void load_alt(
const std::string & tag_,
const std::string & alt_tag_, Data & data_)
622 "No more record tag!");
628 <<
" != " << tag_ <<
"')!");
632 this->read_next_tag();
636 template <
typename Data>
638 typename boost::disable_if< has_bsts<Data> >::type * dummy_ = 0)
640 if(!dummy_) dummy_ = 0;
641 this->
load(data_.get_serial_tag(), data_);
645 template <
typename Data>
647 typename boost::enable_if< has_bsts<Data> >::type * dummy_ = 0)
649 if(!dummy_) dummy_ = 0;
650 this->
load_alt(data_.get_serial_tag(),
651 ::datatools::backward_serial_tag<Data> (0),
656 template <
typename Data>
664 template <
typename Data>
671 _reader_->
load(data_);
672 }
catch (std::exception & x) {
676 std::string msg = x.what();
677 if (msg.find(
"EOF") != msg.npos) {
683 "Cannot read data: exception=" 684 << x.what() <<
" !");
692 <<
"Unexpected exception" <<
" !");
695 DT_THROW_IF(
true, std::logic_error,
"Unexpected error!");
703 void read_next_tag();
705 void init_reader(
const std::string & filename_,
int mode_);
713 std::string _next_tag_;
747 data_writer(
const std::string & filename_,
int mode_);
751 bool use_multiple_archives_ = using_single_archive,
752 bool append_mode_ = no_append_mode);
781 void init(
const std::string & filename_,
782 bool a_multiple_archives_ = using_single_archive,
783 bool a_append_mode_ = no_append_mode);
785 void init_multi(
const std::string & filename_);
789 void init(
const std::string & filename_,
int mode_);
793 template <
typename Data>
794 void store(
const std::string & tag_,
const Data & data_)
799 this->basic_store<std::string>(tag_);
800 this->basic_store<Data>(data_);
807 template <
typename Data>
811 static_cast<const datatools::i_serializable &>(data_);
818 template <
typename Data>
824 _writer_->
store<Data>(data_);
830 void init_writer(
const std::string & filename_,
int mode_);
842 #endif // DATATOOLS_IO_FACTORY_H
#define DT_THROW(ExceptionType, Message)
Definition: exception.h:121
#define DT_LOG_WARNING(Priority, Message)
Definition: logger_macros.h:100
Utility macros for exception handling.
#define DT_LOG_ERROR(Priority, Message)
Definition: logger_macros.h:82
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
Utilities for logging information.