tb_node_tree.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef TB_NODE_TREE_H
  6. #define TB_NODE_TREE_H
  7. #include "parser/tb_parser.h"
  8. #include "tb_linklist.h"
  9. namespace tb {
  10. enum TB_NODE_READ_FLAGS {
  11. TB_NODE_READ_FLAGS_NONE = 0,
  12. /** Read nodes without clearing first. Can be used to append
  13. data from multiple sources, or inject dependencies. */
  14. TB_NODE_READ_FLAGS_APPEND = 1,
  15. };
  16. MAKE_ENUM_FLAG_COMBO(TB_NODE_READ_FLAGS);
  17. /** TBNode is a tree node with a string name and a value (TBValue).
  18. It may have a parent TBNode and child TBNodes.
  19. Getting the value of this node or any child, may optionally follow
  20. references to nodes in any existing TBNodeRefTree (by name).
  21. During ReadFile/ReadData, it may also select which branches to include
  22. or exclude conditionally by lookup up values in TBNodeRefTree. */
  23. class TBNode : public TBLinkOf<TBNode>
  24. {
  25. public:
  26. TBNode() : m_name(nullptr), m_parent(nullptr), m_cycle_id(0) {}
  27. ~TBNode();
  28. /** Create a new node with the given name. */
  29. static TBNode *Create(const char *name);
  30. /** Read a tree of nodes from file into this node. Returns true on success. */
  31. bool ReadFile(const char *filename, TB_NODE_READ_FLAGS flags = TB_NODE_READ_FLAGS_NONE);
  32. /** Read a tree of nodes from a null terminated string buffer. */
  33. void ReadData(const char *data, TB_NODE_READ_FLAGS flags = TB_NODE_READ_FLAGS_NONE);
  34. /** Read a tree of nodes from a buffer with a known length. */
  35. void ReadData(const char *data, int data_len, TB_NODE_READ_FLAGS flags = TB_NODE_READ_FLAGS_NONE);
  36. /** Clear the contens of this node. */
  37. void Clear();
  38. // FIX: Add write support!
  39. //bool WriteFile(const char *filename);
  40. /** Add node as child to this node. */
  41. void Add(TBNode *n) { m_children.AddLast(n); n->m_parent = this; }
  42. /** Add node before the reference node (which must be a child to this node). */
  43. void AddBefore(TBNode *n, TBNode *reference) { m_children.AddBefore(n, reference); n->m_parent = this; }
  44. /** Add node after the reference node (which must be a child to this node). */
  45. void AddAfter(TBNode *n, TBNode *reference) { m_children.AddAfter(n, reference); n->m_parent = this; }
  46. /** Remove child node n from this node. */
  47. void Remove(TBNode *n) { m_children.Remove(n); n->m_parent = nullptr; }
  48. /** Remove and delete child node n from this node. */
  49. void Delete(TBNode *n) { m_children.Delete(n); }
  50. /** Create duplicates of all items in source and add them to this node.
  51. Note: Nodes does not replace existing nodes with the same name. Cloned nodes
  52. are added after any existing nodes. */
  53. bool CloneChildren(TBNode *source);
  54. enum GET_MISS_POLICY {
  55. /** GetNode will return nullptr if the node doesn't exist. */
  56. GET_MISS_POLICY_NULL,
  57. /** GetNode will create all missing nodes for the request. */
  58. GET_MISS_POLICY_CREATE
  59. };
  60. /** Get a node from the given request.
  61. If the node doesn't exist, it will either return nullptr or create
  62. missing nodes, depending on the miss policy.
  63. It can find nodes in children as well. Names are separated by a ">".
  64. Ex: GetNode("dishes>pizza>special>batman") */
  65. TBNode *GetNode(const char *request, GET_MISS_POLICY mp = GET_MISS_POLICY_NULL);
  66. /** Returns the name of this node. */
  67. const char *GetName() const { return m_name; }
  68. /** Returns the value of this node. */
  69. TBValue &GetValue() { return m_value; }
  70. /** Returns the value of this node.
  71. Will follow eventual references to TBNodeRefTree. */
  72. TBValue &GetValueFollowRef();
  73. /** Get a value from the given request as an integer.
  74. Will follow eventual references to TBNodeRefTree.
  75. If the value is not specified, it returns the default value (def). */
  76. int GetValueInt(const char *request, int def);
  77. /** Get a value from the given request as an float.
  78. Will follow eventual references to TBNodeRefTree.
  79. If the value is not specified, it returns the default value (def). */
  80. float GetValueFloat(const char *request, float def);
  81. /** Get a value from the given request as an string.
  82. Will follow eventual references to TBNodeRefTree.
  83. Will also return any referenced language string.
  84. If the value is not specified, it returns the default value (def). */
  85. const char *GetValueString(const char *request, const char *def);
  86. /** Same as GetValueString, but won't look up language string references. */
  87. const char *GetValueStringRaw(const char *request, const char *def);
  88. /** Get the next position in request that is a sub node separator,
  89. or the end of the string. */
  90. static const char *GetNextNodeSeparator(const char *request);
  91. inline TBNode *GetParent() const { return m_parent; }
  92. inline TBNode *GetFirstChild() const { return m_children.GetFirst(); }
  93. inline TBNode *GetLastChild() const { return m_children.GetLast(); }
  94. private:
  95. friend class TBNodeTarget;
  96. friend class TBNodeRefTree;
  97. TBNode *GetNodeFollowRef(const char *request,
  98. GET_MISS_POLICY mp = GET_MISS_POLICY_NULL);
  99. TBNode *GetNodeInternal(const char *name, int name_len) const;
  100. static TBNode *Create(const char *name, int name_len);
  101. char *m_name;
  102. TBValue m_value;
  103. TBLinkListOf<TBNode> m_children;
  104. TBNode *m_parent;
  105. uint32 m_cycle_id; ///< Used to detect circular references.
  106. };
  107. }; // namespace tb
  108. #endif // TB_NODE_TREE_H