Pyrogenesis  trunk
DynamicSubscription.h
Go to the documentation of this file.
1 /* Copyright (C) 2014 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_DYNAMICSUBSCRIPTION
19 #define INCLUDED_DYNAMICSUBSCRIPTION
20 
21 #include "IComponent.h"
22 
23 #include <set>
24 
25 /**
26  * A list of components that are dynamically subscribed to a particular
27  * message. The components list is sorted by (entity_id, ComponentTypeId),
28  * with no duplicates.
29  *
30  * To cope with changes to the subscription list while a message is still
31  * being broadcast, all changes are stored in the added/removed sets. The
32  * next time a message is sent, they will be merged into the main components
33  * list.
34  */
36 {
38  {
39  bool operator()(const IComponent* cmpA, const IComponent* cmpB)
40  {
41  entity_id_t entityA = cmpA->GetEntityId();
42  entity_id_t entityB = cmpB->GetEntityId();
43  if (entityA < entityB)
44  return true;
45  if (entityB < entityA)
46  return false;
47  int cidA = cmpA->GetComponentTypeId();
48  int cidB = cmpB->GetComponentTypeId();
49  if (cidA < cidB)
50  return true;
51  return false;
52  }
53  };
54 public:
55  void Add(IComponent* cmp);
56  void Remove(IComponent* cmp);
57  void Flatten();
58  const std::vector<IComponent*>& GetComponents();
59  void DebugDump();
60 
61 private:
62  std::vector<IComponent*> m_Components; // always in CompareIComponent order
63  std::set<IComponent*, CompareIComponent> m_Added;
64  std::set<IComponent*, CompareIComponent> m_Removed;
65 };
66 
67 #endif // INCLUDED_DYNAMICSUBSCRIPTION
Definition: IComponent.h:33
std::vector< IComponent * > m_Components
Definition: DynamicSubscription.h:62
void DebugDump()
Definition: DynamicSubscription.cpp:70
void Remove(IComponent *cmp)
Definition: DynamicSubscription.cpp:28
std::set< IComponent *, CompareIComponent > m_Removed
Definition: DynamicSubscription.h:64
std::set< IComponent *, CompareIComponent > m_Added
Definition: DynamicSubscription.h:63
virtual int GetComponentTypeId() const =0
entity_id_t GetEntityId() const
Definition: IComponent.h:48
void Flatten()
Definition: DynamicSubscription.cpp:34
A list of components that are dynamically subscribed to a particular message.
Definition: DynamicSubscription.h:35
void Add(IComponent *cmp)
Definition: DynamicSubscription.cpp:22
Definition: DynamicSubscription.h:37
bool operator()(const IComponent *cmpA, const IComponent *cmpB)
Definition: DynamicSubscription.h:39
const std::vector< IComponent * > & GetComponents()
Definition: DynamicSubscription.cpp:62
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23