CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
component_interface.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CORGI_COMPONENT_INTERFACE_H_
16 #define CORGI_COMPONENT_INTERFACE_H_
17 
18 #include <stdint.h>
19 #include <functional>
20 #include <memory>
21 #include "corgi/entity.h"
22 #include "corgi/entity_common.h"
23 #include "corgi/entity_manager.h"
24 #include "corgi/vector_pool.h"
25 
26 namespace corgi {
27 
28 class EntityManager;
29 
30 /// @file
31 /// @addtogroup corgi_component
32 /// @{
33 ///
34 /// @typedef EntityRef
35 ///
36 /// @brief A typedef that should be used as the primary way to reference
37 /// an Entity.
38 ///
39 /// An EntityRef can be treated like a pointer to an Entity. In most cases,
40 /// it functions interchangeably with a normal pointer, but it also contains
41 /// extra functionality for determining if the EntityRef is invalid. For
42 /// instance, if the Entity that the EntityRef points to is deallocated, the
43 /// EntityRef will no longer be valid (even if new data exists in the same
44 /// memory location previously held by the deallocated Entity).
45 ///
46 /// EntityRefs are typically passed by reference or const reference. If an
47 /// EntityRef is const, then you can only access the underlying Entity data
48 /// in a read-only manner.
50 
51 /// @class ComponentInterface
52 ///
53 /// @brief An interface that provides basic Component functionality.
54 /// All Components will inherit from this class. It provides the minimum set
55 /// of things that are uniform across all Components (without needing to know
56 /// the specific type of Component that it is).
58  public:
59  /// @typedef RawDataUniquePtr
60  ///
61  /// @brief A pointer type for exported raw data.
62  typedef std::unique_ptr<uint8_t, std::function<void(uint8_t*)>>
64 
65  /// @brief A destructor for the Component interface.
66  virtual ~ComponentInterface() {}
67 
68  /// @brief Add an Entity to the Component.
69  ///
70  /// @note Usually you will want to use Component::AddEntity, since that
71  /// returns a pointer to the data assigned to the Component.
72  ///
73  /// @param[in,out] entity An EntityRef reference to the Entity being added to
74  /// this Component.
75  virtual void AddEntityGenerically(EntityRef& entity) = 0;
76 
77  /// @brief Remove an Entity from the Component's list.
78  ///
79  /// @param[in] entity An EntityRef reference to the Entity being remove
80  /// from this Component.
81  virtual void RemoveEntity(EntityRef& entity) = 0;
82 
83  /// @brief Update all Entities that contain this Component.
84  ///
85  /// @param[in] delta_time A WorldTime corresponding to the
86  /// delta time for this frame.
87  virtual void UpdateAllEntities(WorldTime delta_time) = 0;
88 
89  /// @brief Returns true if this component has data associated with the
90  /// entity provided.
91  virtual bool HasDataForEntity(const EntityRef&) = 0;
92 
93  /// @brief Clears all Component data, effectively disassociating this
94  /// Component from any Entities.
95  virtual void ClearComponentData() = 0;
96 
97  /// @brief Gets the data for a given Entity as a void pointer.
98  ///
99  /// @note When using GetComponentDataAsVoid, the calling function is expected
100  /// to know how to handle the data (since it is returned as a void pointer).
101  ///
102  /// @warning This pointer is NOT stable in memory. Calls to
103  /// AddEntityGenerically may force the storage class to resize,
104  /// shuffling around the location of this data.
105  ///
106  /// @return Returns the Entity's data as a void pointer, or returns a nullptr
107  /// if the data does not exist.
108  virtual void* GetComponentDataAsVoid(const EntityRef&) = 0;
109 
110  /// @brief Gets the data for a given ntity as a const void pointer.
111  ///
112  /// @note When using GetComponentDataAsVoid, the calling function is expected
113  /// to know how to handle the data (since it is returned as a const
114  /// void pointer).
115  ///
116  /// @warning This pointer is NOT stable in memory. Calls to AddEntity and
117  /// AddEntityGenerically may force the storage class to resize,
118  /// shuffling around the location of this data.
119  ///
120  /// @return Returns the Entity's data as a const void pointer, or returns a
121  /// nullptr if the data does not exist.
122  virtual const void* GetComponentDataAsVoid(const EntityRef&) const = 0;
123 
124  /// @brief This function is called after the Component is added to the
125  /// EntityManager. (i.e. This typically happens once, at the beginning
126  /// of the game before any Entities are added.)
127  virtual void Init() = 0;
128 
129  /// @brief Called by the EntityManager every time an Entity is added to this
130  /// Component.
131  ///
132  /// @param[in] entity An EntityRef pointing to an Entity that is being added
133  /// to this Component and may need initialized.
134  virtual void InitEntity(EntityRef& entity) = 0;
135 
136  /// @brief Creates and populates an Entity from raw data. Components that want
137  /// to be able to be constructed via the EntityFactory need to implement this.
138  ///
139  /// @param[in,out] entity An EntityRef that points to an Entity that is being
140  /// added from the raw data.
141  /// @param[in] data A void pointer to the raw data.
142  virtual void AddFromRawData(EntityRef& entity, const void* data) = 0;
143 
144  /// @brief Serializes a Component's data for a specific Entity.
145  ///
146  /// If you do not support this functionality, this function
147  /// should return a nullptr.
148  ///
149  /// @param[in] entity An EntityRef reference to an Entity whose raw data
150  /// should be returned.
151  ///
152  /// @return Returns a RawDataUniquePtr to the raw data.
153  virtual RawDataUniquePtr ExportRawData(const EntityRef& entity) const = 0;
154 
155  /// @brief Called just before removal from the EntityManager. (i.e.
156  /// Usually when the game/state is over and everything is shutting
157  /// down.)
158  virtual void Cleanup() = 0;
159 
160  /// @brief Called when the Entity is being removed from the Component.
161  /// Components should implement this if they need to perform any cleanup
162  /// on the Entity data.
163  ///
164  /// @param[in] entity An EntityRef reference to the Entity that is being
165  /// removed and may need to be cleaned up.
166  virtual void CleanupEntity(EntityRef& entity) = 0;
167 
168  /// @brief Set the EntityManager for this Component. Usually this
169  /// is assigned by the EntityManager itself.
170  ///
171  /// @note The EntityManager is used as the main point of contact
172  /// for Components that need to talk to other things.
173  ///
174  /// @param[in] entity_manager A pointer to an EntityManager to associate
175  /// with this Component.
176  virtual void SetEntityManager(EntityManager* entity_manager) = 0;
177 
178  /// @brief Sets the Component ID for the data type.
179  ///
180  /// @note This is normally only called once by the EntityManager.
181  ///
182  /// @param[in] id The Component ID to set for the data type.
183  virtual void SetComponentIdOnDataType(ComponentId id) = 0;
184 };
185 /// @}
186 
187 } // corgi
188 
189 #endif // CORGI_COMPONENT_INTERFACE_H_
std::unique_ptr< uint8_t, std::function< void(uint8_t *)> > RawDataUniquePtr
A pointer type for exported raw data.
Definition: component_interface.h:63
virtual void * GetComponentDataAsVoid(const EntityRef &)=0
Gets the data for a given Entity as a void pointer.
virtual bool HasDataForEntity(const EntityRef &)=0
Returns true if this component has data associated with the entity provided.
virtual void ClearComponentData()=0
Clears all Component data, effectively disassociating this Component from any Entities.
The EntityManager is the code that manages all Entities and Components in the game. Normally the game will instantiate EntityManager, and then use it to create and control all of its Entities.
Definition: entity_manager.h:61
int WorldTime
A typedef that represents time in the game.
Definition: entity_common.h:49
virtual void UpdateAllEntities(WorldTime delta_time)=0
Update all Entities that contain this Component.
virtual void AddFromRawData(EntityRef &entity, const void *data)=0
Creates and populates an Entity from raw data. Components that want to be able to be constructed via ...
virtual ~ComponentInterface()
A destructor for the Component interface.
Definition: component_interface.h:66
virtual void SetEntityManager(EntityManager *entity_manager)=0
Set the EntityManager for this Component. Usually this is assigned by the EntityManager itself...
A pool allocator, implemented as a vector-based pair of linked lists.
Definition: vector_pool.h:43
VectorPool< corgi::Entity >::VectorPoolReference EntityRef
A typedef that should be used as the primary way to reference an Entity.
Definition: component_interface.h:49
virtual void RemoveEntity(EntityRef &entity)=0
Remove an Entity from the Component's list.
virtual void Init()=0
This function is called after the Component is added to the EntityManager. (i.e. This typically happe...
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
virtual RawDataUniquePtr ExportRawData(const EntityRef &entity) const =0
Serializes a Component's data for a specific Entity.
virtual void InitEntity(EntityRef &entity)=0
Called by the EntityManager every time an Entity is added to this Component.
virtual void SetComponentIdOnDataType(ComponentId id)=0
Sets the Component ID for the data type.
virtual void AddEntityGenerically(EntityRef &entity)=0
Add an Entity to the Component.
uint16_t ComponentId
This represents the ID of a Component.
Definition: entity_common.h:36
virtual void Cleanup()=0
Called just before removal from the EntityManager. (i.e. Usually when the game/state is over and ever...
An interface that provides basic Component functionality. All Components will inherit from this class...
Definition: component_interface.h:57
virtual void CleanupEntity(EntityRef &entity)=0
Called when the Entity is being removed from the Component. Components should implement this if they ...