#ifndef __JROOT__JROOTPRINTER__ #define __JROOT__JROOTPRINTER__ #include #include #include "JLang/JEquationParameters.hh" #include "JLang/JException.hh" #include "JLang/JLangToolkit.hh" #include "JROOT/JRootClass.hh" #include "JROOT/JRootStreamer.hh" #include "JROOT/JRootDictionary.hh" /** * \file * * Print objects in ASCII format using ROOT dictionary. * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { using JLANG::JEquationParameters; using JLANG::JParseError; /** * Auxiliary class for printing objects in ASCII format using JRootWriter. */ struct JRootPrinter { /** * Get equation parameters. * * \return equation parameters */ static inline JEquationParameters& getEquationParameters() { static JEquationParameters equation("=", "\n", ".", ""); return equation; } /** * Set equation parameters. * * \param equation equation parameters */ static inline void setEquationParameters(const JEquationParameters& equation) { getEquationParameters() = equation; } /** * Write class contents to output. * * \param writer ROOT writer * \param cls ROOT class */ static inline void print(JRootWriter& writer, const JRootWritableClass& cls) { if (cls.is_valid()) { if (writer.getDictionary().find(cls.getTypename()) != writer.getDictionary().end()) writer.putObject(cls); else writer.put(cls); } } /** * Write part of object to output using ROOT dictionary. * * \param out output stream * \param object object * \param key data member name, optionally separated by division character * \param parameters equation parameters * \param dictionary dictionary */ template static inline void print(std::ostream& out, const T& object, const std::string& key, const JEquationParameters& parameters, const JRootDictionary_t& dictionary = JRootDictionary::getInstance()) { using namespace std; using namespace JPP; JRootWritableClass cls(object); for (string::size_type il = 0, ir = 0; ir != string::npos && cls.is_valid(); il = ir + 1) { ir = key.substr(il).find(parameters.getDefaultDivision()); cls = cls.find(trim(key.substr(il, ir - il)).c_str()); } if (cls.is_valid()) { JRootWriter writer(out, parameters, dictionary); print(writer, cls); } else { THROW(JParseError, "Invalid key " << key); } } /** * Write object to output using ROOT dictionary. * * \param out output stream * \param object object * \param parameters equation parameters * \param dictionary dictionary */ template static inline void print(std::ostream& out, const T& object, const JEquationParameters& parameters, const JRootDictionary_t& dictionary = JRootDictionary::getInstance()) { JRootWriter writer(out, parameters, dictionary); JRootWritableClass cls(object); print(writer, cls); } /** * Write part of object to output using ROOT dictionary. * * \param out output stream * \param object object * \param key data member name, optionally separated by division character '.' * \param dictionary dictionary */ template static inline void print(std::ostream& out, const T& object, const std::string& key, const JRootDictionary_t& dictionary = JRootDictionary::getInstance()) { print(out, object, key, JRootPrinter::getEquationParameters(), dictionary); } /** * Write object to output using ROOT dictionary. * * \param out output stream * \param object object * \param dictionary dictionary */ template static inline void print(std::ostream& out, const T& object, const JRootDictionary_t& dictionary = JRootDictionary::getInstance()) { print(out, object, JRootPrinter::getEquationParameters(), dictionary); } }; } #endif