#ifndef __JROOT_JASCIIFILESTREAMER__ #define __JROOT_JASCIIFILESTREAMER__ #include #include #include #include #include "JLang/JEquationParameters.hh" #include "JLang/JRedirectString.hh" #include "JROOT/JRootClass.hh" #include "JROOT/JRootDictionary.hh" #include "JROOT/JRootStreamer.hh" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { /** * Auxiliary class to read objects with ROOT dictionary from ASCII formatted file. * * The names of in the first line are one-to-one mapped to the names of the data members * of the corresponding data structure using the corresponding ROOT dictionary. */ template struct JASCIIFileReader : public std::ifstream { using std::ifstream::operator>>; /** * Constructor. * * \param dictionary dictionary */ JASCIIFileReader(const JRootDictionary& dictionary = JRootDictionary::getInstance()) : reader(static_cast(*this), JLANG::JEquationParameters(), dictionary) {} /** * Constructor. * * \param file_name file name * \param dictionary dictionary */ JASCIIFileReader(const char* const file_name, const JRootDictionary& dictionary = JRootDictionary::getInstance()) : reader(static_cast(*this), JLANG::JEquationParameters(), dictionary) { this->open(file_name); } /** * Read object from this file. * * \param object object * \return this input stream */ JASCIIFileReader& operator>>(T& object) { using namespace std; using namespace JPP; if (columns.empty()) { JRootReadableClass cls(this->object); string buffer; if (std::getline(*this, buffer)) { istringstream is(buffer); while (is >> buffer) { columns.push_back(cls.find(buffer.c_str())); } } } object = value; reader.flags(this->flags()); string buffer; if (std::getline(*this, buffer)) { istringstream is(buffer); for (size_t i = 0; i != columns.size() && is >> buffer; ++i) { if (columns[i].is_valid()) { JRedirectString redirect(reader, buffer); reader.getObject(columns[i]); } } object = this->object; } return *this; } private: T object; //!< internal value T value; //!< default value std::vector columns; //!< column count -> data member JRootReader reader; }; /** * Auxiliary class to write object with ROOT dictionary from ASCII formatted file. * * The names of in the first line are one-to-one mapped to the names of the data members * of the corresponding data structure using the corresponding ROOT dictionary. */ template struct JASCIIFileWriter : public std::ofstream { using std::ofstream::operator<<; /** * Constructor. * * \param dictionary dictionary */ JASCIIFileWriter(const JRootDictionary& dictionary = JRootDictionary::getInstance()) : writer(static_cast(*this), JLANG::JEquationParameters(), dictionary) {} /** * Constructor. * * \param file_name file name * \param dictionary dictionary */ JASCIIFileWriter(const char* const file_name, const JRootDictionary& dictionary = JRootDictionary::getInstance()) : writer(static_cast(*this), JLANG::JEquationParameters(), dictionary) { this->open(file_name); } /** * Write object to this file. * * \param object object * \return this input stream */ JASCIIFileWriter& operator<<(const T& object) { using namespace std; using namespace JPP; if (columns.empty()) { JRootWritableClass cls(this->object); for (const TDataMember* p : getListOfDataMembers()) { static_cast(*this) << ' ' << p->GetName(); columns.push_back(cls.get(*p)); } static_cast(*this) << endl; } this->object = object; writer.flags(this->flags()); for (size_t i = 0; i != columns.size(); ++i) { writer.putObject(columns[i]); } return *this; } private: T object; //!< internal value std::vector columns; //!< column count -> data member JRootWriter writer; }; } #endif