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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
97 #ifndef STEAM_MOBJECT_PLACEMENT_INDEX_H
98 #define STEAM_MOBJECT_PLACEMENT_INDEX_H
99 
100 #include "lib/error.hpp"
101 #include "lib/symbol.hpp"
102 #include "lib/itertools.hpp"
105 #include "lib/nocopy.hpp"
106 #include "lib/util.hpp"
107 
108 #include <unordered_map>
109 #include <memory>
110 #include <vector>
111 
112 namespace std {
113 
115  template<>
116  struct hash<steam::mobject::PlacementMO::ID>
117  {
118  size_t
119  operator() (steam::mobject::PlacementMO::ID const& val) const noexcept
120  {
121  return hash_value(val);
122  }
123  };
124 }
125 
126 
127 namespace lumiera {
128 namespace error {
129  LUMIERA_ERROR_DECLARE (NOT_IN_SESSION);
130  LUMIERA_ERROR_DECLARE (PLACEMENT_TYPE);
131  LUMIERA_ERROR_DECLARE (NONEMPTY_SCOPE);
132 }}
133 
134 namespace steam {
135 namespace mobject {
136 
137  class MObject;
138 
139 namespace session {
140 
141  using std::unique_ptr;
142 
143 
148  template<typename PLA>
149  struct BuildID;
150 
152  template<typename MO, typename BMO>
153  struct BuildID<Placement<MO,BMO>>
154  {
155  typedef PlacementMO::Id<MO> Type;
156  typedef MO Target;
157  };
158 
159 
160 
174  {
175  class Table;
176  class Validator;
177 
178  unique_ptr<Table> pTab_;
179 
180 
181  using _PID = PlacementMO::ID;
182  using ScopeIter = std::unordered_multimap<_PID,_PID>::const_iterator;
185 
186 
187 
188  public:
189  using PRef = PlacementRef<MObject>;
190  using ID = PlacementMO::ID const&;
191 
192  using iterator = _ID_TableIterator;
193 
194 
195  /* == query operations == */
196 
197  PlacementMO& find (ID) const;
198 
199  template<class MO>
200  Placement<MO>& find (PlacementMO::Id<MO>) const;
201  template<class MO>
202  Placement<MO>& find (PlacementRef<MO> const&) const;
203 
204  PlacementMO& getScope (PlacementMO const&) const;
205  PlacementMO& getScope (ID) const;
206 
207  iterator getReferrers (ID) const;
208 
209 
211  PlacementMO& getRoot() const;
212 
213  size_t size() const;
214  bool contains (PlacementMO const&) const;
215  bool contains (ID) const;
216 
217  bool isValid() const;
218 
219 
220 
221 
222  /* == mutating operations == */
223 
224  ID insert (PlacementMO const& newObj, ID targetScope);
225  bool remove (PlacementMO&);
226  bool remove (ID);
227 
228  template<class PLA>
229  typename BuildID<PLA>::Type insert (PLA const&, ID);
230 
231 
232 
233  PlacementIndex(PlacementMO const&);
234  ~PlacementIndex() ;
235 
236  void clear (ID targetScope);
237  void clear ();
238 
239  };
240 
241 
242 
243 
244 
245 
246 
247 
248  /* === forwarding implementations of the templated API === */
249 
250 
251  namespace { // shortcuts...
252 
253  template<class MOX>
254  inline void
255  ___check_compatibleType(PlacementMO& questionable)
256  {
257  if (!questionable.isCompatible<MOX>())
258  throw lumiera::error::Logic ("Attempt to retrieve a Placement of specific type, "
259  "while the actual type of the pointee (MObject) "
260  "registered within the index isn't compatible with the "
261  "requested specific MObject subclass"
262  , LERR_(PLACEMENT_TYPE));
263  }
264 
265  inline void
266  __check_knownID(PlacementIndex const& idx, PlacementMO::ID id)
267  {
268  if (!id)
269  throw lumiera::error::Logic ("Encountered a NIL Placement-ID marker"
270  , LERR_(BOTTOM_PLACEMENTREF));
271  if (!idx.contains (id))
272  throw lumiera::error::Invalid ("Accessing Placement not registered within the index"
273  , LERR_(NOT_IN_SESSION));
274  }
275  }//(End) shortcuts
276 
277 
278 
279 
280  template<class MO>
281  inline Placement<MO>&
282  PlacementIndex::find (PlacementMO::Id<MO> id) const
283  {
284  PlacementMO& result (find ((ID)id));
285 
286  ___check_compatibleType<MO> (result);
287  return static_cast<Placement<MO>&> (result);
288  }
289 
290 
291  template<class MO>
292  inline Placement<MO>&
293  PlacementIndex::find (PlacementRef<MO> const& pRef) const
294  {
295  PlacementMO::Id<MO> id (pRef);
296  return find (id);
297  }
298 
299 
300  inline Placement<MObject>&
301  PlacementIndex::getScope (PlacementMO const& p) const
302  {
303  return getScope(p.getID());
304  }
305 
306 
307  inline bool
308  PlacementIndex::contains (PlacementMO const& p) const
309  {
310  return contains (p.getID());
311  }
312 
313 
320  template<class PLA>
321  typename BuildID<PLA>::Type
322  PlacementIndex::insert (PLA const& newObj, ID targetScope)
323  {
324  typedef typename BuildID<PLA>::Target TargetMO;
325  PlacementMO const& genericPlacement(newObj);
326 
327  return find (insert (genericPlacement, targetScope))
328  .template recastID<TargetMO>();
329  }
330 
331 
332  inline bool
333  PlacementIndex::remove (PlacementMO& p)
334  {
335  return remove (p.getID());
336  }
337 
338 
339 
340 }}} // namespace steam::mobject::session
341 #endif
A refcounting Handle to an MObject of type MO, used to constrain or explicitly specify the location w...
Definition: trait.hpp:82
Core abstraction: placement of a media object into session context.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
STL namespace.
Primary class template for std::hash.
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:62
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:65
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
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:754
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:104
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.