Dynalib Utils
Node.h
Go to the documentation of this file.
1 #ifndef KAML_NODE_H
2 #define KAML_NODE_H
3 
4 #include <chrono>
5 using namespace std;
6 using namespace std::chrono;
7 
8 #include "../String.h"
9 #include "../DynaList.h"
10 #include "../DynaHashMap.h"
11 #include "../IntWrapper.h"
12 #include "../TimeUtils.h"
13 
14 #define MAKE_LOCAL_REF_NODE Node(NodeType::UNTYPED, false)
15 #define NODE_GET_INIT Node _presult = Node(NodeType::UNTYPED, false)
16 #define NODE_GET_STR(N,K,V) if (Node::present(N, K, _presult)) { V = _presult(); }
17 #define NODE_GET_SEQ(N,K,V) if (Node::present(N, K, _presult)) { V = _presult(); }
18 #define NODE_GET_PROPER(N,K,V) if (Node::present(N, K, _presult)) { V = _presult(); }
19 #define NODE_GET_INT(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asInt(); }
20 #define NODE_GET_FLOAT(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asFloat(); }
21 #define NODE_GET_BOOL(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asBool(); }
22 #define NODE_GET_TIME(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asTime(); }
23 #define NODE_GET_TS(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asTimestamp(); }
24 #define NODE_GET_DATE(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asDate(); }
25 #define NODE_GET_LIST(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asList(); }
26 #define NODE_GET_MAP(N,K,V) if (Node::present(N, K, _presult)) { V = _presult.asMap(); }
27 
28 namespace Graph {
29  class Node;
30 
31  MAKE_LISTTYPE_DEF(Node, Node);
32  MAKE_MAPTYPE_DEF(String, Node, Node);
33 
34  enum class NodeType { UNTYPED, SCALAR, LIST, MAP };
36 
37  union NodeValue {
38  bool boolValue;
39  long long intValue;
40  long double floatValue;
41  String* strValue = nullptr;
43  NodeList* listValue;
44  NodeMap* mapValue;
45  };
46 
47  class Node : public IHashable<Node>, public ICopyable<Node> {
48  bool _ownsData;
49  String* _key;
50  NodeValue _value;
51  NodeType _nodeType;
52  ScalarType _scalarType;
53 
54  private:
55  void _ensureNodeType(NodeType nodeType);
56  Node* _addToList(Node* node);
57  Node* _addToMap(const String& key, Node* node, bool append = true);
58  void _setNode(const Node& other);
59  void _reset();
60 
61  public:
62  static bool present(const Node& node, const String& fldName, Node& result);
63  static int getListLeadingScalarCount(const Node& node);
64 
65  Node();
66  explicit Node(NodeType type, bool ownsData = true);
67  Node(const char* value, bool ownsData = true);
68  // explicit Node(char* value, bool ownsData = true);
69  // explicit Node(String* value, bool ownsData = true);
70  // explicit Node(const String& value, bool ownsData = true);
71  // explicit Node(String& value, bool ownsData = true);
72  Node(int value, bool ownsData = true);
73  Node(long long value, bool ownsData = true);
74 
75  virtual ~Node();
76  Node(const Node& other);
77  Node* copy() override;
78  void setOwnsData(bool ownsData);
79  bool isOwnsData();
80  String* key() const;
81  NodeValue& value();
82  const NodeType type() const;
83  const ScalarType scalarType() const;
84  Node& reset();
85 
86  NodeList& list() const;
87  NodeMap& map() const;
88  Node* listElem(int index, bool throwInvalid = false) const;
89  Node* mapElem(const String& key, bool throwInvalid = false) const;
90 
91  Node* operator()(int index);
92  const Node* operator()(int index) const;
93  Node* operator()(const String& key);
94  const Node* operator()(const String& key) const;
95  const String operator()() const;
96 
97  Node& operator[](int index);
98  const Node& operator[](int index) const;
99  Node& operator[](const String& key);
100  const Node& operator[](const String& key) const;
101 
102  Node& operator=(const char* value);
103  Node& operator=(const String& value);
104  Node& operator=(nullptr_t value);
105  Node& operator=(bool value);
106  Node& operator=(char value);
107  Node& operator=(int value);
108  Node& operator=(long long value);
109  Node& operator=(float value);
110  Node& operator=(long double value);
111  Node& operator=(system_clock::time_point value);
112  Node& operator=(Time value);
113  Node& operator=(Timestamp value);
114 
115  Node& operator+=(const char* value);
116  Node& operator+=(const String& value);
117  Node& operator+=(nullptr_t value);
118  Node& operator+=(bool value);
119  Node& operator+=(char value);
120  Node& operator+=(int value);
121  Node& operator+=(long long value);
122  Node& operator+=(float value);
123  Node& operator+=(long double value);
125  Node& operator+=(Time value);
126  Node& operator+=(Timestamp value);
127 
128  static Node* newUntypedNode();
129 
130  const bool isUntyped() const;
131  const bool isScalar() const;
132  const bool isList() const;
133  const bool isMap() const;
134  const bool isBool() const;
135  const bool isString() const;
136  const bool isSeq() const;
137  const bool isProper() const;
138  const bool isInt() const;
139  const bool isFloat() const;
140  const bool isTime() const;
141  const bool isTimestamp() const;
142  const bool isDate() const;
143 
144  const String asString() const;
145  const bool asBool() const;
146  const long long asInt() const;
147  const long double asFloat() const;
148  const String asNullPtr() const;
149  const system_clock::time_point asTime() const;
150  const system_clock::time_point asTimestamp() const;
151  const system_clock::time_point asDate() const;
152 
153  // These methods are used to set the type and value of this node
154  void setUntypedNode();
155  void setNode(const Node& other);
156  void setScalar(const char* value, ScalarType strType = ScalarType::STRING);
157  void setScalar(bool value);
158  void setScalar(long long value);
159  void setScalar(long double value);
160  void setScalar(system_clock::time_point& value, ScalarType timeType);
161  void setList();
162  void setMap();
163 
164  Node* addNode(Node* node);
165  Node* addUntypedNode();
166  Node* addNode(const String& key, Node* node, bool append = true);
167  Node* addUntypedNode(const String& key, bool append = true);
168 
169  // These methods are used to add a value to this node, ensuring this node is a list
170  Node* addScalar(const char* value, ScalarType strType = ScalarType::STRING);
171  Node* addScalar(bool value);
172  Node* addScalar(long long value);
173  Node* addScalar(long double value);
174  Node* addScalar(system_clock::time_point& value, ScalarType timeType);
175  Node* addList();
176  Node* addMap();
177 
178  // These methods are used to add a value to this node, ensuring this node is a map
179  Node* addScalar(const String& key, const char* value, ScalarType strType = ScalarType::STRING);
180  Node* addScalar(const String& key, bool value);
181  Node* addScalar(const String& key, long long value);
182  Node* addScalar(const String& key, long double value);
183  Node* addScalar(const String& key, system_clock::time_point& value, ScalarType timeType);
184  Node* addList(const String& key);
185  Node* addMap(const String& key);
186 
187  int hashCode() const override;
188  bool operator== (const Node &other) const override;
189  };
190 
191 }
192 
193 #endif
CONSTCD11 bool operator==(const day &x, const day &y) NOEXCEPT
Definition: date.h:1274
Definition: IHashable.h:10
Definition: Node.h:37
NodeList * listValue
Definition: Node.h:43
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4005
NodeType
Definition: Node.h:34
std::chrono::time_point< Clock, Duration > time_point
Definition: tz.h:2265
Definition: String.h:60
Definition: Node.h:47
ScalarType
Definition: Node.h:35
system_clock::time_point * timeValue
Definition: Node.h:42
auto operator+=(std::string &lhs, StringRef const &sr) -> std::string &
MAKE_MAPTYPE_DEF(String, Node, Node)
bool boolValue
Definition: Node.h:38
Definition: TimeUtils.h:14
Definition: Node.cpp:13
long double floatValue
Definition: Node.h:40
long long intValue
Definition: Node.h:39
NodeMap * mapValue
Definition: Node.h:44
MAKE_LISTTYPE_DEF(String, String)
int hashCode(bool key)
Definition: HashCoder.cpp:27
Definition: ICopyable.h:8
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:4282
Definition: TimeUtils.h:9