LCOV - code coverage report
Current view: top level - source/simulation2/system - DynamicSubscription.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 20 38 52.6 %
Date: 2026-12-03 01:19:39 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 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             : #include "precompiled.h"
      19             : 
      20             : #include "DynamicSubscription.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : 
      24             : #include <algorithm>
      25             : #include <cstddef>
      26             : #include <iterator>
      27             : 
      28         170 : void CDynamicSubscription::Add(IComponent* cmp)
      29             : {
      30         170 :     m_Removed.erase(cmp);
      31         170 :     m_Added.insert(cmp);
      32         170 : }
      33             : 
      34          35 : void CDynamicSubscription::Remove(IComponent* cmp)
      35             : {
      36          35 :     m_Added.erase(cmp);
      37          35 :     m_Removed.insert(cmp);
      38          35 : }
      39             : 
      40          59 : void CDynamicSubscription::Flatten()
      41             : {
      42          59 :     if (m_Added.empty() && m_Removed.empty())
      43          24 :         return;
      44             : 
      45          35 :     std::vector<IComponent*> tmp;
      46          35 :     tmp.reserve(m_Components.size() + m_Added.size());
      47             : 
      48             :     // tmp = m_Components - m_Removed
      49          35 :     std::set_difference(
      50             :             m_Components.begin(), m_Components.end(),
      51             :             m_Removed.begin(), m_Removed.end(),
      52             :             std::back_inserter(tmp),
      53             :             CompareIComponent());
      54             : 
      55          35 :     m_Components.clear();
      56             : 
      57             :     // m_Components = tmp + m_Added
      58          70 :     std::set_union(
      59             :             tmp.begin(), tmp.end(),
      60             :             m_Added.begin(), m_Added.end(),
      61          35 :             std::back_inserter(m_Components),
      62             :             CompareIComponent());
      63             : 
      64          35 :     m_Added.clear();
      65          35 :     m_Removed.clear();
      66          35 : }
      67             : 
      68           0 : const std::vector<IComponent*>& CDynamicSubscription::GetComponents()
      69             : {
      70             :     // Must be flattened before calling this function
      71           0 :     ENSURE(m_Added.empty() && m_Removed.empty());
      72             : 
      73           0 :     return m_Components;
      74             : }
      75             : 
      76           0 : void CDynamicSubscription::DebugDump()
      77             : {
      78           0 :     std::set<IComponent*, CompareIComponent>::iterator it;
      79             : 
      80           0 :     debug_printf("components:");
      81           0 :     for (size_t i = 0; i < m_Components.size(); i++)
      82           0 :         debug_printf(" %p", (void *)m_Components[i]);
      83           0 :     debug_printf("\n");
      84             : 
      85           0 :     debug_printf("added:");
      86           0 :     for (it = m_Added.begin(); it != m_Added.end(); ++it)
      87           0 :         debug_printf(" %p", (void *)*it);
      88           0 :     debug_printf("\n");
      89             : 
      90           0 :     debug_printf("removed:");
      91           0 :     for (it = m_Removed.begin(); it != m_Removed.end(); ++it)
      92           0 :         debug_printf(" %p", (void *)*it);
      93           0 :     debug_printf("\n");
      94           0 : }

Generated by: LCOV version 1.16