00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef ASSET_DB_H
00025 #define ASSET_DB_H
00026
00027
00028 #include "pre_a.hpp"
00029
00030 #include "proc/asset.hpp"
00031 #include "common/error.hpp"
00032
00033 #include <tr1/memory>
00034 #include <tr1/unordered_map>
00035 #include <boost/utility.hpp>
00036
00037
00038 namespace asset
00039 {
00040 using std::tr1::static_pointer_cast;
00041 using std::tr1::dynamic_pointer_cast;
00042
00043
00044
00045
00046 size_t
00047 hash_value (const Asset::Ident& idi)
00048 {
00049 size_t hash = 0;
00050 boost::hash_combine(hash, idi.org);
00051 boost::hash_combine(hash, idi.name);
00052 boost::hash_combine(hash, idi.category);
00053 return hash;
00054 }
00055
00056 size_t
00057 hash_value (const Asset& asset)
00058 {
00059 return asset.getID();
00060 }
00061
00062
00069 struct IdentityHash
00070 : public std::unary_function<size_t, size_t>
00071 {
00072 size_t
00073 operator() (size_t val) const { return val; }
00074 };
00075
00076 typedef std::tr1::unordered_map<size_t, PAsset, IdentityHash> IdHashtable;
00077
00078
00079
00080
00086 class DB : private boost::noncopyable
00087 {
00088 IdHashtable table;
00089
00090 DB () : table() { }
00091 ~DB () {clear();}
00092
00093 friend class lumiera::singleton::StaticCreate<DB>;
00094
00095
00096 public:
00097 template<class KIND>
00098 void put (ID<KIND> hash, P<KIND>& ptr) { table[hash] = static_pointer_cast (ptr); }
00099 void put (ID<Asset> hash, PAsset& ptr) { table[hash] = ptr; }
00100 bool del (ID<Asset> hash) { return table.erase (hash); }
00101
00102 template<class KIND>
00103 P<KIND>
00104 get (ID<KIND> hash) const
00105 {
00106 return dynamic_pointer_cast<KIND,Asset> (find (hash));
00107 }
00108
00119 void
00120 clear () throw()
00121 {
00122 try
00123 {
00124 IdHashtable::iterator i = table.begin();
00125 IdHashtable::iterator e = table.end();
00126 for ( ; i!=e ; ++i )
00127 i->second->dependants.clear();
00128
00129 table.clear();
00130 }
00131 catch (lumiera::Error& EX)
00132 {
00133 WARN (oper, "Problems while clearing Asset registry: %s", EX.what());
00134 }
00135 catch (...)
00136 {
00137 ERROR (oper, "Serious trouble while clearing Asset registry.");
00138 } }
00139
00140
00142 void
00143 asList (list<PcAsset>& output) const
00144 {
00145 IdHashtable::const_iterator i = table.begin();
00146 IdHashtable::const_iterator e = table.end();
00147 for ( ; i!=e ; ++i )
00148 output.push_back (i->second);
00149 }
00150
00151 private:
00152 const PAsset &
00153 find (size_t hash) const
00154 {
00155 static const PAsset NULLP;
00156 IdHashtable::const_iterator i = table.find (hash);
00157 if (i == table.end())
00158 return NULLP;
00159 else
00160 return i->second;
00161 }
00162 };
00163
00164
00165 }
00166 #endif