00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "proc/asset.hpp"
00025 #include "proc/assetmanager.hpp"
00026 #include "common/util.hpp"
00027
00028 #include <boost/function.hpp>
00029 #include <boost/format.hpp>
00030 #include <boost/bind.hpp>
00031
00032
00033 using boost::bind;
00034 using boost::format;
00035 using boost::function;
00036 using util::contains;
00037 using util::removeall;
00038 using util::for_each;
00039 using util::and_all;
00040 using util::cStr;
00041
00042
00043 namespace asset
00044 {
00045
00046 Asset::Ident::Ident(const string& n, const Category& cat, const string& o, const uint ver)
00047 : name(util::sanitize (n)),
00048 category(cat), org(o), version(ver)
00049 { }
00050
00051
00055 Asset::Asset (const Ident& idi)
00056 : ident(idi), id(AssetManager::reg (this, idi)), enabled(true)
00057 {
00058 TRACE (assetmem, "ctor Asset(id=%lu) : adr=%x %s", size_t(id), this, cStr(this->ident) );
00059 }
00060
00061 Asset::~Asset ()
00062 {
00063 TRACE (assetmem, "dtor Asset(id=%lu) : adr=%x", size_t(id), this );
00064 }
00065
00066
00067 Asset::Ident::operator string () const
00068 {
00069 format id_tuple("(%2%:%3%.%1% v%4%)");
00070 return str (id_tuple % name % category % org % version);
00071 }
00072
00073
00074 Asset::operator string () const
00075 {
00076 format id_tuple("Asset(%2%:%3%.%1% v%4%)");
00077 return str (id_tuple % ident.name % ident.category % ident.org % ident.version);
00078 }
00079
00080
00081
00082
00083 function<bool(const PAsset&)> check_isActive
00084 = bind ( &Asset::isActive
00085 , bind (&PAsset::get, _1 )
00086 );
00087
00088 bool
00089 all_parents_enabled (const vector<PAsset>& parents)
00090 {
00091 return and_all (parents, check_isActive);
00092 }
00093
00098 bool
00099 Asset::isActive () const
00100 {
00101 return this->enabled
00102 && all_parents_enabled (parents);
00103 }
00104
00105
00106 void
00107 propagate_down (PAsset child, bool on)
00108 {
00109 child->enable(on);
00110 }
00111
00113 bool
00114 Asset::enable (bool on) throw(lumiera::error::State)
00115 {
00116 if (on == this->enabled)
00117 return true;
00118 if (on && !all_parents_enabled (parents))
00119 return false;
00120
00121
00122 this->enabled = on;
00123 for_each (dependants, bind (&propagate_down, _1 ,on));
00124 return true;
00125 }
00126
00127
00128
00129
00130 void
00131 Asset::unregister (PAsset& other)
00132 {
00133 other->unlink (this->id);
00134 }
00135
00144 void
00145 Asset::unlink ()
00146 {
00147 function<void(PAsset&)> forget_me = bind(&Asset::unregister, this,_1);
00148
00149 for_each (parents, forget_me);
00150 dependants.clear();
00151 }
00152
00154 void
00155 Asset::unlink (IDA target)
00156 {
00157 PAsset asset (AssetManager::instance().getAsset (target));
00158 removeall (dependants,asset);
00159 removeall (parents,asset);
00160 }
00161
00162
00163 void
00164 Asset::defineDependency (PAsset parent)
00165 {
00166 PAsset p_this (AssetManager::wrap(*this));
00167 REQUIRE (!contains (parent->dependants, p_this));
00168 REQUIRE (!contains (this->parents, parent));
00169 parents.push_back (parent);
00170 parent->dependants.push_back(p_this);
00171 }
00172
00173 void
00174 Asset::defineDependency (Asset& parent)
00175 {
00176 PAsset p_parent (AssetManager::wrap(parent));
00177 ASSERT (p_parent);
00178 defineDependency (p_parent);
00179 }
00180
00181
00182 }