value_type.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_TYPE_HXX
21 #define MODULE_LOGGER_VALUE_TYPE_HXX
22 
23 #include <Logger/typedef.hxx>
24 
25 namespace SHA_Logger
26 {
29  class ValueType
30  {
31  public:
32  // Assert correct JSON construction.
33  ~ValueType() { assert(this->writer->IsComplete()); }
34 
40  template <typename T>
41  static std::ostream& Build(std::ostream& os, const T& value)
42  {
43  // Create ValueType and add it to the logger
44  std::unique_ptr<ValueType> builder = std::unique_ptr<ValueType>(new Iterator(os));
45  builder->Write(value);
46 
47  return os;
48  }
49 
54  template <typename T>
55  static Writer& Build(Writer& writer, const T& value)
56  {
57  Write(writer, value);
58 
59  return writer;
60  }
61 
67  template <typename IteratorT>
68  static std::ostream& BuildArray(std::ostream& os, const IteratorT& begin, const IteratorT& end)
69  {
70  // Create ValueType logger
71  std::unique_ptr<ValueType> builder = std::unique_ptr<ValueType>(new ValueType(os));
72 
73  // @todo check & log error? --> time consuming
74  builder->writer->StartArray();
75  for (auto it = begin; it != end; ++it)
76  builder->Write(*it);
77  builder->writer->EndArray();
78 
79  return os;
80  }
81 
86  template <typename IteratorT>
87  static Writer& BuildArray(Writer& writer, const IteratorT& begin, const IteratorT& end)
88  {
89  // @todo check & log error? --> time consuming
90  writer.StartArray();
91  for (auto it = begin; it != end; ++it)
92  Write(writer, *it);
93  writer.EndArray();
94 
95  return writer;
96  }
97 
98  private:
99  ValueType(std::ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
100  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
101  ValueType operator=(ValueType&) {} // Not Implemented
102 
103  // Wrapper using internal writer
104  template <typename T>
105  bool Write(T value) { return this->Write(*this->writer, value); }
106 
107  // Specifications
108  static bool Write(Writer& writer, char value) { return writer.String(String(1, value)); }
109  static bool Write(Writer& writer, bool value) { return writer.Bool(value); }
110  static bool Write(Writer& writer, double value) { return writer.Double(value); }
111  static bool Write(Writer& writer, int value) { return writer.Int(value); }
112  static bool Write(Writer& writer, int64_t value) { return writer.Int64(value); }
113  static bool Write(Writer& writer, const String& value) { return writer.String(value); }
114  static bool Write(Writer& writer, unsigned value) { return writer.Uint(value); }
115  static bool Write(Writer& writer, uint64_t value) { return writer.Uint64(value); }
116 
117  std::unique_ptr<Stream> stream; // Stream wrapper
118  std::unique_ptr<Writer> writer; // Writer used to fill the stream
119  };
120 }
121 
122 #endif // MODULE_LOGGER_VALUE_TYPE_HXX
static bool Write(Writer &writer, char value)
Definition: value_type.hxx:108
static bool Write(Writer &writer, int64_t value)
Definition: value_type.hxx:112
static Writer & BuildArray(Writer &writer, const IteratorT &begin, const IteratorT &end)
Definition: value_type.hxx:87
static Writer & Build(Writer &writer, const T &value)
Definition: value_type.hxx:55
static bool Write(Writer &writer, bool value)
Definition: value_type.hxx:109
bool Write(T value)
Definition: value_type.hxx:105
std::unique_ptr< Stream > stream
Definition: value_type.hxx:117
const std::string String
Definition: typedef.hxx:36
static bool Write(Writer &writer, int value)
Definition: value_type.hxx:111
static bool Write(Writer &writer, uint64_t value)
Definition: value_type.hxx:115
rapidjson::OStreamWrapper Stream
Definition: typedef.hxx:32
static bool Write(Writer &writer, const String &value)
Definition: value_type.hxx:113
static std::ostream & Build(std::ostream &os, const T &value)
Definition: value_type.hxx:41
static bool Write(Writer &writer, unsigned value)
Definition: value_type.hxx:114
static bool Write(Writer &writer, double value)
Definition: value_type.hxx:110
ValueType(std::ostream &os)
Definition: value_type.hxx:99
std::unique_ptr< Writer > writer
Definition: value_type.hxx:118
ValueType operator=(ValueType &)
Definition: value_type.hxx:101
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33
static std::ostream & BuildArray(std::ostream &os, const IteratorT &begin, const IteratorT &end)
Definition: value_type.hxx:68