Lumiera  0.pre.03
»edit your freedom«
state-map-grouping-storage.hpp
Go to the documentation of this file.
1 /*
2  STATE-MAP-GROUPING-STORAGE.hpp - grouping storage to track presentation state
3 
4  Copyright (C) Lumiera.org
5  2016, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
36 #ifndef STAGE_CTRL_STATE_MAP_GROUPING_STORAGE_H
37 #define STAGE_CTRL_STATE_MAP_GROUPING_STORAGE_H
38 
39 
40 #include "lib/error.hpp"
41 #include "lib/nocopy.hpp"
42 #include "lib/idi/entry-id.hpp"
43 #include "lib/diff/gen-node.hpp"
44 #include "lib/util.hpp"
45 
46 #include <unordered_map>
47 #include <algorithm>
48 #include <set>
49 
50 
51 namespace stage {
52 namespace ctrl {
53 
55  using lib::diff::GenNode;
56  using lib::diff::Ref;
57  using ::util::unConst;
58  using std::string;
59 
60 
61 
76  {
77  using StateData = std::set<GenNode, GenNode::IDComparator>;
78  using Storage = std::unordered_map<BareEntryID, StateData, BareEntryID::UseEmbeddedHash>;
79 
80 
81  Storage elmTable_;
82 
83  public:
84  using Record = Storage::value_type;
85 
86  bool
87  empty() const
88  {
89  return elmTable_.empty();
90  }
91 
92  size_t
93  size() const
94  {
95  size_t siz{0};
96  for (Record const& entry : elmTable_)
97  siz += entry.second.size();
98  return siz;
99  }
100 
101  void
102  clear()
103  {
104  elmTable_.clear();
105  }
106 
107 
112  GenNode const&
113  retrieve (BareEntryID const& elementID, string propertyKey) const
114  {
115  iterator entry = find (elementID);
116  if (entry == elmTable_.end())
117  return Ref::NO;
118  else
119  return getState (*entry, propertyKey);
120  }
121 
127  void
128  record (BareEntryID const& elementID, GenNode const& stateMark)
129  {
130  auto res = elmTable_[elementID].emplace (stateMark);
131  if (not res.second)
132  unConst(*res.first) = stateMark; // update existing contents
133  }
134 
136  void
137  clearProperty (BareEntryID const& elementID, string propertyKey)
138  {
139  iterator entry = find (elementID);
140  if (entry != elmTable_.end())
141  {
142  StateData const& stateSet = entry->second;
143  auto propertyRecord = findProperty(stateSet, propertyKey);
144 
145  if (propertyRecord != stateSet.end())
146  unConst(stateSet).erase (propertyRecord);
147  }
148  }
149 
151  void
152  clearState (BareEntryID const& elementID)
153  {
154  iterator entry = find (elementID);
155  if (entry != elmTable_.end())
156  elmTable_.erase (entry);
157  }
158 
159 
160  using iterator = Storage::const_iterator;
161 
162  iterator begin() const { return elmTable_.begin(); }
163  iterator end() const { return elmTable_.end(); }
164 
165  iterator
166  find (BareEntryID const& elementID) const
167  {
168  return elmTable_.find (elementID);
169  }
170 
171  static BareEntryID const&
172  getID (Record const& entry)
173  {
174  return entry.first;
175  }
176 
177  static StateData const&
178  getState (Record const& entry)
179  {
180  return entry.second;
181  }
182 
183 
198  static GenNode const&
199  getState (Record const& entry, string propertyKey)
200  {
201  StateData const& stateSet = entry.second;
202  StateData::const_iterator propertyRecord = findProperty (stateSet, propertyKey);
203 
204  if (propertyRecord == stateSet.end())
205  return Ref::NO;
206  else
207  return *propertyRecord;
208  }
209 
210  private:
211  static StateData::const_iterator
212  findProperty (StateData const& stateSet, string propertyKey)
213  {
214  return std::find_if (stateSet.begin()
215  ,stateSet.end()
216  ,[=](GenNode const& stateMark)
217  {
218  return propertyKey == stateMark.idi.getSym();
219  });
220  }
221  };
222 
223 
224 
225 }} // namespace stage::ctrl
226 #endif /*STAGE_CTRL_STATE_MAP_GROUPING_STORAGE_H*/
type erased baseclass for building a combined hash and symbolic ID.
Definition: entry-id.hpp:142
Constructor for a specially crafted &#39;ref GenNode&#39;.
Definition: gen-node.hpp:849
void record(BareEntryID const &elementID, GenNode const &stateMark)
remember the state mark for the denoted element
AnyPair entry(Query< TY > const &query, typename WrapReturn< TY >::Wrapper &obj)
helper to simplify creating mock table entries, wrapped correctly
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
void clearProperty(BareEntryID const &elementID, string propertyKey)
clear previously recorded state for a given element and specific property
void clearState(BareEntryID const &elementID)
clear any previously recorded state for a given element
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Map storage for captured presentation state information.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:46
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Generic building block for tree shaped (meta)data structures.
static const Ref NO
symbolic ID ref "_NO_"
Definition: gen-node.hpp:870
GenNode const & retrieve(BareEntryID const &elementID, string propertyKey) const
retrieve captured state
Lumiera error handling (C++ interface).
Bare symbolic and hash ID used for accounting of asset like entries.
generic data element node within a tree
Definition: gen-node.hpp:231
static GenNode const & getState(Record const &entry, string propertyKey)
Access the recorded state mark, if any.