MT Showcase SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
ShowcaseTypes.hpp
1 #pragma once
2 
3 #include <Patterns/NotCopyable.hpp>
4 
5 #include <cassert>
6 #include <functional>
7 #include <map>
8 #include <memory>
9 #include <typeinfo>
10 #include "Schema.hpp"
11 
12 namespace Showcase
13 {
14 
17  template <typename T, template <typename> class Ptr>
18  class ShowcaseFactoryT : public Patterns::NotCopyable
19  {
20  public:
21  typedef std::function<Ptr<T>()> CreateFunc;
22 
23  ShowcaseFactoryT(const std::type_info & typeinfo, CreateFunc create);
24  ~ShowcaseFactoryT() {}
25 
26  Ptr<T> create() const;
27 
28  void setSchema(const Schema& schema);
29 
30  private:
31  CreateFunc m_create;
32  Schema m_schema;
33  };
34 
35  // -------------------------------------------------------------------
36 
37  template <typename T>
38  struct hasSchemaInitialization
39  {
40  private:
41  typedef char one;
42  typedef long two;
43 
44  template <typename C> static one test(decltype(&C::setSchema));
45  template <typename C> static two test(...);
46 
47  public:
48  static const bool value = sizeof(test<T>(0)) == sizeof(char);
49  };
50 
51  template <typename T, template <typename> class Ptr, bool = hasSchemaInitialization<T>::value>
52  struct ComponentInitializer
53  {
54  inline static Ptr<T> create(std::function<Ptr<T>()> createFunc,
55  const Schema& schema)
56  {
57  (void) schema;
58  return createFunc();
59  }
60  };
61 
62  template <typename T, template <typename> class Ptr>
63  struct ComponentInitializer<T, Ptr, true>
64  {
65  inline static Ptr<T> create(std::function<Ptr<T>()> createFunc,
66  const Schema& schema)
67  {
68  auto c = createFunc();
69  c->setSchema(schema);
70  return c;
71  }
72  };
73 
74  // -------------------------------------------------------------------
75 
76  template <typename T, template <typename> class Ptr>
77  ShowcaseFactoryT<T, Ptr>::ShowcaseFactoryT(const std::type_info & typeinfo,
78  CreateFunc create)
79  : m_create(create)
80  {
81  (void) typeinfo;
82  }
83 
84  template <typename T, template <typename> class Ptr>
86  {
87  m_schema = schema;
88  }
89 
90  template <typename T, template <typename> class Ptr>
91  Ptr<T> ShowcaseFactoryT<T, Ptr>::create() const
92  {
93  assert(m_create);
94  return ComponentInitializer<T,Ptr>::create(m_create, m_schema);
95  }
96 
97  // -------------------------------------------------------------------
98 
99  template <typename T>
100  using ShowcaseFactory = ShowcaseFactoryT<T, std::shared_ptr>;
101  template <typename T>
102  using ShowcaseFactoryPtr = std::shared_ptr<ShowcaseFactory<T>>;
103 
104  template <typename T>
105  using FactoryMap = std::map<QString, ShowcaseFactoryPtr<T>>;
106 
107  template <typename T>
108  using FactoryKey = typename FactoryMap<T>::iterator;
109 
110  // -------------------------------------------------------------------
111 
112 }