NAP
factory.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "object.h"
9 
10 // External Includes
11 #include <utility/dllexport.h>
12 
13 namespace nap
14 {
15  namespace rtti
16  {
17  class Object;
18 
24  class NAPAPI IObjectCreator
25  {
26  public:
27  virtual ~IObjectCreator() = default;
28 
32  virtual Object* create() = 0;
33 
37  virtual rtti::TypeInfo getTypeToCreate() const = 0;
38  };
39 
40 
48  template <typename Object, typename T>
50  {
51  public:
56  ObjectCreator(T& argument) :
57  mArgument(argument) { }
58 
62  rtti::TypeInfo getTypeToCreate() const override { return RTTI_OF(Object); }
63 
67  virtual rtti::Object* create() override { return new Object(mArgument); }
68 
69  private:
70  T& mArgument;
71  };
72 
73 
78  class NAPAPI Factory
79  {
80  public:
81  Factory() = default;
82  Factory(const Factory&) = delete;
83  virtual ~Factory() = default;
84 
85  Factory& operator=(const Factory&) = delete;
86 
91  void addObjectCreator(std::unique_ptr<IObjectCreator> objectCreator);
92 
99  Object* create(rtti::TypeInfo typeInfo);
100 
105  bool canCreate(rtti::TypeInfo typeInfo) const;
106 
107  protected:
108  virtual Object* createDefaultObject(rtti::TypeInfo typeInfo);
109 
110  private:
111  using CreatorMap = std::unordered_map<rtti::TypeInfo, std::unique_ptr<IObjectCreator>>;
112  CreatorMap mCreators;
113  };
114 
115 
116  } //< End Namespace nap
117 
118 }
nap::rtti::ObjectCreator::ObjectCreator
ObjectCreator(T &argument)
Definition: factory.h:56
nap::rtti::ObjectCreator::create
virtual rtti::Object * create() override
Definition: factory.h:67
nap::rtti::Object
Definition: object.h:30
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::rtti::ObjectCreator::getTypeToCreate
rtti::TypeInfo getTypeToCreate() const override
Definition: factory.h:62
nap::rtti::Factory
Definition: factory.h:78
nap
Definition: templateapp.h:17
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::rtti::IObjectCreator
Definition: factory.h:24