Line data Source code
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 : */ 36 4 : class CDynamicSubscription 37 : { 38 : struct CompareIComponent 39 : { 40 2243 : bool operator()(const IComponent* cmpA, const IComponent* cmpB) const 41 : { 42 2243 : entity_id_t entityA = cmpA->GetEntityId(); 43 2243 : entity_id_t entityB = cmpB->GetEntityId(); 44 2243 : if (entityA < entityB) 45 : return true; 46 767 : if (entityB < entityA) 47 : return false; 48 74 : int cidA = cmpA->GetComponentTypeId(); 49 74 : int cidB = cmpB->GetComponentTypeId(); 50 74 : if (cidA < cidB) 51 : return true; 52 : return false; 53 : } 54 : }; 55 : public: 56 : void Add(IComponent* cmp); 57 : void Remove(IComponent* cmp); 58 : void Flatten(); 59 : const std::vector<IComponent*>& GetComponents(); 60 : void DebugDump(); 61 : 62 : private: 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