Fission FSN
Loading...
Searching...
No Matches
fsCComponentFactory.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: fsCComponentFactory.h
4*
5* Purpose: The place to make the fsAppCore components that make
6* up the entities.
7*
8*********************************************************************/
9
10#ifndef fsCComponentFactoryhdr
11#define fsCComponentFactoryhdr
12
13#include <map>
14#include <memory>
15
18#include <fsCore/fsBaseTypes.h>
19
20class fsIComponent;
21class fsCEntity;
22class fsCUniqueId;
23
24
26{
27 friend class fsCEntity;
28
29public:
30
31 typedef fsIComponent* (*TCreateFnc )(fsCEntity* pParent);
32
33 static std::shared_ptr<fsCComponentFactory> create();
34
35 void merge(fsCComponentFactory* const pRHS);
37
38 template <typename T>
39 void componentRegister();
40
41 virtual ~fsCComponentFactory();
42
43protected:
44
45 static void componentDestroy(fsIComponent* pComponent);
46 fsIComponent* componentCreate(fsCEntity* pParent, const fsCUniqueId& pTypeId);
47
48 virtual void componentsRegister();
49
51
52private:
53
54 std::map<fsU32, TCreateFnc> mComponents;
55};
56
57#include <type_traits>
58
59template <typename T>
61{
62#if !defined fsRELEASE
63 fsAssert( mComponents.find( T::mTypeId.idGet() ) == mComponents.end(), "Already registered component of type %s.\n", typeid( T ).name() );
64#endif
65
66 // Watch out for classes with more than one create method - need to comment out
67 // registration to find errors eslewhere.
68 // static_assert(std::is_same_v<TCreateFnc, decltype(&T::create)>, __FUNCSIG__);
69 // static_assert(std::is_same_v<TCreateFnc, decltype(&T::create)>, __PRETTY_FUNCTION__);
70
71 TCreateFnc const createFnc = [](fsCEntity* const pParent) -> fsIComponent*
72 {
73 return T::create(pParent);
74 };
75 mComponents.insert(std::pair<fsU32, TCreateFnc>(T::mTypeId.idGet(), createFnc));
76 fsTrace("registering: %s", typeid(T).name() );
77}
78
79#endif
fsBool componentTypeIsRegisteredGet(const fsCUniqueId &pTypeId) const
Definition fsCComponentFactory.cpp:59
static std::shared_ptr< fsCComponentFactory > create()
Definition fsCComponentFactory.cpp:145
virtual void componentsRegister()
Definition fsCComponentFactory.cpp:64
friend class fsCEntity
Definition fsCComponentFactory.h:27
static void componentDestroy(fsIComponent *pComponent)
Definition fsCComponentFactory.cpp:114
fsIComponent *(* TCreateFnc)(fsCEntity *pParent)
Definition fsCComponentFactory.h:31
fsCComponentFactory()
Definition fsCComponentFactory.cpp:152
void componentRegister()
Definition fsCComponentFactory.h:60
virtual ~fsCComponentFactory()
Definition fsCComponentFactory.cpp:157
fsIComponent * componentCreate(fsCEntity *pParent, const fsCUniqueId &pTypeId)
Definition fsCComponentFactory.cpp:124
void merge(fsCComponentFactory *const pRHS)
Definition fsCComponentFactory.cpp:54
Definition fsCEntity.h:39
Definition fsCUniqueId.h:21
Definition fsIComponent.h:26
#define fsAssert(pStatement, pFmtString,...)
Definition fsAssert.h:23
#define fsTrace(...)
Definition fsTrace.h:20
bool fsBool
Definition fsBaseTypes.h:26