#ifndef __JLANG__JTEMPLATEWRITER__ #define __JLANG__JTEMPLATEWRITER__ #include #include "JLang/JNullType.hh" #include "JLang/JTypeList.hh" #include "JLang/JType.hh" /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * Template definition of auxiliary base class for composite data types which derive * from one or more base classes for which the redirect operator << is defined. * The various specialisations of this class implement the redirect operator << * for the derived class. */ template struct JTemplateWriter; /** * Specialisation of class JTemplateWriter for general purpose write methods. */ template struct JTemplateWriter { protected: /** * Write method for single data types. * * \param out writer * \param object object * \param type type * \return writer */ template static inline JWriter_t& write(JWriter_t& out, const JClass_t& object, const JType & type) { return out << object; } /** * Write method for composite data types. * * \param out writer * \param object object * \param type type * \return writer */ template static inline JWriter_t& write(JWriter_t& out, const JClass_t& object, const JType >& type) { out << static_cast(object); if (WHITE_SPACE) { out << WHITE_SPACE; } return write(out, object, JType()); } /** * Write method for composite data types. * * \param out writer * \param object object * \param type type * \return writer */ template static inline JWriter_t& write(JWriter_t& out, const JClass_t& object, const JType >& type) { return out << static_cast(object); } public: static const char* WHITE_SPACE; //!< White space between consecutive items in a type list. }; /** * Default white space is empty. */ template const char* JTemplateWriter::WHITE_SPACE = NULL; /** * Specialisation of white space for std::ostream. */ template<> const char* JTemplateWriter::WHITE_SPACE = " "; /** * Specialisation of class JTemplateWriter for composite data type. * * This class uses in-class friend operators (see Barton-Nackman trick). */ template struct JTemplateWriter > : public JTemplateWriter { /** * Write operator. * * \param out writer * \param object object * \return true if first object is less than second object; else false */ friend JWriter_t& operator<<(JWriter_t& out, const JClass_t& object) { return JTemplateWriter::write(out, object, JType< JTypeList >()); } }; } #endif