Replace remained old ini-manager calls

This commit is contained in:
O1L 2015-11-14 23:59:46 +04:00
parent 7dfe9415c4
commit fd13a495de
18 changed files with 194 additions and 119 deletions

View file

@ -6,7 +6,8 @@
void config_context_t::group::init()
{
m_cfg->m_groups[full_name()] = this;
if(!m_cfg->m_groups[full_name()])
m_cfg->m_groups[full_name()] = this;
}
config_context_t::group::group(config_context_t* cfg, const std::string& name)
@ -52,7 +53,10 @@ void config_context_t::assign(const config_context_t& rhs)
for (auto rhs_e : rhs_g.second->entries)
{
g->entries[rhs_e.first]->value_from(rhs_e.second);
if (g->entries[rhs_e.first])
g->entries[rhs_e.first]->value_from(rhs_e.second);
else
g->add_entry(rhs_e.first, rhs_e.second->string_value());
}
}
}
@ -99,11 +103,27 @@ void config_context_t::deserialize(std::istream& stream)
auto name_value = fmt::split(line, { "=" });
switch (name_value.size())
{
case 1: current_group->entries[fmt::trim(name_value[0])]->string_value({}); break;
case 1:
{
if (current_group->entries[fmt::trim(name_value[0])])
current_group->entries[fmt::trim(name_value[0])]->string_value({});
else
current_group->add_entry(fmt::trim(name_value[0]), std::string{});
}
break;
default:
std::cerr << line_index << ": line '" << line << "' has more than one symbol '='. used only first" << std::endl;
case 2: current_group->entries[fmt::trim(name_value[0])]->string_value(fmt::trim(name_value[1])); break;
case 2:
{
if (current_group->entries[fmt::trim(name_value[0])])
current_group->entries[fmt::trim(name_value[0])]->string_value(fmt::trim(name_value[1]));
else
current_group->add_entry(fmt::trim(name_value[0]), fmt::trim(name_value[1]));
}
break;
}
}
@ -142,4 +162,17 @@ std::string config_context_t::to_string() const
serialize(result);
return result.str();
}
}
void config_context_t::add_group(const std::string& name)
{
new group(this, name);
}
config_context_t::group& config_context_t::get_group(const std::string& name)
{
if (!m_groups[name])
add_group(name);
return *m_groups[name];
}

View file

@ -27,6 +27,31 @@ protected:
std::string name() const;
std::string full_name() const;
template<typename T>
void add_entry(const std::string& name, const T& def_value)
{
new entry<T>(this, name, def_value);
}
template<typename T>
T get_entry_value(const std::string& name, const T& def_value)
{
if (!entries[name])
add_entry(name, def_value);
return convert::to<T>(entries[name]->string_value());
}
template<typename T>
void set_entry_value(const std::string& name, const T& value)
{
if (entries[name])
entries[name]->string_value(convert::to<std::string>(value));
else
add_entry(name, value);
}
friend config_context_t;
};
@ -56,7 +81,8 @@ public:
, m_default_value(default_value)
, m_value(default_value)
{
parent->entries[name] = this;
if(!parent->entries[name])
parent->entries[name] = this;
}
T default_value() const
@ -132,4 +158,7 @@ public:
void set_defaults();
std::string to_string() const;
void add_group(const std::string& name);
group& get_group(const std::string& name);
};

View file

@ -1,5 +1,6 @@
#pragma once
#include <string>
#include "types.h"
namespace convert
{
@ -151,6 +152,24 @@ namespace convert
}
};
template<>
struct to_impl_t<std::string, size2i>
{
static std::string func(size2i value)
{
return std::to_string(value.width) + "x" + std::to_string(value.height);
}
};
template<>
struct to_impl_t<std::string, position2i>
{
static std::string func(position2i value)
{
return std::to_string(value.x) + ":" + std::to_string(value.y);
}
};
template<>
struct to_impl_t<int, std::string>
{
@ -231,10 +250,30 @@ namespace convert
return std::stold(value);
}
};
template<>
struct to_impl_t<size2i, std::string>
{
static size2i func(const std::string& value)
{
const auto& data = fmt::split(value, { "x" });
return { std::stoi(data[0]), std::stoi(data[1]) };
}
};
template<>
struct to_impl_t<position2i, std::string>
{
static position2i func(const std::string& value)
{
const auto& data = fmt::split(value, { ":" });
return { std::stoi(data[0]), std::stoi(data[1]) };
}
};
template<typename ReturnType, typename FromType>
ReturnType to(FromType value)
{
return to_impl_t<std::remove_all_extents_t<ReturnType>, std::remove_all_extents_t<FromType>>::func(value);
}
}
}