Lumiera  0.pre.03
»edit your freedom«
placement-index.hpp
Go to the documentation of this file.
1 /*
2  PLACEMENT-INDEX.hpp - tracking individual Placements and their relations
3 
4  Copyright (C) Lumiera.org
5  2009, 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 
106 #ifndef STEAM_MOBJECT_PLACEMENT_INDEX_H
107 #define STEAM_MOBJECT_PLACEMENT_INDEX_H
108 
109 #include "lib/error.hpp"
110 #include "lib/symbol.hpp"
111 #include "lib/itertools.hpp"
114 #include "lib/nocopy.hpp"
115 #include "lib/util.hpp"
116 
117 #include <unordered_map>
118 #include <memory>
119 #include <vector>
120 
121 namespace std {
122 
124  template<>
125  struct hash<steam::mobject::PlacementMO::ID>
126  {
127  size_t
128  operator() (steam::mobject::PlacementMO::ID const& val) const noexcept
129  {
130  return hash_value(val);
131  }
132  };
133 }
134 
135 
136 namespace lumiera {
137 namespace error {
138  LUMIERA_ERROR_DECLARE (NOT_IN_SESSION);
139  LUMIERA_ERROR_DECLARE (PLACEMENT_TYPE);
140  LUMIERA_ERROR_DECLARE (NONEMPTY_SCOPE);
141 }}
142 
143 namespace steam {
144 namespace mobject {
145 
146  class MObject;
147 
148 namespace session {
149 
150  using std::unique_ptr;
151 
152 
157  template<typename PLA>
158  struct BuildID;
159 
161  template<typename MO, typename BMO>
162  struct BuildID<Placement<MO,BMO>>
163  {
164  typedef PlacementMO::Id<MO> Type;
165  typedef MO Target;
166  };
167 
168 
169 
183  {
184  class Table;
185  class Validator;
186 
187  unique_ptr<Table> pTab_;
188 
189 
190  using _PID = PlacementMO::ID;
191  using ScopeIter = std::unordered_multimap<_PID,_PID>::const_iterator;
194 
195 
196 
197  public:
198  using PRef = PlacementRef<MObject>;
199  using ID = PlacementMO::ID const&;
200 
201  using iterator = _ID_TableIterator;
202 
203 
204  /* == query operations == */
205 
206  PlacementMO& find (ID) const;
207 
208  template<class MO>
209  Placement<MO>& find (PlacementMO::Id<MO>) const;
210  template<class MO>
211  Placement<MO>& find (PlacementRef<MO> const&) const;
212 
213  PlacementMO& getScope (PlacementMO const&) const;
214  PlacementMO& getScope (ID) const;
215 
216  iterator getReferrers (ID) const;
217 
218 
220  PlacementMO& getRoot() const;
221 
222  size_t size() const;
223  bool contains (PlacementMO const&) const;
224  bool contains (ID) const;
225 
226  bool isValid() const;
227 
228 
229 
230 
231  /* == mutating operations == */
232 
233  ID insert (PlacementMO const& newObj, ID targetScope);
234  bool remove (PlacementMO&);
235  bool remove (ID);
236 
237  template<class PLA>
238  typename BuildID<PLA>::Type insert (PLA const&, ID);
239 
240 
241 
242  PlacementIndex(PlacementMO const&);
243  ~PlacementIndex() ;
244 
245  void clear (ID targetScope);
246  void clear ();
247 
248  };
249 
250 
251 
252 
253 
254 
255 
256 
257  /* === forwarding implementations of the templated API === */
258 
259 
260  namespace { // shortcuts...
261 
262  template<class MOX>
263  inline void
264  ___check_compatibleType(PlacementMO& questionable)
265  {
266  if (!questionable.isCompatible<MOX>())
267  throw lumiera::error::Logic ("Attempt to retrieve a Placement of specific type, "
268  "while the actual type of the pointee (MObject) "
269  "registered within the index isn't compatible with the "
270  "requested specific MObject subclass"
271  , LERR_(PLACEMENT_TYPE));
272  }
273 
274  inline void
275  __check_knownID(PlacementIndex const& idx, PlacementMO::ID id)
276  {
277  if (!id)
278  throw lumiera::error::Logic ("Encountered a NIL Placement-ID marker"
279  , LERR_(BOTTOM_PLACEMENTREF));
280  if (!idx.contains (id))
281  throw lumiera::error::Invalid ("Accessing Placement not registered within the index"
282  , LERR_(NOT_IN_SESSION));
283  }
284  }//(End) shortcuts
285 
286 
287 
288 
289  template<class MO>
290  inline Placement<MO>&
291  PlacementIndex::find (PlacementMO::Id<MO> id) const
292  {
293  PlacementMO& result (find ((ID)id));
294 
295  ___check_compatibleType<MO> (result);
296  return static_cast<Placement<MO>&> (result);
297  }
298 
299 
300  template<class MO>
301  inline Placement<MO>&
302  PlacementIndex::find (PlacementRef<MO> const& pRef) const
303  {
304  PlacementMO::Id<MO> id (pRef);
305  return find (id);
306  }
307 
308 
309  inline Placement<MObject>&
310  PlacementIndex::getScope (PlacementMO const& p) const
311  {
312  return getScope(p.getID());
313  }
314 
315 
316  inline bool
317  PlacementIndex::contains (PlacementMO const& p) const
318  {
319  return contains (p.getID());
320  }
321 
322 
329  template<class PLA>
330  typename BuildID<PLA>::Type
331  PlacementIndex::insert (PLA const& newObj, ID targetScope)
332  {
333  typedef typename BuildID<PLA>::Target TargetMO;
334  PlacementMO const& genericPlacement(newObj);
335 
336  return find (insert (genericPlacement, targetScope))
337  .template recastID<TargetMO>();
338  }
339 
340 
341  inline bool
342  PlacementIndex::remove (PlacementMO& p)
343  {
344  return remove (p.getID());
345  }
346 
347 
348 
349 }}} // namespace steam::mobject::session
350 #endif
A refcounting Handle to an MObject of type MO, used to constrain or explicitly specify the location w...
Definition: trait.hpp:91
Core abstraction: placement of a media object into session context.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
STL namespace.
Primary class template for std::hash.
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:71
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:74
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Marker types to indicate a literal string and a Symbol.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Iterator tool treating pulled data by a custom transformation (function)
Definition: itertools.hpp:763
A generic reference mechanism for Placements, as added to the current session.
Structured compound of Placement instances with lookup capabilities.
Lumiera error handling (C++ interface).
PlacementIndex self-verification code Executes all built-in checks automatically on object creation...
Lumiera public interface.
Definition: advice.cpp:113
Helper for building Placement-ID types.
Helpers for working with iterators based on the pipeline model.
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
Storage and implementation backing the PlacementIndex.