1 #ifndef UGDK_BASE_GENERICCONTAINER_H_
2 #define UGDK_BASE_GENERICCONTAINER_H_
11 #define TOSTRING(X) typeid(X).name()
17 template <
class T,
class Loader_T = T (*) (const std::
string&)>
20 typedef std::map<std::string, T> DataMap;
21 typedef typename DataMap::iterator DataIterator;
30 void Insert(
const std::string& tag, T val) {
31 DataIterator it = database_.find(tag);
32 if(it == database_.end()) {
36 fprintf(stderr,
"UGDK::GenericContainer<%s> Error - Tag '%s' already exists.\n", TOSTRING(T), tag.c_str());
41 void Replace(
const std::string& tag, T val) {
45 bool Exists(
const std::string& tag)
const {
46 return database_.count(tag) > 0;
49 T&
Find(
const std::string& tag) {
50 return database_[tag];
54 virtual T&
Load(
const std::string& filepath,
const std::string& tag) {
63 typedef std::map<std::string, T*>
DataMap;
64 typedef typename DataMap::iterator DataIterator;
65 typedef T* (*Loader_T) (
const std::string&);
75 for(it = database_.begin(); it != database_.end(); ++it) {
80 void Insert(
const std::string& tag, T* val) {
81 DataIterator it = database_.find(tag);
82 if(it == database_.end() || it->second == NULL) {
86 fprintf(stderr,
"UGDK::GenericContainer<%s> Error - Tag '%s' already exists.\n", TOSTRING(T), tag.c_str());
91 void Replace(
const std::string& tag, T* val) {
92 DataIterator it = database_.find(tag);
93 if(it == database_.end() || it->second == NULL) {
101 bool Exists(
const std::string& tag)
const {
102 typename DataMap::const_iterator it = database_.find(tag);
103 return (it != database_.end() && it->second);
106 T*
Find(
const std::string& tag) {
107 return database_[tag];
111 virtual T*
Load(
const std::string& filepath,
const std::string& tag) {
112 if(Exists(tag))
return Find(tag);
113 T* obj = loader_(filepath);
122 #endif // UGDK_BASE_GENERICCONTAINER_H_