3 This file is part of libmspdatafile
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_DATAFILE_COLLECTION_H_
9 #define MSP_DATAFILE_COLLECTION_H_
11 #include <msp/core/meta.h>
12 #include <msp/core/refptr.h>
19 Helper struct to determine whether a Loader has a Collection typedef.
22 struct NeedsCollection
24 struct Yes { char c[2]; };
25 struct No { char c; };
28 static Yes f(typename U::Collection *);
32 enum { result=(sizeof(f<T>(0))==sizeof(Yes)) };
36 A collection of objects that can be loaded from a datafile. Each object is
37 identified by a name, which must be unique across the entire collection.
45 /* XXX I don't really like sticking all this stuff in here, but there's some
46 complex inter-class relationships, especially between ItemKeyword and
47 Collection::Loader. */
51 virtual ~ItemBase() { }
55 struct Item: public ItemBase
59 Item(T *d): data(d) { }
60 ~Item() { delete data; }
64 Used to store keywords for types that can be loaded.
66 struct ItemKeywordBase
68 virtual ~ItemKeywordBase() { }
69 virtual void add_to_loader(Loader &) const { };
72 template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
73 struct ItemKeyword: public ItemKeywordBase
77 ItemKeyword(const std::string &kw): keyword(kw) { }
79 void add_to_loader(Loader &ldr) const
80 { ldr.add(keyword, &Loader::item<T, S>); }
83 template<typename T, typename S>
84 struct ItemKeyword<T, S, true>: public ItemKeywordBase
88 ItemKeyword(const std::string &kw): keyword(kw) { }
90 virtual void add_to_loader(Loader &ldr) const
91 { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
95 Used to store types that can be created automatically.
97 struct ItemCreatorBase
99 virtual ~ItemCreatorBase() { }
102 bool create(Collection &coll, const std::string &name, S *&ptr)
104 ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
107 ptr=creator->create(coll, name);
115 struct ItemCreatorBridge: public ItemCreatorBase
117 virtual S *create(Collection &, const std::string &) const =0;
120 template<typename T, typename S, typename C>
121 struct ItemCreator: public ItemCreatorBridge<S>
123 typedef T *(C::*fCreate)(const std::string &);
127 ItemCreator(fCreate cf): create_func(cf) { }
128 virtual S *create(Collection &coll, const std::string &name) const
129 { return (dynamic_cast<C &>(coll).*create_func)(name); }
134 Loads objects into a Collection.
136 class Loader: public DataFile::Loader
142 Loader(Collection &);
143 Collection &get_object() const { return coll; }
145 template<typename T, typename S, typename C>
146 void coll_item(const std::string &n)
149 load_sub(*it, dynamic_cast<C &>(coll));
150 coll.add<S>(n, it.get());
154 template<typename T, typename S>
155 void item(const std::string &n)
159 coll.add<S>(n, it.get());
163 template<typename, typename, bool> friend class ItemKeyword;
167 typedef std::map<std::string, ItemBase *> ItemMap;
168 typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
169 typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
172 ItemKeywordSeq keywords;
173 ItemCreatorSeq creators;
175 Collection(const Collection &);
176 Collection &operator=(const Collection &);
179 virtual ~Collection();
182 Adds an object into the collection. If a name collision occurs, an
183 exception is thrown. The collection takes ownership of the object.
186 void add(const std::string &name, T *d)
188 if(items.count(name))
189 throw KeyError("Duplicate key in collection", name);
191 items[name]=new Item<typename RemoveConst<T>::Type>(d);
195 Gets an object of a specific type from the collection.
198 T *get(const std::string &name) const
200 typedef typename RemoveConst<T>::Type NCT;
202 ItemMap::const_iterator i=items.find(name);
204 throw KeyError("Item not found in collection", name);
206 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
208 throw TypeError("Type mismatch on item '"+name+"'");
214 Gets an object of a specific type from the collection. If the name is not
215 found in the collection and there is a creator for the item type, it is
219 T *get(const std::string &name)
221 typedef typename RemoveConst<T>::Type NCT;
223 ItemMap::const_iterator i=items.find(name);
226 for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
229 if((*j)->create(*this, name, d))
231 // We already know that the item didn't exist yet
232 items[name]=new Item<NCT>(d);
236 throw KeyError("Item not found in collection", name);
239 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
241 throw TypeError("Type mismatch on item '"+name+"'");
247 Returns a list of the names of objects of a specific type in the collection.
250 std::list<std::string> get_names() const
252 std::list<std::string> result;
253 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
254 if(dynamic_cast<const Item<typename RemoveConst<T>::Type> *>(i->second))
255 result.push_back(i->first);
260 Returns a list of objects of a specific type in the collection.
263 std::list<T *> get_list() const
265 typedef typename RemoveConst<T>::Type NCT;
267 std::list<T *> result;
268 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
269 if(Item<NCT> *item=dynamic_cast<Item<NCT> *>(i->second))
270 result.push_back(item->data);
275 Checks whether a name exists in the collection. Does not care about the
278 bool contains(const std::string &n) const;
281 Returns the name of an item in the collection.
284 const std::string &get_name(T *d) const
286 typedef typename RemoveConst<T>::Type NCT;
288 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
289 if(Item<NCT> *item=dynamic_cast<Item<NCT> *>(i->second))
293 throw KeyError("Item not found in collection");
298 Adds a type that can be loaded from datafiles.
301 void add_keyword(const std::string &keyword)
302 { add_keyword<T, T>(keyword); }
305 Adds a type that can be loaded from datafiles, with different storage type.
307 template<typename T, typename S>
308 void add_keyword(const std::string &keyword)
309 { keywords.push_back(new ItemKeyword<T, S>(keyword)); }
312 Adds a type that can be created automatically.
314 template<typename T, typename C>
315 void add_creator(T *(C::*func)(const std::string &))
316 { add_creator<T, T, C>(func); }
318 template<typename T, typename S, typename C>
319 void add_creator(T *(C::*func)(const std::string &))
320 { creators.push_back(new ItemCreator<T, S, C>(func)); }
323 } // namespace DataFile