Lumiera  0.pre.03
»edit your freedom«
placement-ref.hpp
Go to the documentation of this file.
1 /*
2  PLACEMENT-REF.hpp - generic reference to an individual placement added to the session
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 
56 #ifndef MOBJECT_PLACEMENT_REF_H
57 #define MOBJECT_PLACEMENT_REF_H
58 
59 #include "lib/error.hpp"
63 
64 
65 namespace lumiera {
66 namespace error {
67  LUMIERA_ERROR_DECLARE (INVALID_PLACEMENTREF);
68  LUMIERA_ERROR_DECLARE (BOTTOM_PLACEMENTREF);
69 }}
70 
71 
72 namespace steam {
73  namespace error = lumiera::error;
74 
75  namespace mobject {
76 
77  class MObject;
78 
79 
80 
89  template<class MX =MObject>
90  class PlacementRef
91  {
92  typedef Placement<MX> PlacementMX;
93  typedef Placement<MObject>::ID _ID;
94  typedef Placement<MObject>::Id<MX> _Id;
95 
96  _Id id_;
97 
98  public:
114  template<class Y>
115  explicit
116  PlacementRef (Y const& refID)
117  : id_(recast (refID))
118  {
119  validate(id_);
120  }
121 
124  : id_( bottomID() )
125  {
126  REQUIRE (!isValid(), "hash-ID clash with existing ID");
127  }
128 
130  : id_(r.id_)
131  {
132  validate(id_);
133  }
134 
135  template<class X>
137  : id_(recast(r))
138  {
139  validate(id_);
140  }
141 
142 
143  PlacementRef&
144  operator= (PlacementRef const& r)
145  {
146  validate(r.id_);
147  id_ = r.id_;
148  return *this;
149  }
150 
151  template<class X>
152  PlacementRef&
153  operator= (PlacementRef<X> const& r)
154  {
155  validate(recast (r));
156  id_ = recast(r);
157  return *this;
158  }
159 
160  template<class Y>
161  PlacementRef&
162  operator= (Y const& refID)
163  {
164  validate (recast (refID));
165  id_ = recast (refID);
166  return *this;
167  }
168 
169  void clear () { id_ = bottomID(); }
170 
171 
172 
173  /* == forwarding smart-ptr operations == */
174 
175  PlacementMX& operator*() const { return access(id_); }
176 
177  PlacementMX& operator->() const { return access(id_); }
178 
179  operator string() const { return access(id_).operator string(); }
180  size_t use_count() const { return access(id_).use_count(); }
181 
182 
183  /* == accessing the embedded ID == */
184  operator _Id const&() const { return id_; }
185  LumieraUid getLUID() const { return id_.get(); }
186 
187  template<class O>
188  bool operator== (PlacementRef<O> const& o) const { return id_ == o; }
189  template<class O>
190  bool operator!= (PlacementRef<O> const& o) const { return id_ != o; }
191 
192 
193 
194 
195 
196  /* == forwarding part of the Placement-API == */
197 
198  explicit
199  operator bool() const
200  {
201  return isValid();
202  }
203 
204  bool isValid() const
205  {
206  if (checkValidity())
207  try
208  {
209  return access(id_).isValid();
210  }
211  catch (error::Invalid&) {}
212 
213  return false;
214  }
215 
217  resolve() const
218  {
219  return access(id_).resolve();
220  }
221 
222 
224 
225  private:
226  bool
227  checkValidity () const
228  {
229  return session::SessionServiceFetch::isAccessible() // session interface opened?
231  }
232 
233  static void
234  validate (_Id const& rId)
235  {
236  access (rId); // may throw
238  }
239 
240  static _Id const&
241  recast (_ID const& someID)
242  {
243  return static_cast<_Id const&> (someID);
244  }
245 
246  static _Id const&
247  recast (const LumieraUid luid)
248  {
249  REQUIRE (luid);
250  return reinterpret_cast<_Id const&> (*luid);
251  }
252 
253  static _Id const&
254  bottomID ()
255  {
256  static lumiera_uid invalidLUID;
257  return recast (&invalidLUID);
258  }
259 
260  static PlacementMX&
261  access (_Id const& placementID)
262  {
263  if (!placementID)
264  throw error::Logic{"Attempt to access a NIL PlacementRef"
265  , LERR_(BOTTOM_PLACEMENTREF)};
266 
267  Placement<MObject> & genericPlacement (session::SessionServiceFetch::resolveID (placementID)); // may throw
268  REQUIRE (genericPlacement.isValid());
269 
270  if (!(genericPlacement.template isCompatible<MX>()))
271  throw error::Invalid{"actual type of the resolved placement is incompatible"
272  , LERR_(INVALID_PLACEMENTREF)};
275 
276  return static_cast<PlacementMX&> (genericPlacement);
277  }
278  };
279 
280 
283 
284 
285 
286 }} // namespace steam::mobject
287 #endif
Reference tag denoting a placement attached to the session.
static bool isRegisteredID(PlacementMO::ID const &)
verify the given placement-ID (hash) is valid, by checking if it refers to a Placement instance curre...
Core abstraction: completely resolved placement of an MObject Within the session model, all media objects are attached with the help of mobject::Placement elements.
Core abstraction: placement of a media object into session context.
Special kind of Placement, where the location of the MObject has been nailed down to a fixed position...
PlacementRef()
Default is an NIL Placement ref.
PlacementRef(PlacementRef const &r)
Implementation level session API: resolve a Placement by hash-ID.
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:62
static PlacementMO & resolveID(PlacementMO::ID const &)
actually retrieve a Placement tracked by the index.
Steam-Layer implementation namespace root.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
static bool isAccessible()
is the element-fetch service usable? Effectively this means: is the session up?
PlacementMX & operator->() const
provide access to pointee API by smart-ptr chaining
PlacementRef< MObject > RefPlacement
frequently-used shorthand
Lumiera error handling (C++ interface).
Lumiera public interface.
Definition: advice.cpp:104
PlacementRef(PlacementRef< X > const &r)
PlacementMX & operator*() const
dereferencing fetches referred Placement from Index
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:40
PlacementRef(Y const &refID)
Creating a PlacementRef from a compatible reference source.
string resolve(fsys::path iniSpec)
use the general mechanism for resolving a search path to get the absolute path of the setup...
Definition: basic-setup.cpp:56