Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
DynamicSubscription.h
Go to the documentation of this file.
1/* Copyright (C) 2020 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#include <vector>
25
26/**
27 * A list of components that are dynamically subscribed to a particular
28 * message. The components list is sorted by (entity_id, ComponentTypeId),
29 * with no duplicates.
30 *
31 * To cope with changes to the subscription list while a message is still
32 * being broadcast, all changes are stored in the added/removed sets. The
33 * next time a message is sent, they will be merged into the main components
34 * list.
35 */
37{
39 {
40 bool operator()(const IComponent* cmpA, const IComponent* cmpB) const
41 {
42 entity_id_t entityA = cmpA->GetEntityId();
43 entity_id_t entityB = cmpB->GetEntityId();
44 if (entityA < entityB)
45 return true;
46 if (entityB < entityA)
47 return false;
48 int cidA = cmpA->GetComponentTypeId();
49 int cidB = cmpB->GetComponentTypeId();
50 if (cidA < cidB)
51 return true;
52 return false;
53 }
54 };
55public:
56 void Add(IComponent* cmp);
57 void Remove(IComponent* cmp);
58 void Flatten();
59 const std::vector<IComponent*>& GetComponents();
60 void DebugDump();
61
62private:
63 std::vector<IComponent*> m_Components; // always in CompareIComponent order
64 std::set<IComponent*, CompareIComponent> m_Added;
65 std::set<IComponent*, CompareIComponent> m_Removed;
66};
67
68#endif // INCLUDED_DYNAMICSUBSCRIPTION
A list of components that are dynamically subscribed to a particular message.
Definition: DynamicSubscription.h:37
const std::vector< IComponent * > & GetComponents()
Definition: DynamicSubscription.cpp:62
std::set< IComponent *, CompareIComponent > m_Added
Definition: DynamicSubscription.h:64
void Remove(IComponent *cmp)
Definition: DynamicSubscription.cpp:28
void Flatten()
Definition: DynamicSubscription.cpp:34
void DebugDump()
Definition: DynamicSubscription.cpp:70
void Add(IComponent *cmp)
Definition: DynamicSubscription.cpp:22
std::set< IComponent *, CompareIComponent > m_Removed
Definition: DynamicSubscription.h:65
std::vector< IComponent * > m_Components
Definition: DynamicSubscription.h:63
Definition: IComponent.h:33
entity_id_t GetEntityId() const
Definition: IComponent.h:54
virtual int GetComponentTypeId() const =0
u32 entity_id_t
Entity ID type.
Definition: Entity.h:29
Definition: DynamicSubscription.h:39
bool operator()(const IComponent *cmpA, const IComponent *cmpB) const
Definition: DynamicSubscription.h:40