Lumiera  0.pre.03
»edit your freedom«
assetmanager.cpp
Go to the documentation of this file.
1 /*
2  AssetManager - Facade for the Asset subsystem
3 
4  Copyright (C) Lumiera.org
5  2008, 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 
29 #include "steam/assetmanager.hpp"
30 #include "steam/asset/db.hpp"
31 
32 #include "lib/sync.hpp"
33 #include "lib/util-foreach.hpp"
34 #include "lib/format-string.hpp"
35 
36 #include <functional>
37 
38 using std::static_pointer_cast;
39 using std::function;
40 using std::placeholders::_1;
41 using util::for_each;
42 using util::_Fmt;
43 
44 using lib::Depend;
45 using lib::Sync;
46 
47 
48 namespace lumiera {
49 namespace error {
50  // ------pre-defined-common-error-cases---------------
51  LUMIERA_ERROR_DEFINE (UNKNOWN_ASSET_ID, "non-registered Asset ID");
52  LUMIERA_ERROR_DEFINE (WRONG_ASSET_KIND, "wrong Asset kind, unable to cast");
53 }}
54 
55 namespace steam {
56 namespace asset {
57  namespace error = lumiera::error;
58 
63  struct IDErr
65  {
66  using error::Invalid::Invalid;
67  };
68 
69 
70 
71  struct UnknownID : IDErr
72  {
73  UnknownID (ID<Asset> aID)
74  : IDErr(_Fmt("Query for Asset with ID=%d, which up to now "
75  "hasn't been created or encountered.") % aID
76  ,LERR_(UNKNOWN_ASSET_ID))
77  { }
78  };
79 
80  struct WrongKind : IDErr
81  {
83  : IDErr (_Fmt("Request for Asset(%s), specifying an Asset kind, "
84  "that doesn't match the actual type (and can't be "
85  "casted either).") % idi
86  ,LERR_(WRONG_ASSET_KIND))
87  { }
88  };
89 
90 
91 
92 
93 
97  Depend<AssetManager> AssetManager::instance;
98 
99  AssetManager::AssetManager ()
100  : registry (Depend<asset::DB>() ())
101  { }
102 
103 
104 
106  ID<Asset>
107  AssetManager::getID (const Asset::Ident& idi)
108  {
109  return asset::hash_value (idi);
110  }
111 
112 
113 
119  template<class KIND>
120  ID<KIND>
121  AssetManager::reg (KIND* obj, const Asset::Ident& idi)
122  {
123  AssetManager& _aMang (AssetManager::instance());
124  DB& registry (_aMang.registry);
126  ID<KIND> asset_id (getID (idi));
127 
128  DB::Lock guard{&registry};
130  lib::P<KIND> smart_ptr (obj, &destroy);
131 
132  registry.put (asset_id, smart_ptr);
133  return asset_id;
134  }
135 
136 
141  template<class KIND>
143  AssetManager::getAsset (const ID<KIND>& id)
144  {
145  if (lib::P<KIND> obj = registry.get (id))
146  return obj;
147  else
148  if (known (id)) // provide Ident tuple of existing Asset
149  throw WrongKind (registry.get(ID<Asset>(id))->ident);
150  else
151  throw UnknownID (id);
152  }
153 
160  template<class KIND>
162  AssetManager::wrap (const KIND& asset)
163  {
164  ENSURE (instance().known(asset.id),
165  "unregistered asset instance encountered.");
166  return static_pointer_cast<KIND,Asset>
167  (instance().registry.get (asset.id));
168  }
169 
170 
171 
175  bool
176  AssetManager::known (IDA id)
177  {
178  return bool(registry.get (ID<Asset>(id)));
179  } // query most general Asset ID-kind and use implicit
180  // conversion from smart-ptr to bool (test if empty)
181 
182 
186  bool
187  AssetManager::known (IDA id, const Category& cat)
188  {
189  PAsset pA = registry.get (id);
190  return ( pA && pA->ident.category.isWithin(cat));
191  }
192 
193 
194  namespace { // details implementing AssetManager::remove
195 
196  void
197  recursive_call (AssetManager* instance, PAsset& pA)
198  {
199  instance->remove (pA->getID());
200  }
201 
202  function<void(PAsset&)>
203  detach_child_recursively ()
204  {
205  return bind( &recursive_call, &AssetManager::instance(), _1 );
206  }
207  }
208 
213  void
214  AssetManager::remove (IDA id)
215  {
216  PAsset asset = getAsset (id);
217  for_each (asset->dependants, detach_child_recursively());
218  asset->unlink();
219  registry.del(id);
220  }
221 
222 
223  void
224  AssetManager::clear()
225  {
226  INFO (progress, "Clearing the Asset registry...");
227  registry.clear();
228  }
229 
230 
231  list<PcAsset>
232  AssetManager::listContent() const
233  {
234  list<PcAsset> res;
235  registry.asList (res);
236  res.sort();
237  return res;
238  }
239 
240 
241 }} // namespace steam::asset
242 
243 
244 
245 
246  /************************************************************/
247  /* explicit template instantiations for various Asset Kinds */
248  /************************************************************/
249 
250 #include "steam/asset/media.hpp"
251 #include "steam/asset/clip.hpp"
252 #include "steam/asset/proc.hpp"
253 #include "steam/asset/struct.hpp"
254 #include "steam/asset/pipe.hpp"
255 #include "steam/asset/meta.hpp"
256 #include "steam/asset/procpatt.hpp"
257 #include "steam/asset/timeline.hpp"
258 #include "steam/asset/sequence.hpp"
261 
262 namespace steam {
263 namespace asset {
264  using lib::P;
265 
266  template ID<Asset> AssetManager::reg (Asset* obj, const Asset::Ident& idi);
267 
268 
269  template P<Asset> AssetManager::getAsset (const ID<Asset>& id);
270  template P<Media> AssetManager::getAsset (const ID<Media>& id);
271  template P<Proc> AssetManager::getAsset (const ID<Proc>& id);
272  template P<Struct> AssetManager::getAsset (const ID<Struct>& id);
273  template P<Meta> AssetManager::getAsset (const ID<Meta>& id);
274  template P<Pipe> AssetManager::getAsset (const ID<Pipe>& id);
275 
276  template P<Asset> AssetManager::wrap (const Asset& asset);
277  template P<Media> AssetManager::wrap (const Media& asset);
278  template P<Clip> AssetManager::wrap (const Clip& asset);
279  template P<Pipe> AssetManager::wrap (const Pipe& asset);
280  template P<ProcPatt> AssetManager::wrap (const ProcPatt& asset);
281  template P<Timeline> AssetManager::wrap (const Timeline& asset);
282  template P<Sequence> AssetManager::wrap (const Sequence& asset);
283 
284  using meta::TimeGrid;
285  using meta::ErrorLog;
286  template P<TimeGrid> AssetManager::wrap (const TimeGrid& asset);
287  template P<ErrorLog> AssetManager::wrap (const ErrorLog& asset);
288 
289 
290 }} // namespace steam::asset
Facility for monitor object based locking.
Definition: sync.hpp:217
Interface: a grid and scale definition for time quantisation.
Definition: time-grid.hpp:86
Steam-Layer Interface: Asset Lookup and Organisation.
Definition of a structural asset to express patterns of wiring or processing Processing patterns can ...
A "processing pipe" represented as Asset.
"Processing Pattern" is a structural Asset representing information how to build some part of the ren...
Definition: procpatt.hpp:61
TODO type comment.
Definition: sequence.hpp:92
key abstraction: media-like assets
Definition: media.hpp:72
Media data represented a specific kind of Asset.
Tree like classification of Assets.
Definition: category.hpp:75
void remove(IDA id)
remove the given asset from the internal DB.
Front-end for printf-style string template interpolation.
scoped guard to control the actual locking.
Definition: sync.hpp:234
Facade for the Asset subsystem.
Steam-Layer implementation namespace root.
A front-end for using printf-style formatting.
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:289
structural asset corresponding to the part of the model forming a processing pipe for generating medi...
Definition: pipe.hpp:79
AssetManager error responses, caused by querying invalid Asset IDs from the internal DB...
Object Monitor based synchronisation.
Implementation of the registry holding all Asset instances known to the Asset Manager subsystem...
Definition: db.hpp:98
An entity to collect, possibly filter and persist incident records.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Structural building block of the session: a sequence of clips.
Internal and organisational metadata.
Data processing Plugins and Codecs can be treated as a specific Kind of Asset.
a POD comprised of all the information sufficiently identifying any given Asset.
Definition: asset.hpp:158
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:148
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
Definition of Asset representation for a media clip.
Implementation of the Asset database.
bookkeeping (Asset) view of a media clip.
Definition: asset/clip.hpp:44
Lumiera public interface.
Definition: advice.cpp:113
To establish a reference scale for quantised time values.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:44
Receive, collect, filter and possibly persist incident records.
Definition: error-log.hpp:74
Asset representation of structural elements within the model.
Perform operations "for each element" of a collection.
Top level structural element within the session.
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:80