Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / common / src / game_share / persistent_data_tree.h
blob1b79618859f99fb88d476957dbaa76ada616e370
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file contains an extension of the 'persistent data record' system
19 * It represents the contents of arbitrary CPersistentDataRecord records in a tree structure
20 * that can be interrogated or written to / read from easy to read text files
22 **/
24 #ifndef PERSISTENT_DATA_TREE_H
25 #define PERSISTENT_DATA_TREE_H
27 //-----------------------------------------------------------------------------
28 // Includes
29 //-----------------------------------------------------------------------------
31 #include "nel/misc/smart_ptr.h"
32 #include "persistent_data.h"
35 //-----------------------------------------------------------------------------
36 // class CPersistentDataTreeNode
37 //-----------------------------------------------------------------------------
39 class CPersistentDataTreeNode: public NLMISC::CRefCount
41 public:
42 // typedefs
43 typedef NLMISC::CSString TValue;
44 typedef NLMISC::CSmartPtr<CPersistentDataTreeNode> TNodePtr;
45 typedef NLMISC::CRefPtr<CPersistentDataTreeNode> TNodeRefPtr;
46 typedef std::vector<TNodePtr> TChildren;
47 typedef std::map<NLMISC::CSString,TNodePtr> TChildIndex;
48 typedef std::map<NLMISC::CSString,int> TNextPoundValue;
50 // ctor
51 CPersistentDataTreeNode(const NLMISC::CSString& name=NLMISC::CSString(),CPersistentDataTreeNode* parent=NULL);
52 CPersistentDataTreeNode(const NLMISC::CSString& name,CPersistentDataTreeNode* parent,uint32 idx);
54 // attaching a node to a parent node
55 // - note: it is not possible to detach a node once attached
56 bool attachToParent(CPersistentDataTreeNode* parent);
57 bool attachToParent(CPersistentDataTreeNode* parent,uint32 idx);
59 // reading / writing pdr objects
60 bool readFromPdr(CPersistentDataRecord& pdr);
61 bool writeToPdr(CPersistentDataRecord& pdr) const;
63 // writing to a text buffer
64 bool writeToBuffer(NLMISC::CSString& buffer) const;
66 // 'name' accessors
67 const NLMISC::CSString& getName() const;
68 NLMISC::CSString getNodeName() const;
70 // accessor to find out whether this is a standard property or a map entry
71 bool isMapEntry() const;
72 // flag a branch as being a map (children are map entries)
73 // returns true on success, false if the branch
74 // contains a value or if the branch contains children that are not map entries
75 bool flagAsMap();
77 // 'value' accessors
78 void setValue(const TValue& value);
79 const TValue& getValue() const;
80 const TValue& getValue(const NLMISC::CSString& nameList) const;
82 // 'child' accessors
83 const CPersistentDataTreeNode* getChild(const NLMISC::CSString& name) const;
84 CPersistentDataTreeNode* getChild(const NLMISC::CSString& name);
85 const TChildren& getChildren() const;
86 CPersistentDataTreeNode* getDescendant(const NLMISC::CSString& nameList,bool createIfNotExist=false);
88 // compare two tree nodes (verify that their contents are eqivalent)
89 bool operator==(const CPersistentDataTreeNode& other) const;
91 private:
92 // private data common to all nodes
93 NLMISC::CSString _Name;
94 TNodeRefPtr _Parent;
95 bool _IsValue;
97 // value for leaf nodes
98 TValue _Value;
100 // containers for children and associated indexes etc for branch nodes
101 TChildren _Children;
102 TChildIndex _ChildIndex;
103 TNextPoundValue _NextPoundValue;
104 bool _IsMap;
108 //-----------------------------------------------------------------------------
109 // class CPersistentDataTree
110 //-----------------------------------------------------------------------------
112 class CPersistentDataTree
114 public:
115 // typedefs
116 typedef CPersistentDataTreeNode::TValue TValue;
117 typedef CPersistentDataTreeNode::TNodePtr TNodePtr;
119 // ctor
120 CPersistentDataTree();
122 // reading from different sources
123 bool readFromBuffer(const NLMISC::CSString& buffer);
124 bool readFromFile(const NLMISC::CSString& fileName);
125 bool readFromPdr(CPersistentDataRecord& pdr);
127 // writing to different sources
128 bool writeToBuffer(NLMISC::CSString& buffer) const;
129 bool writeToFile(const NLMISC::CSString& fileName) const;
130 bool writeToPdr(CPersistentDataRecord& pdr) const;
132 // 'value' accessors
133 void setValue(const TValue& nodeName,const NLMISC::CSString& value);
134 const TValue& getValue(const NLMISC::CSString& nodeName) const;
136 // accessors for navigaiting in the tree
137 CPersistentDataTreeNode* getNode(const NLMISC::CSString& nodeName);
139 // compare two data trees (verify that their contents are eqivalent)
140 bool operator==(const CPersistentDataTree& other) const;
142 private:
143 // private data
144 TNodePtr _Child;
148 //-----------------------------------------------------------------------------
149 #endif