feat update
Some checks failed
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 1m3s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 1m26s
linux-arm-gcc / linux-gcc-armhf (push) Successful in 1m40s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m36s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 2m12s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 2m21s

This commit is contained in:
tqcq 2024-04-10 22:28:21 +08:00
parent 80de3add78
commit 59b2bf781f
3 changed files with 54 additions and 32 deletions

View File

@ -13,6 +13,12 @@ Config::Config(sled::string_view name, sled::string_view path) : config_name_(na
config_paths_.emplace_back(path.to_string()); config_paths_.emplace_back(path.to_string());
} }
void
Config::AddConfigFullPath(sled::string_view path)
{
config_full_paths_.emplace_back(path.to_string());
}
void void
Config::SetConfigName(sled::string_view name) Config::SetConfigName(sled::string_view name)
{ {
@ -29,32 +35,35 @@ bool
Config::ReadInConfig() Config::ReadInConfig()
{ {
const static std::vector<std::string> extensions = {".toml"}; const static std::vector<std::string> extensions = {".toml"};
for (const auto &path : config_paths_) { auto load_config_from = [](sled::string_view full_path, toml::value &value) {
auto name = path + config_name_; const std::ifstream file(full_path.to_string());
for (const auto &ext : extensions) {
const std::ifstream file(name + ext);
if (file.good()) { if (file.good()) {
try { try {
std::stringstream ss; std::stringstream ss;
ss << file.rdbuf(); ss << file.rdbuf();
std::istringstream stream_data(ss.str(), std::ios_base::binary | std::ios_base::in); std::istringstream stream_data(ss.str(), std::ios_base::binary | std::ios_base::in);
toml_ = toml::parse(stream_data, "string"); value = toml::parse(stream_data, "string");
return true; return true;
// goto config_read_success; // goto config_read_success;
} catch (...) { } catch (...) {
LOGD("Failed to parse config file: {}", name + ext); LOGD("Failed to parse config file: {}", full_path.to_string());
}
} }
} }
return false;
};
for (const auto &full_path : config_full_paths_) {
if (load_config_from(full_path, toml_)) { return true; }
} }
// config_read_success: for (const auto &path : config_paths_) {
// // pair<key, value> auto name = path + "/" + config_name_;
// for (auto &pair : default_values_) { for (const auto &ext : extensions) {
// toml::value value; auto full_path = name + ext;
// if (!GetNode(pair.first, value)) { AddDefaultNode(pair.first, pair.second); } if (load_config_from(full_path, toml_)) { return true; }
// } }
}
return false; return false;
} }

View File

@ -17,6 +17,18 @@ public:
Config &operator=(const Config &lhs) = delete; Config &operator=(const Config &lhs) = delete;
Config &operator=(Config &&rhs) noexcept = delete; Config &operator=(Config &&rhs) noexcept = delete;
/**
* case 1
* - fullpath = /path/to/config.toml
* - path = /path/to
* - name = config
*
* case 2
* - fullpath = ./path/to/config.toml
* - path = ./path/to
* - name = config
**/
void AddConfigFullPath(sled::string_view path);
void SetConfigName(sled::string_view name); void SetConfigName(sled::string_view name);
void AddConfigPath(sled::string_view path); void AddConfigPath(sled::string_view path);
@ -54,6 +66,7 @@ private:
std::unordered_map<std::string, ValueType> default_values_;// low_priority std::unordered_map<std::string, ValueType> default_values_;// low_priority
std::unordered_map<std::string, ValueType> values_; // high_priority std::unordered_map<std::string, ValueType> values_; // high_priority
std::vector<std::string> config_full_paths_;
std::vector<std::string> config_paths_; std::vector<std::string> config_paths_;
std::string config_name_; std::string config_name_;
toml::value toml_; toml::value toml_;

View File

@ -163,20 +163,20 @@ class InjectionContext {
public: public:
InjectionContext(Container &container, component_type requesterComponent) : container_(container) InjectionContext(Container &container, component_type requesterComponent) : container_(container)
{ {
pushType(requesterComponent); PushType(requesterComponent);
} }
~InjectionContext() { popType(); } ~InjectionContext() { PopType(); }
Container &getContainer() { return container_; } Container &GetContainer() { return container_; }
void pushType(component_type &type) { componentStack_.emplace_back(type); } void PushType(component_type &type) { componentStack_.emplace_back(type); }
void popType() { componentStack_.pop_back(); } void PopType() { componentStack_.pop_back(); }
const std::vector<component_type> &getComponentStack() { return componentStack_; } const std::vector<component_type> &getComponentStack() { return componentStack_; }
const component_type &getRequester() const component_type &GetRequester()
{ {
if (componentStack_.size() < 2) { throw InvalidOperationException("Context not valid."); } if (componentStack_.size() < 2) { throw InvalidOperationException("Context not valid."); }
@ -201,10 +201,10 @@ class ContextGuard {
public: public:
ContextGuard(InjectionContext *context, component_type type) : context_(context), type_(type) ContextGuard(InjectionContext *context, component_type type) : context_(context), type_(type)
{ {
context_->pushType(type); context_->PushType(type);
} }
~ContextGuard() { context_->popType(); } ~ContextGuard() { context_->PopType(); }
void ensureNoCycle() void ensureNoCycle()
{ {
@ -411,7 +411,7 @@ struct ctor_arg_resolver {
template<typename TCtorArgument, typename std::enable_if<!std::is_pointer<TCtorArgument>::value, int>::type = 0> template<typename TCtorArgument, typename std::enable_if<!std::is_pointer<TCtorArgument>::value, int>::type = 0>
operator TCtorArgument() operator TCtorArgument()
{ {
return context_->getContainer().Get<TCtorArgument>(context_); return context_->GetContainer().Get<TCtorArgument>(context_);
} }
InjectionContext *context_; InjectionContext *context_;
@ -429,7 +429,7 @@ struct ctor_arg_resolver_1st {
= 0> = 0>
operator TCtorArgument() operator TCtorArgument()
{ {
return context_->getContainer().Get<TCtorArgument>(context_); return context_->GetContainer().Get<TCtorArgument>(context_);
} }
InjectionContext *context_; InjectionContext *context_;
@ -511,7 +511,7 @@ template<typename TInstance, typename... TConstructorArgs>
struct ConstructorInvoker<TInstance(TConstructorArgs...)> { struct ConstructorInvoker<TInstance(TConstructorArgs...)> {
static std::shared_ptr<TInstance> invoke(InjectionContext *context) static std::shared_ptr<TInstance> invoke(InjectionContext *context)
{ {
Container &container = context->getContainer(); Container &container = context->GetContainer();
return std::make_shared<TInstance>(container.Get<TConstructorArgs>(context)...); return std::make_shared<TInstance>(container.Get<TConstructorArgs>(context)...);
} }