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
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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
47 changes: 47 additions & 0 deletions include/rfl/unwrap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef RFL_UNWRAP_HPP_
#define RFL_UNWRAP_HPP_

#include <type_traits>
#include <utility>

#include "internal/TransparentWrapper.hpp"
#include "internal/is_default_val_v.hpp"
#include "internal/is_deprecated.hpp"
#include "internal/is_description.hpp"
#include "internal/is_positional.hpp"
#include "internal/is_rename.hpp"
#include "internal/is_short.hpp"

namespace rfl {

namespace internal {

template <class T>
constexpr bool is_metadata_wrapper_v =
is_default_val_v<T> || is_description_v<T> || is_deprecated_v<T> ||
is_rename_v<T> || is_short_v<T> || is_positional_v<T> ||
is_transparent_wrapper_v<T>;

} // namespace internal

/// Recursively unwraps reflect-cpp metadata wrappers (DefaultVal, Description,
/// Deprecated, Rename, Short, Positional, and all transparent CLI wrappers)
/// and returns a reference to the underlying value. Does not unwrap
/// std::optional, pointers, or Validator.
template <class T>
constexpr decltype(auto) unwrap(T&& _val) noexcept {
using U = std::remove_cvref_t<T>;
if constexpr (internal::is_metadata_wrapper_v<U>) {
if constexpr (std::is_lvalue_reference_v<T>) {
return unwrap(_val.value());
} else {
return unwrap(std::move(_val.value()));
}
} else {
return std::forward<T>(_val);
}
}

} // namespace rfl

#endif
37 changes: 37 additions & 0 deletions tests/cli/test_unwrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <optional>
#include <string>

namespace test_unwrap {

struct Config {
rfl::Group<"COMMON OPTIONS",
rfl::Short<"v", rfl::Description<"Verbose", rfl::DefaultVal<bool>>>>
verbose = false;
rfl::Group<"OUTPUT OPTIONS",
rfl::Short<"o", rfl::Description<"Output",
std::optional<std::string>>>>
output;
rfl::Hidden<rfl::Positional<rfl::DefaultVal<std::string>>> input = "in.osm";
};

TEST(cli, test_unwrap_nested_wrappers) {
const std::vector<std::string> args = {"-v", "-o", "out.osm"};
const auto res =
rfl::cli::read<Config, rfl::cli::SeparateOptionValues>(args);
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(rfl::unwrap(res->verbose));
ASSERT_TRUE(rfl::unwrap(res->output).has_value());
EXPECT_EQ(*rfl::unwrap(res->output), "out.osm");
EXPECT_EQ(rfl::unwrap(res->input), "in.osm");

// Mutable reference works too.
auto cfg = *res;
rfl::unwrap(cfg.verbose) = false;
EXPECT_FALSE(rfl::unwrap(cfg.verbose));
}

} // namespace test_unwrap