3 This file is part of libmspdatafile
4 Copyright © 2006 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/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 void add_to_loader(Loader &) const { };
71 template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
72 struct ItemKeyword: public ItemKeywordBase
76 ItemKeyword(const std::string &kw): keyword(kw) { }
78 void add_to_loader(Loader &ldr) const
79 { ldr.add(keyword, &Loader::item<T, S>); }
82 template<typename T, typename S>
83 struct ItemKeyword<T, S, true>: public ItemKeywordBase
87 ItemKeyword(const std::string &kw): keyword(kw) { }
89 virtual void add_to_loader(Loader &ldr) const
90 { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
94 Used to store types that can be created automatically.
96 struct ItemCreatorBase
98 virtual ~ItemCreatorBase() { }
101 S *create(Collection &coll, const std::string &name)
103 ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
105 return creator->create(coll, name);
111 struct ItemCreatorBridge: public ItemCreatorBase
113 virtual S *create(Collection &, const std::string &) const =0;
116 template<typename T, typename S, typename C>
117 struct ItemCreator: public ItemCreatorBridge<S>
119 typedef T *(C::*fCreate)(const std::string &);
123 ItemCreator(fCreate cf): create_func(cf) { }
124 virtual S *create(Collection &coll, const std::string &name) const
125 { return (dynamic_cast<C &>(coll).*create_func)(name); }
130 Loads objects into a Collection.
132 class Loader: public DataFile::Loader
138 Loader(Collection &);
139 Collection &get_object() const { return coll; }
141 template<typename T, typename S, typename C>
142 void coll_item(const std::string &n)
145 load_sub(*it, dynamic_cast<C &>(coll));
146 coll.add<S>(n, it.get());
150 template<typename T, typename S>
151 void item(const std::string &n)
155 coll.add<S>(n, it.get());
159 template<typename, typename, bool> friend class ItemKeyword;
163 typedef std::map<std::string, ItemBase *> ItemMap;
164 typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
165 typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
168 ItemKeywordSeq keywords;
169 ItemCreatorSeq creators;
172 virtual ~Collection();
175 Adds an object into the collection. If a name collision occurs, an
176 exception is thrown. The collection takes ownership of the object.
179 void add(const std::string &name, T *d)
181 if(items.count(name))
182 throw KeyError("Duplicate key '"+name+"' in collection");
184 items[name]=new Item<T>(d);
188 Gets an object of a specific type from the collection.
191 T &get(const std::string &name) const
193 ItemMap::const_iterator i=items.find(name);
195 throw KeyError("Item '"+name+"' not found in collection");
197 const Item<T> *item=dynamic_cast<const Item<T> *>(i->second);
199 throw TypeError("Item '"+name+"' is not of correct type");
205 Gets an object of a specific type from the collection. If the name is not
206 found in the collection and there is a creator for the item type, it is
210 T &get(const std::string &name)
212 ItemMap::const_iterator i=items.find(name);
215 for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
216 if(T *d=(*j)->create<T>(*this, name))
218 // We already know that the item didn't exist yet
219 items[name]=new Item<T>(d);
222 throw KeyError("Item '"+name+"' not found in collection");
225 const Item<T> *item=dynamic_cast<const Item<T> *>(i->second);
227 throw TypeError("Item '"+name+"' is not of correct type");
233 Returns a list of the names of objects of a specific type in the collection.
236 std::list<std::string> get_names() const
238 std::list<std::string> result;
239 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
240 if(dynamic_cast<const Item<T> *>(i->second))
241 result.push_back(i->first);
246 Checks whether a name exists in the collection. Does not care about the
249 bool contains(const std::string &n) const;
253 Adds a type that can be loaded from datafiles.
256 void add_keyword(const std::string &keyword)
257 { add_keyword<T, T>(keyword); }
260 Adds a type that can be loaded from datafiles, with different storage type.
262 template<typename T, typename S>
263 void add_keyword(const std::string &keyword)
264 { keywords.push_back(new ItemKeyword<T, S>(keyword)); }
267 Adds a type that can be created automatically.
269 template<typename T, typename C>
270 void add_creator(T *(C::*func)(const std::string &))
271 { add_creator<T, T, C>(func); }
273 template<typename T, typename S, typename C>
274 void add_creator(T *(C::*func)(const std::string &))
275 { creators.push_back(new ItemCreator<T, S, C>(func)); }
278 } // namespace DataFile