2007-09-07 23:24:13 +02:00
|
|
|
/*
|
|
|
|
|
DB.hpp - registry holding known Asset instances.
|
|
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2007-09-07 23:24:13 +02:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ASSET_DB_H
|
|
|
|
|
#define ASSET_DB_H
|
|
|
|
|
|
|
|
|
|
|
2008-04-12 04:55:18 +02:00
|
|
|
#include "pre_a.hpp"
|
|
|
|
|
|
2007-09-07 23:24:13 +02:00
|
|
|
#include "proc/asset.hpp"
|
2007-11-22 06:26:55 +01:00
|
|
|
#include "common/error.hpp"
|
2007-09-07 23:24:13 +02:00
|
|
|
|
2008-04-05 07:26:54 +02:00
|
|
|
#include <tr1/memory>
|
2007-09-07 23:24:13 +02:00
|
|
|
#include <tr1/unordered_map>
|
|
|
|
|
#include <boost/utility.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace asset
|
|
|
|
|
{
|
2007-09-16 03:02:05 +02:00
|
|
|
using std::tr1::static_pointer_cast;
|
|
|
|
|
using std::tr1::dynamic_pointer_cast;
|
|
|
|
|
|
2007-09-07 23:24:13 +02:00
|
|
|
|
|
|
|
|
/* ===== hash implementations ===== */
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
|
hash_value (const Asset::Ident& idi)
|
|
|
|
|
{
|
|
|
|
|
size_t hash = 0;
|
|
|
|
|
boost::hash_combine(hash, idi.org);
|
|
|
|
|
boost::hash_combine(hash, idi.name);
|
|
|
|
|
boost::hash_combine(hash, idi.category);
|
|
|
|
|
return hash;
|
2007-09-16 03:02:05 +02:00
|
|
|
}
|
2007-09-07 23:24:13 +02:00
|
|
|
|
2007-09-16 03:02:05 +02:00
|
|
|
size_t
|
2007-09-07 23:24:13 +02:00
|
|
|
hash_value (const Asset& asset)
|
|
|
|
|
{
|
2007-09-10 06:45:36 +02:00
|
|
|
return asset.getID();
|
2007-09-07 23:24:13 +02:00
|
|
|
}
|
2007-09-16 03:02:05 +02:00
|
|
|
|
2007-09-07 23:24:13 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* trivial hash functor.
|
|
|
|
|
* returns any hash value unmodified.
|
|
|
|
|
* For building a hashtable with keys
|
|
|
|
|
* already containing valid hash values.
|
|
|
|
|
*/
|
|
|
|
|
struct IdentityHash
|
|
|
|
|
: public std::unary_function<size_t, size_t>
|
|
|
|
|
{
|
|
|
|
|
size_t
|
|
|
|
|
operator() (size_t val) const { return val; }
|
|
|
|
|
};
|
2007-09-16 03:02:05 +02:00
|
|
|
|
2007-09-07 23:24:13 +02:00
|
|
|
typedef std::tr1::unordered_map<size_t, PAsset, IdentityHash> IdHashtable;
|
2007-09-16 03:02:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-07 23:24:13 +02:00
|
|
|
/**
|
|
|
|
|
* Implementation of the registry holding all Asset
|
|
|
|
|
* instances known to the Asset Manager subsystem.
|
|
|
|
|
* As of 8/2007 implemented by a hashtable.
|
|
|
|
|
*/
|
|
|
|
|
class DB : private boost::noncopyable
|
|
|
|
|
{
|
|
|
|
|
IdHashtable table;
|
|
|
|
|
|
2007-11-22 06:26:55 +01:00
|
|
|
DB () : table() { }
|
|
|
|
|
~DB () {clear();}
|
2007-09-07 23:24:13 +02:00
|
|
|
|
2008-03-10 06:09:44 +01:00
|
|
|
friend class lumiera::singleton::StaticCreate<DB>;
|
2007-09-16 03:02:05 +02:00
|
|
|
|
2007-09-18 05:16:56 +02:00
|
|
|
|
2007-09-16 03:02:05 +02:00
|
|
|
public:
|
|
|
|
|
template<class KIND>
|
2008-04-05 07:26:54 +02:00
|
|
|
void put (ID<KIND> hash, P<KIND>& ptr) { table[hash] = static_pointer_cast (ptr); }
|
|
|
|
|
void put (ID<Asset> hash, PAsset& ptr) { table[hash] = ptr; }
|
|
|
|
|
bool del (ID<Asset> hash) { return table.erase (hash); }
|
2007-09-16 03:02:05 +02:00
|
|
|
|
|
|
|
|
template<class KIND>
|
2008-05-19 08:46:19 +02:00
|
|
|
P<KIND>
|
2007-09-18 05:16:56 +02:00
|
|
|
get (ID<KIND> hash) const
|
2007-09-16 03:02:05 +02:00
|
|
|
{
|
2007-09-18 05:16:56 +02:00
|
|
|
return dynamic_pointer_cast<KIND,Asset> (find (hash));
|
2007-09-16 03:02:05 +02:00
|
|
|
}
|
2007-09-17 05:47:22 +02:00
|
|
|
|
2007-11-23 04:37:50 +01:00
|
|
|
/** removes all registered assets and does something similar
|
|
|
|
|
* to Asset::unlink() on each to break cyclic dependencies
|
|
|
|
|
* (we can't use the real unlink()-function, because this
|
|
|
|
|
* will propagate, including calls to the AssetManager.
|
|
|
|
|
* As the destructor of DB needs to call clear(), this
|
|
|
|
|
* could result in segfaults. This doesn't seem to be
|
|
|
|
|
* a problem, though, because we register and process
|
2008-02-22 04:58:37 +01:00
|
|
|
* \e all assets and the net effect is just breaking
|
2007-11-23 04:37:50 +01:00
|
|
|
* any cyclic dependencies)
|
2007-11-22 06:26:55 +01:00
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
clear () throw()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2008-02-10 17:23:16 +01:00
|
|
|
IdHashtable::iterator i = table.begin();
|
|
|
|
|
IdHashtable::iterator e = table.end();
|
2007-11-23 04:37:50 +01:00
|
|
|
for ( ; i!=e ; ++i )
|
|
|
|
|
i->second->dependants.clear();
|
2007-11-22 06:26:55 +01:00
|
|
|
|
|
|
|
|
table.clear();
|
|
|
|
|
}
|
2008-03-10 06:09:44 +01:00
|
|
|
catch (lumiera::Error& EX)
|
2007-11-22 06:26:55 +01:00
|
|
|
{
|
|
|
|
|
WARN (oper, "Problems while clearing Asset registry: %s", EX.what());
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
ERROR (oper, "Serious trouble while clearing Asset registry.");
|
|
|
|
|
} }
|
|
|
|
|
|
2007-09-18 05:16:56 +02:00
|
|
|
|
2007-09-17 05:47:22 +02:00
|
|
|
/** intended for diagnostics */
|
|
|
|
|
void
|
2007-11-26 04:27:27 +01:00
|
|
|
asList (list<PcAsset>& output) const
|
2007-09-17 05:47:22 +02:00
|
|
|
{
|
|
|
|
|
IdHashtable::const_iterator i = table.begin();
|
|
|
|
|
IdHashtable::const_iterator e = table.end();
|
|
|
|
|
for ( ; i!=e ; ++i )
|
|
|
|
|
output.push_back (i->second);
|
|
|
|
|
}
|
2007-09-18 05:16:56 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const PAsset &
|
|
|
|
|
find (size_t hash) const
|
|
|
|
|
{
|
|
|
|
|
static const PAsset NULLP;
|
|
|
|
|
IdHashtable::const_iterator i = table.find (hash);
|
|
|
|
|
if (i == table.end())
|
|
|
|
|
return NULLP; // empty ptr signaling "not found"
|
|
|
|
|
else
|
|
|
|
|
return i->second;
|
|
|
|
|
}
|
2007-09-07 23:24:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace asset
|
|
|
|
|
#endif
|