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
|
template<class C, typename T>
class shared_structure
{
public:
typedef typename std::shared_ptr<C> ptr;
typedef typename std::map<T, ptr> map_t;
typedef typename std::pair<const T, ptr> pair_t;
typedef typename std::map<T, ptr>::iterator iterator_t;
class does_not_exist
{
public:
does_not_exist() {}
virtual ~does_not_exist() throw() {}
};
class not_loaded
{
public:
not_loaded() {}
virtual ~not_loaded() throw() {}
};
shared_structure(const T& key) {
auto sptr = std::shared_ptr<C>(reinterpret_cast<C*>(this));
structures.insert({key, sptr});
}
virtual ~shared_structure() { }
static void clear() { structures.clear(); }
static const map_t& get() { return structures; }
static ptr get_ptr(const T& key) { return structures.at(key); }
static std::string table_name;
protected:
static map_t structures;
static T find_id(soci::session& sql, const std::string& arg)
{
T id = 0;
sql << "SELECT id FROM `" << T::table_name << "` WHERE `name`='" << arg << "'`", soci::into(id);
return id;
}
}; |