changed copyright years in source files
[fegdk.git] / core / code / system / f_resourcetable.h
blob5890dabfaa2f60da858375ed697067fc64b29083
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #ifndef __F_RESOURCETABLE_H
24 #define __F_RESOURCETABLE_H
26 #include "f_string.h"
27 #include "f_helpers.h"
28 #include "f_baseobject.h"
29 #include "f_resourcemgr.h"
30 #include "f_error.h"
32 namespace fe
35 template <typename T> class resource
38 private:
40 // FIXME: do it without stl.. some hashtable or whatever
41 typedef std::map <cStr, T*> resourceMap;
42 resourceMap mResourceList;
44 public:
46 resource (void) // this will be the owner of all created objects
50 ~resource (void)
52 typename resourceMap::iterator it;
53 std::vector <baseObject*> lst;
54 for (it = mResourceList.begin (); it != mResourceList.end (); it++)
56 lst.push_back ((baseObject*)(*it).second);
58 size_t sz = lst.size ();
59 for (size_t i = 0; i < sz; i++)
60 lst[i]->release ();
63 T* create (const char *name)
65 T *o = getResource (name);
66 if (!o)
68 o = new T (name);
69 if (!o->isValid ())
71 o->release ();
72 return NULL;
74 addResource (name, o);
76 return o;
79 T* create (charParser &parser)
81 parser.getToken ();
82 cStr name = parser.token ();
83 T *o = getResource (name);
84 if (o)
86 fprintf (stderr, "WARNING: object with name %s is already defined\n", name.c_str ());
87 return o;
89 T *obj = new T (parser, name);
90 if (!obj->isValid ())
92 o->release ();
93 return NULL;
95 addResource (name, obj);
96 return obj;
99 void destroy (const char *name)
101 removeResource (name);
104 void flushUnused (void)
106 typename resourceMap::iterator it;
107 std::vector <T*> lst;
108 for (it = mResourceList.begin (); it != mResourceList.end (); it++)
109 lst.push_back ( (*it).second);
110 size_t sz = lst.size ();
111 for (size_t i = 0; i < sz; i++)
113 if (!lst[i]->isUsed ())
114 lst[i]->release (); // this SHOULD remove it from resource manager
118 T* getResource (const char *name)
120 typename resourceMap::iterator it;
121 it = mResourceList.find (name);
122 if (it != mResourceList.end ())
123 return (*it).second;
124 return NULL;
127 int getCount (void) const
129 return (int)mResourceList.size ();
132 T* getObject (int idx) const
134 typename resourceMap::const_iterator it;
135 int i = 0;
136 for (it = mResourceList.begin (); it != mResourceList.end (); it++, i++)
138 if (i == idx)
140 return (*it).second;
143 return NULL;
146 void addResource (const char *name, T *r)
148 mResourceList[name] = r;
149 r->addRef ();
152 void removeResource (const char *name)
154 typename resourceMap::iterator it;
155 it = mResourceList.find (name);
156 if (it != mResourceList.end ())
158 (*it).second->release ();
159 mResourceList.erase (it);
161 else
163 fprintf (stderr, "WARNING: cannot delete '%s' - no such resource\n", name);
170 #endif // __F_RESOURCETABLE_H