Jpp  19.1.0
the software that should make you happy
JSonObjectOutput.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JSONOBJECTOUTPUT__
2 #define __JROOT__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  using JLANG::JTypeList;
26  using JLANG::JNullType;
27 
28  /**
29  * Template implementation of stream output for single data type.
30  *
31  * This class implements the JLANG::JObjectOutput interface.
32  */
33  template<class T>
35  public virtual JObjectOutput<T>
36  {
37  public:
38  /**
39  * Constructor.
40  *
41  * \param out output stream
42  * \param sep token separator
43  */
44  JSonObjectOutput(std::ostream& out,
45  const std::string& sep = "\n") :
46  __out(out),
47  __sep(sep)
48  {}
49 
50 
51  /**
52  * Object output.
53  *
54  * \param object object
55  * \return true if OK; else false
56  */
57  virtual bool put(const T& object) override
58  {
59  using namespace JPP;
60 
62  return (bool) (__out << (const char*) TBufferJSON::ConvertToJSON(&object) << __sep);
63  else
64  return false;
65  }
66 
67 
68  /**
69  * Get token separator.
70  *
71  * \return separator
72  */
73  const std::string& getSeparator() const
74  {
75  return this->__sep;
76  }
77 
78 
79  /**
80  * Set token separator.
81  *
82  * \param sep separator
83  */
84  void setSeparator(const std::string& sep)
85  {
86  this->__sep = sep;
87  }
88 
89  private:
90  std::ostream& __out;
91  std::string __sep;
92  };
93 
94 
95  /**
96  * Template specialisationimplementation of stream object output for multiple data types.
97  *
98  * This class recursively implements the JLANG::JObjectOutput interface
99  * for all data types by deriving from:
100  * - JSonObjectOutput<JHead_t>; and
101  * - JSonObjectOutput<JTail_t>.
102  */
103  template<class JHead_t, class JTail_t>
104  class JSonObjectOutput< JTypeList<JHead_t, JTail_t> > :
105  public virtual JObjectOutput< JTypeList<JHead_t, JTail_t> >,
106  public JSonObjectOutput<JHead_t>,
107  public JSonObjectOutput<JTail_t>
108 
109  {
110  public:
111 
114 
115 
116  /**
117  * Constructor.
118  *
119  * \param out output stream
120  * \param sep token separator
121  */
122  JSonObjectOutput(std::ostream& out,
123  const std::string& sep = "") :
124  JSonObjectOutput<JHead_t>(out, sep),
125  JSonObjectOutput<JTail_t>(out, sep)
126  {}
127 
128 
129  /**
130  * Set token separator.
131  *
132  * \param sep separator
133  */
134  void setSeparator(const std::string& sep)
135  {
138  }
139  };
140 
141 
142  /**
143  * Terminator class of recursive JSonObjectOutput class.
144  */
145  template<class JHead_t>
146  class JSonObjectOutput< JTypeList<JHead_t, JNullType> > :
147  public JSonObjectOutput<JHead_t>
148  {
149  public:
150 
152 
153 
154  /**
155  * Constructor.
156  *
157  * \param out output stream
158  * \param sep token separator
159  */
160  JSonObjectOutput(std::ostream& out,
161  const std::string& sep = "") :
162  JSonObjectOutput<JHead_t>(out, sep)
163  {}
164  };
165 }
166 
167 #endif
Template interface of object output for single data type.
JSonObjectOutput(std::ostream &out, const std::string &sep="")
Constructor.
JSonObjectOutput(std::ostream &out, const std::string &sep="")
Constructor.
void setSeparator(const std::string &sep)
Set token separator.
Template implementation of stream output for single data type.
JSonObjectOutput(std::ostream &out, const std::string &sep="\n")
Constructor.
const std::string & getSeparator() const
Get token separator.
virtual bool put(const T &object) override
Object output.
void setSeparator(const std::string &sep)
Set token separator.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
Template class test for polymorphism.
Definition: JConversion.hh:22
Auxiliary class for no type definition.
Definition: JNullType.hh:19
Type list.
Definition: JTypeList.hh:23