Skip to content

Commit

Permalink
Add constructor in CommonOption, fix IndicesOption
Browse files Browse the repository at this point in the history
  • Loading branch information
aartdem committed Dec 7, 2023
1 parent bdd1505 commit 6100801
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
36 changes: 23 additions & 13 deletions src/core/config/common_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <optional>
#include <string_view>
#include <variant>

#include <boost/functional/overloaded_function.hpp>

#include "config/option.h"

Expand All @@ -12,30 +15,37 @@ namespace config {
template <typename T>
class CommonOption {
private:
using DefaultFuncType = typename Option<T>::DefaultFunc;

std::string_view const name_;
std::string_view const description_;
std::optional<T> const default_value_;
std::variant<std::optional<T>, DefaultFuncType> const default_init_v_;
typename Option<T>::NormalizeFunc const normalize_func_;
typename Option<T>::ValueCheckFunc const value_check_func_;
typename Option<T>::DefaultFunc const default_func_;

public:
CommonOption(std::string_view name, std::string_view description,
std::optional<T> default_value = std::nullopt,
std::variant<std::optional<T>, DefaultFuncType> default_init_v = std::nullopt,
typename Option<T>::NormalizeFunc normalize_func = nullptr,
typename Option<T>::ValueCheckFunc value_check_func = nullptr,
typename Option<T>::DefaultFunc default_func_ = nullptr)
typename Option<T>::ValueCheckFunc value_check_func = nullptr)
: name_(name),
description_(description),
default_value_(default_value),
default_init_v_(default_init_v),
normalize_func_(normalize_func),
value_check_func_(value_check_func),
default_func_(default_func_) {}

[[nodiscard]] Option<T> operator()(T *value_ptr) const {
auto option = default_value_.has_value()
? Option{value_ptr, name_, description_, default_value_.value()}
: Option{value_ptr, name_, description_, default_func_};
value_check_func_(value_check_func) {}

[[nodiscard]] Option<T> operator()(T* value_ptr) const {
auto from_default_func = [value_ptr, this](DefaultFuncType default_func) {
return Option{value_ptr, name_, description_, default_func};
};
auto from_default_value = [value_ptr, this](std::optional<T> const& default_value) {
return default_value.has_value()
? Option{value_ptr, name_, description_, default_value.value()}
: Option{value_ptr, name_, description_};
};
Option<T> option =
std::visit(boost::make_overloaded_function(from_default_func, from_default_value),
default_init_v_);
if (normalize_func_) option.SetNormalizeFunc(normalize_func_);
if (value_check_func_) option.SetValueCheck(value_check_func_);
return option;
Expand Down
3 changes: 1 addition & 2 deletions src/core/config/indices/option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ static void NormalizeIndices(config::IndicesType& indices) {

IndicesOption::IndicesOption(std::string_view name, std::string_view description,
typename Option<config::IndicesType>::DefaultFunc calculate_default)
: common_option_(name, description, {}, NormalizeIndices, nullptr,
std::move(calculate_default)) {}
: common_option_(name, description, std::move(calculate_default), NormalizeIndices, nullptr) {}

std::string_view IndicesOption::GetName() const {
return common_option_.GetName();
Expand Down

0 comments on commit 6100801

Please sign in to comment.