Skip to content
6 changes: 5 additions & 1 deletion docs/content/default_val.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const auto homer = rfl::json::read<Person, rfl::NoFieldNames>(R"(["Homer"])").va
// homer.town == ""
```

An `rfl::DefaultVal` inside `rfl::Short`, `rfl::Positional`, `rfl::Description`, `rfl::Deprecated` or `rfl::Rename`, in any order, works like a bare one: a field `rfl::Description<"Worker threads", rfl::DefaultVal<int>> jobs = 4;` may be omitted and is then 4.

## All fields need a valid default value

A struct that contains an `rfl::DefaultVal` is default-constructed before it is read. The fields found in the input are then written into it. This means that every field of the struct needs a valid default value, even a required field that is in the input.
Expand Down Expand Up @@ -88,7 +90,9 @@ The field is still required. If `favorite_foods` is missing in the input, you ge

The same restriction applies to every struct that is read with the `rfl::DefaultIfMissing` processor, whether it contains an `rfl::DefaultVal` or not.

An `rfl::NamedTuple` has no in-class initializers. A missing `rfl::DefaultVal<T>` field in it is set to `T{}`. If `T` has no default constructor, or `T{}` throws (for instance a validator that rejects its zero value), the field must be present in the input.
An `rfl::DefaultVal` inside `rfl::Short`, `rfl::Positional`, `rfl::Description` or `rfl::Deprecated` gets its default only if the struct can be default-constructed. If all its `rfl::DefaultVal` fields are wrapped and the struct cannot be default-constructed, because a field's type has no default constructor or the default constructor throws, the struct is read without its default values, so those fields must be present in the input. With a bare `rfl::DefaultVal`, such a struct does not compile, or you get the error above.

An `rfl::NamedTuple` has no in-class initializers. A missing bare `rfl::DefaultVal<T>` field in it is set to `T{}`. If `T` has no default constructor, or `T{}` throws (for instance a validator that rejects its zero value), the field must be present in the input. An `rfl::DefaultVal` inside `rfl::Short`, `rfl::Positional`, `rfl::Description` or `rfl::Deprecated` must always be present in an `rfl::NamedTuple`.

## When to use

Expand Down
73 changes: 73 additions & 0 deletions docs/content/json_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,76 @@ const std::string json_schema = rfl::json::to_schema<Person>(rfl::json::pretty);
```

`rfl::Deprecated` behaves like a thin wrapper around the underlying type, just like `rfl::Description`. You can access the underlying value using `.get()`, `.value()`, `operator()()`, `operator*()`, `operator->()`, or the assignment operator.

## Default values with `rfl::DefaultIfMissing`

When you generate a schema with the `rfl::DefaultIfMissing` processor, all fields are omitted from `"required"` and—whenever the struct is default-constructible—each field's default value from `T{}` is emitted as a JSON Schema `"default"` annotation:

```cpp
struct Config {
int port = 8080;
std::string host = "localhost";
bool verbose = true;
std::optional<std::string> tls_cert;
std::optional<int> timeout = 30;
};

const std::string json_schema =
rfl::json::to_schema<Config, rfl::DefaultIfMissing>(rfl::json::pretty);
```

This produces:

```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/Config",
"$defs": {
"Config": {
"type": "object",
"properties": {
"port": {
"type": "integer",
"default": 8080
},
"host": {
"type": "string",
"default": "localhost"
},
"verbose": {
"default": true,
"type": "boolean"
},
"tls_cert": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"timeout": {
"default": 30,
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
]
}
},
"required": []
}
}
}
```

A few details to keep in mind:

- Empty nullable fields (`std::optional`, `std::shared_ptr`, `std::unique_ptr` initialized to `std::nullopt` / `nullptr`) do not emit `"default": null`; initialized ones (like `timeout = 30`) emit their default value.
- If a struct is not default-constructible, or if default-constructing it throws an exception (for example, an uninitialized `rfl::Validator` field whose zero value is invalid), schema generation still succeeds and simply omits `"default"` annotations for that struct.
- Without `rfl::DefaultIfMissing`, plain fields do not emit `"default"` annotations (use `rfl::DefaultVal<T>` for per-field defaults).
5 changes: 5 additions & 0 deletions docs/content/supported_formats/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ struct Config {
// ./app data.csv -o /tmp/out -v --count=10
```

The type inside `rfl::Short`, `rfl::Positional` or `rfl::Description` can be
a `std::optional` or an `rfl::DefaultVal`, so the argument may be omitted:
with `rfl::Short<"o", rfl::DefaultVal<std::string>> output_dir = "out";`,
leaving out `-o` gives `"out"`.

## Supported types

| Type | CLI format | Notes |
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
2 changes: 2 additions & 0 deletions include/rfl/DefaultVal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct DefaultVal {
using Type = std::remove_cvref_t<T>;

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

/// Constructs from a const reference to the value.
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
2 changes: 2 additions & 0 deletions include/rfl/Rename.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct Rename {
using Name = rfl::Literal<_name>;

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

/// Constructs from a const reference to the value.
Expand Down
4 changes: 3 additions & 1 deletion include/rfl/Tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class Tuple {

/// Default constructor. Initializes each element with its default
/// constructor.
Tuple() : Tuple(Types()...) {}
Tuple()
requires(std::is_default_constructible_v<Types> && ...)
: Tuple(Types()...) {}

/// Copy constructor. Copies all elements from another Tuple.
/// @param _other The Tuple to copy from.
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
4 changes: 3 additions & 1 deletion include/rfl/Variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ class Variant {
/// alternative type.
/// @details The first alternative type is default-constructed and stored in
/// the variant.
template <class FirstAlternative =
internal::nth_element_t<0, AlternativeTypes...>>
requires std::is_default_constructible_v<FirstAlternative>
Variant() : index_(IndexType()), data_(DataType()) {
using FirstAlternative = internal::nth_element_t<0, AlternativeTypes...>;
move_from_type(FirstAlternative());
}

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
43 changes: 28 additions & 15 deletions include/rfl/internal/has_default_val_v.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,49 @@

namespace rfl::internal {

template <class T>
/// With _unwrapped_only, only counts fields for which
/// is_unwrapped_default_val_v is true.
template <class T, bool _unwrapped_only = false>
struct HasDefaultVal;

template <class T>
template <class T, bool _unwrapped_only>
struct HasDefaultVal {
static constexpr bool value = false;
};

template <class... Fields>
struct HasDefaultVal<NamedTuple<Fields...>> {
static constexpr bool value =
(false || ... ||
is_default_val_v<
std::remove_cvref_t<std::remove_pointer_t<typename Fields::Type>>>);
template <class... Fields, bool _unwrapped_only>
struct HasDefaultVal<NamedTuple<Fields...>, _unwrapped_only> {
template <class F,
class U =
std::remove_cvref_t<std::remove_pointer_t<typename F::Type>>>
static constexpr bool is_default_val_field =
_unwrapped_only ? is_unwrapped_default_val_v<U> : is_default_val_v<U>;

static constexpr bool value = (false || ... || is_default_val_field<Fields>);
};

template <class T>
template <class T, bool _unwrapped_only>
requires(std::is_class_v<T> && std::is_aggregate_v<T>)
struct HasDefaultVal<T> : HasDefaultVal<named_tuple_t<T>> {};
struct HasDefaultVal<T, _unwrapped_only>
: HasDefaultVal<named_tuple_t<T>, _unwrapped_only> {};

template <class T, size_t N>
struct HasDefaultVal<std::array<T, N>> : HasDefaultVal<std::remove_cvref_t<T>> {
};
template <class T, size_t N, bool _unwrapped_only>
struct HasDefaultVal<std::array<T, N>, _unwrapped_only>
: HasDefaultVal<std::remove_cvref_t<T>, _unwrapped_only> {};

template <class T, size_t N>
struct HasDefaultVal<T[N]> : HasDefaultVal<std::remove_cvref_t<T>> {};
template <class T, size_t N, bool _unwrapped_only>
struct HasDefaultVal<T[N], _unwrapped_only>
: HasDefaultVal<std::remove_cvref_t<T>, _unwrapped_only> {};

template <class T>
constexpr bool has_default_val_v = HasDefaultVal<std::remove_cvref_t<T>>::value;

/// Whether T has an rfl::DefaultVal field that is not inside rfl::Short,
/// rfl::Positional, rfl::Description or rfl::Deprecated.
template <class T>
constexpr bool has_unwrapped_default_val_v =
HasDefaultVal<std::remove_cvref_t<T>, true>::value;

} // namespace rfl::internal

#endif
46 changes: 43 additions & 3 deletions include/rfl/internal/is_default_val_v.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
#include <utility>

#include "../DefaultVal.hpp"
#include "../Deprecated.hpp"
#include "../Description.hpp"
#include "../Positional.hpp"
#include "../Rename.hpp"
#include "../Short.hpp"
#include "StringLiteral.hpp"

namespace rfl::internal {
Expand All @@ -19,14 +23,50 @@ class is_default_val : public std::false_type {};
template <class T>
class is_default_val<DefaultVal<T>> : public std::true_type {};

template <class T, internal::StringLiteral _new_name>
class is_default_val<Rename<_new_name, DefaultVal<T>>> : public std::true_type {
};
// The wrappers below do not change how a field is read, so a DefaultVal
// inside them, in any order, still provides the field's default value.

template <internal::StringLiteral _new_name, class T>
class is_default_val<Rename<_new_name, T>>
: public is_default_val<std::remove_cvref_t<T>> {};

template <internal::StringLiteral _short_name, class T>
class is_default_val<Short<_short_name, T>>
: public is_default_val<std::remove_cvref_t<T>> {};

template <class T>
class is_default_val<Positional<T>>
: public is_default_val<std::remove_cvref_t<T>> {};

template <internal::StringLiteral _description, class T>
class is_default_val<Description<_description, T>>
: public is_default_val<std::remove_cvref_t<T>> {};

template <internal::StringLiteral _deprecation_message,
internal::StringLiteral _description, class T>
class is_default_val<Deprecated<_deprecation_message, _description, T>>
: public is_default_val<std::remove_cvref_t<T>> {};

template <class T>
constexpr bool is_default_val_v =
is_default_val<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

/// Whether T is an rfl::DefaultVal itself or inside rfl::Rename, but not
/// inside rfl::Short, rfl::Positional, rfl::Description or rfl::Deprecated.
template <class T>
class is_unwrapped_default_val : public std::false_type {};

template <class T>
class is_unwrapped_default_val<DefaultVal<T>> : public std::true_type {};

template <internal::StringLiteral _new_name, class T>
class is_unwrapped_default_val<Rename<_new_name, T>>
: public is_unwrapped_default_val<std::remove_cvref_t<T>> {};

template <class T>
constexpr bool is_unwrapped_default_val_v = is_unwrapped_default_val<
std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

/// Returns the DefaultVal inside a field for which is_default_val_v is true.
template <class T>
const DefaultVal<T>& get_default_val(const DefaultVal<T>& _field) noexcept {
Expand Down
Loading
Loading