iterator.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_ITERATOR_HXX
21 #define MODULE_LOGGER_ITERATOR_HXX
22 
23 #include <Logger/error.hxx>
24 #include <Logger/typedef.hxx>
25 
26 namespace SHA_Logger
27 {
31  class Iterator
32  {
33  public:
34  // Assert correct JSON construction.
35  ~Iterator() { assert(this->writer->IsComplete()); }
36 
42  static Ostream& Build
43  (Ostream& os, const String& parentId, const String& name, int index, const String& comment = "")
44  {
45  std::unique_ptr<Iterator> builder = std::unique_ptr<Iterator>(new Iterator(os));
46  builder->Write(parentId, name, index, comment);
47 
48  return os;
49  }
50 
55  static Writer& Build
56  (Writer& writer, const String& parentId, const String& name, int index, const String& comment = "")
57  {
58  Write(writer, parentId, name, index, comment);
59 
60  return writer;
61  }
62 
68  template <typename T>
69  static const T& BuildIt (Ostream& os, const String& parentId, const String& name, int index,
70  const T& it, const String& comment = "")
71  {
72  std::unique_ptr<Iterator> builder = std::unique_ptr<Iterator>(new Iterator(os));
73  builder->Write(parentId, name, index, comment);
74 
75  return it;
76  }
77 
82  template <typename T>
83  static const T& BuildIt (Writer& writer, const String& parentId, const String& name, int index,
84  const T& it, const String& comment = "")
85  {
86  Write(writer, parentId, name, index, comment);
87 
88  return it;
89  }
90 
91  private:
92  Iterator(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
93  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
94  Iterator operator=(Iterator&) {} // Not Implemented
95 
96  bool Write(const String& parentId, const String& name, int index, const String& comment)
97  { return Write(*this->writer, parentId, name, index, comment); }
98 
99  static bool Write
100  (Writer& writer, const String& parentId, const String& name, int index, const String& comment)
101  {
102  // Add Error Object log in case of failure
103  if (parentId.empty() || name.empty())
104  {
105  Error::Build(writer, __FILE__, __LINE__,
106  "Missing parameter: " +
107  (parentId.empty()) ? "+ parentId " : "" +
108  (name.empty()) ? "+ name " : "");
109  return false;
110  }
111 
112  // Add iterator information
113  writer.StartObject();
114  writer.Key("type");
115  writer.String("iterator");
116 
117  writer.Key("name");
118  writer.String(name);
119 
120  writer.Key("ref");
121  writer.String(parentId);
122 
123  writer.Key("data");
124  writer.Int(index);
125 
126  if (!comment.empty())
127  {
128  writer.Key("comment");
129  writer.String(comment);
130  }
131  writer.EndObject();
132 
133  return true;
134  }
135 
136  std::unique_ptr<Stream> stream; // Stream wrapper
137  std::unique_ptr<Writer> writer; // Writer used to fill the stream
138  };
139 }
140 
141 #endif // MODULE_LOGGER_ITERATOR_HXX
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
std::unique_ptr< Writer > writer
Definition: iterator.hxx:137
static const T & BuildIt(Ostream &os, const String &parentId, const String &name, int index, const T &it, const String &comment="")
Definition: iterator.hxx:69
bool Write(const String &parentId, const String &name, int index, const String &comment)
Definition: iterator.hxx:96
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
Iterator operator=(Iterator &)
Definition: iterator.hxx:94
std::unique_ptr< Stream > stream
Definition: iterator.hxx:136
static const T & BuildIt(Writer &writer, const String &parentId, const String &name, int index, const T &it, const String &comment="")
Definition: iterator.hxx:83
std::ostream Ostream
Definition: typedef.hxx:37
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33
Iterator(Ostream &os)
Definition: iterator.hxx:92