Commit 59b2bf78 authored by tqcq's avatar tqcq
Browse files

feat update

parent 80de3add
Loading
Loading
Loading
Loading
+31 −22
Original line number Diff line number Diff line
@@ -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<std::string> extensions = {".toml"};
    for (const auto &path : config_paths_) {
        auto name = path + config_name_;
        for (const auto &ext : extensions) {
            const std::ifstream file(name + ext);
    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");
                value = toml::parse(stream_data, "string");
                return true;
                // goto config_read_success;
            } 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:
    //     // pair<key, value>
    //     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;
}
+13 −0
Original line number Diff line number Diff line
@@ -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<std::string, ValueType> default_values_;// low_priority
    std::unordered_map<std::string, ValueType> values_;        // high_priority

    std::vector<std::string> config_full_paths_;
    std::vector<std::string> config_paths_;
    std::string config_name_;
    toml::value toml_;
+11 −11
Original line number Diff line number Diff line
@@ -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<component_type> &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<typename TCtorArgument, typename std::enable_if<!std::is_pointer<TCtorArgument>::value, int>::type = 0>
    operator TCtorArgument()
    {
        return context_->getContainer().Get<TCtorArgument>(context_);
        return context_->GetContainer().Get<TCtorArgument>(context_);
    }

    InjectionContext *context_;
@@ -429,7 +429,7 @@ struct ctor_arg_resolver_1st {
             = 0>
    operator TCtorArgument()
    {
        return context_->getContainer().Get<TCtorArgument>(context_);
        return context_->GetContainer().Get<TCtorArgument>(context_);
    }

    InjectionContext *context_;
@@ -511,7 +511,7 @@ template<typename TInstance, typename... TConstructorArgs>
struct ConstructorInvoker<TInstance(TConstructorArgs...)> {
    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)...);
    }