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