Isle
Loading...
Searching...
No Matches
legotree.cpp
Go to the documentation of this file.
1#include "legotree.h"
2
3#include "decomp.h"
4#include "legostorage.h"
5
9
10// FUNCTION: LEGO1 0x10099d60
11// FUNCTION: BETA10 0x10187dd0
13{
14 m_data = NULL;
15 m_numChildren = 0;
16 m_children = NULL;
17}
18
19// FUNCTION: LEGO1 0x10099da0
20// FUNCTION: BETA10 0x10187e10
22{
23 if (m_data) {
24 delete m_data;
25 }
26 if (m_children) {
27 delete[] m_children;
28 }
29}
30
31// FUNCTION: LEGO1 0x10099dd0
33{
34 m_root = NULL;
35}
36
37// FUNCTION: LEGO1 0x10099e00
39{
40 if (m_root) {
42 }
43}
44
45// FUNCTION: LEGO1 0x10099e20
47{
48 return Read(p_storage, m_root);
49}
50
51// FUNCTION: LEGO1 0x10099e40
53{
54 return Write(p_storage, m_root);
55}
56
57// FUNCTION: LEGO1 0x10099e60
59{
60 LegoResult result;
61 p_node = new LegoTreeNode();
62 p_node->SetData(CreateData());
63 if ((result = p_node->GetData()->Read(p_storage)) != SUCCESS) {
64 return result;
65 }
66 LegoU32 numChildren;
67 if ((result = p_storage->Read(&numChildren, sizeof(numChildren))) != SUCCESS) {
68 return result;
69 }
70 if (numChildren) {
71 p_node->SetChildren(new LegoTreeNode*[numChildren]);
72 for (LegoU32 i = 0; i < numChildren; i++) {
73 LegoTreeNode* node;
74 if ((result = Read(p_storage, node)) != SUCCESS) {
75 return result;
76 }
77 p_node->SetNumChildren(p_node->GetNumChildren() + 1);
78 p_node->SetChild(i, node);
79 }
80 }
81 return SUCCESS;
82}
83
84// FUNCTION: LEGO1 0x1009a020
86{
87 LegoResult result;
88 if (p_node->GetData()) {
89 if ((result = p_node->GetData()->Write(p_storage)) != SUCCESS) {
90 return result;
91 }
92 }
93 LegoU32 numChildren = p_node->GetNumChildren();
94 if ((result = p_storage->Write(&numChildren, sizeof(numChildren))) != SUCCESS) {
95 return result;
96 }
97 for (LegoU32 i = 0; i < p_node->GetNumChildren(); i++) {
98 if ((result = Write(p_storage, p_node->GetChild(i))) != SUCCESS) {
99 return result;
100 }
101 }
102 return SUCCESS;
103}
104
105// FUNCTION: LEGO1 0x1009a0a0
107{
108 for (LegoU32 i = 0; i < p_node->GetNumChildren(); i++) {
109 Delete(p_node->GetChild(i));
110 }
111 delete p_node;
112}
Abstract base class providing an interface for file-like storage with binary and text read/write oper...
Definition: legostorage.h:16
virtual LegoResult Write(const void *p_buffer, LegoU32 p_size)=0
Write bytes from buffer into storage.
virtual LegoResult Read(void *p_buffer, LegoU32 p_size)=0
Read bytes from storage into buffer.
[AI] Abstract base class for storing data payloads inside nodes of a tree structure,...
Definition: legotree.h:15
virtual LegoResult Read(LegoStorage *p_storage)
[AI] Virtual function for deserializing node data from a generic LegoStorage device.
Definition: legotree.h:28
virtual LegoResult Write(LegoStorage *p_storage)
[AI] Virtual function for serializing node data to a generic LegoStorage device.
Definition: legotree.h:34
[AI] Represents a node within a general, N-ary tree structure.
Definition: legotree.h:44
void SetData(LegoTreeNodeData *p_data)
[AI] Associates a data payload with this node.
Definition: legotree.h:59
void SetChild(LegoU32 p_i, LegoTreeNode *p_child)
[AI] Assigns a child node pointer at the specified index in this node's children array.
Definition: legotree.h:77
LegoTreeNodeData * GetData()
[AI] Returns the data payload stored at this node (may be nullptr). [AI]
Definition: legotree.h:54
LegoTreeNode ** m_children
[AI] Pointer to array of tree node children (size = m_numChildren). nullptr if no children allocated....
Definition: legotree.h:95
LegoU32 GetNumChildren()
[AI] Returns the number of direct children of this node. [AI]
Definition: legotree.h:63
void SetChildren(LegoTreeNode **p_children)
[AI] Assigns the entire children pointer array.
Definition: legotree.h:86
LegoTreeNode * GetChild(LegoU32 p_i)
[AI] Gets the child node at the specified index (no bounds checking).
Definition: legotree.h:72
void SetNumChildren(LegoU32 p_numChildren)
[AI] Sets the number of children for this node (does not resize pointer array).
Definition: legotree.h:68
virtual ~LegoTreeNode()
[AI] Destructor cleans up node data and children recursively. [AI]
Definition: legotree.cpp:21
LegoTreeNodeData * m_data
[AI] Pointer to node data payload, owned by node. nullptr indicates no data present....
Definition: legotree.h:93
[AI] Represents an N-ary tree of LegoTreeNode objects, with support for recursive reading and writing...
Definition: legotree.h:102
LegoTreeNode * m_root
[AI] Root node of the tree. nullptr if tree is empty. Owned by the tree instance. [AI]
Definition: legotree.h:153
virtual LegoResult Write(LegoStorage *p_storage)
[AI] Serializes the tree structure recursively to the given storage.
Definition: legotree.cpp:52
virtual LegoTreeNodeData * CreateData()
[AI] Virtual function to instantiate node payload objects for the tree structure.
Definition: legotree.h:151
LegoTree()
[AI] Constructs an empty tree (root not allocated). [AI]
Definition: legotree.cpp:32
void Delete(LegoTreeNode *p_node)
[AI] Recursively deletes an entire subtree starting at the given node.
Definition: legotree.cpp:106
virtual ~LegoTree()
[AI] Recursively destroys all tree nodes and their payloads. [AI]
Definition: legotree.cpp:38
virtual LegoResult Read(LegoStorage *p_storage)
[AI] Loads the tree structure recursively from the given storage.
Definition: legotree.cpp:46
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
unsigned long LegoU32
[AI] Unsigned 32-bit integer type for cross-platform compatibility.
Definition: legotypes.h:71
LegoS32 LegoResult
[AI] Function result type (return code): typically SUCCESS (0) or FAILURE (-1).
Definition: legotypes.h:101
#define SUCCESS
[AI] Used to indicate a successful operation in result codes.
Definition: legotypes.h:30