array.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_ARRAY_HXX
21 #define MODULE_LOGGER_ARRAY_HXX
22 
23 #include <Logger/iterator.hxx>
24 #include <Logger/value_type.hxx>
25 
26 namespace SHA_Logger
27 {
31  template <typename IT>
32  class Array
33  {
34  public:
35  ~Array() { assert(this->writer->IsComplete()); }
36 
42  static Ostream& Build(Ostream& os, const String& name,
43  const String& beginName, const IT& begin,
44  const String& endName, const IT& end)
45  {
46  std::unique_ptr<Array> builder = std::unique_ptr<Array>(new Array(os));
47  builder->Write(name, beginName, begin, endName, end);
48 
49  return os;
50  }
51 
56  static Writer& Build(Writer& writer, const String& name,
57  const String& beginName, const IT& begin,
58  const String& endName, const IT& end)
59  {
60  Write(writer, name, beginName, begin, endName, end);
61 
62  return writer;
63  }
64 
65  private:
66  Array(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
67  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
68  Array operator=(Array&) {} // Not Implemented
69 
70  bool Write(const String& name,
71  const String& beginName, const IT& begin,
72  const String& endName, const IT& end)
73  { return Write(*this->writer, name, beginName, begin, endName, end); }
74 
75  static bool Write(Writer& writer, const String& name,
76  const String& beginName, const IT& begin,
77  const String& endName, const IT& end)
78  {
79  // Add Error Object log in case of failure
80  if (name.empty() || beginName.empty() || endName.empty())
81  {
82  Error::Build(writer, __FILE__, __LINE__,
83  "Missing parameter: " +
84  (name.empty()) ? "+ name " : "" +
85  (beginName.empty()) ? "+ beginName " : "" +
86  (endName.empty()) ? "+ endName " : "");
87  return false;
88  }
89 
90  // 2*N for non random accessible iterator
91  const int kdataSize = static_cast<int>(std::distance(begin, end));
92  if (kdataSize < 1)
93  {
94  Error::Build(writer, __FILE__, __LINE__, "empty/invalid sequence detected.");
95  return false;
96  }
97 
98  // StartBuild - Main information
99  writer.StartObject();
100  writer.Key("type");
101  writer.String("array");
102  writer.Key("name");
103  writer.String(name);
104 
105  // Add data
106  writer.Key("data");
107  ValueType::BuildArray<IT>(writer, begin, end);
108 
109  // Add Iterators
110  writer.Key("iterators");
111  writer.StartArray();
112  Iterator::Build(writer, name, beginName, 0);
113  Iterator::Build(writer, name, endName, static_cast<int>(kdataSize));
114  writer.EndArray();
115 
116  // Finish object
117  writer.EndObject();
118 
119  return true;
120  }
121 
122  std::unique_ptr<Stream> stream; // Stream wrapper
123  std::unique_ptr<Writer> writer; // Writer used to fill the stream
124  };
125 }
126 
127 #endif // MODULE_LOGGER_ARRAY_HXX
Array operator=(Array &)
Definition: array.hxx:68
bool Write(const String &name, const String &beginName, const IT &begin, const String &endName, const IT &end)
Definition: array.hxx:70
std::unique_ptr< Stream > stream
Definition: array.hxx:122
Array(Ostream &os)
Definition: array.hxx:66
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
static bool Write(Writer &writer, const String &name, const String &beginName, const IT &begin, const String &endName, const IT &end)
Definition: array.hxx:75
static Ostream & Build(Ostream &os, const String &name, const String &beginName, const IT &begin, const String &endName, const IT &end)
Definition: array.hxx:42
static Ostream & Build(Ostream &os, const String &parentId, const String &name, int index, const String &comment="")
Definition: iterator.hxx:43
rapidjson::OStreamWrapper Stream
Definition: typedef.hxx:32
std::ostream Ostream
Definition: typedef.hxx:37
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33
static Writer & Build(Writer &writer, const String &name, const String &beginName, const IT &begin, const String &endName, const IT &end)
Definition: array.hxx:56
std::unique_ptr< Writer > writer
Definition: array.hxx:123