Lumiera  0.pre.03
»edit your freedom«
asset.cpp
Go to the documentation of this file.
1 /*
2  Asset - Superinterface: bookkeeping view of "things" present in the session
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/asset.hpp"
21 #include "steam/assetmanager.hpp"
22 #include "lib/format-string.hpp"
23 #include "lib/util-foreach.hpp"
24 #include "lib/util.hpp"
25 
26 #include <functional>
27 #include <string>
28 
29 
30 using std::function;
31 using std::placeholders::_1;
32 using std::bind;
33 using util::contains;
34 using util::removeall;
35 using util::for_each;
36 using util::and_all;
37 using util::isnil;
38 using util::_Fmt;
39 
40 
41 namespace steam {
42 namespace asset {
43 
44 
45 
46  Asset::Ident::Ident(const string& n, const Category& cat, const string& o, const uint ver)
47  : name(util::sanitise (n))
48  , category(cat)
49  , org(o)
50  , version(ver)
51  { }
52 
53 
57  Asset::Asset (Ident const& idi)
58  : ident(idi)
59  , id(AssetManager::reg (this, idi))
60  , enabled(true)
61  {
62  TRACE (asset_mem, "ctor Asset(id=%zu) : adr=%p %s", size_t(id), this, cStr(this->ident) );
63  }
64 
66  {
67  TRACE (asset_mem, "dtor Asset(id=%zu) : adr=%p", size_t(id), this );
68  }
69 
70 
71  Asset::Ident::operator string () const
72  {
73  return string (_Fmt("(%2%:%3%.%1% v%4%)")
74  % name
75  % category
76  % org
77  % version);
78  }
79 
80 
81  Asset::operator string () const
82  {
83  return string (_Fmt("Asset(%2%:%3%.%1% v%4%)")
84  % ident.name
85  % ident.category
86  % ident.org
87  % ident.version);
88  }
89 
90 
91  bool
92  Asset::Ident::isValid() const
93  {
94  return not isnil (name)
95  and not isnil (org)
96  and version <= 1000000;
97  }
98 
99 
100 
101 
102  function<bool(const PAsset&)> check_isActive
103  = bind ( &Asset::isActive
104  , bind (&PAsset::get, _1 )
105  );
106 
107  bool
108  all_parents_enabled (const vector<PAsset>& parents)
109  {
110  return and_all (parents, check_isActive);
111  }
112 
117  bool
119  {
120  return this->enabled
121  and all_parents_enabled (parents);
122  }
123 
124 
125  void
126  propagate_down (PAsset child, bool on)
127  {
128  child->enable(on);
129  }
130 
132  bool
133  Asset::enable (bool on)
134  {
135  if (on == this->enabled)
136  return true;
137  if (on and not all_parents_enabled (parents))
138  return false;
139 
140  // can indeed to do the toggle...
141  this->enabled = on;
142  for_each (dependants, &propagate_down, _1 ,on);
143  return true;
144  }
145 
146 
147 
148 
149  void
151  {
152  other->unlink (this->id);
153  }
154 
163  void
165  {
166  function<void(PAsset&)> forget_me = bind(&Asset::unregister, this, _1);
167 
168  for_each (parents, forget_me);
169  dependants.clear();
170  }
171 
173  void
175  {
176  PAsset asset (AssetManager::instance().getAsset (target));
177  removeall (dependants,asset);
178  removeall (parents,asset);
179  }
180 
181 
182  void
184  {
185  PAsset p_this (AssetManager::wrap(*this));
186  REQUIRE (!contains (parent->dependants, p_this));
187  REQUIRE (!contains (this->parents, parent));
188  parents.push_back (parent);
189  parent->dependants.push_back(p_this);
190  }
191 
192  void
194  {
195  PAsset p_parent (AssetManager::wrap(parent));
196  ASSERT (p_parent);
197  defineDependency (p_parent);
198  }
199 
200 
201 }} // namespace steam::asset
virtual void unlink()
release all links to other Asset objects held internally.
Definition: asset.cpp:164
virtual ~Asset()=0
Definition: asset.cpp:65
string name
element ID, comprehensible but sanitised.
Definition: asset.hpp:155
Steam-Layer Interface: Asset Lookup and Organisation.
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:59
bool isActive() const
weather this asset is switched on and consequently included in the fixture and participates in render...
Definition: asset.cpp:118
Front-end for printf-style string template interpolation.
const Ident ident
Asset identification tuple.
Definition: asset.hpp:199
std::string sanitise(std::string const &)
produce an identifier based on the given string.
Definition: util.cpp:56
bool and_all(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
Facade for the Asset subsystem.
Steam-Layer implementation namespace root.
A front-end for using printf-style formatting.
static lib::Depend< AssetManager > instance
get at the system-wide asset manager instance.
bool enable(bool on=true)
change the enabled status of this asset.
Definition: asset.cpp:133
a POD comprised of all the information sufficiently identifying any given Asset.
Definition: asset.hpp:149
void unregister(PAsset &other)
Definition: asset.cpp:150
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:139
Steam-Layer Interface: Assets.
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
const string org
origin or authorship id.
Definition: asset.hpp:167
static lib::P< KIND > wrap(const KIND &asset)
retrieve the registered smart-ptr for any asset
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:71
SEQ::iterator removeall(SEQ &coll, typename SEQ::value_type const &val)
shortcut for removing all copies of an Element in any sequential collection
Definition: util.hpp:306
void defineDependency(PAsset parent)
establish a connection between this and the given parent asset, denoting we are in some way dependent...
Definition: asset.cpp:183
const uint version
version number of the thing or concept represented by this asset.
Definition: asset.hpp:175
asset::Category category
primary tree like classification of the asset.
Definition: asset.hpp:160
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:35
Perform operations "for each element" of a collection.
ElementBoxWidget::Config::Qualifier name(string id)
define the name-ID displayed in the caption
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255