美文网首页
c/c++学习笔记7-代码实现IOC

c/c++学习笔记7-代码实现IOC

作者: scott_yu779 | 来源:发表于2018-01-19 09:47 被阅读0次

原文:c++代码框架实现ioc

改进如下:

#include <string>
#include <map>
#include <memory>
#include <functional>
using namespace std;
#include <Any>
#include<NonCopyable>

class IocContainer : NonCopyable
{
public:
    IocContainer(void){}
    ~IocContainer(void){}

    template <class T>
    void RegisterType(string strKey){
        typedef T* I;
        std::function<I()> function = Construct<I, T>::invoke;
        RegisterType(strKey, function);
    }

    template <class I, class T, typename... Ts>
    void RegisterType(string strKey){
        std::function<I* (Ts...)> function = Construct<I*, T, Ts...>::invoke;
        RegisterType(strKey, function);
    }

    template <class I>
    I* Resolve(string strKey){
        if (m_creatorMap.find(strKey) == m_creatorMap.end())
            return nullptr;
        Any resolver = m_creatorMap[strKey];
        std::function<I* ()> function = resolver.AnyCast<std::function<I* ()>>();
        return function();
    }

    template <class I>
    std::shared_ptr<I> ResolveShared(string strKey){
        auto b = Resolve<I>(strKey);
        return std::shared_ptr<I>(b);
    }

    template <class I, typename... Ts>
    I* Resolve(string strKey, Ts... Args){
        if (m_creatorMap.find(strKey) == m_creatorMap.end())
            return nullptr;
        Any resolver = m_creatorMap[strKey];
        std::function<I* (Ts...)> function = resolver.AnyCast<std::function<I* (Ts...)>>();
        return function(Args...);
    }

    template <class I, typename... Ts>
    std::shared_ptr<I> ResolveShared(string strKey, Ts... Args){
        auto b = Resolve<I, Ts...>(strKey, Args...);
        return std::shared_ptr<I>(b);
    }

private:
    template<typename I, typename T, typename... Ts>
    struct Construct{
        static I invoke(Ts... Args) { return I(new T(Args...)); }
    };
    void RegisterType(string strKey, Any constructor){
        if (m_creatorMap.find(strKey) != m_creatorMap.end())
            throw std::logic_exception("this key has already exist!");
        m_creatorMap.insert(make_pair(strKey, constructor));
    }

private:
    unordered_map<string, Any> m_creatorMap;
};

相关文章

  • c/c++学习笔记7-代码实现IOC

    原文:c++代码框架实现ioc 改进如下:

  • Linux下基于C++的TCP连接demo代码分享(C++,Li

    #C++实现TCP连接 @(C++代码)[网络编程, tcp, C++, C++实现] server.cpp: #...

  • OC对象的本质

    ~ Objective-C代码,底层实现?Objective-C底层实现是C\C++代码,C\C++代码转换成汇编...

  • C++学习笔记 day3

    C++学习笔记 day3 03 运算符 笔记&代码 #include using namespace std; /...

  • jni新手笔记二:java调用c++

    jni作为 java代码与c/c++ 代码之间的桥梁,通过jni可以实现java代码和c/c++代码互相调用 在 ...

  • iOS OC对象总结

    OC的实现 OC的对象、类主要是基于C、C++的结构体来实现的。编写的 OC代码,底层实现其实都是C、C++代码。...

  • OC 对象本质

    1 . 编写的Objective-C代码,其底层都是由c/c++ 代码实现的。OC语言 —> c/c++ 语言—>...

  • c++ 实现队列

    相关资料: 用C++实现一个队列 数据结构代码实现之队列的链表实现(C/C++)

  • OC对象

    我们平时编写的Objective-C代码,底层实现其实都是C\C++代码,Objective-C转换成C\C++,...

  • 技能

    C++ C++特性 C++11 多态和继承 构造函数 析构函数 手写代码实现string类 手写代码实现智能指针 ...

网友评论

      本文标题:c/c++学习笔记7-代码实现IOC

      本文链接:https://www.haomeiwen.com/subject/jxqooxtx.html