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
20 waker@users.sourceforge.net
23 #ifndef __F_RESOURCETABLE_H
24 #define __F_RESOURCETABLE_H
27 #include "f_helpers.h"
28 #include "f_baseobject.h"
29 #include "f_resourcemgr.h"
35 template <typename T
> class resource
40 // FIXME: do it without stl.. some hashtable or whatever
41 typedef std::map
<cStr
, T
*> resourceMap
;
42 resourceMap mResourceList
;
46 resource (void) // this will be the owner of all created objects
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
++)
63 T
* create (const char *name
)
65 T
*o
= getResource (name
);
74 addResource (name
, o
);
79 T
* create (charParser
&parser
)
82 cStr name
= parser
.token ();
83 T
*o
= getResource (name
);
86 fprintf (stderr
, "WARNING: object with name %s is already defined\n", name
.c_str ());
89 T
*obj
= new T (parser
, name
);
95 addResource (name
, 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 ())
127 int getCount (void) const
129 return (int)mResourceList
.size ();
132 T
* getObject (int idx
) const
134 typename
resourceMap::const_iterator it
;
136 for (it
= mResourceList
.begin (); it
!= mResourceList
.end (); it
++, i
++)
146 void addResource (const char *name
, T
*r
)
148 mResourceList
[name
] = r
;
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
);
163 fprintf (stderr
, "WARNING: cannot delete '%s' - no such resource\n", name
);
170 #endif // __F_RESOURCETABLE_H