1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
/* ====================================================================
* Copyright (c) 2006, 2008 Martin Hauner
* http://subcommander.tigris.org
*
* Subcommander is licensed as described in the file doc/COPYING, which
* you should have received as part of this distribution.
* ====================================================================
*/
// sc
#include "TargetRepository.h"
#include "util/Mutex.h"
#include "util/Guard.h"
// sys
#include <map>
#include <cassert>
typedef std::map<unsigned long,QObject*> TargetMap;
typedef std::pair<TargetMap::iterator,bool> ResPair;
class TargetRepositoryData
{
public:
sc::Mutex targetMutex;
TargetMap targetMap;
ID error;
};
static TargetRepositoryData* data = NULL;
void TargetRepository::setup()
{
data = new TargetRepositoryData();
data->error = 0;
}
void TargetRepository::teardown()
{
delete data;
data = NULL;
}
ID TargetRepository::create()
{
return Id::next();
}
void TargetRepository::add( ID tid, QObject* o )
{
sc::Guard<sc::Mutex> guard(data->targetMutex);
ResPair p = data->targetMap.insert( TargetMap::value_type(tid,o) );
if( ! p.second )
{
assert(false);
}
}
QObject* TargetRepository::get( ID tid )
{
sc::Guard<sc::Mutex> guard(data->targetMutex);
TargetMap::iterator it = data->targetMap.find(tid);
if( it != data->targetMap.end() )
{
return (*it).second;
}
else
{
return NULL;
}
}
void TargetRepository::del( ID tid )
{
sc::Guard<sc::Mutex> guard(data->targetMutex);
data->targetMap.erase(tid);
}
void TargetRepository::setError( ID errortid )
{
sc::Guard<sc::Mutex> guard(data->targetMutex);
data->error = errortid;
}
QObject* TargetRepository::getError()
{
return get(data->error);
}
|