CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
meta.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_LIBRARY_META_H_
16 #define CORGI_COMPONENT_LIBRARY_META_H_
17 
18 #include <set>
19 #include <string>
20 #include <unordered_map>
21 #include "corgi/component.h"
22 #include "library_components_generated.h"
23 
24 namespace corgi {
25 namespace component_library {
26 
27 /// @file
28 /// @addtogroup corgi_component_library
29 /// @{
30 ///
31 /// @struct MetaData
32 ///
33 /// @brief Holds specific metadata for an Entity.
34 struct MetaData {
35  MetaData() {}
36  /// @var entity_id
37  ///
38  /// @brief A std::string to uniquely identify this Entity.
39  std::string entity_id;
40 
41  /// @var prototype
42  ///
43  /// @brief If this Entity is based on a prototype, the prototype's Entity ID
44  /// is stored here here.
45  std::string prototype;
46 
47  /// @var source_file
48  ///
49  /// @brief If the Entity came from a source file, the std::string name of the
50  /// file is stored here.
51  std::string source_file;
52 
53  /// @var comment
54  ///
55  /// @brief A human-readable comment to remember what this entity is for.
56  std::string comment;
57 
58  /// @var components_from_prototype
59  ///
60  /// @brief Used to keep track of which of this Entity's Components
61  /// came from the prototype.
62  std::set<corgi::ComponentId> components_from_prototype;
63 };
64 
65 /// @class MetaComponent
66 ///
67 /// @brief A Component used to track the metadata about the Entities
68 /// themselves.
69 class MetaComponent : public corgi::Component<MetaData> {
70  public:
71  /// @brief The destructor for MetaComponent.
72  virtual ~MetaComponent() {}
73 
74  /// @brief Deserialize a flat binary buffer to create and populate an Entity's
75  /// MetaData from raw data.
76  ///
77  /// @brief entity An EntityRef reference that points to an Entity whose
78  /// MetaData is being populated from raw data.
79  /// @brief raw_data A void pointer to the raw FlatBuffer data.
80  virtual void AddFromRawData(corgi::EntityRef& entity, const void* raw_data);
81 
82  /// @brief Adds the comment from the prototype data to an Entity's MetaData.
83  ///
84  /// @param[in] entity The Entity whose MetaData should be updated.
85  /// @param[in] meta_def A const pointer to the MetaDef whose data
86  /// should be added to the Entity's MetaData.
88  const corgi::MetaDef* meta_def);
89 
90  /// @brief Adds a source file name to an Entity's MetaData.
91  ///
92  /// @param[in] entity The Entity whose MetaData should be updated.
93  /// @param[in] source_file A const referenec to the std::string that contains
94  /// the name of the source file that this Entity came from.
96  const std::string& source_file);
97 
98  /// @brief Serializes a MetaComponent's data for a given Entity.
99  ///
100  /// @param[in] entity An EntityRef referene to an Entity whose corresponding
101  /// MetaData will be serialized.
102  ///
103  /// @return Returns a RawDataUniquePtr to the start of the raw data in a
104  /// flat binary buffer.
105  virtual RawDataUniquePtr ExportRawData(const corgi::EntityRef& entity) const;
106 
107  /// @brief Adds the given Entity to the dictionary tracked by this Component.
108  ///
109  /// @param[in] entity The EntityRef to the Entity to add to the dictionary.
110  virtual void InitEntity(corgi::EntityRef& entity);
111 
112  /// @brief Removes the given Entity from the dictionary tracked by this
113  /// Component.
114  ///
115  /// @param[in] entity The EntityRef to the Entity that should be removed
116  /// from the dictionary.
117  virtual void CleanupEntity(corgi::EntityRef& entity);
118 
119  /// @brief Does nothing. This is only implemented as part of the
120  /// ComponentInterface.
121  virtual void UpdateAllEntities(corgi::WorldTime /*delta_time*/) {}
122 
123  /// @brief Get the ID for this Entity.
124  ///
125  /// @note If this Entity does not already have an ID, a random one
126  /// will be generated.
127  ///
128  /// @param[in] entity A const EntityRef reference to the Entity whose
129  /// ID should be returned.
130  ///
131  /// @return Returns a std::string containing the Entity ID.
132  const std::string& GetEntityID(const corgi::EntityRef& entity);
133 
134  /// @brief Get an Entity from the dictionary at a given key.
135  ///
136  /// @note If an invalid Entity is found, it gets silently removed.
137  ///
138  /// @param[in] key A const reference to the std::string Entity ID that is
139  /// used as the index into the dictionary to lookup the Entity.
140  ///
141  /// @return Returns an EntityRef to the Entity at the given key. If
142  /// the `key` or Entity were invalid, it returns an empty EntityRef.
143  corgi::EntityRef GetEntityFromDictionary(const std::string& key);
144 
145  private:
146  void AddEntityToDictionary(const std::string& key,
147  const corgi::EntityRef& entity);
148  void RemoveEntityFromDictionary(const std::string& key);
149  void GenerateRandomEntityID(std::string* output);
150 
151  std::unordered_map<std::string, corgi::EntityRef> entity_dictionary_;
152  std::string empty_string;
153 };
154 /// @}
155 
156 } // namespace component_library
157 } // namespace corgi
158 
161 
162 #endif // CORGI_COMPONENT_LIBRARY_META_H_
std::unique_ptr< uint8_t, std::function< void(uint8_t *)> > RawDataUniquePtr
A pointer type for exported raw data.
Definition: component_interface.h:63
A Component is an object that encapsulates all data and logic for Entities of a particular type...
Definition: component.h:43
virtual void UpdateAllEntities(corgi::WorldTime)
Does nothing. This is only implemented as part of the ComponentInterface.
Definition: meta.h:121
virtual void CleanupEntity(corgi::EntityRef &entity)
Removes the given Entity from the dictionary tracked by this Component.
std::string entity_id
A std::string to uniquely identify this Entity.
Definition: meta.h:39
void AddFromPrototypeData(corgi::EntityRef &entity, const corgi::MetaDef *meta_def)
Adds the comment from the prototype data to an Entity's MetaData.
std::string source_file
If the Entity came from a source file, the std::string name of the file is stored here...
Definition: meta.h:51
const std::string & GetEntityID(const corgi::EntityRef &entity)
Get the ID for this Entity.
int WorldTime
A typedef that represents time in the game.
Definition: entity_common.h:49
corgi::EntityRef GetEntityFromDictionary(const std::string &key)
Get an Entity from the dictionary at a given key.
virtual ~MetaComponent()
The destructor for MetaComponent.
Definition: meta.h:72
void AddWithSourceFile(corgi::EntityRef &entity, const std::string &source_file)
Adds a source file name to an Entity's MetaData.
std::string comment
A human-readable comment to remember what this entity is for.
Definition: meta.h:56
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
std::set< corgi::ComponentId > components_from_prototype
Used to keep track of which of this Entity's Components came from the prototype.
Definition: meta.h:62
virtual void AddFromRawData(corgi::EntityRef &entity, const void *raw_data)
Deserialize a flat binary buffer to create and populate an Entity's MetaData from raw data...
virtual RawDataUniquePtr ExportRawData(const corgi::EntityRef &entity) const
Serializes a MetaComponent's data for a given Entity.
#define CORGI_REGISTER_COMPONENT(ComponentType, DataType)
Definition: component_id_lookup.h:48
std::string prototype
If this Entity is based on a prototype, the prototype's Entity ID is stored here here.
Definition: meta.h:45
A Component used to track the metadata about the Entities themselves.
Definition: meta.h:69
Holds specific metadata for an Entity.
Definition: meta.h:34
virtual void InitEntity(corgi::EntityRef &entity)
Adds the given Entity to the dictionary tracked by this Component.