Skip to content

Commit 3351515

Browse files
Added more comments
1 parent d9c5e8b commit 3351515

13 files changed

Lines changed: 665 additions & 29 deletions

File tree

include/rfl/Commented.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010

1111
namespace rfl {
1212

13-
/// Wraps a field value with an optional comment for formats that support comments.
14-
/// The comment is preserved during serialization and deserialization for formats like TOML and YAML
15-
/// that support inline comments. For formats without comment support, the comment is ignored.
16-
/// This allows you to maintain documentation/metadata alongside your data.
13+
/// Wraps a field value with an optional comment for formats that support
14+
/// comments. The comment is preserved during serialization for formats like
15+
/// TOML and YAML that support inline comments. For formats without comment
16+
/// support, the comment is ignored. This allows you to maintain
17+
/// documentation/metadata alongside your data.
1718
/// @tparam T The type of the field value
1819
template <class T>
1920
struct Commented {
2021
public:
2122
using Type = std::remove_cvref_t<T>;
2223
using ReflectionType = Type;
2324

24-
/// Default constructor - initializes with default-constructed value and no comment.
25+
/// Default constructor - initializes with default-constructed value and no
26+
/// comment.
2527
Commented() : value_(Type()) {}
2628

2729
/// Constructs from a value without a comment.
@@ -89,7 +91,8 @@ struct Commented {
8991
requires(std::is_convertible_v<U, Type>)
9092
Commented(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
9193

92-
/// Assigns the underlying object to its default value using the Default sentinel.
94+
/// Assigns the underlying object to its default value using the Default
95+
/// sentinel.
9396
/// @tparam U The type (must be default constructible)
9497
/// @param The default sentinel value
9598
template <class U = Type>
@@ -104,7 +107,8 @@ struct Commented {
104107
void add_comment(std::string _comment) { comment_ = std::move(_comment); }
105108

106109
/// Returns the comment associated with the field, if any.
107-
/// @return An optional containing the comment string, or nullopt if no comment
110+
/// @return An optional containing the comment string, or nullopt if no
111+
/// comment
108112
const std::optional<std::string>& comment() const { return comment_; }
109113

110114
/// Returns the underlying value.

include/rfl/comparisons.hpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ namespace rfl {
1111

1212
template <auto _threshold>
1313
struct EqualTo {
14+
/// @brief Validates that the given value is equal to the threshold.
15+
/// @tparam T The type of the value to validate.
16+
/// @param _value The value to validate.
17+
/// @return Result<T> containing the value if valid, or an error if not.
18+
/// @details This function checks if the input value is exactly equal to the
19+
/// compile-time threshold. If not, it returns an error with a descriptive
20+
/// message.
1421
template <class T>
1522
static Result<T> validate(T _value) noexcept {
1623
constexpr auto threshold = static_cast<T>(_threshold);
@@ -23,6 +30,12 @@ struct EqualTo {
2330
return _value;
2431
}
2532

33+
/// @brief Converts the validation rule to a schema representation.
34+
/// @tparam T The type for which the schema is generated.
35+
/// @return parsing::schema::ValidationType representing the EqualTo
36+
/// constraint.
37+
/// @details This function creates a schema object that encodes the equality
38+
/// constraint for serialization or documentation purposes.
2639
template <class T>
2740
static parsing::schema::ValidationType to_schema() {
2841
using ValidationType = parsing::schema::ValidationType;
@@ -36,6 +49,14 @@ struct EqualTo {
3649

3750
template <auto _threshold>
3851
struct Minimum {
52+
/// @brief Validates that the given value is greater than or equal to the
53+
/// threshold.
54+
/// @tparam T The type of the value to validate.
55+
/// @param _value The value to validate.
56+
/// @return Result<T> containing the value if valid, or an error if not.
57+
/// @details This function checks if the input value is at least the
58+
/// compile-time threshold. If not, it returns an error with a descriptive
59+
/// message.
3960
template <class T>
4061
static Result<T> validate(T _value) noexcept {
4162
constexpr auto threshold = static_cast<T>(_threshold);
@@ -48,6 +69,12 @@ struct Minimum {
4869
return _value;
4970
}
5071

72+
/// @brief Converts the validation rule to a schema representation.
73+
/// @tparam T The type for which the schema is generated.
74+
/// @return parsing::schema::ValidationType representing the Minimum
75+
/// constraint.
76+
/// @details This function creates a schema object that encodes the minimum
77+
/// value constraint for serialization or documentation purposes.
5178
template <class T>
5279
static parsing::schema::ValidationType to_schema() {
5380
using ValidationType = parsing::schema::ValidationType;
@@ -61,6 +88,14 @@ struct Minimum {
6188

6289
template <auto _threshold>
6390
struct ExclusiveMinimum {
91+
/// @brief Validates that the given value is strictly greater than the
92+
/// threshold.
93+
/// @tparam T The type of the value to validate.
94+
/// @param _value The value to validate.
95+
/// @return Result<T> containing the value if valid, or an error if not.
96+
/// @details This function checks if the input value is strictly greater than
97+
/// the compile-time threshold. If not, it returns an error with a descriptive
98+
/// message.
6499
template <class T>
65100
static Result<T> validate(T _value) noexcept {
66101
constexpr auto threshold = static_cast<T>(_threshold);
@@ -73,6 +108,12 @@ struct ExclusiveMinimum {
73108
return _value;
74109
}
75110

111+
/// @brief Converts the validation rule to a schema representation.
112+
/// @tparam T The type for which the schema is generated.
113+
/// @return parsing::schema::ValidationType representing the ExclusiveMinimum
114+
/// constraint.
115+
/// @details This function creates a schema object that encodes the exclusive
116+
/// minimum value constraint for serialization or documentation purposes.
76117
template <class T>
77118
static parsing::schema::ValidationType to_schema() {
78119
using ValidationType = parsing::schema::ValidationType;
@@ -86,6 +127,14 @@ struct ExclusiveMinimum {
86127

87128
template <auto _threshold>
88129
struct Maximum {
130+
/// @brief Validates that the given value is less than or equal to the
131+
/// threshold.
132+
/// @tparam T The type of the value to validate.
133+
/// @param _value The value to validate.
134+
/// @return Result<T> containing the value if valid, or an error if not.
135+
/// @details This function checks if the input value is at most the
136+
/// compile-time threshold. If not, it returns an error with a descriptive
137+
/// message.
89138
template <class T>
90139
static Result<T> validate(T _value) noexcept {
91140
constexpr auto threshold = static_cast<T>(_threshold);
@@ -98,6 +147,12 @@ struct Maximum {
98147
return _value;
99148
}
100149

150+
/// @brief Converts the validation rule to a schema representation.
151+
/// @tparam T The type for which the schema is generated.
152+
/// @return parsing::schema::ValidationType representing the Maximum
153+
/// constraint.
154+
/// @details This function creates a schema object that encodes the maximum
155+
/// value constraint for serialization or documentation purposes.
101156
template <class T>
102157
static parsing::schema::ValidationType to_schema() {
103158
using ValidationType = parsing::schema::ValidationType;
@@ -111,6 +166,13 @@ struct Maximum {
111166

112167
template <auto _threshold>
113168
struct ExclusiveMaximum {
169+
/// @brief Validates that the given value is strictly less than the threshold.
170+
/// @tparam T The type of the value to validate.
171+
/// @param _value The value to validate.
172+
/// @return Result<T> containing the value if valid, or an error if not.
173+
/// @details This function checks if the input value is strictly less than the
174+
/// compile-time threshold. If not, it returns an error with a descriptive
175+
/// message.
114176
template <class T>
115177
static Result<T> validate(T _value) noexcept {
116178
constexpr auto threshold = static_cast<T>(_threshold);
@@ -123,6 +185,12 @@ struct ExclusiveMaximum {
123185
return _value;
124186
}
125187

188+
/// @brief Converts the validation rule to a schema representation.
189+
/// @tparam T The type for which the schema is generated.
190+
/// @return parsing::schema::ValidationType representing the ExclusiveMaximum
191+
/// constraint.
192+
/// @details This function creates a schema object that encodes the exclusive
193+
/// maximum value constraint for serialization or documentation purposes.
126194
template <class T>
127195
static parsing::schema::ValidationType to_schema() {
128196
using ValidationType = parsing::schema::ValidationType;
@@ -136,6 +204,13 @@ struct ExclusiveMaximum {
136204

137205
template <auto _threshold>
138206
struct NotEqualTo {
207+
/// @brief Validates that the given value is not equal to the threshold.
208+
/// @tparam T The type of the value to validate.
209+
/// @param _value The value to validate.
210+
/// @return Result<T> containing the value if valid, or an error if not.
211+
/// @details This function checks if the input value is not equal to the
212+
/// compile-time threshold. If it is, it returns an error with a descriptive
213+
/// message.
139214
template <class T>
140215
static Result<T> validate(T _value) noexcept {
141216
constexpr auto threshold = static_cast<T>(_threshold);
@@ -148,6 +223,12 @@ struct NotEqualTo {
148223
return _value;
149224
}
150225

226+
/// @brief Converts the validation rule to a schema representation.
227+
/// @tparam T The type for which the schema is generated.
228+
/// @return parsing::schema::ValidationType representing the NotEqualTo
229+
/// constraint.
230+
/// @details This function creates a schema object that encodes the
231+
/// not-equal-to constraint for serialization or documentation purposes.
151232
template <class T>
152233
static parsing::schema::ValidationType to_schema() {
153234
using ValidationType = parsing::schema::ValidationType;

include/rfl/define_tagged_union.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
namespace rfl {
99

10+
/// Defines a tagged union type by combining multiple variant types into a
11+
/// single std::variant. Each alternative from the provided variant types is
12+
/// included in the resulting union variant. For example, combining
13+
/// std::variant<int, double> and std::variant<std::string, bool> produces
14+
/// std::variant<int, double, std::string, bool>.
15+
/// @tparam _discriminator A string literal used to distinguish this tagged
16+
/// union type
17+
/// @tparam TaggedUnionTypes The variant types to merge into the tagged union
1018
template <internal::StringLiteral _discriminator, class... TaggedUnionTypes>
1119
using define_tagged_union_t =
1220
typename internal::define_tagged_union<_discriminator,

include/rfl/msgpack/Reader.hpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ struct Reader {
2222
using InputObjectType = msgpack_object_map;
2323
using InputVarType = msgpack_object;
2424

25+
/**
26+
* @brief Checks if type T has a custom constructor from a msgpack object.
27+
* @tparam T The type to check.
28+
*/
2529
template <class T>
2630
static constexpr bool has_custom_constructor =
2731
(requires(InputVarType var) { T::from_msgpack_obj(var); });
2832

33+
/**
34+
* @brief Retrieves a field from a msgpack array by index.
35+
* @param _idx The index of the field to retrieve.
36+
* @param _arr The msgpack array.
37+
* @return The field at the given index or an error if out of bounds.
38+
*/
2939
rfl::Result<InputVarType> get_field_from_array(
3040
const size_t _idx, const InputArrayType _arr) const noexcept {
3141
if (_idx >= _arr.size) {
@@ -34,6 +44,12 @@ struct Reader {
3444
return _arr.ptr[_idx];
3545
}
3646

47+
/**
48+
* @brief Retrieves a field from a msgpack object by name.
49+
* @param _name The name of the field to retrieve.
50+
* @param _obj The msgpack object.
51+
* @return The value of the field or an error if not found.
52+
*/
3753
rfl::Result<InputVarType> get_field_from_object(
3854
const std::string& _name, const InputObjectType& _obj) const noexcept {
3955
for (uint32_t i = 0; i < _obj.size; ++i) {
@@ -51,10 +67,23 @@ struct Reader {
5167
return error("No field named '" + _name + "' was found.");
5268
}
5369

70+
/**
71+
* @brief Checks if a msgpack object is empty (nil).
72+
* @param _var The msgpack object to check.
73+
* @return True if the object is nil, false otherwise.
74+
*/
5475
bool is_empty(const InputVarType& _var) const noexcept {
5576
return _var.type == MSGPACK_OBJECT_NIL;
5677
}
5778

79+
/**
80+
* @brief Converts a msgpack object to a basic C++ type.
81+
* Basic types include strings, byte containers, booleans,
82+
* floating-point numbers, integers, and literal types (enums).
83+
* @tparam T The target type.
84+
* @param _var The msgpack object to convert.
85+
* @return The converted value or an error if conversion fails.
86+
*/
5887
template <class T>
5988
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
6089
const auto type = _var.type;
@@ -105,6 +134,11 @@ struct Reader {
105134
}
106135
}
107136

137+
/**
138+
* @brief Converts a msgpack object to an array type.
139+
* @param _var The msgpack object to convert.
140+
* @return The array or an error if conversion fails.
141+
*/
108142
rfl::Result<InputArrayType> to_array(
109143
const InputVarType& _var) const noexcept {
110144
if (_var.type != MSGPACK_OBJECT_ARRAY) {
@@ -113,6 +147,11 @@ struct Reader {
113147
return _var.via.array;
114148
}
115149

150+
/**
151+
* @brief Converts a msgpack object to a map/object type.
152+
* @param _var The msgpack object to convert.
153+
* @return The object or an error if conversion fails.
154+
*/
116155
rfl::Result<InputObjectType> to_object(
117156
const InputVarType& _var) const noexcept {
118157
if (_var.type != MSGPACK_OBJECT_MAP) {
@@ -121,6 +160,13 @@ struct Reader {
121160
return _var.via.map;
122161
}
123162

163+
/**
164+
* @brief Reads all elements of a msgpack array using a provided reader.
165+
* @tparam ArrayReader The type of the array reader.
166+
* @param _array_reader The reader to use for each element.
167+
* @param _arr The msgpack array to read.
168+
* @return An optional error if any element fails to be read.
169+
*/
124170
template <class ArrayReader>
125171
std::optional<Error> read_array(const ArrayReader& _array_reader,
126172
const InputArrayType& _arr) const noexcept {
@@ -133,6 +179,13 @@ struct Reader {
133179
return std::nullopt;
134180
}
135181

182+
/**
183+
* @brief Reads all fields of a msgpack object using a provided reader.
184+
* @tparam ObjectReader The type of the object reader.
185+
* @param _object_reader The reader to use for each field.
186+
* @param _obj The msgpack object to read.
187+
* @return An optional error if any field fails to be read.
188+
*/
136189
template <class ObjectReader>
137190
std::optional<Error> read_object(const ObjectReader& _object_reader,
138191
const InputObjectType& _obj) const noexcept {
@@ -149,6 +202,12 @@ struct Reader {
149202
return std::nullopt;
150203
}
151204

205+
/**
206+
* @brief Uses a custom constructor to convert a msgpack object to type T.
207+
* @tparam T The target type.
208+
* @param _var The msgpack object to convert.
209+
* @return The constructed object or an error if construction fails.
210+
*/
152211
template <class T>
153212
rfl::Result<T> use_custom_constructor(
154213
const InputVarType& _var) const noexcept {

include/rfl/parquet/load.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
namespace rfl::parquet {
99

10+
/**
11+
* @brief Loads an object of type T from a file.
12+
*
13+
* This function reads the contents of the file specified by _fname,
14+
* then attempts to deserialize it into an object of type T using the
15+
* read<T, Ps...> function.
16+
*
17+
* @tparam T The type to load from the file.
18+
* @tparam Ps Additional template parameters for the read function.
19+
* @param _fname The name of the file to load.
20+
* @return Result<T> The result of loading and deserializing the file contents.
21+
*/
1022
template <class T, class... Ps>
1123
Result<T> load(const std::string& _fname) {
1224
const auto read_bytes = [](const auto& _bytes) {

0 commit comments

Comments
 (0)