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) 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 
65 #ifndef MOBJECT_PLACEMENT_REF_H
66 #define MOBJECT_PLACEMENT_REF_H
67 
68 #include "lib/error.hpp"
72 
73 
74 namespace lumiera {
75 namespace error {
76  LUMIERA_ERROR_DECLARE (INVALID_PLACEMENTREF);
77  LUMIERA_ERROR_DECLARE (BOTTOM_PLACEMENTREF);
78 }}
79 
80 
81 namespace steam {
82  namespace error = lumiera::error;
83 
84  namespace mobject {
85 
86  class MObject;
87 
88 
89 
98  template<class MX =MObject>
99  class PlacementRef
100  {
101  typedef Placement<MX> PlacementMX;
102  typedef Placement<MObject>::ID _ID;
103  typedef Placement<MObject>::Id<MX> _Id;
104 
105  _Id id_;
106 
107  public:
123  template<class Y>
124  explicit
125  PlacementRef (Y const& refID)
126  : id_(recast (refID))
127  {
128  validate(id_);
129  }
130 
133  : id_( bottomID() )
134  {
135  REQUIRE (!isValid(), "hash-ID clash with existing ID");
136  }
137 
139  : id_(r.id_)
140  {
141  validate(id_);
142  }
143 
144  template<class X>
146  : id_(recast(r))
147  {
148  validate(id_);
149  }
150 
151 
152  PlacementRef&
153  operator= (PlacementRef const& r)
154  {
155  validate(r.id_);
156  id_ = r.id_;
157  return *this;
158  }
159 
160  template<class X>
161  PlacementRef&
162  operator= (PlacementRef<X> const& r)
163  {
164  validate(recast (r));
165  id_ = recast(r);
166  return *this;
167  }
168 
169  template<class Y>
170  PlacementRef&
171  operator= (Y const& refID)
172  {
173  validate (recast (refID));
174  id_ = recast (refID);
175  return *this;
176  }
177 
178  void clear () { id_ = bottomID(); }
179 
180 
181 
182  /* == forwarding smart-ptr operations == */
183 
184  PlacementMX& operator*() const { return access(id_); }
185 
186  PlacementMX& operator->() const { return access(id_); }
187 
188  operator string() const { return access(id_).operator string(); }
189  size_t use_count() const { return access(id_).use_count(); }
190 
191 
192  /* == accessing the embedded ID == */
193  operator _Id const&() const { return id_; }
194  LumieraUid getLUID() const { return id_.get(); }
195 
196  template<class O>
197  bool operator== (PlacementRef<O> const& o) const { return id_ == o; }
198  template<class O>
199  bool operator!= (PlacementRef<O> const& o) const { return id_ != o; }
200 
201 
202 
203 
204 
205  /* == forwarding part of the Placement-API == */
206 
207  explicit
208  operator bool() const
209  {
210  return isValid();
211  }
212 
213  bool isValid() const
214  {
215  if (checkValidity())
216  try
217  {
218  return access(id_).isValid();
219  }
220  catch (error::Invalid&) {}
221 
222  return false;
223  }
224 
226  resolve() const
227  {
228  return access(id_).resolve();
229  }
230 
231 
233 
234  private:
235  bool
236  checkValidity () const
237  {
238  return session::SessionServiceFetch::isAccessible() // session interface opened?
240  }
241 
242  static void
243  validate (_Id const& rId)
244  {
245  access (rId); // may throw
247  }
248 
249  static _Id const&
250  recast (_ID const& someID)
251  {
252  return static_cast<_Id const&> (someID);
253  }
254 
255  static _Id const&
256  recast (const LumieraUid luid)
257  {
258  REQUIRE (luid);
259  return reinterpret_cast<_Id const&> (*luid);
260  }
261 
262  static _Id const&
263  bottomID ()
264  {
265  static lumiera_uid invalidLUID;
266  return recast (&invalidLUID);
267  }
268 
269  static PlacementMX&
270  access (_Id const& placementID)
271  {
272  if (!placementID)
273  throw error::Logic{"Attempt to access a NIL PlacementRef"
274  , LERR_(BOTTOM_PLACEMENTREF)};
275 
276  Placement<MObject> & genericPlacement (session::SessionServiceFetch::resolveID (placementID)); // may throw
277  REQUIRE (genericPlacement.isValid());
278 
279  if (!(genericPlacement.template isCompatible<MX>()))
280  throw error::Invalid{"actual type of the resolved placement is incompatible"
281  , LERR_(INVALID_PLACEMENTREF)};
284 
285  return static_cast<PlacementMX&> (genericPlacement);
286  }
287  };
288 
289 
292 
293 
294 
295 }} // namespace steam::mobject
296 #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:71
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:199
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:113
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:45
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:65