tahoma2d/toonz/sources/common/tcore/tidentifiable.cpp

88 lines
2.2 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tidentifiable.h"
#include <set>
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
class IdentifierTable { // singleton
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
unsigned long m_lastId;
std::map<unsigned long, TIdentifiable *> m_table;
std::set<TIdentifiable *> m_objects;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
IdentifierTable() : m_lastId(0) {}
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
static IdentifierTable *instance() {
// NON DEVE MORIRE
// static IdentifierTable _instance;
// return &_instance;
static IdentifierTable *_instance = 0;
if (!_instance) _instance = new IdentifierTable;
return _instance;
}
unsigned long getNextId() { return ++m_lastId; }
void insert(TIdentifiable *o) {
unsigned long id = o->getIdentifier();
std::map<unsigned long, TIdentifiable *>::iterator it = m_table.find(id);
if (it != m_table.end()) {
if (it->second == o) return;
m_objects.erase(it->second);
it->second = o;
} else {
m_table[id] = o;
}
m_objects.insert(o);
}
void erase(TIdentifiable *o) {
unsigned long id = o->getIdentifier();
m_table.erase(id);
m_objects.erase(o);
}
TIdentifiable *fetch(unsigned long id) {
std::map<unsigned long, TIdentifiable *>::iterator it = m_table.find(id);
return it == m_table.end() ? 0 : it->second;
}
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TIdentifiable::TIdentifiable() : m_id(0) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TIdentifiable::~TIdentifiable() {
if (m_id != 0) IdentifierTable::instance()->erase(this);
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
TIdentifiable::TIdentifiable(const TIdentifiable &src) : m_id(src.m_id) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TIdentifiable &TIdentifiable::operator=(const TIdentifiable &src) {
if (src.m_id != m_id && m_id != 0) IdentifierTable::instance()->erase(this);
m_id = src.m_id;
return *this;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void TIdentifiable::setIdentifier(unsigned long id) {
bool wasStored = m_id > 0 && IdentifierTable::instance()->fetch(m_id) == this;
if (m_id != id && m_id != 0) IdentifierTable::instance()->erase(this);
m_id = id;
if (wasStored) IdentifierTable::instance()->insert(this);
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void TIdentifiable::setNewIdentifier() {
setIdentifier(IdentifierTable::instance()->getNextId());
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void TIdentifiable::storeByIdentifier() {
assert(getIdentifier() >= 1);
IdentifierTable::instance()->insert(this);
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
TIdentifiable *TIdentifiable::fetchByIdentifier(unsigned long id) {
return IdentifierTable::instance()->fetch(id);
2016-03-19 06:57:51 +13:00
}