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)
5  2008, 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 
20 #include "steam/assetmanager.hpp"
21 #include "steam/asset/db.hpp"
22 
23 #include "lib/sync.hpp"
24 #include "lib/util-foreach.hpp"
25 #include "lib/format-string.hpp"
26 
27 #include <functional>
28 
29 using std::static_pointer_cast;
30 using std::function;
31 using std::placeholders::_1;
32 using util::for_each;
33 using util::_Fmt;
34 
35 using lib::Depend;
36 using lib::Sync;
37 
38 
39 namespace lumiera {
40 namespace error {
41  // ------pre-defined-common-error-cases---------------
42  LUMIERA_ERROR_DEFINE (UNKNOWN_ASSET_ID, "non-registered Asset ID");
43  LUMIERA_ERROR_DEFINE (WRONG_ASSET_KIND, "wrong Asset kind, unable to cast");
44 }}
45 
46 namespace steam {
47 namespace asset {
48  namespace error = lumiera::error;
49 
54  struct IDErr
56  {
57  using error::Invalid::Invalid;
58  };
59 
60 
61 
62  struct UnknownID : IDErr
63  {
64  UnknownID (ID<Asset> aID)
65  : IDErr(_Fmt("Query for Asset with ID=%d, which up to now "
66  "hasn't been created or encountered.") % aID
67  ,LERR_(UNKNOWN_ASSET_ID))
68  { }
69  };
70 
71  struct WrongKind : IDErr
72  {
74  : IDErr (_Fmt("Request for Asset(%s), specifying an Asset kind, "
75  "that doesn't match the actual type (and can't be "
76  "casted either).") % idi
77  ,LERR_(WRONG_ASSET_KIND))
78  { }
79  };
80 
81 
82 
83 
84 
88  Depend<AssetManager> AssetManager::instance;
89 
90  AssetManager::AssetManager ()
91  : registry (Depend<asset::DB>() ())
92  { }
93 
94 
95 
97  ID<Asset>
98  AssetManager::getID (const Asset::Ident& idi)
99  {
100  return asset::hash_value (idi);
101  }
102 
103 
104 
110  template<class KIND>
111  ID<KIND>
112  AssetManager::reg (KIND* obj, const Asset::Ident& idi)
113  {
114  AssetManager& _aMang (AssetManager::instance());
115  DB& registry (_aMang.registry);
117  ID<KIND> asset_id (getID (idi));
118 
119  DB::Lock guard{&registry};
121  lib::P<KIND> smart_ptr (obj, &destroy);
122 
123  registry.put (asset_id, smart_ptr);
124  return asset_id;
125  }
126 
127 
132  template<class KIND>
134  AssetManager::getAsset (const ID<KIND>& id)
135  {
136  if (lib::P<KIND> obj = registry.get (id))
137  return obj;
138  else
139  if (known (id)) // provide Ident tuple of existing Asset
140  throw WrongKind (registry.get(ID<Asset>(id))->ident);
141  else
142  throw UnknownID (id);
143  }
144 
151  template<class KIND>
153  AssetManager::wrap (const KIND& asset)
154  {
155  ENSURE (instance().known(asset.id),
156  "unregistered asset instance encountered.");
157  return static_pointer_cast<KIND,Asset>
158  (instance().registry.get (asset.id));
159  }
160 
161 
162 
166  bool
167  AssetManager::known (IDA id)
168  {
169  return bool(registry.get (ID<Asset>(id)));
170  } // query most general Asset ID-kind and use implicit
171  // conversion from smart-ptr to bool (test if empty)
172 
173 
177  bool
178  AssetManager::known (IDA id, const Category& cat)
179  {
180  PAsset pA = registry.get (id);
181  return ( pA && pA->ident.category.isWithin(cat));
182  }
183 
184 
185  namespace { // details implementing AssetManager::remove
186 
187  void
188  recursive_call (AssetManager* instance, PAsset& pA)
189  {
190  instance->remove (pA->getID());
191  }
192 
193  function<void(PAsset&)>
194  detach_child_recursively ()
195  {
196  return bind( &recursive_call, &AssetManager::instance(), _1 );
197  }
198  }
199 
204  void
205  AssetManager::remove (IDA id)
206  {
207  PAsset asset = getAsset (id);
208  for_each (asset->dependants, detach_child_recursively());
209  asset->unlink();
210  registry.del(id);
211  }
212 
213 
214  void
215  AssetManager::clear()
216  {
217  INFO (progress, "Clearing the Asset registry...");
218  registry.clear();
219  }
220 
221 
222  list<PcAsset>
223  AssetManager::listContent() const
224  {
225  list<PcAsset> res;
226  registry.asList (res);
227  res.sort();
228  return res;
229  }
230 
231 
232 }} // namespace steam::asset
233 
234 
235 
236 
237  /************************************************************/
238  /* explicit template instantiations for various Asset Kinds */
239  /************************************************************/
240 
241 #include "steam/asset/media.hpp"
242 #include "steam/asset/clip.hpp"
243 #include "steam/asset/proc.hpp"
244 #include "steam/asset/struct.hpp"
245 #include "steam/asset/pipe.hpp"
246 #include "steam/asset/meta.hpp"
247 #include "steam/asset/procpatt.hpp"
248 #include "steam/asset/timeline.hpp"
249 #include "steam/asset/sequence.hpp"
252 
253 namespace steam {
254 namespace asset {
255  using lib::P;
256 
257  template ID<Asset> AssetManager::reg (Asset* obj, const Asset::Ident& idi);
258 
259 
260  template P<Asset> AssetManager::getAsset (const ID<Asset>& id);
261  template P<Media> AssetManager::getAsset (const ID<Media>& id);
262  template P<Proc> AssetManager::getAsset (const ID<Proc>& id);
263  template P<Struct> AssetManager::getAsset (const ID<Struct>& id);
264  template P<Meta> AssetManager::getAsset (const ID<Meta>& id);
265  template P<Pipe> AssetManager::getAsset (const ID<Pipe>& id);
266 
267  template P<Asset> AssetManager::wrap (const Asset& asset);
268  template P<Media> AssetManager::wrap (const Media& asset);
269  template P<Clip> AssetManager::wrap (const Clip& asset);
270  template P<Pipe> AssetManager::wrap (const Pipe& asset);
271  template P<ProcPatt> AssetManager::wrap (const ProcPatt& asset);
272  template P<Timeline> AssetManager::wrap (const Timeline& asset);
273  template P<Sequence> AssetManager::wrap (const Sequence& asset);
274 
275  using meta::TimeGrid;
276  using meta::ErrorLog;
277  template P<TimeGrid> AssetManager::wrap (const TimeGrid& asset);
278  template P<ErrorLog> AssetManager::wrap (const ErrorLog& asset);
279 
280 
281 }} // namespace steam::asset
Facility for monitor object based locking.
Definition: sync.hpp:209
Interface: a grid and scale definition for time quantisation.
Definition: time-grid.hpp:77
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:52
TODO type comment.
Definition: sequence.hpp:83
key abstraction: media-like assets
Definition: media.hpp:63
Media data represented a specific kind of Asset.
Tree like classification of Assets.
Definition: category.hpp:66
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:226
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:280
structural asset corresponding to the part of the model forming a processing pipe for generating medi...
Definition: pipe.hpp:70
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:89
An entity to collect, possibly filter and persist incident records.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
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:149
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:139
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:35
Lumiera public interface.
Definition: advice.cpp:104
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:71
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:35
Receive, collect, filter and possibly persist incident records.
Definition: error-log.hpp:65
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:71