Skip to content
Closed
5 changes: 5 additions & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
#include "rfl/Field.hpp"
#include "rfl/Flatten.hpp"
#include "rfl/Generic.hpp"
#include "rfl/Group.hpp"
#include "rfl/Hex.hpp"
#include "rfl/Hidden.hpp"
#include "rfl/ImplicitValue.hpp"
#include "rfl/Literal.hpp"
#include "rfl/MultiToken.hpp"
#include "rfl/NamedTuple.hpp"
#include "rfl/NoExtraFields.hpp"
#include "rfl/NoFieldNames.hpp"
Expand All @@ -57,6 +60,7 @@
#include "rfl/ToAllCaps.hpp"
#include "rfl/UnderlyingEnums.hpp"
#include "rfl/Validator.hpp"
#include "rfl/ValueName.hpp"
#include "rfl/Variant.hpp"
#include "rfl/Vectorstring.hpp"
#include "rfl/always_false.hpp"
Expand Down Expand Up @@ -94,6 +98,7 @@
#include "rfl/to_view.hpp"
#include "rfl/tuple_cat.hpp"
#include "rfl/type_name_t.hpp"
#include "rfl/unwrap.hpp"
#include "rfl/view_t.hpp"
#include "rfl/visit.hpp"

Expand Down
52 changes: 52 additions & 0 deletions include/rfl/Group.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef RFL_GROUP_HPP_
#define RFL_GROUP_HPP_

#include "internal/StringLiteral.hpp"
#include "internal/TransparentWrapper.hpp"

namespace rfl {

/// Assigns a CLI option to a named help section (e.g. "OUTPUT OPTIONS") in
/// `rfl::cli::usage` and `rfl::cli::read_app`.
/// For non-CLI formats (JSON, YAML, etc.), this is transparent.
template <internal::StringLiteral _group, class T>
struct Group : internal::TransparentWrapper<Group<_group, T>, T> {
using Base = internal::TransparentWrapper<Group<_group, T>, T>;
using Base::Base;
using Base::operator=;

static constexpr auto group_name_ = _group;
static_assert(_group.length > 0, "Group name must not be empty.");

Group()
requires std::is_default_constructible_v<T>
= default;
Group(const Group&) = default;
Group(Group&&) = default;
Group& operator=(const Group&) = default;
Group& operator=(Group&&) = default;

template <class U>
Group(const Group<_group, U>& _field) : Base(_field.get()) {}

template <class U>
Group(Group<_group, U>&& _field) noexcept(
std::is_nothrow_constructible_v<T, U&&>)
: Base(std::move(_field.value_)) {}

template <class U>
auto& operator=(const Group<_group, U>& _field) {
this->value_ = _field.get();
return *this;
}

template <class U>
auto& operator=(Group<_group, U>&& _field) {
this->value_ = std::move(_field.value_);
return *this;
}
};

} // namespace rfl

#endif
48 changes: 48 additions & 0 deletions include/rfl/Hidden.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef RFL_HIDDEN_HPP_
#define RFL_HIDDEN_HPP_

#include "internal/TransparentWrapper.hpp"

namespace rfl {

/// Marks a CLI option or positional argument as hidden from generated usage
/// and help text (`rfl::cli::usage` and `rfl::cli::read_app`), while still
/// parsing it normally.
/// For non-CLI formats (JSON, YAML, etc.), this is transparent.
template <class T>
struct Hidden : internal::TransparentWrapper<Hidden<T>, T> {
using Base = internal::TransparentWrapper<Hidden<T>, T>;
using Base::Base;
using Base::operator=;

Hidden()
requires std::is_default_constructible_v<T>
= default;
Hidden(const Hidden&) = default;
Hidden(Hidden&&) = default;
Hidden& operator=(const Hidden&) = default;
Hidden& operator=(Hidden&&) = default;

template <class U>
Hidden(const Hidden<U>& _field) : Base(_field.get()) {}

template <class U>
Hidden(Hidden<U>&& _field) noexcept(std::is_nothrow_constructible_v<T, U&&>)
: Base(std::move(_field.value_)) {}

template <class U>
auto& operator=(const Hidden<U>& _field) {
this->value_ = _field.get();
return *this;
}

template <class U>
auto& operator=(Hidden<U>&& _field) {
this->value_ = std::move(_field.value_);
return *this;
}
};

} // namespace rfl

#endif
214 changes: 17 additions & 197 deletions include/rfl/ImplicitValue.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#ifndef RFL_IMPLICITVALUE_HPP_
#define RFL_IMPLICITVALUE_HPP_

#include <type_traits>
#include <utility>

#include "default.hpp"
#include "internal/StringLiteral.hpp"
#include "internal/TransparentWrapper.hpp"

namespace rfl {

Expand All @@ -15,220 +12,43 @@ namespace rfl {
/// positional argument. If the option is absent, the field is read as usual
/// (std::optional gives std::nullopt, rfl::DefaultVal gives its default).
/// For non-CLI formats (JSON, YAML, etc.), this is transparent.
/// @tparam T The type of the field
/// @tparam _implicit The CLI text that a bare option stands for. It is parsed
/// like any other value, so `rfl::ImplicitValue<int, "1">` is 1.
template <class T, internal::StringLiteral _implicit = "">
struct ImplicitValue {
/// The underlying type.
using Type = T;

/// Every format but the command line reads and writes the underlying
/// value, as for rfl::Description.
using ReflectionType = Type;
struct ImplicitValue
: internal::TransparentWrapper<ImplicitValue<T, _implicit>, T> {
using Base = internal::TransparentWrapper<ImplicitValue<T, _implicit>, T>;
using Base::Base;
using Base::operator=;

/// The value that a bare option stands for.
static constexpr auto implicit_value_ = _implicit;

/// @brief Default constructor.
/// @details Constructs the underlying value using its default constructor.
/// @requires Type must be default constructible.
ImplicitValue()
requires std::is_default_constructible_v<Type>
: value_(Type()) {}

/// @brief Constructs the ImplicitValue wrapper from a const reference to
/// the underlying value.
/// @param _value The value to assign to the underlying field.
ImplicitValue(const Type& _value) : value_(_value) {}

/// @brief Constructs the ImplicitValue wrapper by moving the underlying
/// value.
/// @param _value The value to move into the underlying field.
ImplicitValue(Type&& _value) noexcept(
std::is_nothrow_move_constructible_v<Type>)
: value_(std::move(_value)) {}

/// @brief Move constructor.
/// @param _field The ImplicitValue field to move from.
ImplicitValue(ImplicitValue<T, _implicit>&& _field) = default;

/// @brief Copy constructor.
/// @param _field The ImplicitValue field to copy from.
ImplicitValue(const ImplicitValue<T, _implicit>& _field) = default;
requires std::is_default_constructible_v<T>
= default;
ImplicitValue(const ImplicitValue&) = default;
ImplicitValue(ImplicitValue&&) = default;
ImplicitValue& operator=(const ImplicitValue&) = default;
ImplicitValue& operator=(ImplicitValue&&) = default;

/// @brief Constructs from another ImplicitValue with a convertible
/// underlying type.
/// @tparam U The type of the other ImplicitValue's underlying value.
/// @param _field The other ImplicitValue field to copy from.
template <class U>
ImplicitValue(const ImplicitValue<U, _implicit>& _field)
: value_(_field.get()) {}
: Base(_field.get()) {}

/// @brief Move constructs from another ImplicitValue with a convertible
/// underlying type.
/// @tparam U The type of the other ImplicitValue's underlying value.
/// @param _field The other ImplicitValue field to move from.
template <class U>
ImplicitValue(ImplicitValue<U, _implicit>&& _field) noexcept(
std::is_nothrow_constructible_v<Type, U&&>)
: value_(std::move(_field.value_)) {}
std::is_nothrow_constructible_v<T, U&&>)
: Base(std::move(_field.value_)) {}

/// @brief Constructs from a value convertible to the underlying type.
/// @tparam U The type of the value.
/// @param _value The value to assign.
template <class U>
requires std::is_convertible_v<U, Type>
ImplicitValue(const U& _value) : value_(_value) {}

/// @brief Move constructs from a value convertible to the underlying type.
/// @tparam U The type of the value.
/// @param _value The value to move.
template <class U>
requires std::is_convertible_v<U, Type>
ImplicitValue(U&& _value) noexcept(
std::is_nothrow_constructible_v<Type, U&&>)
: value_(std::forward<U>(_value)) {}

/// @brief Constructs the underlying object to its default value using
/// Default tag.
/// @tparam U The type to default construct (defaults to Type).
/// @param unused Default tag.
template <class U = Type>
requires std::is_default_constructible_v<U>
ImplicitValue(const Default&) : value_(Type()) {}

/// @brief Destructor.
/// @details Defaulted destructor for ImplicitValue.
~ImplicitValue() = default;

/// @brief Returns a const reference to the underlying object.
/// @return Const reference to the underlying value.
const Type& get() const noexcept { return value_; }

/// @brief Returns a reference to the underlying object.
/// @return Reference to the underlying value.
Type& get() noexcept { return value_; }

/// @brief Dereference operator.
/// @return Reference to the underlying value.
Type& operator*() noexcept { return value_; }

/// @brief Dereference operator (const).
/// @return Const reference to the underlying value.
const Type& operator*() const noexcept { return value_; }

/// @brief Function call operator.
/// @return Reference to the underlying value.
Type& operator()() noexcept { return value_; }

/// @brief Function call operator (const).
/// @return Const reference to the underlying value.
const Type& operator()() const noexcept { return value_; }

/// @brief Pointer to the underlying value.
/// @return Pointer to the underlying value.
Type* operator->() noexcept { return &value_; }

/// @brief Pointer to the underlying value (const).
/// @return Const pointer to the underlying value.
const Type* operator->() const noexcept { return &value_; }

/// @brief Assigns a new value to the underlying object.
/// @param _value The value to assign.
/// @return Reference to this ImplicitValue.
auto& operator=(const Type& _value) {
value_ = _value;
return *this;
}

/// @brief Move-assigns a new value to the underlying object.
/// @param _value The value to move.
/// @return Reference to this ImplicitValue.
auto& operator=(Type&& _value) noexcept(
std::is_nothrow_move_assignable_v<Type>) {
value_ = std::move(_value);
return *this;
}

/// @brief Assigns a value convertible to the underlying type.
/// @tparam U The type of the value.
/// @param _value The value to assign.
/// @return Reference to this ImplicitValue.
template <class U>
requires std::is_convertible_v<U, Type>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
}

/// @brief Assigns the underlying object to its default value using Default
/// tag.
/// @tparam U The type to default construct (defaults to Type).
/// @param unused Default tag.
/// @return Reference to this ImplicitValue.
template <class U = Type>
requires std::is_default_constructible_v<U>
auto& operator=(const Default&) {
value_ = Type();
return *this;
}

/// @brief Copy assignment operator.
/// @param _field The ImplicitValue field to copy from.
/// @return Reference to this ImplicitValue.
ImplicitValue<T, _implicit>& operator=(
const ImplicitValue<T, _implicit>& _field) = default;

/// @brief Move assignment operator.
/// @param _field The ImplicitValue field to move from.
/// @return Reference to this ImplicitValue.
ImplicitValue<T, _implicit>& operator=(
ImplicitValue<T, _implicit>&& _field) = default;

/// @brief Assigns from another ImplicitValue with a convertible underlying
/// type.
/// @tparam U The type of the other ImplicitValue's underlying value.
/// @param _field The other ImplicitValue field to copy from.
/// @return Reference to this ImplicitValue.
template <class U>
auto& operator=(const ImplicitValue<U, _implicit>& _field) {
value_ = _field.get();
this->value_ = _field.get();
return *this;
}

/// @brief Move-assigns from another ImplicitValue with a convertible
/// underlying type.
/// @tparam U The type of the other ImplicitValue's underlying value.
/// @param _field The other ImplicitValue field to move from.
/// @return Reference to this ImplicitValue.
template <class U>
auto& operator=(ImplicitValue<U, _implicit>&& _field) {
value_ = std::move(_field.value_);
this->value_ = std::move(_field.value_);
return *this;
}

/// @brief Sets the underlying object to a new value.
/// @param _value The value to assign.
void set(const Type& _value) { value_ = _value; }

/// @brief Sets the underlying object to a new value by moving.
/// @param _value The value to move.
void set(Type&& _value) { value_ = std::move(_value); }

/// @brief Returns a reference to the underlying value.
/// @return Reference to the underlying value.
Type& value() noexcept { return value_; }

/// @brief Returns a const reference to the underlying value.
/// @return Const reference to the underlying value.
const Type& value() const noexcept { return value_; }

/// Returns the underlying value for reflection (used by parsers).
const Type& reflection() const { return value_; }

/// The underlying value.
Type value_;
};

} // namespace rfl
Expand Down
Loading