LCOV - code coverage report
Current view: top level - source/simulation2/system - DynamicSubscription.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 11 13 84.6 %
Date: 2023-01-19 00:18:29 Functions: 3 3 100.0 %

          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           2 : class CDynamicSubscription
      37             : {
      38             :     struct CompareIComponent
      39             :     {
      40           8 :         bool operator()(const IComponent* cmpA, const IComponent* cmpB) const
      41             :         {
      42           8 :             entity_id_t entityA = cmpA->GetEntityId();
      43           8 :             entity_id_t entityB = cmpB->GetEntityId();
      44           8 :             if (entityA < entityB)
      45           0 :                 return true;
      46           8 :             if (entityB < entityA)
      47           0 :                 return false;
      48           8 :             int cidA = cmpA->GetComponentTypeId();
      49           8 :             int cidB = cmpB->GetComponentTypeId();
      50           8 :             if (cidA < cidB)
      51           2 :                 return true;
      52           6 :             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

Generated by: LCOV version 1.13