diff --git a/src/sled/config.cc b/src/sled/config.cc index fece12a..6a191a9 100644 --- a/src/sled/config.cc +++ b/src/sled/config.cc @@ -13,6 +13,12 @@ Config::Config(sled::string_view name, sled::string_view path) : config_name_(na config_paths_.emplace_back(path.to_string()); } +void +Config::AddConfigFullPath(sled::string_view path) +{ + config_full_paths_.emplace_back(path.to_string()); +} + void Config::SetConfigName(sled::string_view name) { @@ -29,32 +35,35 @@ bool Config::ReadInConfig() { const static std::vector extensions = {".toml"}; - for (const auto &path : config_paths_) { - auto name = path + config_name_; - for (const auto &ext : extensions) { - const std::ifstream file(name + ext); - if (file.good()) { - try { - std::stringstream ss; - ss << file.rdbuf(); + auto load_config_from = [](sled::string_view full_path, toml::value &value) { + const std::ifstream file(full_path.to_string()); + if (file.good()) { + try { + std::stringstream ss; + ss << file.rdbuf(); - std::istringstream stream_data(ss.str(), std::ios_base::binary | std::ios_base::in); - toml_ = toml::parse(stream_data, "string"); - return true; - // goto config_read_success; - } catch (...) { - LOGD("Failed to parse config file: {}", name + ext); - } + std::istringstream stream_data(ss.str(), std::ios_base::binary | std::ios_base::in); + value = toml::parse(stream_data, "string"); + return true; + // goto config_read_success; + } catch (...) { + 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: - // // pair - // for (auto &pair : default_values_) { - // toml::value value; - // if (!GetNode(pair.first, value)) { AddDefaultNode(pair.first, pair.second); } - // } + for (const auto &path : config_paths_) { + auto name = path + "/" + config_name_; + for (const auto &ext : extensions) { + auto full_path = name + ext; + if (load_config_from(full_path, toml_)) { return true; } + } + } return false; } diff --git a/src/sled/config.h b/src/sled/config.h index 2e24ea8..853912e 100644 --- a/src/sled/config.h +++ b/src/sled/config.h @@ -17,6 +17,18 @@ public: Config &operator=(const Config &lhs) = 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 AddConfigPath(sled::string_view path); @@ -54,6 +66,7 @@ private: std::unordered_map default_values_;// low_priority std::unordered_map values_; // high_priority + std::vector config_full_paths_; std::vector config_paths_; std::string config_name_; toml::value toml_; diff --git a/src/sled/ioc/ioc.h b/src/sled/ioc/ioc.h index a387725..f32e251 100644 --- a/src/sled/ioc/ioc.h +++ b/src/sled/ioc/ioc.h @@ -163,20 +163,20 @@ class InjectionContext { public: 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 &getComponentStack() { return componentStack_; } - const component_type &getRequester() + const component_type &GetRequester() { if (componentStack_.size() < 2) { throw InvalidOperationException("Context not valid."); } @@ -201,10 +201,10 @@ class ContextGuard { public: ContextGuard(InjectionContext *context, component_type type) : context_(context), type_(type) { - context_->pushType(type); + context_->PushType(type); } - ~ContextGuard() { context_->popType(); } + ~ContextGuard() { context_->PopType(); } void ensureNoCycle() { @@ -411,7 +411,7 @@ struct ctor_arg_resolver { template::value, int>::type = 0> operator TCtorArgument() { - return context_->getContainer().Get(context_); + return context_->GetContainer().Get(context_); } InjectionContext *context_; @@ -429,7 +429,7 @@ struct ctor_arg_resolver_1st { = 0> operator TCtorArgument() { - return context_->getContainer().Get(context_); + return context_->GetContainer().Get(context_); } InjectionContext *context_; @@ -511,7 +511,7 @@ template struct ConstructorInvoker { static std::shared_ptr invoke(InjectionContext *context) { - Container &container = context->getContainer(); + Container &container = context->GetContainer(); return std::make_shared(container.Get(context)...); }