Skip to content
Closed
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
6 changes: 6 additions & 0 deletions 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,6 +90,10 @@ 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.

If the struct cannot be default-constructed at all, for instance because a field's type has no default constructor, it is read without its default values, so its `rfl::DefaultVal` fields must be present in the input. The same goes for a struct whose default constructor throws if all its `rfl::DefaultVal` fields are inside `rfl::Short`, `rfl::Positional`, `rfl::Description` or `rfl::Deprecated`; with a bare `rfl::DefaultVal`, 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, and so must an `rfl::DefaultVal` inside `rfl::Short`, `rfl::Positional`, `rfl::Description` or `rfl::Deprecated`.

## When to use

Use `rfl::DefaultVal` when you want a field to be optional at the input side but still available as a value on the resulting object (no std::optional or pointer indirection). It is particularly useful for fields with sensible defaults (for example, a common last name, a default configuration value, or empty containers/strings).
Expand Down
5 changes: 5 additions & 0 deletions docs/content/supported_formats/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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
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
59 changes: 57 additions & 2 deletions include/rfl/internal/is_default_val_v.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#define RFL_INTERNAL_ISDEFAULTVAL_HPP_

#include <type_traits>
#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 @@ -18,14 +23,64 @@ 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<T> {};

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

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

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

template <internal::StringLiteral _deprecation_message,
internal::StringLiteral _description, class T>
class is_default_val<Deprecated<_deprecation_message, _description, T>>
: public is_default_val<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, DefaultVal<T>>>
: public std::true_type {};

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 {
return _field;
}

template <class T>
requires is_default_val_v<T>
const auto& get_default_val(const T& _field) noexcept {
return get_default_val(_field.value());
}

/// The DefaultVal type inside a field for which is_default_val_v is true.
template <class T>
using default_val_t = std::remove_cvref_t<decltype(get_default_val(
std::declval<const std::remove_cvref_t<T>&>()))>;

} // namespace rfl::internal

#endif
Loading