CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
graph.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_COMPONENTS_GRAPH_H_
16 #define CORGI_COMPONENTS_GRAPH_H_
17 
18 #include <memory>
19 
20 #include "breadboard/event.h"
21 #include "breadboard/graph.h"
22 #include "breadboard/graph_factory.h"
23 #include "breadboard/graph_state.h"
24 #include "corgi/component.h"
25 
26 namespace corgi {
27 namespace component_library {
28 
29 BREADBOARD_DECLARE_EVENT(kAdvanceFrameEventId)
30 
31 /// @file
32 /// @addtogroup corgi_component_library
33 /// @{
34 ///
35 /// @struct SerializableGraphState
36 ///
37 /// @brief Contains the graph state information, as well as the
38 /// filename for serialization.
40  /// @brief The filename for serialization.
41  std::string filename;
42 
43  /// @brief The Breadboard graph state data.
44  std::unique_ptr<breadboard::GraphState> graph_state;
45 
46  /// @brief Default constructor for a SerializableGraphState.
48 
49  /// @brief Move constructor for a SerializableGraphState.
50  ///
51  /// @param[in] other A SerializableGraphState whose data will be
52  /// moved into this SerializableGraphState.
53  SerializableGraphState(SerializableGraphState&& other) {
54  *this = std::move(other);
55  }
56 
57  /// @brief Move assignment operator for SerializableGraphState.
58  ///
59  /// @param[in] other A SerializableGraphState whose data will be
60  /// moved into this SerializableGraphState.
61  SerializableGraphState& operator=(SerializableGraphState&& other) {
62  filename = std::move(other.filename);
63  graph_state = std::move(other.graph_state);
64  return *this;
65  }
66 
67 /// @cond COMPONENT_LIBRARY_INTERNAL
68 #ifdef _MSC_VER
69  // Provide fake versions of copy contructor and operator to let VS2012
70  // compile and link.
71  //
72  // These should never be called, since unique_ptr is not copyable.
73  // However, Visual Studio 2012 not only calls them, but links them in,
74  // when this class is in an std::pair (as it is when in an std::map).
75  //
76  // The asserts never get hit here. These functions are not actually called.
77  SerializableGraphState(const SerializableGraphState&) { assert(false); }
78  SerializableGraphState& operator=(const SerializableGraphState&) {
79  assert(false);
80  return *this;
81  }
82 #endif // _MSC_VER
83 /// @endcond
84 };
85 
86 /// @struct GraphData
87 ///
88 /// @brief Contains the data for each Entity registered with the GraphComponent.
89 struct GraphData {
90  /// @var graphs
91  ///
92  /// @brief A vector of all the graph states.
93  std::vector<SerializableGraphState> graphs;
94 
95  /// @var broadcaster
96  ///
97  /// @brief A NodeEventBroadcaster that broadcasts events to any registered
98  /// NodeEventListeners.
99  breadboard::NodeEventBroadcaster broadcaster;
100 
101  /// @brief The default constructor for an empty GraphData.
103 
104  /// @brief The move constructor for a GraphData.
105  ///
106  /// @param[in] other A GraphData whose data will be moved into this
107  /// GraphData.
108  GraphData(GraphData&& other) { *this = std::move(other); }
109 
110  /// @brief The move assignment operator for GraphData.
111  ///
112  /// @param[in] other The GraphData whose data will be move into this
113  /// GraphData.
114  ///
115  /// @return Returns a reference to the
117  graphs = std::move(other.graphs);
118  broadcaster = std::move(other.broadcaster);
119  return *this;
120  }
121 
122  private:
125 };
126 
127 /// @class GraphComponent
128 ///
129 /// @brief Manages the event graphs for any Entity that wishes to utilize the
130 /// event system.
131 ///
132 /// @note Once the Entities themselves have been initialized, initialize the
133 /// graphs. The graphs must be initialized after the Entities because the graphs
134 /// may reference the Entities.
135 class GraphComponent : public corgi::Component<GraphData> {
136  public:
137  /// @brief Fixes up all loaded Entities.
138  ///
139  /// @note If you create single Entities later, individual calls to
140  /// `EntityPostLoadFixup` are required on a per-Entity basis.
141  void PostLoadFixup();
142 
143  /// @brief Fixes up a given Entity.
144  ///
145  /// @param[in] entity An EntityRef reference to the Entity that will
146  /// be fixed up.
148 
149  /// @brief Get the broadcaster for a given Entity.
150  ///
151  /// @param[in] entity An EntityRef to the Entity whose broadcaster should be
152  /// returned.
153  ///
154  /// @return Returns a NodeEventBroadcaster for the Entity, even if it does not
155  /// yet have one.
156  breadboard::NodeEventBroadcaster* GetCreateBroadcaster(
157  corgi::EntityRef entity);
158 
159  /// @brief Destructor for GraphComponent.
160  virtual ~GraphComponent() {}
161 
162  /// @brief Initialize the GraphComponent.
163  virtual void Init();
164 
165  /// @brief Deserialize a flat binary buffer to create and populate an Entity
166  /// from raw data.
167  ///
168  /// @param[in,out] entity An EntityRef reference that points to an Entity that
169  /// is being added from the raw data.
170  /// @param[in] raw_data A void pointer to the raw FlatBuffer data.
171  virtual void AddFromRawData(corgi::EntityRef& entity, const void* raw_data);
172 
173  /// @brief Serializes a GraphComponent's data for a given Entity.
174  ///
175  /// @param[in] entity An EntityRef reference to an Entity whose corresponding
176  /// GraphData will be serialized.
177  ///
178  /// @return Returns a RawDataUniquePtr to the start of the raw data in a flat
179  /// binary buffer.
180  virtual RawDataUniquePtr ExportRawData(const corgi::EntityRef& entity) const;
181 
182  /// @cond COMPONENT_LIBRARY_INTERNAL
183  virtual void InitEntity(corgi::EntityRef& /*entity*/) {}
184  /// @endcond
185 
186  /// @brief Broadcasts a `kAdvanceFrameEventId` event to update all Entities'
187  /// graphs.
188  ///
189  /// @param[in] delta_time A WorldTime corresponding to the delta time
190  /// since the last call to this function.
191  virtual void UpdateAllEntities(corgi::WorldTime delta_time);
192 
193  /// @return Returns a const EntityRef reference to the Entity that is
194  /// currently being initialized.
195  ///
196  /// During initialization, this value iterates over each Entity, one at a
197  /// time.
198  ///
199  /// @note After initialization, the return value from this function is not
200  /// meaningful. It would simply return the last Entity that was initialized.
201  const corgi::EntityRef& graph_entity() const { return graph_entity_; }
202 
203  /// @return Returns a pointer to the NodeEventBroadcaster that is called
204  /// once per frame.
205  breadboard::NodeEventBroadcaster* advance_frame_broadcaster() {
206  return &advance_frame_broadcaster_;
207  }
208 
209  private:
210  breadboard::GraphFactory* graph_factory_;
211  breadboard::NodeEventBroadcaster advance_frame_broadcaster_;
212 
213  // TODO: Remove this in favor of a more generic per-graph state interface.
214  // b/24510652
215  corgi::EntityRef graph_entity_;
216 };
217 /// @}
218 
219 } // component_library
220 } // corgi
221 
224 
225 #endif // CORGI_COMPONENTS_GRAPH_H_
void EntityPostLoadFixup(corgi::EntityRef &entity)
Fixes up a given Entity.
std::unique_ptr< uint8_t, std::function< void(uint8_t *)> > RawDataUniquePtr
A pointer type for exported raw data.
Definition: component_interface.h:63
GraphData & operator=(GraphData &&other)
The move assignment operator for GraphData.
Definition: graph.h:116
A Component is an object that encapsulates all data and logic for Entities of a particular type...
Definition: component.h:43
GraphData()
The default constructor for an empty GraphData.
Definition: graph.h:102
Manages the event graphs for any Entity that wishes to utilize the event system.
Definition: graph.h:135
virtual RawDataUniquePtr ExportRawData(const corgi::EntityRef &entity) const
Serializes a GraphComponent's data for a given Entity.
breadboard::NodeEventBroadcaster * advance_frame_broadcaster()
Definition: graph.h:205
virtual void AddFromRawData(corgi::EntityRef &entity, const void *raw_data)
Deserialize a flat binary buffer to create and populate an Entity from raw data.
int WorldTime
A typedef that represents time in the game.
Definition: entity_common.h:49
SerializableGraphState(SerializableGraphState &&other)
Move constructor for a SerializableGraphState.
Definition: graph.h:53
virtual void InitEntity(EntityRef &)
Override this function with code that should be executed when an Entity is added to the Component...
Definition: component.h:407
Contains the graph state information, as well as the filename for serialization.
Definition: graph.h:39
const corgi::EntityRef & graph_entity() const
Definition: graph.h:201
SerializableGraphState()
Default constructor for a SerializableGraphState.
Definition: graph.h:47
breadboard::NodeEventBroadcaster broadcaster
A NodeEventBroadcaster that broadcasts events to any registered NodeEventListeners.
Definition: graph.h:99
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
Contains the data for each Entity registered with the GraphComponent.
Definition: graph.h:89
void PostLoadFixup()
Fixes up all loaded Entities.
SerializableGraphState & operator=(SerializableGraphState &&other)
Move assignment operator for SerializableGraphState.
Definition: graph.h:61
breadboard::NodeEventBroadcaster * GetCreateBroadcaster(corgi::EntityRef entity)
Get the broadcaster for a given Entity.
#define CORGI_REGISTER_COMPONENT(ComponentType, DataType)
Definition: component_id_lookup.h:48
std::unique_ptr< breadboard::GraphState > graph_state
The Breadboard graph state data.
Definition: graph.h:44
virtual void UpdateAllEntities(corgi::WorldTime delta_time)
Broadcasts a kAdvanceFrameEventId event to update all Entities' graphs.
std::string filename
The filename for serialization.
Definition: graph.h:41
virtual void Init()
Initialize the GraphComponent.
virtual ~GraphComponent()
Destructor for GraphComponent.
Definition: graph.h:160
GraphData(GraphData &&other)
The move constructor for a GraphData.
Definition: graph.h:108
std::vector< SerializableGraphState > graphs
A vector of all the graph states.
Definition: graph.h:93