]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.h
2d346d9a4557a72325a18ae6f89c232e5f629681
[libs/datafile.git] / source / collection.h
1 #ifndef MSP_DATAFILE_COLLECTION_H_
2 #define MSP_DATAFILE_COLLECTION_H_
3
4 #include <msp/core/meta.h>
5 #include <msp/core/maputils.h>
6 #include <msp/core/refptr.h>
7 #include "loader.h"
8
9 namespace Msp {
10 namespace DataFile {
11
12 /**
13 Helper struct to determine whether a Loader has a Collection typedef.
14 */
15 template<typename T>
16 struct NeedsCollection
17 {
18         struct Yes { char c[2]; };
19         struct No { char c; };
20
21         template<typename U>
22         static Yes f(typename U::Collection *);
23         template<typename U>
24         static No f(...);
25
26         enum { result=(sizeof(f<T>(0))==sizeof(Yes)) };
27 };
28
29 /**
30 A collection of objects that can be loaded from a datafile.  Each object is
31 identified by a name, which must be unique across the entire collection.
32 */
33 class Collection
34 {
35 public:
36         class Loader;
37
38 private:
39         /* XXX I don't really like sticking all this stuff in here, but there's some
40            complex inter-class relationships, especially between ItemKeyword and
41            Collection::Loader. */
42
43         struct ItemBase
44         {
45                 virtual ~ItemBase() { }
46         };
47
48         template<typename T>
49         struct Item: public ItemBase
50         {
51                 T *data;
52
53                 Item(T *d): data(d) { }
54                 ~Item() { delete data; }
55         };
56
57         /**
58         Used to store keywords for types that can be loaded.
59         */
60         struct ItemKeywordBase
61         {
62                 virtual ~ItemKeywordBase() { }
63                 virtual void add_to_loader(Loader &) const { };
64         };
65
66         template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
67         struct ItemKeyword: public ItemKeywordBase
68         {
69                 std::string keyword;
70
71                 ItemKeyword(const std::string &kw): keyword(kw) { }
72
73                 void add_to_loader(Loader &ldr) const
74                 { ldr.add(keyword, &Loader::item<T, S>); }
75         };
76
77         template<typename T, typename S>
78         struct ItemKeyword<T, S, true>: public ItemKeywordBase
79         {
80                 std::string keyword;
81
82                 ItemKeyword(const std::string &kw): keyword(kw) { }
83
84                 virtual void add_to_loader(Loader &ldr) const
85                 { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
86         };
87
88         /**
89         Used to store types that can be created automatically.
90         */
91         struct ItemCreatorBase
92         {
93                 virtual ~ItemCreatorBase() { }
94
95                 template<typename S>
96                 bool create(Collection &coll, const std::string &name, S *&ptr)
97                 {
98                         ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
99                         if(creator)
100                         {
101                                 ptr=creator->create(coll, name);
102                                 return true;
103                         }
104                         return false;
105                 }
106         };
107
108         template<typename S>
109         struct ItemCreatorBridge: public ItemCreatorBase
110         {
111                 virtual S *create(Collection &, const std::string &) const = 0;
112         };
113
114         template<typename T, typename S, typename C>
115         struct ItemCreator: public ItemCreatorBridge<S>
116         {
117                 typedef T *(C::*fCreate)(const std::string &);
118
119                 fCreate create_func;
120
121                 ItemCreator(fCreate cf): create_func(cf) { }
122                 virtual S *create(Collection &coll, const std::string &name) const
123                 { return (dynamic_cast<C &>(coll).*create_func)(name); }
124         };
125
126 public:
127         /**
128         Loads objects into a Collection.
129         */
130         class Loader: public DataFile::Loader
131         {
132         private:
133                 Collection &coll;
134
135         public:
136                 Loader(Collection &);
137                 Collection &get_object() const { return coll; }
138         private:
139                 template<typename T, typename S, typename C>
140                 void coll_item(const std::string &n)
141                 {
142                         RefPtr<T> it=new T;
143                         load_sub(*it, dynamic_cast<C &>(coll));
144                         coll.add<S>(n, it.get());
145                         it.release();
146                 }
147
148                 template<typename T, typename S>
149                 void item(const std::string &n)
150                 {
151                         RefPtr<T> it=new T;
152                         load_sub(*it);
153                         coll.add<S>(n, it.get());
154                         it.release();
155                 }
156
157                 template<typename, typename, bool> friend class ItemKeyword;
158         };
159
160 private:
161         typedef std::map<std::string, ItemBase *> ItemMap;
162         typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
163         typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
164
165         ItemMap items;
166         ItemKeywordSeq keywords;
167         ItemCreatorSeq creators;
168
169         Collection(const Collection &);
170         Collection &operator=(const Collection &);
171 public:
172         Collection() { }
173         virtual ~Collection();
174
175         /**
176         Adds an object into the collection.  If a name collision occurs, an
177         exception is thrown.  The collection takes ownership of the object.
178         */
179         template<typename T>
180         void add(const std::string &name, T *d)
181         {
182                 typedef typename RemoveConst<T>::Type NCT;
183
184                 RefPtr<Item<NCT> > i=new Item<NCT>(d);
185                 insert_unique(items, i.get());
186                 i.release();
187         }
188
189         /**
190         Gets an object of a specific type from the collection.
191         */
192         template<typename T>
193         T *get(const std::string &name) const
194         {
195                 typedef typename RemoveConst<T>::Type NCT;
196
197                 ItemBase *i=get_item(items, name);
198
199                 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i);
200                 if(!item)
201                         throw TypeError("Type mismatch on item '"+name+"'");
202
203                 return item->data;
204         }
205
206         /**
207         Gets an object of a specific type from the collection.  If the name is not
208         found in the collection and there is a creator for the item type, it is
209         invoked.
210         */
211         template<typename T>
212         T *get(const std::string &name)
213         {
214                 typedef typename RemoveConst<T>::Type NCT;
215
216                 if(!items.count(name))
217                 {
218                         for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
219                         {
220                                 NCT *d=0;
221                                 if((*j)->create(*this, name, d))
222                                 {
223                                         // We already know that the item didn't exist yet
224                                         items[name]=new Item<NCT>(d);
225                                         return d;
226                                 }
227                         }
228                 }
229
230                 ItemBase *i=get_item(items, name);
231
232                 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i);
233                 if(!item)
234                         throw TypeError("Type mismatch on item '"+name+"'");
235
236                 return item->data;
237         }
238
239         /**
240         Returns a list of the names of objects of a specific type in the collection.
241         */
242         template<typename T>
243         std::list<std::string> get_names() const
244         {
245                 std::list<std::string> result;
246                 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
247                         if(dynamic_cast<const Item<typename RemoveConst<T>::Type> *>(i->second))
248                                 result.push_back(i->first);
249                 return result;
250         }
251
252         /**
253         Returns a list of objects of a specific type in the collection.
254         */
255         template<typename T>
256         std::list<T *> get_list() const
257         {
258                 typedef typename RemoveConst<T>::Type NCT;
259
260                 std::list<T *> result;
261                 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
262                         if(Item<NCT> *item=dynamic_cast<Item<NCT> *>(i->second))
263                                 result.push_back(item->data);
264                 return result;
265         }
266
267         /**
268         Checks whether a name exists in the collection.  Does not care about the
269         type of the object.
270         */
271         bool contains(const std::string &n) const;
272
273         /**
274         Returns the name of an item in the collection.
275         */
276         template<typename T>
277         const std::string &get_name(T *d) const
278         {
279                 typedef typename RemoveConst<T>::Type NCT;
280
281                 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
282                         if(Item<NCT> *item=dynamic_cast<Item<NCT> *>(i->second))
283                                 if(item->data==d)
284                                         return i->first;
285         
286                 throw KeyError("Item not found in collection");
287         }
288
289 protected:
290         /**
291         Adds a type that can be loaded from datafiles.
292         */
293         template<typename T>
294         void add_keyword(const std::string &keyword)
295         { add_keyword<T, T>(keyword); }
296
297         /**
298         Adds a type that can be loaded from datafiles, with different storage type.
299         */
300         template<typename T, typename S>
301         void add_keyword(const std::string &keyword)
302         { keywords.push_back(new ItemKeyword<T, S>(keyword)); }
303
304         /**
305         Adds a type that can be created automatically.
306         */
307         template<typename T, typename C>
308         void add_creator(T *(C::*func)(const std::string &))
309         { add_creator<T, T, C>(func); }
310
311         template<typename T, typename S, typename C>
312         void add_creator(T *(C::*func)(const std::string &))
313         { creators.push_back(new ItemCreator<T, S, C>(func)); }
314 };
315
316 } // namespace DataFile
317 } // namespace Msp
318
319 #endif