stencet  v0.1.16
Build C++ web server modules that allow easy routing and deployment.
factory.h
1 /* Copyright (C) 2012-2013 Justin Berger
2  The full license is available in the LICENSE file at the root of this project and is also available at http://opensource.org/licenses/MIT. */
3 
4 #pragma once
5 #include <map>
6 #include <functional>
7 
8 namespace stencet {
9  template <typename T, typename... Args>
10  struct Factory {
11  using Maker = std::function< T*(Args&...) >;
12 
13  static auto Makers() -> std::map<std::string, Maker >& {
14  static std::map<std::string, Maker > makers;
15  return makers;
16  }
17 
18  template <typename S>
19  static auto Register(const std::string& name) -> bool {
20  Makers()[name] = [](Args&... args) {
21  return new S(args...);
22  };
23  return true;
24  }
25 
26  static T* Create(const std::string& name, Args&... args){
27  auto maker = Makers().find( name );
28  if(maker == Makers().end()){
29  return 0;
30  }
31  return maker->second(args...);
32  }
33  };
34 }
35