#ifndef __JLANG__JVALUE__ #define __JLANG__JVALUE__ #include #include #include #include #include "JLang/JAbstractIO.hh" #include "JLang/JEquationFacet.hh" /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * Forward declaration for friend declaration of JValueOutput inside JValueInput. */ template class JValueOutput; /** * Wrapper class around template object. * This class implements the JStreamInput interface. * Note that this class can be used in conjuction with the JEquationFacet. */ template class JValueInput : public JStreamInput { public: friend class JValueOutput; /** * Constructor. * * \param object input object */ JValueInput(T& object) : p(&object) {} /** * Constructor. * * \param ps pointer to valid object */ JValueInput(void* ps) : p((T*) ps) {} operator const T&() const { return *p; } //!< type conversion operator operator T&() { return *p; } //!< type conversion operator /** * Stream input. * * \param in input stream * \return input stream */ virtual std::istream& read(std::istream& in) override { return in >> *p; } protected: T* p; }; /** * Wrapper class around template object. * This class implements the JStreamOutput interface. * Note that this class can be used in conjuction with the JEquationFacet. */ template class JValueOutput : public JStreamOutput { public: /** * Constructor. * * \param object input object */ JValueOutput(const T& object) : p(&object) {} /** * Constructor. * * \param object input object */ JValueOutput(const JValueInput& object) : p(object.p) {} /** * Constructor. * * \param ps pointer to valid object */ JValueOutput(const void* ps) : p((const T*) ps) {} operator const T&() const { return *p; } //!< type conversion operator /** * Stream output. * * \param out output stream * \return output stream */ virtual std::ostream& write(std::ostream& out) const override { return out << *p; } protected: const T* p; }; /** * Wrapper class around template object. * This class implements the JStreamInput and JStreamOutput interfaces. */ template class JValue : public JValueInput , public JValueOutput { public: /** * Constructor. * * \param object input object */ JValue(T& object) : JValueInput (&object), JValueOutput(&object) {} /** * Constructor. * * \param ps pointer to valid object */ JValue(void* ps) : JValueInput (ps) , JValueOutput(ps) {} }; /** * Read JStreamInput from input stream. * * \param in input stream * \param object JStreamInput * \return input stream */ template inline std::istream& operator>>(std::istream& in, JValueInput& object) { using namespace std; istream::sentry sentry(in); // skips white spaces if (sentry) { const locale& loc = in.getloc(); if (has_facet(loc)) { string buffer; const JEquationFacet& facet = use_facet(loc); facet.getline(in, buffer); istringstream is(buffer); is.imbue(locale(in.getloc(), facet.clone())); object.read(is); if (is.fail()) { in.setstate(ios_base::badbit); } } else { object.read(in); } } return in; } /** * Write JStreamOutput to output stream. * * \param out output stream * \param object JStreamOutput * \return output stream */ template inline std::ostream& operator<<(std::ostream& out, const JValueOutput& object) { return object.write(out); } } #endif