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) 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/asset.hpp"
30 #include "steam/assetmanager.hpp"
31 #include "lib/format-string.hpp"
32 #include "lib/util-foreach.hpp"
33 #include "lib/util.hpp"
34 
35 #include <functional>
36 #include <string>
37 
38 
39 using std::function;
40 using std::placeholders::_1;
41 using std::bind;
42 using util::contains;
43 using util::removeall;
44 using util::for_each;
45 using util::and_all;
46 using util::isnil;
47 using util::_Fmt;
48 
49 
50 namespace steam {
51 namespace asset {
52 
53 
54 
55  Asset::Ident::Ident(const string& n, const Category& cat, const string& o, const uint ver)
56  : name(util::sanitise (n))
57  , category(cat)
58  , org(o)
59  , version(ver)
60  { }
61 
62 
66  Asset::Asset (Ident const& idi)
67  : ident(idi)
68  , id(AssetManager::reg (this, idi))
69  , enabled(true)
70  {
71  TRACE (asset_mem, "ctor Asset(id=%zu) : adr=%p %s", size_t(id), this, cStr(this->ident) );
72  }
73 
75  {
76  TRACE (asset_mem, "dtor Asset(id=%zu) : adr=%p", size_t(id), this );
77  }
78 
79 
80  Asset::Ident::operator string () const
81  {
82  return string (_Fmt("(%2%:%3%.%1% v%4%)")
83  % name
84  % category
85  % org
86  % version);
87  }
88 
89 
90  Asset::operator string () const
91  {
92  return string (_Fmt("Asset(%2%:%3%.%1% v%4%)")
93  % ident.name
94  % ident.category
95  % ident.org
96  % ident.version);
97  }
98 
99 
100  bool
101  Asset::Ident::isValid() const
102  {
103  return not isnil (name)
104  and not isnil (org)
105  and version <= 1000000;
106  }
107 
108 
109 
110 
111  function<bool(const PAsset&)> check_isActive
112  = bind ( &Asset::isActive
113  , bind (&PAsset::get, _1 )
114  );
115 
116  bool
117  all_parents_enabled (const vector<PAsset>& parents)
118  {
119  return and_all (parents, check_isActive);
120  }
121 
126  bool
128  {
129  return this->enabled
130  and all_parents_enabled (parents);
131  }
132 
133 
134  void
135  propagate_down (PAsset child, bool on)
136  {
137  child->enable(on);
138  }
139 
141  bool
142  Asset::enable (bool on)
143  {
144  if (on == this->enabled)
145  return true;
146  if (on and not all_parents_enabled (parents))
147  return false;
148 
149  // can indeed to do the toggle...
150  this->enabled = on;
151  for_each (dependants, &propagate_down, _1 ,on);
152  return true;
153  }
154 
155 
156 
157 
158  void
160  {
161  other->unlink (this->id);
162  }
163 
172  void
174  {
175  function<void(PAsset&)> forget_me = bind(&Asset::unregister, this, _1);
176 
177  for_each (parents, forget_me);
178  dependants.clear();
179  }
180 
182  void
184  {
185  PAsset asset (AssetManager::instance().getAsset (target));
186  removeall (dependants,asset);
187  removeall (parents,asset);
188  }
189 
190 
191  void
193  {
194  PAsset p_this (AssetManager::wrap(*this));
195  REQUIRE (!contains (parent->dependants, p_this));
196  REQUIRE (!contains (this->parents, parent));
197  parents.push_back (parent);
198  parent->dependants.push_back(p_this);
199  }
200 
201  void
203  {
204  PAsset p_parent (AssetManager::wrap(parent));
205  ASSERT (p_parent);
206  defineDependency (p_parent);
207  }
208 
209 
210 }} // namespace steam::asset
virtual void unlink()
release all links to other Asset objects held internally.
Definition: asset.cpp:173
virtual ~Asset()=0
Definition: asset.cpp:74
string name
element ID, comprehensible but sanitised.
Definition: asset.hpp:164
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:68
bool isActive() const
weather this asset is switched on and consequently included in the fixture and participates in render...
Definition: asset.cpp:127
Front-end for printf-style string template interpolation.
const Ident ident
Asset identification tuple.
Definition: asset.hpp:208
std::string sanitise(std::string const &)
produce an identifier based on the given string.
Definition: util.cpp:65
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:142
a POD comprised of all the information sufficiently identifying any given Asset.
Definition: asset.hpp:158
void unregister(PAsset &other)
Definition: asset.cpp:159
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:148
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:176
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:80
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:192
const uint version
version number of the thing or concept represented by this asset.
Definition: asset.hpp:184
asset::Category category
primary tree like classification of the asset.
Definition: asset.hpp:169
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:44
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