Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
jsonObject.h
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
22 #ifndef JSONOBJECT_H_INCLUDED
23 #define JSONOBJECT_H_INCLUDED
24 
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 #include <memory>
30 #include <cmath>
31 
36 namespace JSON {
37 
43 class Object {
44 
45  public:
46 
51  explicit
52  Object(std::string const& t_specifier): specifier(t_specifier) {}
53 
57  virtual ~Object() {};
58 
62  std::string getSpecifier() const {
63  return specifier;
64  }
65 
71  virtual bool isNode() const {
72  return false;
73  }
74 
75  protected:
76 
82  std::string specifier;
83 };
84 
89 typedef std::unique_ptr<Object> Child;
90 
97 class Node: public Object {
98 
99  public:
100 
105  explicit
106  Node(std::string const& specifier): Object(specifier) {}
107 
108  static
109  std::unique_ptr<Node> make(std::string const& specifier) {
110  return std::make_unique<Node>(specifier);
111  }
112 
117  bool isNode() const override {
118  return true;
119  }
120 
125  virtual void addChild(Child& child) {
126  children.push_back(std::move(child));
127  }
128 
133  template<class T>
134  inline void addChild(std::unique_ptr<T>& child) {
135  Child c = std::move(child);
136  addChild(c);
137  }
138 
143  virtual void addChild(Child&& child) {
144  children.push_back(std::move(child));
145  }
146 
151  template<class T>
152  inline void addChild(std::unique_ptr<T>&& child) {
153  Child c = std::move(child);
154  addChild(c);
155  }
156 
160  size_t size() const {
161  return children.size();
162  }
163 
169  bool empty() const {
170  return children.empty();
171  }
172 
176  std::vector<Child>::const_iterator begin() const {
177  return children.begin();
178  }
179 
183  std::vector<Child>::const_iterator end() const {
184  return children.end();
185  }
186 
190  std::vector<Child>::iterator begin() {
191  return children.begin();
192  }
193 
197  std::vector<Child>::iterator end() {
198  return children.end();
199  }
200 
208  Object* getChild(std::string const& spec) const {
209  for (const auto& child : children)
210  if (child->getSpecifier() == spec)
211  return child.get();
212  return nullptr;
213  }
214 
215  private:
216 
221  std::vector<Child> children;
222 };
223 
229 class List: public Node {
230 
231  public:
232 
237  explicit
238  List(std::string const& specifier): Node(specifier) {}
239 
240  static
241  std::unique_ptr<List> make(std::string const& specifier) {
242  return std::make_unique<List>(specifier);
243  }
244 
250  void addChild(Child& child) override {
251  if (child->getSpecifier() != specifier) {
252  std::cerr << "JSONParsingError: difference specification"
253  << " \"" << child->getSpecifier() << "\" in list.\n";
254  exit(1);
255  }
256  Node::addChild(child);
257  }
258 
264  template<class T>
265  void addChild(std::unique_ptr<T>& child) {
266  if (child->getSpecifier() != specifier) {
267  std::cerr << "JSONParsingError: difference specification"
268  << " \"" << child->getSpecifier() << "\" in list.\n";
269  exit(1);
270  }
271  Node::addChild(child);
272  }
273 
279  void addChild(Child&& child) override {
280  if (child->getSpecifier() != specifier) {
281  std::cerr << "JSONParsingError: difference specification"
282  << " \"" << child->getSpecifier() << "\" in list.\n";
283  exit(1);
284  }
285  Node::addChild(std::move(child));
286  }
287 
293  template<class T>
294  void addChild(std::unique_ptr<T>&& child) {
295  if (child->getSpecifier() != specifier) {
296  std::cerr << "JSONParsingError: difference specification"
297  << " \"" << child->getSpecifier() << "\" in list.\n";
298  exit(1);
299  }
300  Node::addChild(std::move(child));
301  }
302 };
303 
310 class AbstractLeaf: public Object {
311  public:
312 
317  explicit
318  AbstractLeaf(std::string const& specifier): Object(specifier) {}
319 
324  virtual std::string getArgument() const = 0;
325 };
326 
334 template<typename Type>
335 class Leaf: public AbstractLeaf {
336 
337  public:
338 
346  Leaf(std::string const& specifier, Type t_argument)
347  :AbstractLeaf(specifier), argument(t_argument) {}
348 
349  static
350  Child make(std::string const& specifier, Type t_argument) {
351  return std::make_unique<Leaf<Type>>(specifier, t_argument);
352  }
353 
354  std::string getArgument() const {
355  std::ostringstream sout;
356  if (typeid(argument) == typeid(std::string))
357  sout << "\"" << argument << "\"";
358  else
359  sout << argument;
360  if constexpr(std::is_same<Type, double>::value) {
361  if (argument == std::round(argument)) {
362  sout << '.' << '0';
363  }
364  }
365 
366  return sout.str();
367  }
368 
375  return argument;
376  }
377 
378  private:
379 
384  mutable Type argument;
385 };
386 
387 } // End of namespace JSON
388 
389 #endif
bool empty() const
Tells if the Node is empty.
Definition: jsonObject.h:169
void addChild(Child &child) override
Adds a child in the List. Checks if the specifier is the same as the one of the List (each element mu...
Definition: jsonObject.h:250
void addChild(std::unique_ptr< T > &child)
Adds a child in the Node.
Definition: jsonObject.h:134
virtual void addChild(Child &child)
Adds a child in the Node.
Definition: jsonObject.h:125
Type getTypedArgument() const
Returns the bare argument with its own type, not a string (like the function getArgument() does)...
Definition: jsonObject.h:374
virtual void addChild(Child &&child)
Adds a child in the Node.
Definition: jsonObject.h:143
std::string specifier
String that represents the specifier of the object. For example: "name": "electron" is an object of w...
Definition: jsonObject.h:82
Type
bool isNode() const override
Tells if the Object is a Node or not.
Definition: jsonObject.h:117
virtual bool isNode() const
Tells if the Object is a Node or not. This function is reimplemented in Node.
Definition: jsonObject.h:71
Template class inherited from Object that stores a parameter of a .json file. This class does not hav...
Definition: jsonObject.h:335
std::vector< Child >::const_iterator end() const
Definition: jsonObject.h:183
List(std::string const &specifier)
Construtor with one paramter that initializes specifier.
Definition: jsonObject.h:238
Inherits from JSON::Object, specialized in JSON Node. A Node owns a vector of Object. The Node&#39;s children can either be Leaf or other Node objects. This allows to store the tree structure of a .json file.
Definition: jsonObject.h:97
void addChild(std::unique_ptr< T > &child)
Adds a child in the List. Checks if the specifier is the same as the one of the List (each element mu...
Definition: jsonObject.h:265
std::vector< Child >::const_iterator begin() const
Definition: jsonObject.h:176
Abstract object in JSON tree structure. Can be specialized either in Node or in Leaf<T> with a specif...
Definition: jsonObject.h:43
void addChild(std::unique_ptr< T > &&child)
Adds a child in the Node.
Definition: jsonObject.h:152
void addChild(std::unique_ptr< T > &&child)
Adds a child in the List. Checks if the specifier is the same as the one of the List (each element mu...
Definition: jsonObject.h:294
AbstractLeaf(std::string const &specifier)
Construtor with one paramter that initializes specifier.
Definition: jsonObject.h:318
Abstract class inherited from Object, from which all leafs will derive. This class only contains a pu...
Definition: jsonObject.h:310
size_t size() const
Definition: jsonObject.h:160
Object(std::string const &t_specifier)
Construtor with one paramter that initializes specifier.
Definition: jsonObject.h:52
virtual ~Object()
Destructor.
Definition: jsonObject.h:57
A List is a particular Node of which all children are of the same type and have the same specifier...
Definition: jsonObject.h:229
std::unique_ptr< Object > Child
Type definition for unique_ptr<Object>. Lighten the vector of Object owned by Node objects...
Definition: jsonObject.h:89
Node(std::string const &specifier)
Construtor with one paramter that initializes specifier.
Definition: jsonObject.h:106
std::vector< Child >::iterator end()
Definition: jsonObject.h:197
std::string getArgument() const
Returns a std::string representing the argument of the Leaf.
Definition: jsonObject.h:354
void addChild(Child &&child) override
Adds a child in the List. Checks if the specifier is the same as the one of the List (each element mu...
Definition: jsonObject.h:279
Leaf(std::string const &specifier, Type t_argument)
Constructor with two paramters. The specifier and the argument of the Leaf.
Definition: jsonObject.h:346
Object * getChild(std::string const &spec) const
Returns the first child that has a given specifier.
Definition: jsonObject.h:208
std::vector< Child >::iterator begin()
Definition: jsonObject.h:190
Contains all objects related to JSON reading / writing.
Definition: csldatahandler.h:30
std::string getSpecifier() const
Definition: jsonObject.h:62