Jpp  17.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSonObjectOutput.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSONOBJECTOUTPUT__
2 #define __JLANG__JSONOBJECTOUTPUT__
3 
4 #include <string>
5 #include <ostream>
6 
7 #include "TBufferJSON.h"
8 
9 #include "JLang/JObjectOutput.hh"
10 #include "JLang/JTypeList.hh"
11 #include "JLang/JNullType.hh"
12 #include "JLang/JConversion.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JROOT {}
20 namespace JPP { using namespace JROOT; }
21 
22 namespace JROOT {
23 
25 
26  /**
27  * Template implementation of stream output for single data type.
28  *
29  * This class implements the JLANG::JObjectOutput interface.
30  */
31  template<class T>
33  public virtual JObjectOutput<T>
34  {
35  public:
36  /**
37  * Constructor.
38  *
39  * \param out output stream
40  * \param sep token separator
41  */
42  JSonObjectOutput(std::ostream& out,
43  const std::string& sep = "\n") :
44  __out(out),
45  __sep(sep)
46  {}
47 
48 
49  /**
50  * Object output.
51  *
52  * \param object object
53  * \return true if OK; else false
54  */
55  virtual bool put(const T& object) override
56  {
57  using namespace JPP;
58 
60  return (bool) (__out << (const char*) TBufferJSON::ConvertToJSON(&object) << __sep);
61  else
62  return false;
63  }
64 
65 
66  /**
67  * Get token separator.
68  *
69  * \return separator
70  */
71  const std::string& getSeparator() const
72  {
73  return this->__sep;
74  }
75 
76 
77  /**
78  * Set token separator.
79  *
80  * \param sep separator
81  */
82  void setSeparator(const std::string& sep)
83  {
84  this->__sep = sep;
85  }
86 
87  private:
88  std::ostream& __out;
89  std::string __sep;
90  };
91 
92 
93  /**
94  * Template specialisationimplementation of stream object output for multiple data types.
95  *
96  * This class recursively implements the JLANG::JObjectOutput interface
97  * for all data types by deriving from:
98  * - JSonObjectOutput<JHead_t>; and
99  * - JSonObjectOutput<JTail_t>.
100  */
101  template<class JHead_t, class JTail_t>
102  class JSonObjectOutput< JTypeList<JHead_t, JTail_t> > :
103  public virtual JObjectOutput< JTypeList<JHead_t, JTail_t> >,
104  public JSonObjectOutput<JHead_t>,
105  public JSonObjectOutput<JTail_t>
106 
107  {
108  public:
109 
112 
113 
114  /**
115  * Constructor.
116  *
117  * \param out output stream
118  * \param sep token separator
119  */
120  JSonObjectOutput(std::ostream& out,
121  const std::string& sep = "") :
122  JSonObjectOutput<JHead_t>(out, sep),
123  JSonObjectOutput<JTail_t>(out, sep)
124  {}
125 
126 
127  /**
128  * Set token separator.
129  *
130  * \param sep separator
131  */
132  void setSeparator(const std::string& sep)
133  {
136  }
137  };
138 
139 
140  /**
141  * Terminator class of recursive JSonObjectOutput class.
142  */
143  template<class JHead_t>
144  class JSonObjectOutput< JTypeList<JHead_t, JNullType> > :
145  public JSonObjectOutput<JHead_t>
146  {
147  public:
148 
150 
151 
152  /**
153  * Constructor.
154  *
155  * \param out output stream
156  * \param sep token separator
157  */
158  JSonObjectOutput(std::ostream& out,
159  const std::string& sep = "") :
160  JSonObjectOutput<JHead_t>(out, sep)
161  {}
162  };
163 }
164 
165 #endif
JSonObjectOutput(std::ostream &out, const std::string &sep="")
Constructor.
JSonObjectOutput(std::ostream &out, const std::string &sep="")
Constructor.
Template class test for polymorphism.
Definition: JConversion.hh:21
const std::string & getSeparator() const
Get token separator.
Type list.
Definition: JTypeList.hh:22
Template implementation of stream output for single data type.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for no type definition.
Definition: JNullType.hh:19
void setSeparator(const std::string &sep)
Set token separator.
JSonObjectOutput(std::ostream &out, const std::string &sep="\n")
Constructor.
void setSeparator(const std::string &sep)
Set token separator.
virtual bool put(const T &object) override
Object output.
Template interface of object output for single data type.