ionComponent.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include "ionStandardLibrary.h"
16 #include "ionClass.h"
17 
18 
21 template <typename TComponent>
22 class IEntity
23 {
24 
25 public:
26 
31  template <typename T>
32  T * AddComponent(T * Component)
33  {
34  return (T *) Components.insert(pair<Type, TComponent *>(typeid(T), Component))->second;
35  }
36 
40  template <typename T>
41  pair<typename multimap<Type, TComponent *>::iterator, typename multimap<Type, TComponent *>::iterator> GetComponents()
42  {
43  return Components.equal_range(typeid(T));
44  }
45 
49  template <typename T>
50  size_t GetComponentCount() const
51  {
52  return Components.count(typeid(T));
53  }
54 
55  template <typename T>
56  size_t ExpectSingleComponent(T * & Component)
57  {
58  size_t Count = GetComponentCount<T>();
59  if (Count >= 1)
60  Component = (T *) Components.find(typeid(T))->second;
61 
62  return Count;
63  }
64 
65  template <typename T>
67  {
68  size_t Count = GetComponentCount<T>();
69  if (Count >= 1)
70  return (T *) Components.find(typeid(T))->second;
71  else
72  return AddComponent(new T{});
73  }
74 
75 protected:
76 
77  multimap<Type, TComponent *> Components;
78 
79 };
Brings a lot of elements from std namespace into the global namespace.
Methods and classes for rudimentary reflection and other class related functionality.
T * AddComponent(T *Component)
Add a component to this entity.
Definition: ionComponent.h:32
multimap< Type, TComponent * > Components
Definition: ionComponent.h:77
size_t GetComponentCount() const
Get the number of components of a given type Note: Does not count elements of a type derived from T...
Definition: ionComponent.h:50
Base class for Component-Entity design pattern implementation.
Definition: ionComponent.h:22
size_t ExpectSingleComponent(T *&Component)
Definition: ionComponent.h:56
T * RequireSingleComponent()
Definition: ionComponent.h:66
pair< typename multimap< Type, TComponent * >::iterator, typename multimap< Type, TComponent * >::iterator > GetComponents()
Get an iterator range of all components of a given type Note: Does not return elemtns of a type deriv...
Definition: ionComponent.h:41