1 #ifndef INICPP_OPTION_H 2 #define INICPP_OPTION_H 10 #include "exception.h" 11 #include "option_schema.h" 12 #include "string_utils.h" 72 void set(ValueType value)
95 template <
typename ActualType,
typename ReturnType>
class convertor
105 static ReturnType get_converted_value(
const std::unique_ptr<option_holder> &value)
108 if (ptr ==
nullptr) {
112 return static_cast<ReturnType
>(ptr->
get());
113 }
catch (std::runtime_error &e) {
122 template <
typename ActualType>
class convertor<ActualType, string_ini_t>
125 static string_ini_t get_converted_value(
const std::unique_ptr<option_holder> &value)
128 if (ptr ==
nullptr) {
149 std::vector<std::unique_ptr<option_holder>> values_;
151 std::shared_ptr<option_schema> option_schema_;
154 template <
typename ValueType>
void copy_option(
const std::unique_ptr<option_holder> &opt)
157 auto new_option_value = std::make_unique<option_value<ValueType>>(ptr->
get());
158 values_.push_back(std::move(new_option_value));
162 template <
typename ValueType>
164 const std::unique_ptr<option_holder> &local,
const std::unique_ptr<option_holder> &remote)
const 168 return loc->
get() == rem->
get();
171 template <
typename ReturnType>
172 ReturnType convert_single_value(option_type source_type,
const std::unique_ptr<option_holder> &value)
const 174 switch (source_type) {
175 case option_type::boolean_e:
return convertor<boolean_ini_t, ReturnType>::get_converted_value(value);
break;
176 case option_type::enum_e:
return convertor<enum_ini_t, ReturnType>::get_converted_value(value);
break;
177 case option_type::float_e:
return convertor<float_ini_t, ReturnType>::get_converted_value(value);
break;
178 case option_type::signed_e:
return convertor<signed_ini_t, ReturnType>::get_converted_value(value);
break;
179 case option_type::string_e: {
181 if (ptr ==
nullptr) {
187 return string_utils::parse_string<ReturnType>(ptr->
get(), get_name());
192 case option_type::unsigned_e:
193 return convertor<unsigned_ini_t, ReturnType>::get_converted_value(value);
195 case option_type::invalid_e:
230 option(
const std::string &name,
const std::string &value =
"");
236 option(
const std::string &name,
const std::vector<std::string> &values);
242 const std::string &get_name()
const;
248 option_type get_type()
const;
253 bool is_list()
const;
261 template <
typename ValueType>
void set(ValueType value)
263 this->operator=(value);
270 option &operator=(boolean_ini_t arg);
276 option &operator=(signed_ini_t arg);
282 option &operator=(unsigned_ini_t arg);
288 option &operator=(float_ini_t arg);
294 option &operator=(
const char *arg);
300 option &operator=(string_ini_t arg);
315 template <
typename ReturnType> ReturnType
get()
const 317 if (values_.empty()) {
322 return convert_single_value<ReturnType>(type_, values_[0]);
333 template <
typename ValueType>
void set_list(
const std::vector<ValueType> &list)
336 type_ = get_option_enum_type<ValueType>();
337 for (
const auto &item : list) {
350 template <
typename ReturnType> std::vector<ReturnType>
get_list()
const 352 if (values_.empty()) {
355 std::vector<ReturnType> results;
356 for (
const auto &value : values_) {
357 results.push_back(convert_single_value<ReturnType>(type_, value));
371 if (get_option_enum_type<ValueType>() != type_) {
374 auto new_option_value = std::make_unique<option_value<ValueType>>(value);
375 values_.push_back(std::move(new_option_value));
386 template <
typename ValueType>
void add_to_list(ValueType value,
size_t position)
388 if (get_option_enum_type<ValueType>() != type_) {
391 if (position > values_.size()) {
394 auto new_option_value = std::make_unique<option_value<ValueType>>(value);
395 values_.insert(values_.begin() + position, std::move(new_option_value));
405 if (get_option_enum_type<ValueType>() != type_) {
408 for (
auto it = values_.cbegin(); it != values_.cend(); ++it) {
410 if (ptr->
get() == value) {
422 void remove_from_list_pos(
size_t position);
436 bool operator==(
const option &other)
const;
442 bool operator!=(
const option &other)
const;
450 INICPP_API
friend std::ostream &operator<<(std::ostream &os,
const option &opt);
453 INICPP_API std::ostream &operator<<(std::ostream &os,
const option &opt);
void add_to_list(ValueType value)
option_value(ValueType value)
std::vector< ReturnType > get_list() const
void set_list(const std::vector< ValueType > &list)
void add_to_list(ValueType value, size_t position)
void remove_from_list(ValueType value)
std::string to_string(const enum_ini_t &value)
virtual const char * what() const noexcept