feat udpate config to configuration
This commit is contained in:
parent
76595be62a
commit
34f0461a68
@ -207,9 +207,9 @@ set(TILE_SRCS
|
|||||||
"tile/rpc/protocol/http/buffer_io.cc"
|
"tile/rpc/protocol/http/buffer_io.cc"
|
||||||
"tile/rpc/protocol/message.cc"
|
"tile/rpc/protocol/message.cc"
|
||||||
# "tile/rpc/server.cc"
|
# "tile/rpc/server.cc"
|
||||||
"tile/base/config/config.cc"
|
"tile/base/config/configuration.cc"
|
||||||
"tile/base/config/ini_file_config.cc"
|
"tile/base/config/ini_file_configuration.cc"
|
||||||
"tile/base/config/layered_config.cc"
|
"tile/base/config/layered_configuration.cc"
|
||||||
)
|
)
|
||||||
|
|
||||||
if((NOT TILE_HAVE_GETIFADDRS) OR (NOT TILE_HAVE_FREEIFADDRS))
|
if((NOT TILE_HAVE_GETIFADDRS) OR (NOT TILE_HAVE_FREEIFADDRS))
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
#include "tile/base/config/config.h"
|
#include "tile/base/config/configuration.h"
|
||||||
#include "tile/base/thread/unique_lock.h"
|
#include "tile/base/thread/unique_lock.h"
|
||||||
|
|
||||||
namespace tile {
|
namespace tile {
|
||||||
namespace util {
|
namespace util {
|
||||||
bool Config::Has(const Slice &key) const {
|
bool Configuration::Has(const Slice &key) const {
|
||||||
UniqueLock<tile::Mutex> lock(mutex_);
|
UniqueLock<tile::Mutex> lock(mutex_);
|
||||||
std::string value;
|
std::string value;
|
||||||
return GetRaw(key, &value);
|
return GetRaw(key, &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::Remove(const Slice &key) {}
|
void Configuration::Remove(const Slice &key) {}
|
||||||
|
|
||||||
void Config::EnableEvents(bool enable) { events_enabled_ = enable; }
|
void Configuration::EnableEvents(bool enable) { events_enabled_ = enable; }
|
||||||
bool Config::EventsEnabled() const { return events_enabled_; }
|
bool Configuration::EventsEnabled() const { return events_enabled_; }
|
||||||
|
|
||||||
Config::Keys Config::keys(const Slice &root) const {
|
Configuration::Keys Configuration::keys(const Slice &root) const {
|
||||||
UniqueLock<tile::Mutex> lock(mutex_);
|
UniqueLock<tile::Mutex> lock(mutex_);
|
||||||
return Enumerate(root);
|
return Enumerate(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TILE_DEFINE_CONFIG_GETTER(type, name) \
|
#define TILE_DEFINE_CONFIG_GETTER(type, name) \
|
||||||
std::optional<type> Config::Get##name(const Slice &key) const { \
|
std::optional<type> Configuration::Get##name(const Slice &key) const { \
|
||||||
std::string value; \
|
std::string value; \
|
||||||
if (GetRaw(key, &value)) { \
|
if (GetRaw(key, &value)) { \
|
||||||
auto opt = TryParseTraits<type>::TryParse(value); \
|
auto opt = TryParseTraits<type>::TryParse(value); \
|
||||||
@ -33,7 +33,7 @@ Config::Keys Config::keys(const Slice &root) const {
|
|||||||
return std::nullopt; \
|
return std::nullopt; \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
type Config::Get##name(const Slice &key, type default_value) const { \
|
type Configuration::Get##name(const Slice &key, type default_value) const { \
|
||||||
auto opt = Get##name(key); \
|
auto opt = Get##name(key); \
|
||||||
if (opt.has_value()) { \
|
if (opt.has_value()) { \
|
||||||
return *opt; \
|
return *opt; \
|
||||||
@ -43,11 +43,11 @@ Config::Keys Config::keys(const Slice &root) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define TILE_DEFINE_CONFIG_SETTER(type, name) \
|
#define TILE_DEFINE_CONFIG_SETTER(type, name) \
|
||||||
void Config::Set##name(const Slice &key, type value) { \
|
void Configuration::Set##name(const Slice &key, type value) { \
|
||||||
SetRawWithEvent(key, Format("{}", value)); \
|
SetRawWithEvent(key, Format("{}", value)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::string> Config::GetString(const Slice &key) const {
|
std::optional<std::string> Configuration::GetString(const Slice &key) const {
|
||||||
std::string value;
|
std::string value;
|
||||||
UniqueLock<tile::Mutex> lock(mutex_);
|
UniqueLock<tile::Mutex> lock(mutex_);
|
||||||
if (GetRaw(key, &value)) {
|
if (GetRaw(key, &value)) {
|
||||||
@ -57,16 +57,16 @@ std::optional<std::string> Config::GetString(const Slice &key) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Config::GetString(const Slice &key,
|
std::string Configuration::GetString(const Slice &key,
|
||||||
std::string default_value) const {
|
std::string default_value) const {
|
||||||
auto opt = GetString(key);
|
auto opt = GetString(key);
|
||||||
return opt.has_value() ? *opt : default_value;
|
return opt.has_value() ? *opt : default_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::SetString(const Slice &key, const std::string &value) {
|
void Configuration::SetString(const Slice &key, const std::string &value) {
|
||||||
SetRawWithEvent(key, value);
|
SetRawWithEvent(key, value);
|
||||||
}
|
}
|
||||||
void Config::SetBool(const Slice &key, bool value) {
|
void Configuration::SetBool(const Slice &key, bool value) {
|
||||||
SetRawWithEvent(key, value ? "true" : "false");
|
SetRawWithEvent(key, value ? "true" : "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,8 @@ TILE_DEFINE_CONFIG_SETTER(uint32_t, UInt32)
|
|||||||
TILE_DEFINE_CONFIG_SETTER(uint64_t, UInt64)
|
TILE_DEFINE_CONFIG_SETTER(uint64_t, UInt64)
|
||||||
TILE_DEFINE_CONFIG_SETTER(double, Double)
|
TILE_DEFINE_CONFIG_SETTER(double, Double)
|
||||||
|
|
||||||
void Config::SetRawWithEvent(const Slice &key, const std::string &value) {
|
void Configuration::SetRawWithEvent(const Slice &key,
|
||||||
|
const std::string &value) {
|
||||||
if (events_enabled_) {
|
if (events_enabled_) {
|
||||||
OnChanging(key, value);
|
OnChanging(key, value);
|
||||||
}
|
}
|
||||||
@ -106,7 +107,7 @@ void Config::SetRawWithEvent(const Slice &key, const std::string &value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::~Config() {}
|
Configuration::~Configuration() {}
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
|
|
@ -1,5 +1,5 @@
|
|||||||
#ifndef TILE_BASE_CONFIG_CONFIG_H
|
#ifndef TILE_BASE_CONFIG_CONFIGURATION_H
|
||||||
#define TILE_BASE_CONFIG_CONFIG_H
|
#define TILE_BASE_CONFIG_CONFIGURATION_H
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "tile/base/optional.h"
|
#include "tile/base/optional.h"
|
||||||
@ -12,10 +12,10 @@
|
|||||||
|
|
||||||
namespace tile {
|
namespace tile {
|
||||||
namespace util {
|
namespace util {
|
||||||
class Config : public RefCounted<Config> {
|
class Configuration : public RefCounted<Configuration> {
|
||||||
public:
|
public:
|
||||||
using Keys = std::vector<std::string>;
|
using Keys = std::vector<std::string>;
|
||||||
using Ptr = RefPtr<Config>;
|
using Ptr = RefPtr<Configuration>;
|
||||||
|
|
||||||
// events
|
// events
|
||||||
// Key, Value
|
// Key, Value
|
||||||
@ -98,13 +98,13 @@ protected:
|
|||||||
protected:
|
protected:
|
||||||
class ScopedLock {
|
class ScopedLock {
|
||||||
public:
|
public:
|
||||||
explicit ScopedLock(const Config &config) : config_(config) {
|
explicit ScopedLock(const Configuration &config) : config_(config) {
|
||||||
config_.mutex_.Lock();
|
config_.mutex_.Lock();
|
||||||
}
|
}
|
||||||
~ScopedLock() { config_.mutex_.Unlock(); }
|
~ScopedLock() { config_.mutex_.Unlock(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Config &config_;
|
const Configuration &config_;
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual bool GetRaw(const Slice &key, std::string *value) const = 0;
|
virtual bool GetRaw(const Slice &key, std::string *value) const = 0;
|
||||||
@ -112,10 +112,10 @@ protected:
|
|||||||
virtual void RemoveRaw(const Slice &key) = 0;
|
virtual void RemoveRaw(const Slice &key) = 0;
|
||||||
virtual Keys Enumerate(const Slice &range) const = 0;
|
virtual Keys Enumerate(const Slice &range) const = 0;
|
||||||
void SetRawWithEvent(const Slice &key, const std::string &value);
|
void SetRawWithEvent(const Slice &key, const std::string &value);
|
||||||
virtual ~Config();
|
virtual ~Configuration();
|
||||||
|
|
||||||
friend class std::default_delete<Config>;
|
friend class std::default_delete<Configuration>;
|
||||||
friend class LayeredConfig;
|
friend class LayeredConfiguration;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable Mutex mutex_;
|
mutable Mutex mutex_;
|
||||||
@ -124,4 +124,4 @@ private:
|
|||||||
} // namespace util
|
} // namespace util
|
||||||
} // namespace tile
|
} // namespace tile
|
||||||
|
|
||||||
#endif // TILE_BASE_CONFIG_CONFIG_H
|
#endif // TILE_BASE_CONFIG_CONFIGURATION_H
|
@ -1,16 +1,16 @@
|
|||||||
#include "tile/base/config/ini_file_config.h"
|
#include "tile/base/config/ini_file_configuration.h"
|
||||||
#include "tile/base/thread/scoped_lock.h"
|
#include "tile/base/thread/scoped_lock.h"
|
||||||
|
|
||||||
namespace tile {
|
namespace tile {
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
IniFileConfig::IniFileConfig() {}
|
IniFileConfiguration::IniFileConfiguration() {}
|
||||||
// IniFileConfig::IniFileConfig(std::istream &istr) { load(istr); }
|
// IniFileConfig::IniFileConfig(std::istream &istr) { load(istr); }
|
||||||
// IniFileConfig::IniFileConfig(const std::string &path) { load(path); }
|
// IniFileConfig::IniFileConfig(const std::string &path) { load(path); }
|
||||||
IniFileConfig::~IniFileConfig() {}
|
IniFileConfiguration::~IniFileConfiguration() {}
|
||||||
|
|
||||||
bool IniFileConfig::load(std::istream &istr) {
|
bool IniFileConfiguration::load(std::istream &istr) {
|
||||||
Config::ScopedLock lock(*this);
|
Configuration::ScopedLock lock(*this);
|
||||||
map_.clear();
|
map_.clear();
|
||||||
section_key_.clear();
|
section_key_.clear();
|
||||||
while (!istr.eof()) {
|
while (!istr.eof()) {
|
||||||
@ -18,7 +18,7 @@ bool IniFileConfig::load(std::istream &istr) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool IniFileConfig::load(const std::string &path) {
|
bool IniFileConfiguration::load(const std::string &path) {
|
||||||
std::ifstream istr(path);
|
std::ifstream istr(path);
|
||||||
if (istr.good()) {
|
if (istr.good()) {
|
||||||
return load(istr);
|
return load(istr);
|
||||||
@ -26,7 +26,7 @@ bool IniFileConfig::load(const std::string &path) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool IniFileConfig::GetRaw(const Slice &key, std::string *value) const {
|
bool IniFileConfiguration::GetRaw(const Slice &key, std::string *value) const {
|
||||||
auto iter = map_.find(key.ToString());
|
auto iter = map_.find(key.ToString());
|
||||||
if (iter != map_.end()) {
|
if (iter != map_.end()) {
|
||||||
*value = iter->second;
|
*value = iter->second;
|
||||||
@ -35,12 +35,12 @@ bool IniFileConfig::GetRaw(const Slice &key, std::string *value) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool IniFileConfig::SetRaw(const Slice &key, const Slice &value) {
|
bool IniFileConfiguration::SetRaw(const Slice &key, const Slice &value) {
|
||||||
map_[key] = value;
|
map_[key] = value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IniFileConfig::RemoveRaw(const Slice &key) {
|
void IniFileConfiguration::RemoveRaw(const Slice &key) {
|
||||||
std::string prefix = key;
|
std::string prefix = key;
|
||||||
if (!prefix.empty()) {
|
if (!prefix.empty()) {
|
||||||
prefix.push_back('.');
|
prefix.push_back('.');
|
||||||
@ -57,8 +57,8 @@ void IniFileConfig::RemoveRaw(const Slice &key) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::Keys IniFileConfig::Enumerate(const Slice &key) const {
|
Configuration::Keys IniFileConfiguration::Enumerate(const Slice &key) const {
|
||||||
Config::Keys range;
|
Configuration::Keys range;
|
||||||
std::set<Slice> keys;
|
std::set<Slice> keys;
|
||||||
std::string prefix = key.ToString();
|
std::string prefix = key.ToString();
|
||||||
if (prefix.empty()) {
|
if (prefix.empty()) {
|
||||||
@ -86,7 +86,7 @@ Config::Keys IniFileConfig::Enumerate(const Slice &key) const {
|
|||||||
return range;
|
return range;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IniFileConfig::ParseLine(std::istream &istr) {
|
void IniFileConfiguration::ParseLine(std::istream &istr) {
|
||||||
static const int eof = std::char_traits<char>::eof();
|
static const int eof = std::char_traits<char>::eof();
|
||||||
auto ReadLine = [&](std::string *line) {
|
auto ReadLine = [&](std::string *line) {
|
||||||
line->clear();
|
line->clear();
|
||||||
@ -135,8 +135,8 @@ void IniFileConfig::ParseLine(std::istream &istr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool IniFileConfig::ICompare::operator()(const std::string &s1,
|
bool IniFileConfiguration::ICompare::operator()(const std::string &s1,
|
||||||
const std::string &s2) const {
|
const std::string &s2) const {
|
||||||
auto len = std::min(s1.size(), s2.size());
|
auto len = std::min(s1.size(), s2.size());
|
||||||
return strncmp(s1.c_str(), s2.c_str(), len) < 0;
|
return strncmp(s1.c_str(), s2.c_str(), len) < 0;
|
||||||
}
|
}
|
@ -3,17 +3,17 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "tile/base/config/config.h"
|
#include "tile/base/config/configuration.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
namespace tile {
|
namespace tile {
|
||||||
namespace util {
|
namespace util {
|
||||||
class IniFileConfig : public Config {
|
class IniFileConfiguration : public Configuration {
|
||||||
public:
|
public:
|
||||||
using Ptr = RefPtr<IniFileConfig>;
|
using Ptr = RefPtr<IniFileConfiguration>;
|
||||||
|
|
||||||
IniFileConfig();
|
IniFileConfiguration();
|
||||||
~IniFileConfig() override;
|
~IniFileConfiguration() override;
|
||||||
// IniFileConfig(std::istream &istr);
|
// IniFileConfig(std::istream &istr);
|
||||||
// IniFileConfig(const std::string &path);
|
// IniFileConfig(const std::string &path);
|
||||||
bool load(std::istream &istr);
|
bool load(std::istream &istr);
|
@ -1,4 +1,4 @@
|
|||||||
#include "tile/base/config/ini_file_config.h"
|
#include "tile/base/config/ini_file_configuration.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
const char *kIniFileConfig = R"(
|
const char *kIniFileConfig = R"(
|
||||||
@ -18,16 +18,17 @@ a=4
|
|||||||
namespace tile {
|
namespace tile {
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
static_assert(!detail::HasClassofImpl<IniFileConfig, Config>::value, "");
|
static_assert(
|
||||||
|
!detail::HasClassofImpl<IniFileConfiguration, Configuration>::value, "");
|
||||||
|
|
||||||
TEST(IniFileConfig, LoadFromIStream) {
|
TEST(IniFileConfig, LoadFromIStream) {
|
||||||
std::stringstream ss(kIniFileConfig);
|
std::stringstream ss(kIniFileConfig);
|
||||||
Config::Ptr config = MakeRefCounted<IniFileConfig>();
|
Configuration::Ptr config = MakeRefCounted<IniFileConfiguration>();
|
||||||
ASSERT_FALSE(config->Has("a"));
|
ASSERT_FALSE(config->Has("a"));
|
||||||
ASSERT_FALSE(config->Has("sec1.a"));
|
ASSERT_FALSE(config->Has("sec1.a"));
|
||||||
ASSERT_FALSE(config->Has("sec3.a"));
|
ASSERT_FALSE(config->Has("sec3.a"));
|
||||||
if (config.Is<IniFileConfig>()) {
|
if (config.Is<IniFileConfiguration>()) {
|
||||||
IniFileConfig::Ptr ini = config.As<IniFileConfig>();
|
IniFileConfiguration::Ptr ini = config.As<IniFileConfiguration>();
|
||||||
ASSERT_TRUE(ini->load(ss));
|
ASSERT_TRUE(ini->load(ss));
|
||||||
}
|
}
|
||||||
ASSERT_TRUE(config->Has("a"));
|
ASSERT_TRUE(config->Has("a"));
|
@ -1,129 +0,0 @@
|
|||||||
#include "tile/base/config/layered_config.h"
|
|
||||||
|
|
||||||
namespace tile {
|
|
||||||
namespace util {
|
|
||||||
|
|
||||||
LayeredConfig::LayeredConfig() {}
|
|
||||||
LayeredConfig::~LayeredConfig() {}
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg) { Add(cfg, highest(), false); }
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg, int priority) {
|
|
||||||
Add(cfg, priority, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label) {
|
|
||||||
Add(cfg, label, highest(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label,
|
|
||||||
int priority) {
|
|
||||||
Add(cfg, label, priority, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label,
|
|
||||||
bool writeable) {
|
|
||||||
Add(cfg, label, highest(), writeable);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label, int priority,
|
|
||||||
bool writeable) {
|
|
||||||
Config::ScopedLock lock(*this);
|
|
||||||
ConfigItem item;
|
|
||||||
item.cfg = cfg;
|
|
||||||
item.priority = priority;
|
|
||||||
item.writeable = writeable;
|
|
||||||
item.label = label;
|
|
||||||
auto it = configs_.begin();
|
|
||||||
while (it != configs_.end() && it->priority < priority) {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
configs_.insert(it, item);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::Add(Config::Ptr cfg, int priority, bool writeable) {
|
|
||||||
Add(cfg, std::string(), priority, writeable);
|
|
||||||
}
|
|
||||||
void LayeredConfig::AddWriteable(Config::Ptr cfg, int priority) {
|
|
||||||
Add(cfg, priority, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Config::Ptr LayeredConfig::Find(const Slice &label) const {
|
|
||||||
Config::ScopedLock lock(*this);
|
|
||||||
|
|
||||||
for (const auto &conf : configs_) {
|
|
||||||
if (conf.label == label) {
|
|
||||||
return conf.cfg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::RemoveConfig(Config::Ptr cfg) {
|
|
||||||
Config::ScopedLock lock(*this);
|
|
||||||
for (auto it = configs_.begin(); it != configs_.end();) {
|
|
||||||
if (it->cfg == cfg) {
|
|
||||||
it = configs_.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LayeredConfig::GetRaw(const Slice &key, std::string *value) const {
|
|
||||||
for (const auto &conf : configs_) {
|
|
||||||
if (conf.cfg->GetRaw(key, value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LayeredConfig::SetRaw(const Slice &key, const Slice &value) {
|
|
||||||
for (const auto &conf : configs_) {
|
|
||||||
if (conf.writeable && conf.cfg->SetRaw(key, value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Config::Keys LayeredConfig::Enumerate(const Slice &key) const {
|
|
||||||
Config::Keys keys;
|
|
||||||
std::set<Slice> key_set;
|
|
||||||
for (const auto &conf : configs_) {
|
|
||||||
auto conf_keys = conf.cfg->Enumerate(key);
|
|
||||||
for (const auto &k : conf_keys) {
|
|
||||||
if (key_set.insert(k).second) {
|
|
||||||
keys.push_back(k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LayeredConfig::RemoveRaw(const std::string &key) {
|
|
||||||
for (auto &conf : configs_) {
|
|
||||||
if (conf.writeable) {
|
|
||||||
conf.cfg->RemoveRaw(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int LayeredConfig::lowest() const {
|
|
||||||
if (configs_.empty()) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return configs_.front().priority - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int LayeredConfig::highest() const {
|
|
||||||
if (configs_.empty()) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return configs_.back().priority + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace util
|
|
||||||
} // namespace tile
|
|
@ -1,57 +0,0 @@
|
|||||||
#ifndef TILE_BASE_CONFIG_LAYERED_CONFIG_H
|
|
||||||
#define TILE_BASE_CONFIG_LAYERED_CONFIG_H
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "tile/base/config/config.h"
|
|
||||||
#include <list>
|
|
||||||
|
|
||||||
namespace tile {
|
|
||||||
namespace util {
|
|
||||||
class LayeredConfig : public Config {
|
|
||||||
public:
|
|
||||||
using Ptr = RefPtr<LayeredConfig>;
|
|
||||||
LayeredConfig();
|
|
||||||
LayeredConfig(const LayeredConfig &) = delete;
|
|
||||||
LayeredConfig &operator=(const LayeredConfig &) = delete;
|
|
||||||
|
|
||||||
void Add(Config::Ptr cfg);
|
|
||||||
void Add(Config::Ptr cfg, int priority);
|
|
||||||
void Add(Config::Ptr cfg, const std::string &label);
|
|
||||||
void Add(Config::Ptr cfg, const std::string &label, int priority);
|
|
||||||
void Add(Config::Ptr cfg, const std::string &label, bool writeable);
|
|
||||||
void Add(Config::Ptr cfg, const std::string &label, int priority,
|
|
||||||
bool writeable);
|
|
||||||
void Add(Config::Ptr cfg, int priority, bool writeable);
|
|
||||||
void AddWriteable(Config::Ptr cfg, int priority);
|
|
||||||
Config::Ptr Find(const Slice &label) const;
|
|
||||||
void RemoveConfig(Config::Ptr cfg);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
struct ConfigItem {
|
|
||||||
Config::Ptr cfg;
|
|
||||||
// ... > -2 > -1 > 0 > 1 > 2 > 3 > 4 ...
|
|
||||||
int priority;
|
|
||||||
// can remove or set new?
|
|
||||||
bool writeable;
|
|
||||||
std::string label;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool GetRaw(const Slice &key, std::string *value) const override;
|
|
||||||
bool SetRaw(const Slice &key, const Slice &value) override;
|
|
||||||
Config::Keys Enumerate(const Slice &key) const override;
|
|
||||||
void RemoveRaw(const std::string &key);
|
|
||||||
|
|
||||||
int lowest() const;
|
|
||||||
int highest() const;
|
|
||||||
|
|
||||||
~LayeredConfig();
|
|
||||||
|
|
||||||
private:
|
|
||||||
using ConfigList = std::list<ConfigItem>;
|
|
||||||
ConfigList configs_;
|
|
||||||
};
|
|
||||||
} // namespace util
|
|
||||||
} // namespace tile
|
|
||||||
|
|
||||||
#endif // TILE_BASE_CONFIG_LAYERED_CONFIG_H
|
|
133
tile/base/config/layered_configuration.cc
Normal file
133
tile/base/config/layered_configuration.cc
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
#include "tile/base/config/layered_configuration.h"
|
||||||
|
|
||||||
|
namespace tile {
|
||||||
|
namespace util {
|
||||||
|
|
||||||
|
LayeredConfiguration::LayeredConfiguration() {}
|
||||||
|
LayeredConfiguration::~LayeredConfiguration() {}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg) {
|
||||||
|
Add(cfg, highest(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg, int priority) {
|
||||||
|
Add(cfg, priority, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg,
|
||||||
|
const std::string &label) {
|
||||||
|
Add(cfg, label, highest(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg, const std::string &label,
|
||||||
|
int priority) {
|
||||||
|
Add(cfg, label, priority, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg, const std::string &label,
|
||||||
|
bool writeable) {
|
||||||
|
Add(cfg, label, highest(), writeable);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg, const std::string &label,
|
||||||
|
int priority, bool writeable) {
|
||||||
|
Configuration::ScopedLock lock(*this);
|
||||||
|
ConfigItem item;
|
||||||
|
item.cfg = cfg;
|
||||||
|
item.priority = priority;
|
||||||
|
item.writeable = writeable;
|
||||||
|
item.label = label;
|
||||||
|
auto it = configs_.begin();
|
||||||
|
while (it != configs_.end() && it->priority < priority) {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
configs_.insert(it, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::Add(Configuration::Ptr cfg, int priority,
|
||||||
|
bool writeable) {
|
||||||
|
Add(cfg, std::string(), priority, writeable);
|
||||||
|
}
|
||||||
|
void LayeredConfiguration::AddWriteable(Configuration::Ptr cfg, int priority) {
|
||||||
|
Add(cfg, priority, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Configuration::Ptr LayeredConfiguration::Find(const Slice &label) const {
|
||||||
|
Configuration::ScopedLock lock(*this);
|
||||||
|
|
||||||
|
for (const auto &conf : configs_) {
|
||||||
|
if (conf.label == label) {
|
||||||
|
return conf.cfg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::RemoveConfig(Configuration::Ptr cfg) {
|
||||||
|
Configuration::ScopedLock lock(*this);
|
||||||
|
for (auto it = configs_.begin(); it != configs_.end();) {
|
||||||
|
if (it->cfg == cfg) {
|
||||||
|
it = configs_.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LayeredConfiguration::GetRaw(const Slice &key, std::string *value) const {
|
||||||
|
for (const auto &conf : configs_) {
|
||||||
|
if (conf.cfg->GetRaw(key, value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LayeredConfiguration::SetRaw(const Slice &key, const Slice &value) {
|
||||||
|
for (const auto &conf : configs_) {
|
||||||
|
if (conf.writeable && conf.cfg->SetRaw(key, value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Configuration::Keys LayeredConfiguration::Enumerate(const Slice &key) const {
|
||||||
|
Configuration::Keys keys;
|
||||||
|
std::set<Slice> key_set;
|
||||||
|
for (const auto &conf : configs_) {
|
||||||
|
auto conf_keys = conf.cfg->Enumerate(key);
|
||||||
|
for (const auto &k : conf_keys) {
|
||||||
|
if (key_set.insert(k).second) {
|
||||||
|
keys.push_back(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LayeredConfiguration::RemoveRaw(const std::string &key) {
|
||||||
|
for (auto &conf : configs_) {
|
||||||
|
if (conf.writeable) {
|
||||||
|
conf.cfg->RemoveRaw(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int LayeredConfiguration::lowest() const {
|
||||||
|
if (configs_.empty()) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return configs_.front().priority - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int LayeredConfiguration::highest() const {
|
||||||
|
if (configs_.empty()) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return configs_.back().priority + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace util
|
||||||
|
} // namespace tile
|
57
tile/base/config/layered_configuration.h
Normal file
57
tile/base/config/layered_configuration.h
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#ifndef TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H
|
||||||
|
#define TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "tile/base/config/configuration.h"
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
namespace tile {
|
||||||
|
namespace util {
|
||||||
|
class LayeredConfiguration : public Configuration {
|
||||||
|
public:
|
||||||
|
using Ptr = RefPtr<LayeredConfiguration>;
|
||||||
|
LayeredConfiguration();
|
||||||
|
LayeredConfiguration(const LayeredConfiguration &) = delete;
|
||||||
|
LayeredConfiguration &operator=(const LayeredConfiguration &) = delete;
|
||||||
|
|
||||||
|
void Add(Configuration::Ptr cfg);
|
||||||
|
void Add(Configuration::Ptr cfg, int priority);
|
||||||
|
void Add(Configuration::Ptr cfg, const std::string &label);
|
||||||
|
void Add(Configuration::Ptr cfg, const std::string &label, int priority);
|
||||||
|
void Add(Configuration::Ptr cfg, const std::string &label, bool writeable);
|
||||||
|
void Add(Configuration::Ptr cfg, const std::string &label, int priority,
|
||||||
|
bool writeable);
|
||||||
|
void Add(Configuration::Ptr cfg, int priority, bool writeable);
|
||||||
|
void AddWriteable(Configuration::Ptr cfg, int priority);
|
||||||
|
Configuration::Ptr Find(const Slice &label) const;
|
||||||
|
void RemoveConfig(Configuration::Ptr cfg);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
struct ConfigItem {
|
||||||
|
Configuration::Ptr cfg;
|
||||||
|
// ... > -2 > -1 > 0 > 1 > 2 > 3 > 4 ...
|
||||||
|
int priority;
|
||||||
|
// can remove or set new?
|
||||||
|
bool writeable;
|
||||||
|
std::string label;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool GetRaw(const Slice &key, std::string *value) const override;
|
||||||
|
bool SetRaw(const Slice &key, const Slice &value) override;
|
||||||
|
Configuration::Keys Enumerate(const Slice &key) const override;
|
||||||
|
void RemoveRaw(const std::string &key);
|
||||||
|
|
||||||
|
int lowest() const;
|
||||||
|
int highest() const;
|
||||||
|
|
||||||
|
~LayeredConfiguration();
|
||||||
|
|
||||||
|
private:
|
||||||
|
using ConfigList = std::list<ConfigItem>;
|
||||||
|
ConfigList configs_;
|
||||||
|
};
|
||||||
|
} // namespace util
|
||||||
|
} // namespace tile
|
||||||
|
|
||||||
|
#endif // TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H
|
11
tile/base/configuration.h
Normal file
11
tile/base/configuration.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef TILE_BASE_CONFIGURATION_H
|
||||||
|
#define TILE_BASE_CONFIGURATION_H
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "tile/base/config/configurable.h"
|
||||||
|
#include "tile/base/config/configuration.h"
|
||||||
|
#include "tile/base/config/ini_file_configuration.h"
|
||||||
|
#include "tile/base/config/layered_configuration.h"
|
||||||
|
|
||||||
|
#endif // TILE_BASE_CONFIGURATION_H
|
@ -9,7 +9,6 @@
|
|||||||
#include "tile/base/casting.h"
|
#include "tile/base/casting.h"
|
||||||
#include "tile/base/chrono.h"
|
#include "tile/base/chrono.h"
|
||||||
#include "tile/base/compression.h"
|
#include "tile/base/compression.h"
|
||||||
#include "tile/base/config/ini_file_config.h"
|
|
||||||
#include "tile/base/data.h"
|
#include "tile/base/data.h"
|
||||||
#include "tile/base/deferred.h"
|
#include "tile/base/deferred.h"
|
||||||
#include "tile/base/demangle.h"
|
#include "tile/base/demangle.h"
|
||||||
@ -40,13 +39,12 @@
|
|||||||
#include "tile/base/write_mostly.h"
|
#include "tile/base/write_mostly.h"
|
||||||
|
|
||||||
// util
|
// util
|
||||||
#include "tile/util/config.h"
|
#include "tile/base/configuration.h"
|
||||||
#include "tile/util/ini_file_config.h"
|
|
||||||
|
|
||||||
// init module
|
// init module
|
||||||
#include "tile/init/override_flag.h"
|
#include "tile/init/override_flag.h"
|
||||||
|
|
||||||
#include "sigslot/signal.h"
|
#include "sigslot/sigslot.h"
|
||||||
|
|
||||||
// Tile Init
|
// Tile Init
|
||||||
#include "mustache.hpp"
|
#include "mustache.hpp"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user