Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/atomic.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if (!res) {
## Limitations and notes

- Structs containing atomic fields must be default-constructible.
- Atomic types cannot be mixed with `rfl::DefaultVal` or the `rfl::DefaultIfMissing` processor; attempting to do so triggers a static assertion at compile-time (see parser implementations).
- Atomic types cannot be mixed with `rfl::DefaultVal`, `rfl::DefaultIfMissing`, or `rfl::DefaultIfNull`; attempting to do so triggers a static assertion at compile-time (see parser implementations).
- The semantics used for setting atomic values use relaxed memory order (`std::memory_order_relaxed`).
- For complex aggregates, `rfl::atomic` will recurse into nested fields and arrays to set atomic members.

49 changes: 49 additions & 0 deletions docs/content/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ reflect-cpp currently supports the following processors:
- `rfl::AddNamespacedTagsToVariants`
- `rfl::AllowRawPtrs`
- `rfl::DefaultIfMissing`
- `rfl::DefaultIfNull`
- `rfl::DurationsAsCount`
- `rfl::EnumNamesOnly`
- `rfl::FailFast`
Expand Down Expand Up @@ -314,6 +315,54 @@ Note that the struct is default-constructed before it is read. Every field
therefore needs a valid default value, even if the field is in the input.
This is explained in the documentation on `rfl::DefaultVal`.

### `rfl::DefaultIfNull`

The `rfl::DefaultIfNull` processor is only relevant for reading data. For writing data, it will make no difference.

Usually, when a non-nullable value is `null` or empty in the input (for example, an empty or comment-only YAML document, a YAML key with no value such as `tags:` or `logging:`, or a JSON `null`), reading fails with a parse error.
If you pass `rfl::DefaultIfNull`, null/empty values are replaced by the target's default value instead (when the target type is default-constructible):

```cpp
struct Logging {
std::string level = "info";
};

struct Config {
int port = 8080;
std::string host = "localhost";
std::vector<std::string> tags;
std::map<std::string, std::string> env;
Logging logging;
std::map<std::string, Logging> modules;
};
```

```yaml
port: null
host:
tags:
env:
logging:
modules:
network:
```

```cpp
const auto cfg = rfl::yaml::read<Config, rfl::DefaultIfNull>(yaml_str).value();
// cfg.port == 8080, cfg.host == "localhost", cfg.tags.empty(), cfg.env.empty(),
// cfg.logging.level == "info", cfg.modules["network"].level == "info"
```

Empty or comment-only YAML documents (and root `null` in JSON) also produce a default-constructed `Config{}`.

Key details:

- **In-class member initializers are preserved:** For default-constructible structs, `port: null` preserves `8080` rather than overwriting it with `0`.
- **Nullable types still decode `null` as empty:** `std::optional<T>`, `std::unique_ptr<T>`, `std::shared_ptr<T>`, raw pointers, and `rfl::Generic` always decode an explicit `null` as `std::nullopt` / `nullptr` / `rfl::Generic::Null` — even if the field has a non-null in-class initializer such as `std::optional<int> x = 5`.
- **Missing fields remain required unless combined with `rfl::DefaultIfMissing`:** `rfl::DefaultIfNull` only applies to null/empty values that are present (or a null root document). Combine `rfl::DefaultIfMissing, rfl::DefaultIfNull` when you want both missing fields and null values to fall back to defaults.
- **Non-default-constructible types:** If a type is not default-constructible, encountering `null` falls back to normal error reporting rather than failing to compile.
- **Default-constructible structs are default-constructed before reading:** To preserve in-class member initializers, default-constructible structs are default-constructed before their fields are read. Every field of such a struct therefore needs a valid default value (for example, a `rfl::Validator` whose default constructor throws will cause reading to return an error, even if the field is present in the input). This is explained in the documentation on `rfl::DefaultVal`.

### `rfl::DurationsAsCount`

By default, `std::chrono::duration` types are written as an object containing
Expand Down
1 change: 1 addition & 0 deletions docs/content/supported_formats/parquet.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ The following processors are **NOT supported** and will cause compilation errors
- `rfl::AddTagsToVariants` - Cannot be used for tabular data
- `rfl::NoOptionals` - Cannot be used for tabular data
- `rfl::DefaultIfMissing` - Cannot be used for tabular data
- `rfl::DefaultIfNull` - Cannot be used for tabular data
- `rfl::NoExtraFields` - Cannot be used for tabular data
- `rfl::NoFieldNames` - Cannot be used for tabular data

Expand Down
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "rfl/CodePoints.hpp"
#include "rfl/Commented.hpp"
#include "rfl/DefaultIfMissing.hpp"
#include "rfl/DefaultIfNull.hpp"
#include "rfl/DefaultVal.hpp"
#include "rfl/Description.hpp"
#include "rfl/DurationsAsCount.hpp"
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/Attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct Attribute {
using ReflectionType = T;

/// Default constructor.
template <class U = Type>
requires std::is_default_constructible_v<U>
Attribute() : value_(Type()) {}

/// Constructs from a const reference to the value.
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/Commented.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct Commented {

/// Default constructor - initializes with default-constructed value and no
/// comment.
template <class U = Type>
requires std::is_default_constructible_v<U>
Commented() : value_(Type()) {}

/// Constructs from a value without a comment.
Expand Down
32 changes: 32 additions & 0 deletions include/rfl/DefaultIfNull.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef RFL_DEFAULTIFNULL_HPP_
#define RFL_DEFAULTIFNULL_HPP_

namespace rfl {

/// A processor that instructs parsers to use default values when encountering
/// null or empty input values.
/// This is a marker type (doesn't modify data) that changes parser behavior.
/// When an input value is null/empty (for example, `null`, `~`, or an empty
/// scalar in YAML, or `null` in JSON) and the target type is
/// default-constructible, the default value is used instead of treating it as
/// an error. On struct fields, in-class member initializers are preserved.
/// Nullable types such as `std::optional`, `std::unique_ptr`,
/// `std::shared_ptr`, and raw pointers continue to decode `null` as empty
/// (`std::nullopt` / `nullptr`).
/// Usage: rfl::yaml::read<MyStruct, rfl::DefaultIfNull>(yaml_str)
struct DefaultIfNull {
public:
/// Identity process function - returns the named tuple unchanged.
/// The actual default value application happens in the parser, not here.
/// @tparam StructType The struct type being processed
/// @param _named_tuple The named tuple representation of the struct
/// @return The same named tuple (unchanged)
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

} // namespace rfl

#endif
4 changes: 3 additions & 1 deletion include/rfl/DefaultVal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ struct DefaultVal {
using Type = std::remove_cvref_t<T>;

/// Default constructor - initializes with default-constructed value.
DefaultVal() : value_(Type()) {}
DefaultVal()
requires std::is_default_constructible_v<Type>
: value_(Type()) {}

/// Constructs from a const reference to the value.
/// @param _value The value to store
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/Deprecated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct Deprecated {
using ReflectionType = Type;

/// Default constructor.
template <class U = Type>
requires std::is_default_constructible_v<U>
Deprecated() : value_(Type()) {}

/// Constructs from a const reference to the value.
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/Description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct Description {
using ReflectionType = Type;

/// Default constructor.
template <class U = Type>
requires std::is_default_constructible_v<U>
Description() : value_(Type()) {}

/// Constructs from a const reference to the value.
Expand Down
4 changes: 3 additions & 1 deletion include/rfl/Rename.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ struct Rename {
using Name = rfl::Literal<_name>;

/// Default constructor - creates a Rename with a default-constructed value.
Rename() : value_(Type()) {}
Rename()
requires std::is_default_constructible_v<Type>
: value_(Type()) {}

/// Constructs from a const reference to the value.
/// @param _value The value to store
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/Validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct Validator {

/// Default constructor - validates a default-constructed T.
/// @throws std::exception if validation of the default value fails
template <class U = T>
requires std::is_default_constructible_v<U>
Validator() : value_(ValidationType::validate(T()).value()) {}

/// Move constructor.
Expand Down
5 changes: 3 additions & 2 deletions include/rfl/Variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class Variant {
/// alternative type.
/// @details The first alternative type is default-constructed and stored in
/// the variant.
template <class U = internal::nth_element_t<0, AlternativeTypes...>>
requires std::is_default_constructible_v<U>
Variant() : index_(IndexType()), data_(DataType()) {
using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>;
move_from_type(FirstAlternative());
move_from_type(U());
}

/// @brief Copy-constructs the Variant from another Variant.
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/internal/Skip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Skip {
using Type = T;
using ReflectionType = std::optional<T>;

template <class U = Type>
requires std::is_default_constructible_v<U>
Skip() : value_(Type()) {}

Skip(const Type& _value) : value_(_value) {}
Expand Down
32 changes: 32 additions & 0 deletions include/rfl/internal/default_if_null_v.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef RFL_INTERNAL_DEFAULTIFNULL_HPP_
#define RFL_INTERNAL_DEFAULTIFNULL_HPP_

#include <type_traits>

#include "../DefaultIfNull.hpp"
#include "../Processors.hpp"

namespace rfl::internal {

template <class T>
class default_if_null;

template <class T>
class default_if_null : public std::false_type {};

template <>
class default_if_null<DefaultIfNull> : public std::true_type {};

template <class Head, class... Tail>
struct default_if_null<Processors<Head, Tail...>> {
static constexpr bool value =
(default_if_null<Head>::value || ... || default_if_null<Tail>::value);
};

template <class T>
constexpr bool default_if_null_v =
default_if_null<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace rfl::internal

#endif
Loading
Loading