Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_node_tree.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_NODE_TREE_H
7 #define TB_NODE_TREE_H
8 
9 #include "parser/tb_parser.h"
10 #include "tb_linklist.h"
11 
12 namespace tb {
13 
15  TB_NODE_READ_FLAGS_NONE = 0,
19 };
20 MAKE_ENUM_FLAG_COMBO(TB_NODE_READ_FLAGS);
21 
30 class TBNode : public TBLinkOf<TBNode>
31 {
32 public:
33  TBNode() : m_name(nullptr), m_parent(nullptr), m_cycle_id(0) {}
34  ~TBNode();
35 
37  static TBNode *Create(TBStr name);
38 
40  bool ReadFile(const TBStr & filename, TB_NODE_READ_FLAGS flags = TB_NODE_READ_FLAGS_NONE);
41 
43  void ReadData(const char *data, TB_NODE_READ_FLAGS flags = TB_NODE_READ_FLAGS_NONE);
44 
46  void ReadData(const char *data, int data_len, TB_NODE_READ_FLAGS flags = TB_NODE_READ_FLAGS_NONE);
47 
49  void Clear();
50 
52  bool WriteFile(const TBStr & filename);
53 
56  bool WriteNode(TBStr & str, int depth = 0);
57 
59  void Add(TBNode *n) { m_children.AddLast(n); n->m_parent = this; }
60 
62  void AddBefore(TBNode *n, TBNode *reference) { m_children.AddBefore(n, reference); n->m_parent = this; }
63 
65  void AddAfter(TBNode *n, TBNode *reference) { m_children.AddAfter(n, reference); n->m_parent = this; }
66 
68  void Remove(TBNode *n) { m_children.Remove(n); n->m_parent = nullptr; }
69 
71  void Delete(TBNode *n) { m_children.Delete(n); }
72 
76  bool CloneChildren(TBNode *source);
77 
83  };
84 
91  TBNode *GetNode(const char *request, GET_MISS_POLICY mp = GET_MISS_POLICY_NULL);
92 
94  const TBStr & GetName() const { return m_name; }
95 
97  TBValue &GetValue() { return m_value; }
98 
102 
106  int GetValueInt(const char *request, int def);
107 
111  float GetValueFloat(const char *request, float def);
112 
117  TBStr GetValueString(const char *request, const char *def);
118 
120  TBStr GetValueStringRaw(const char *request, const char *def);
121 
124  static const char *GetNextNodeSeparator(const char *request);
125 
126  inline TBNode *GetParent() const { return m_parent; }
127  inline TBNode *GetFirstChild() const { return m_children.GetFirst(); }
128  inline TBNode *GetLastChild() const { return m_children.GetLast(); }
129 private:
130 friend class TBNodeTarget;
131 friend class TBNodeRefTree;
132  TBNode *GetNodeFollowRef(const char *request,
134  TBNode *GetNodeInternal(const char *name, int name_len) const;
135  TBStr m_name;
136  TBValue m_value;
137  TBLinkListOf<TBNode> m_children;
138  TBNode *m_parent;
139  uint32_t m_cycle_id;
140 };
141 
142 } // namespace tb
143 
144 #endif // TB_NODE_TREE_H
static const char * GetNextNodeSeparator(const char *request)
Get the next position in request that is a sub node separator, or the end of the string.
Definition: tb_node_tree.cpp:32
float GetValueFloat(const char *request, float def)
Get a value from the given request as an float.
Definition: tb_node_tree.cpp:108
bool ReadFile(const TBStr &filename, TB_NODE_READ_FLAGS flags=TB_NODE_READ_FLAGS_NONE)
Read a tree of nodes from file into this node.
Definition: tb_node_tree.cpp:290
void ReadData(const char *data, TB_NODE_READ_FLAGS flags=TB_NODE_READ_FLAGS_NONE)
Read a tree of nodes from a null terminated string buffer.
Definition: tb_node_tree.cpp:304
bool WriteNode(TBStr &str, int depth=0)
Recursively write a node and it&#39;s children to a string at depth.
Definition: tb_node_tree.cpp:338
static TBNode * Create(TBStr name)
Create a new node with the given name.
Definition: tb_node_tree.cpp:23
void AddBefore(TBNode *n, TBNode *reference)
Add node before the reference node (which must be a child to this node).
Definition: tb_node_tree.h:62
GET_MISS_POLICY
Definition: tb_node_tree.h:78
GetNode will return nullptr if the node doesn&#39;t exist.
Definition: tb_node_tree.h:80
bool WriteFile(const TBStr &filename)
Write a node and it&#39;s children to a file.
Definition: tb_node_tree.cpp:325
TBValue & GetValueFollowRef()
Returns the value of this node.
Definition: tb_node_tree.cpp:97
void Remove(TBNode *n)
Remove child node n from this node.
Definition: tb_node_tree.h:68
void Clear()
Clear the contens of this node.
Definition: tb_node_tree.cpp:319
void AddAfter(TBNode *n, TBNode *reference)
Add node after the reference node (which must be a child to this node).
Definition: tb_node_tree.h:65
const TBStr & GetName() const
Returns the name of this node.
Definition: tb_node_tree.h:94
void Add(TBNode *n)
Add node as child to this node.
Definition: tb_node_tree.h:59
bool CloneChildren(TBNode *source)
Create duplicates of all items in source and add them to this node.
Definition: tb_node_tree.cpp:78
void Delete(TBNode *n)
Remove and delete child node n from this node.
Definition: tb_node_tree.h:71
TBStr is a simple string class.
Definition: tb_str.h:62
TBNode is a tree node with a string name and a value (TBValue).
Definition: tb_node_tree.h:30
TBNode * GetNode(const char *request, GET_MISS_POLICY mp=GET_MISS_POLICY_NULL)
Get a node from the given request.
Definition: tb_node_tree.cpp:39
TB_NODE_READ_FLAGS
Definition: tb_node_tree.h:14
GetNode will create all missing nodes for the request.
Definition: tb_node_tree.h:82
TBValue holds value of a specific type.
Definition: tb_value.h:59
TBStr GetValueStringRaw(const char *request, const char *def)
Same as GetValueString, but won&#39;t look up language string references.
Definition: tb_node_tree.cpp:132
TBValue & GetValue()
Returns the value of this node.
Definition: tb_node_tree.h:97
Read nodes without clearing first.
Definition: tb_node_tree.h:18
int GetValueInt(const char *request, int def)
Get a value from the given request as an integer.
Definition: tb_node_tree.cpp:102
TBStr GetValueString(const char *request, const char *def)
Get a value from the given request as an string.
Definition: tb_node_tree.cpp:114