value.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA-L - Simple Hybesis Algorithm Logger
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms-Logger/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #ifndef MODULE_LOGGER_VALUE_HXX
21 #define MODULE_LOGGER_VALUE_HXX
22 
23 #include <Logger/error.hxx>
24 #include <Logger/typedef.hxx>
25 #include <Logger/value_type.hxx>
26 
27 namespace SHA_Logger
28 {
32  template <typename T>
33  class Value
34  {
35  public:
36  // Assert correct JSON construction.
37  ~Value() { assert(this->writer->IsComplete()); }
38 
44  static Ostream& Build(Ostream& os, const String& name, const T& value, const String& comment = "")
45  {
46  std::unique_ptr<Value> builder = std::unique_ptr<Value>(new Value(os));
47  builder->Write(name, value, comment);
48 
49  return os;
50  }
51 
56  static const T& BuildValue
57  (Writer& writer, const String& name, const T& value, const String& comment = "")
58  {
59  Write(writer, name, value, comment);
60 
61  return value;
62  }
63 
69  static const T& BuildValue(Ostream& os, const String& name, const T& value, const String& comment = "")
70  {
71  std::unique_ptr<Value> builder = std::unique_ptr<Value>(new Value(os));
72  builder->Write(name, value, comment);
73 
74  return value;
75  }
76 
81  static Writer& Build(Writer& writer, const String& name, const T& value, const String& comment = "")
82  {
83  Write(writer, name, value, comment);
84 
85  return writer;
86  }
87 
88  private:
89  Value(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
90  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
91  Value operator=(Value&) {} // Not Implemented
92 
93  bool Write(const String& name, const T& value, const String& comment)
94  { return Write(*this->writer, name, value, comment); }
95 
96  static bool Write(Writer& writer, const String& name, const T& value, const String& comment)
97  {
98  // Add Error Object log in case of failure
99  if (name.empty())
100  {
101  Error::Build(writer, __FILE__, __LINE__, "name parameter empty.");
102  return false;
103  }
104 
105  // Add iterator information
106  writer.StartObject();
107  writer.Key("type");
108  writer.String("value");
109 
110  writer.Key("name");
111  writer.String(name);
112 
113  writer.Key("data");
114  ValueType::Build<T>(writer, value);
115 
116  if (!comment.empty())
117  {
118  writer.Key("comment");
119  writer.String(comment);
120  }
121  writer.EndObject();
122 
123  return true;
124  }
125 
126  std::unique_ptr<Stream> stream; // Stream wrapper
127  std::unique_ptr<Writer> writer; // Writer used to fill the stream
128  };
129 }
130 
131 #endif // MODULE_LOGGER_VALUE_HXX
static Ostream & Build(Ostream &os, const String &name, const T &value, const String &comment="")
Definition: value.hxx:44
static Ostream & Build(Ostream &os, const String &file, int line, const String &message)
Definition: error.hxx:41
const std::string String
Definition: typedef.hxx:36
Value(Ostream &os)
Definition: value.hxx:89
static const T & BuildValue(Writer &writer, const String &name, const T &value, const String &comment="")
Definition: value.hxx:57
static bool Write(Writer &writer, const String &name, const T &value, const String &comment)
Definition: value.hxx:96
Value operator=(Value &)
Definition: value.hxx:91
rapidjson::OStreamWrapper Stream
Definition: typedef.hxx:32
static const T & BuildValue(Ostream &os, const String &name, const T &value, const String &comment="")
Definition: value.hxx:69
std::unique_ptr< Stream > stream
Definition: value.hxx:126
std::unique_ptr< Writer > writer
Definition: value.hxx:127
std::ostream Ostream
Definition: typedef.hxx:37
bool Write(const String &name, const T &value, const String &comment)
Definition: value.hxx:93
static Writer & Build(Writer &writer, const String &name, const T &value, const String &comment="")
Definition: value.hxx:81
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33