Skip to content

Commit 30883a3

Browse files
Add property tree documentation
1 parent 5a523e7 commit 30883a3

File tree

2 files changed

+144
-8
lines changed

2 files changed

+144
-8
lines changed

tesseract_common/include/tesseract_common/property_tree.h

Lines changed: 132 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,53 +26,180 @@ constexpr std::string_view MINIMUM{ "minimum" };
2626
constexpr std::string_view MAXIMUM{ "maximum" };
2727
} // namespace property_attribute
2828

29+
/**
30+
* @brief A hierarchical property tree that stores values, metadata attributes,
31+
* and supports schema merging, default handling, and custom validation.
32+
*
33+
* Each node may contain:
34+
* - A YAML::Node value (scalar, sequence, or map)
35+
* - Metadata attributes (as a map of YAML::Node)
36+
* - Child PropertyTree nodes for nested structures
37+
* - Custom validator functions to enforce constraints
38+
*/
2939
class PropertyTree
3040
{
3141
public:
42+
/**
43+
* @brief Signature for custom validator functions.
44+
* @param node The PropertyTree node being validated.
45+
*/
3246
using ValidatorFn = std::function<void(const PropertyTree&)>;
3347

48+
/**
49+
* @brief Default constructor.
50+
*/
3451
PropertyTree() = default;
3552

53+
/**
54+
* @brief Merge a schema tree into this node.
55+
* * Copies schema attributes, applies non-required defaults,
56+
* merges child structure, and registers schema validators.
57+
* @param schema The schema node to merge from.
58+
*/
3659
void mergeSchema(const PropertyTree& schema);
3760

61+
/**
62+
* @brief Validate this tree using registered validators.
63+
* * Traverses the tree, invoking each node's validators.
64+
*/
3865
void validate() const;
3966

67+
/**
68+
* @brief Register a custom validator for this node.
69+
* @param fn Validator function to invoke during validate().
70+
*/
4071
void addValidator(ValidatorFn fn);
4172

73+
/**
74+
* @brief Access or create a child node by key.
75+
* @param key Child node identifier.
76+
* @return Reference to the child node.
77+
*/
4278
PropertyTree& get(std::string_view key);
79+
80+
/**
81+
* @brief Access a child node by key (const).
82+
* @param key Child node identifier.
83+
* @return Const reference to the child node.
84+
*/
4385
const PropertyTree& get(std::string_view key) const;
86+
87+
/**
88+
* @brief Find a child node without creating it.
89+
* @param key Child node identifier.
90+
* @return Pointer to the child or nullptr if missing.
91+
*/
4492
const PropertyTree* find(std::string_view key) const;
4593

94+
/**
95+
* @brief Set the YAML value for this node.
96+
* @param v YAML::Node representing the value.
97+
*/
4698
void setValue(const YAML::Node& v);
99+
100+
/**
101+
* @brief Get the YAML value stored in this node.
102+
* @return Const reference to YAML::Node.
103+
*/
47104
const YAML::Node& getValue() const;
48105

106+
/**
107+
* @brief List all immediate child keys.
108+
* @return Vector of child key strings.
109+
*/
49110
std::vector<std::string> keys() const;
50111

112+
/**
113+
* @brief Set a metadata attribute (YAML::Node form).
114+
* @param name Attribute name.
115+
* @param attr YAML::Node value.
116+
*/
51117
void setAttribute(std::string_view name, const YAML::Node& attr);
118+
119+
/**
120+
* @brief Set a string attribute.
121+
* @param name Attribute name.
122+
* @param attr String value.
123+
*/
52124
void setAttribute(std::string_view name, std::string_view attr);
125+
126+
/**
127+
* @brief Set a C-string attribute.
128+
*/
53129
void setAttribute(std::string_view name, const char* attr);
130+
131+
/**
132+
* @brief Set a boolean attribute.
133+
*/
54134
void setAttribute(std::string_view name, bool attr);
135+
136+
/**
137+
* @brief Set an integer attribute.
138+
*/
55139
void setAttribute(std::string_view name, int attr);
140+
141+
/**
142+
* @brief Set a double attribute.
143+
*/
56144
void setAttribute(std::string_view name, double attr);
57145

58-
std::optional<YAML::Node> getAttribute(std::string_view name) const;
146+
/**
147+
* @brief Check if an attribute exists and is non-null.
148+
* @param name Attribute name.
149+
* @return True if present and non-null.
150+
*/
59151
bool hasAttribute(std::string_view name) const;
60152

153+
/**
154+
* @brief Retrieve an attribute value.
155+
* @param name Attribute name.
156+
* @return Optional<YAML::Node> if attribute exists.
157+
*/
158+
std::optional<YAML::Node> getAttribute(std::string_view name) const;
159+
160+
/**
161+
* @brief List all metadata attribute keys.
162+
* @return Vector of attribute key strings.
163+
*/
164+
std::vector<std::string> getAttributeKeys() const;
165+
166+
/**
167+
* @brief Build a PropertyTree from a YAML::Node.
168+
* @param node Root YAML::Node.
169+
* @return Populated PropertyTree hierarchy.
170+
*/
61171
static PropertyTree fromYAML(const YAML::Node& node);
62172

173+
/**
174+
* @brief Serialize this tree to a YAML::Node.
175+
* @param exclude_attributes If true, omit the attributes map.
176+
* @return YAML::Node representation of the tree.
177+
*/
63178
YAML::Node toYAML(bool exclude_attributes = true) const;
64179

65180
private:
66-
YAML::Node value_;
67-
std::map<std::string, YAML::Node> attributes_;
68-
std::map<std::string, PropertyTree> children_;
69-
std::vector<ValidatorFn> validators_;
181+
YAML::Node value_; /**< Stored YAML value */
182+
std::map<std::string, YAML::Node> attributes_; /**< Metadata attributes */
183+
std::map<std::string, PropertyTree> children_; /**< Nested child nodes */
184+
std::vector<ValidatorFn> validators_; /**< Registered validators */
70185
};
71186

187+
/**
188+
* @brief Ensure a required attribute exists and is non-null.
189+
* @param node PropertyTree node to validate.
190+
*/
72191
void validateRequired(const PropertyTree& node);
73192

193+
/**
194+
* @brief Enforce numeric range if 'minimum'/'maximum' attributes are set.
195+
* @param node PropertyTree node to validate.
196+
*/
74197
void validateRange(const PropertyTree& node);
75198

199+
/**
200+
* @brief Enforce that the node's value is one of the 'enum' attribute list.
201+
* @param node PropertyTree node to validate.
202+
*/
76203
void validateEnum(const PropertyTree& node);
77204

78205
} // namespace tesseract_common

tesseract_common/src/property_tree.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ void PropertyTree::setAttribute(std::string_view name, int attr) { setAttribute(
107107

108108
void PropertyTree::setAttribute(std::string_view name, double attr) { setAttribute(name, YAML::Node(attr)); }
109109

110+
bool PropertyTree::hasAttribute(std::string_view name) const
111+
{
112+
auto it = attributes_.find(std::string(name));
113+
return (it != attributes_.end() && it->second && !it->second.IsNull());
114+
}
115+
110116
std::optional<YAML::Node> PropertyTree::getAttribute(std::string_view name) const
111117
{
112118
auto it = attributes_.find(std::string(name));
@@ -115,10 +121,13 @@ std::optional<YAML::Node> PropertyTree::getAttribute(std::string_view name) cons
115121
return std::nullopt;
116122
}
117123

118-
bool PropertyTree::hasAttribute(std::string_view name) const
124+
std::vector<std::string> PropertyTree::getAttributeKeys() const
119125
{
120-
auto it = attributes_.find(std::string(name));
121-
return (it != attributes_.end() && it->second && !it->second.IsNull());
126+
std::vector<std::string> res;
127+
res.reserve(attributes_.size());
128+
for (const auto& pair : attributes_)
129+
res.push_back(pair.first);
130+
return res;
122131
}
123132

124133
PropertyTree PropertyTree::fromYAML(const YAML::Node& node)

0 commit comments

Comments
 (0)