47 requires(!std::same_as<std::decay_t<T>,
option>)
48 explicit option(T&& value) : wrapper_{std::make_shared<wrapper<T>>(std::forward<T>(value))} {}
50 [[nodiscard]] abi::string bound_name()
const {
51 return wrapper_->bound_name();
54 const option& set_bound_name(std::string_view name)
const {
55 wrapper_->set_bound_name(name);
60 [[nodiscard]] abi::string description()
const {
61 return wrapper_->description();
64 [[nodiscard]]
const option& set_description(std::string_view description)
const {
65 wrapper_->set_description(description);
70 [[nodiscard]] std::span<const abi::string> aliases()
const {
71 return wrapper_->aliases();
74 const option& add_aliases(std::span<const abi::string> aliases)
const {
75 wrapper_->add_aliases(aliases);
80 [[nodiscard]] std::optional<abi::string> default_value_str()
const {
81 return wrapper_->default_value_str();
84 [[nodiscard]] std::span<const abi::string> valid_value_strs()
const {
85 return wrapper_->valid_value_strs();
88 [[nodiscard]] abi::string name_hints()
const {
89 return wrapper_->name_hints();
92 [[nodiscard]] abi::string value_hints()
const {
93 return wrapper_->value_hints();
97 return wrapper_->check_target_type(
id);
100 [[nodiscard]]
bool parse_value_and_cache(std::string_view value)
const {
101 return wrapper_->parse_value_and_cache(value);
105 wrapper_->validate(value, result);
108 void raise_error(std::string_view message)
const {
109 wrapper_->raise_error(message);
112 [[nodiscard]]
void* underlying_ptr()
const noexcept {
113 return wrapper_.get();
116 void on_validation(
const validation_handler& handler)
const {
117 wrapper_->on_validation(handler);
120 void on_error(
const output_handler& handler)
const {
121 wrapper_->on_error(handler);
124 template <std::convertible_to<std::
string_view>... Args>
125 const option& add_aliases(Args&&... args)
const {
126 return add_aliases(std::array{abi::string{std::string_view{std::forward<Args>(args)...}}});
129 template <
typename T>
130 void set_target_from_cache(T& target)
const {
133 if (check_target_type(
id)) {
134 wrapper_->set_target_from_cache(&target);
140 virtual ~base() =
default;
141 virtual abi::string bound_name() = 0;
142 virtual void set_bound_name(std::string_view name) = 0;
143 virtual abi::string description() = 0;
144 virtual void set_description(std::string_view description) = 0;
145 virtual std::span<const abi::string> aliases() = 0;
146 virtual void add_aliases(std::span<const abi::string> aliases) = 0;
147 virtual std::optional<abi::string> default_value_str() = 0;
148 virtual std::span<const abi::string> valid_value_strs() = 0;
149 virtual abi::string name_hints() = 0;
150 virtual abi::string value_hints() = 0;
152 virtual bool parse_value_and_cache(std::string_view value) = 0;
153 virtual void set_target_from_cache(
void* target) = 0;
155 virtual void raise_error(std::string_view message) = 0;
156 virtual void on_validation(
const validation_handler& handler) = 0;
157 virtual void on_error(
const output_handler& handler) = 0;
160 template <
typename T>
161 class wrapper final :
public base {
163 template <std::convertible_to<T> U>
164 explicit wrapper(U&& value) : value_{std::forward<U>(value)} {}
166 abi::string bound_name()
override {
167 return value_.bound_name();
170 void set_bound_name(std::string_view name)
override {
171 value_.set_bound_name(name);
174 abi::string description()
override {
175 return value_.description();
178 void set_description(std::string_view description)
override {
179 value_.set_description(description);
182 std::span<const abi::string> aliases()
override {
183 return value_.aliases();
186 void add_aliases(std::span<const abi::string> aliases)
override {
187 value_.add_aliases(aliases);
190 std::optional<abi::string> default_value_str()
override {
191 return value_.default_value_str();
194 std::span<const abi::string> valid_value_strs()
override {
195 return value_.valid_value_strs();
198 abi::string name_hints()
override {
199 return value_.name_hints();
202 abi::string value_hints()
override {
203 return value_.value_hints();
207 return value_.check_target_type(
id);
210 bool parse_value_and_cache(std::string_view value)
override {
211 return value_.parse_value_and_cache(value);
214 void set_target_from_cache(
void* target)
override {
215 value_.set_target_from_cache(target);
219 value_.validate(value, result);
222 void raise_error(std::string_view message)
override {
223 value_.raise_error(message);
226 void on_validation(
const validation_handler& handler)
override {
227 value_.on_validation(handler);
230 void on_error(
const output_handler& handler)
override {
231 value_.on_error(handler);
238 std::shared_ptr<base> wrapper_;