tb_node_ref_tree.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_REF_TREE_H
  6. #define TB_NODE_REF_TREE_H
  7. #include "tb_linklist.h"
  8. #include "tb_node_tree.h"
  9. #include "tb_id.h"
  10. namespace tb {
  11. class TBNode;
  12. class TBNodeRefTreeListener;
  13. /** TBNodeRefTree is a named TBNode.
  14. Nodes under this node may be referenced from other nodes, either when
  15. requesting a value (TBNode::GetValueFollowRef), or while parsing the
  16. node tree. While parsing, the values can be used for branch conditions
  17. or branches of nodes can be included. */
  18. class TBNodeRefTree : public TBLinkOf<TBNodeRefTree>
  19. {
  20. public:
  21. TBNodeRefTree(const char *name);
  22. virtual ~TBNodeRefTree();
  23. const char *GetName() const { return m_name; }
  24. const TBID &GetNameID() const { return m_name_id; }
  25. /** Read the data file. This will *not* invoke any change listener! */
  26. bool ReadFile(const char *filename) { return m_node.ReadFile(filename); }
  27. void ReadData(const char *data) { m_node.ReadData(data); }
  28. /** Add a listener that is invoked on changes in this tree. */
  29. void AddListener(TBNodeRefTreeListener *listener) { m_listeners.AddLast(listener); }
  30. /** Remove a change listener from this tree. */
  31. void RemoveListener(TBNodeRefTreeListener *listener) { m_listeners.Remove(listener); }
  32. /** Set the value for the given request and invoke the change listener.
  33. Creates the nodes that doesn't exist. */
  34. virtual void SetValue(const char *request, const TBValue &value);
  35. /** Get the value of the given request. Follows references if any.
  36. Returns a null value if the request doesn't exist. */
  37. virtual TBValue &GetValue(const char *request);
  38. /** Get the value of the given tree name and request (@treename>noderequest).
  39. Returns a null value if the given tree or request doesn't exist. */
  40. static TBValue &GetValueFromTree(const char *request);
  41. /** Return the tree with the given name, or nullptr if no matching tree exists. */
  42. static TBNodeRefTree *GetRefTree(const char *name, int name_len);
  43. /** Go through the tree of nodes recursively and include
  44. or remove branches depending on any conditions. */
  45. static void ResolveConditions(TBNode *parent_node);
  46. private:
  47. friend class TBNode;
  48. friend class TBNodeTarget;
  49. /** Follow any references to data trees and return the destination node.
  50. If there's broken references, the node will be returned. */
  51. static TBNode *FollowNodeRef(TBNode *node);
  52. void InvokeChangeListenersInternal(const char *request);
  53. TBNode m_node;
  54. TBStr m_name;
  55. TBID m_name_id;
  56. TBLinkListOf<TBNodeRefTreeListener> m_listeners;
  57. static TBLinkListOf<TBNodeRefTree> s_ref_trees;
  58. };
  59. /** TBNodeRefTreeListener receive OnDataChanged when the
  60. value of a node in a TBNodeRefTree is changed.
  61. FIX: The listener can currently only listen to one tree. */
  62. class TBNodeRefTreeListener : public TBLinkOf<TBNodeRefTreeListener>
  63. {
  64. public:
  65. /** Called when the value is changed for the given node
  66. in the given ref tree. The request is without tree name. */
  67. virtual void OnDataChanged(TBNodeRefTree *rt, const char *request) = 0;
  68. };
  69. }; // namespace tb
  70. #endif // TB_NODE_REF_TREE_H