Lumiera  0.pre.03
»edit your freedom«
model-port-registry.cpp
Go to the documentation of this file.
1 /*
2  ModelPortRegistry - creating and organising and accessing model ports
3 
4  Copyright (C)
5  2010, 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 
30 #include "lib/error.hpp"
31 #include "include/logging.h"
32 #include "lib/sync-classlock.hpp"
36 
37 namespace steam {
38 namespace fixture {
39 
40  namespace error = lumiera::error;
41 
42  using mobject::LUMIERA_ERROR_BUILDER_LIFECYCLE;
43  using mobject::LUMIERA_ERROR_INVALID_MODEL_PORT;
44  using mobject::LUMIERA_ERROR_UNCONNECTED_MODEL_PORT;
45 
46 
47  using MPDescriptor = ModelPortRegistry::ModelPortDescriptor const&;
48  using LockRegistry = lib::ClassLock<ModelPortRegistry>;
49 
50 
54 
55 
56 
58  void
60  {
61  INFO (builder, "disabling ModelPort registry....");
62  LockRegistry global_lock;
63  theGlobalRegistry.clear();
64  }
65 
66 
75  {
76  LockRegistry global_lock;
77  ModelPortRegistry *previous = theGlobalRegistry.isValid()?
78  &( theGlobalRegistry()) : 0;
79  INFO_IF (!previous, builder, "activating new ModelPort registry.");
80  WARN_IF ( previous, builder, "switching ModelPort registry instance.");
81  theGlobalRegistry.link_to (newRegistry);
82  return previous;
83  }
84 
85 
91  {
92  LockRegistry global_lock;
93  if (theGlobalRegistry.isValid())
94  return theGlobalRegistry();
95 
96  throw error::State ("global model port registry is not accessible"
97  , LUMIERA_ERROR_BUILDER_LIFECYCLE);
98  }
99 
100 
101 
106  bool
108  {
109  return bool(key)
110  && util::contains (transaction_, key);
111  }
112 
113 
120  bool
122  {
123  return bool(key)
124  && util::contains (currentReg_, key);
125  }
126 
127 
134  MPDescriptor
136  {
137  if (!key)
138  throw error::State ("This model port is disconnected or NIL"
139  , LUMIERA_ERROR_UNCONNECTED_MODEL_PORT);
140  if (!isRegistered (key))
141  throw error::Logic ("Model port was never registered, or got unregistered meanwhile."
142  ,LUMIERA_ERROR_INVALID_MODEL_PORT);
143 
144  MPTable::const_iterator pos = currentReg_.find (key);
145  ASSERT (pos != currentReg_.end());
146  ASSERT (pos->second.isValid());
147  return pos->second;
148  }
149 
150 
157  MPDescriptor
159  {
160  LockRegistry global_lock;
161  return theGlobalRegistry().get(key);
162  }
163 
164 
165  /* === Mutations === */
166 
169  MPDescriptor
170  ModelPortRegistry::definePort (ID<Pipe> pipe, ID<Struct> element_exposing_this_port)
171  {
172  LockRegistry global_lock;
173  if (contains (pipe))
174  throw error::Logic ("attempt to register a model port with a pipe-ID, "
175  "which has already been used to register a "
176  "model port within this transaction (build process)."
177  , LUMIERA_ERROR_DUPLICATE_MODEL_PORT);
178  return (transaction_[pipe] = ModelPortDescriptor(pipe, element_exposing_this_port));
179  }
180 
181 
183  void
185  {
186  LockRegistry global_lock;
187  transaction_.erase (key);
188  }
189 
190 
194  void
196  {
197  LockRegistry global_lock;
198  transaction_.clear();
199  }
200 
201 
208  void
210  {
211  LockRegistry global_lock;
212  MPTable newTransaction(transaction_);
213  TRACE (builder, "committing new ModelPort list....");
214  swap (currentReg_, transaction_);
215  swap (transaction_, newTransaction);
216  }
217 
218 
222  void
224  {
225  LockRegistry global_lock;
226  TRACE (builder, "discarding changes to ModelPort list (rollback)....");
227  MPTable newTransaction(currentReg_);
228  swap (transaction_, newTransaction);
229  }
230 
231 
232 
233  LUMIERA_ERROR_DEFINE (DUPLICATE_MODEL_PORT, "Attempt to define a new model port with an pipe-ID already denoting an existing port");
234 
235 }// namespace fixture
236 
237 
238 namespace mobject {
239 
240  LUMIERA_ERROR_DEFINE (INVALID_MODEL_PORT, "Referral to unknown model port");
241  LUMIERA_ERROR_DEFINE (UNCONNECTED_MODEL_PORT, "Attempt to operate on an existing but unconnected model port");
242 
243 
244  ModelPort::ModelPort (ID<asset::Pipe> refID)
245  : id_(refID)
246  {
248  }
249 
250 
253  bool
255  {
257  }
258 
259 
266  {
267  ENSURE (this->id_ == fixture::ModelPortRegistry::accessDescriptor(this->id_).id());
268 
269  return fixture::ModelPortRegistry::accessDescriptor(this->id_).id();
270  }
271 
272 
279  {
280  return fixture::ModelPortRegistry::accessDescriptor(this->id_).holder();
281  }
282 
283 
291  {
292  return this->id_.streamType();
293  }
294 
295 
296 
297 }} // namespace steam::mobject
Organising the output data calculation possibilities.
ModelPortDescriptor const & get(PID) const
basic access operation: access the descriptor of a currently valid model port.
ModelPortDescriptor const & definePort(PID pipe, StID element_exposing_this_port)
create and register a new model port entry, within the pending transaction
void clear()
schedule removal of all registry contents.
Optional or switchable link to an existing object.
bool contains(PID) const
does the transaction currently being built already contain a model port registration for the given ID...
void commit()
activate pending model port changes.
static lib::OptionalRef< ModelPortRegistry > theGlobalRegistry
storage for the link to the global Registry instance currently in charge
static bool exists(ID< asset::Pipe >)
check if the global model port registration contains a mapping for the given pipe-ID ...
Mutation and management facility for model ports within the builder.
static ModelPortDescriptor const & accessDescriptor(PID)
access the globally valid model port for the given pipe.
This header is for including and configuring NoBug.
Steam-Layer implementation namespace root.
ModelPortDescriptor records are used as actual storage within the model port registration table; they...
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
ID< asset::Pipe > pipe() const
access the Pipe (ID) of the global model port registered with the ID underlying this model port...
static ModelPortRegistry * setActiveInstance(ModelPortRegistry &newRegistry)
switch the implicit link to the global ModelPort registry to point to the given implementation instan...
static void shutdown()
globally deactivate access to model ports
Management facility for tracking model ports.
Steam-Layer Facade: access to the Builder.
A special implementation of lib::Sync, where the storage of the object monitor is associated directly...
Lumiera error handling (C++ interface).
A synchronisation protection guard employing a lock scoped to the parameter type as a whole...
static ModelPortRegistry & globalInstance()
access the globally valid registry instance.
ID< asset::Struct > holder() const
access the timeline (or similar structural element) holding a global pipe which corresponds to this m...
StreamType::ID streamType() const
convenience shortcut to access the stream type associated with the pipe-ID corresponding to this mode...
void rollback()
discard pending changes.
void remove(PID)
remove a model port entry from the pending transaction
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:71
thin wrapper around a size_t hash ID used as primary key for all Asset objects.
Definition: asset.hpp:99