IEntity.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ionTypes.h"
5 #include "ionClass.h"
6 
7 
8 namespace ion
9 {
10 
15  template <typename TComponent>
16  class IEntity
17  {
18 
19  public:
20 
25  template <typename T>
26  T * AddComponent(T * Component)
27  {
28  return (T *) Components.insert(pair<Type, TComponent *>(typeid(T), Component))->second;
29  }
30 
34  template <typename T>
35  pair<typename std::multimap<Type, TComponent *>::iterator, typename std::multimap<Type, TComponent *>::iterator> GetComponents()
36  {
37  return Components.equal_range(typeid(T));
38  }
39 
43  template <typename T>
44  size_t GetComponentCount() const
45  {
46  return Components.count(typeid(T));
47  }
48 
49  template <typename T>
50  size_t ExpectSingleComponent(T * & Component)
51  {
52  size_t Count = GetComponentCount<T>();
53  if (Count >= 1)
54  Component = (T *) Components.find(typeid(T))->second;
55 
56  return Count;
57  }
58 
59  template <typename T>
61  {
62  size_t Count = GetComponentCount<T>();
63  if (Count >= 1)
64  return (T *) Components.find(typeid(T))->second;
65  else
66  return AddComponent(new T{});
67  }
68 
69  protected:
70 
71  std::multimap<Type, TComponent *> Components;
72 
73  };
74 
75 }
T * AddComponent(T *Component)
Add a component to this entity.
Definition: IEntity.h:26
size_t ExpectSingleComponent(T *&Component)
Definition: IEntity.h:50
T * RequireSingleComponent()
Definition: IEntity.h:60
Base class for Component-Entity design pattern implementation driven by typesBasic Usage: Derive your...
Definition: IEntity.h:16
pair< typename std::multimap< Type, TComponent * >::iterator, typename std::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: IEntity.h:35
std::multimap< Type, TComponent * > Components
Definition: IEntity.h:71
Methods and classes for rudimentary reflection and other class related functionality.
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
Defines typedefs for integer and floating point types.
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: IEntity.h:44