Thrive
component_factory.h
1 #pragma once
2 
3 #include "engine/component.h"
4 #include "util/make_unique.h"
5 
6 namespace luabind {
7  class scope;
8 }
9 
10 namespace thrive {
11 
12 class StorageContainer;
13 
20 
21 public:
22 
29  using ComponentLoader = std::function<
30  std::unique_ptr<Component>(const StorageContainer& storage)
31  >;
32 
39  static luabind::scope
40  luaBindings();
41 
48 
53 
67  template<typename C>
68  static ComponentTypeId
71  C::TYPE_NAME(),
72  [](const StorageContainer& storage) {
73  std::unique_ptr<Component> component = make_unique<C>();
74  component->load(storage);
75  return component;
76  }
77  );
78  }
79 
91  ComponentTypeId
92  getTypeId(
93  const std::string& name
94  ) const;
95 
105  std::string
106  getTypeName(
107  ComponentTypeId typeId
108  ) const;
109 
122  std::unique_ptr<Component>
123  load(
124  const std::string& typeName,
125  const StorageContainer& storage
126  ) const;
127 
139  ComponentTypeId
141  const std::string& name,
142  ComponentLoader loader
143  );
144 
151  void
153  const std::string& name
154  );
155 
156 private:
157 
158  static ComponentTypeId
160  const std::string& name,
161  ComponentLoader loader
162  );
163 
164  struct Implementation;
165  std::unique_ptr<Implementation> m_impl;
166 
167 };
168 
174 #define REGISTER_COMPONENT(cls) \
175  const ComponentTypeId cls::TYPE_ID = thrive::ComponentFactory::registerGlobalComponentType<cls>();
176 
177 }
static luabind::scope luaBindings()
Lua bindings.
Definition: component_factory.cpp:69
void unregisterComponentType(const std::string &name)
Unregisters a component type.
Definition: component_factory.cpp:173
std::function< std::unique_ptr< Component >(const StorageContainer &storage) > ComponentLoader
Typedef for a component factory function.
Definition: component_factory.h:31
static ComponentTypeId registerGlobalComponentType()
Registers a component type for all factories.
Definition: component_factory.h:69
A factory for components.
Definition: component_factory.h:19
std::unique_ptr< Component > load(const std::string &typeName, const StorageContainer &storage) const
Loads a component from storage.
Definition: component_factory.cpp:138
Definition: bullet_to_ogre_system.h:5
Definition: bullet_ogre_conversion.h:6
ComponentTypeId registerComponentType(const std::string &name, ComponentLoader loader)
Registers a component type with this factory.
Definition: component_factory.cpp:155
std::string getTypeName(ComponentTypeId typeId) const
Looks up a component type id and returns its name.
Definition: component_factory.cpp:120
ComponentTypeId getTypeId(const std::string &name) const
Looks up a component type name and returns its id.
Definition: component_factory.cpp:105
~ComponentFactory()
Destructor.
Definition: component_factory.cpp:101
A key-value storage for serialization.
Definition: serialization.h:36
ComponentFactory()
Constructor.
Definition: component_factory.cpp:95