#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <memory>
#include <functional>
#include <stdexcept>
#include <boost/noncopyable.hpp>
#include <boost/any.hpp>
class ioc_container : public boost::noncopyable {
public:
ioc_container() = default;
virtual ~ioc_container() = default;
public:
template <typename T, typename depend, typename ...Args>
void register_type(const std::string &key) {
std::function<T *(Args...)>fun = [] (Args ...args) {
return new T(new depend(args...));
};
register_type(key, fun);
}
template <typename T, typename ...Args>
std::shared_ptr<T>resolve_shared(const std::string &key, Args ...args) {
T *p = resolve<T>(key, args...);
return std::shared_ptr<T>(p);
}
private:
void register_type(const std::string &key, boost::any value) {
if (map_.find(key) != end(map_)) {
throw std::invalid_argument("key has already existed.");
}
map_.emplace(key, value);
}
template <typename T, typename ...Args>
T *resolve(const std::string &key, Args ...args) {
auto it = map_.find(key);
if (end(map_) == it) {
return nullptr;
}
boost::any resolver = it->second;
auto f = boost::any_cast<std::function<T *(Args...)>>(resolver);
return f(args...);
}
private:
std::unordered_map<std::string, boost::any>map_;
};
struct base {
virtual ~base() = default;
virtual void fun() {
std::cout << "I am base fun." << std::endl;
}
};
struct derived0 : base {
public:
derived0(int x, double y) : a(x), b(y) {
}
virtual void fun() override {
std::cout << "I am derived0 fun = " << a + b << std::endl;
}
private:
int a;
double b;
};
struct derived1 : base {
virtual void fun() override {
std::cout << "I am derived1 fun." << std::endl;
}
};
struct Test {
Test(base *p) : p_base(p) {
}
~Test() {
if (nullptr != p_base) {
delete p_base;
p_base = nullptr;
}
}
void fun() {
if (nullptr != p_base) {
p_base->fun();
}
}
private:
base *p_base = nullptr;
};
int main() {
ioc_container ioc;
ioc.register_type<Test, derived1>("derived1");
auto c = ioc.resolve_shared<Test>("derived1");
if (c != nullptr) {
c->fun();
}
ioc.register_type<Test, derived0, int, double>("derived0");
c = ioc.resolve_shared<Test>("derived0", 1, 1.0);
if (c != nullptr) {
c->fun();
}
return 0;
}