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>
18 Helper struct to determine whether a Loader has a Collection typedef.
21 struct NeedsCollection
23 struct Yes { char c[2]; };
24 struct No { char c; };
27 static Yes f(typename U::Collection *);
31 enum { result=(sizeof(f<T>(0))==sizeof(Yes)) };
35 A collection of objects that can be loaded from a datafile. Each object is
36 identified by a name, which must be unique across the entire collection.
44 /* XXX I don't really like sticking all this stuff in here, but there's some
45 complex inter-class relationships, especially between ItemKeyword and
46 Collection::Loader. */
50 virtual ~ItemBase() { }
54 struct Item: public ItemBase
58 Item(T *d): data(d) { }
59 ~Item() { delete data; }
63 Used to store keywords for types that can be loaded.
65 struct ItemKeywordBase
67 virtual void add_to_loader(Loader &) const { };
70 template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
71 struct ItemKeyword: public ItemKeywordBase
75 ItemKeyword(const std::string &kw): keyword(kw) { }
77 void add_to_loader(Loader &ldr) const
78 { ldr.add(keyword, &Loader::item<T, S>); }
81 template<typename T, typename S>
82 struct ItemKeyword<T, S, true>: public ItemKeywordBase
86 ItemKeyword(const std::string &kw): keyword(kw) { }
88 virtual void add_to_loader(Loader &ldr) const
89 { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
93 Used to store types that can be created automatically.
95 struct ItemCreatorBase
97 virtual ~ItemCreatorBase() { }
100 S *create(Collection &coll, const std::string &name)
102 ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
104 return creator->create(coll, name);
110 struct ItemCreatorBridge: public ItemCreatorBase
112 virtual S *create(Collection &, const std::string &) const =0;
115 template<typename T, typename S, typename C>
116 struct ItemCreator: public ItemCreatorBridge<S>
118 typedef T *(C::*fCreate)(const std::string &);
122 ItemCreator(fCreate cf): create_func(cf) { }
123 virtual S *create(Collection &coll, const std::string &name) const
124 { return (dynamic_cast<C &>(coll).*create_func)(name); }
129 Loads objects into a Collection.
131 class Loader: public DataFile::Loader
137 Loader(Collection &);
138 Collection &get_object() const { return coll; }
140 template<typename T, typename S, typename C>
141 void coll_item(const std::string &n)
144 load_sub(*it, dynamic_cast<C &>(coll));
145 coll.add<S>(n, it.get());
149 template<typename T, typename S>
150 void item(const std::string &n)
154 coll.add<S>(n, it.get());
158 template<typename, typename, bool> friend class ItemKeyword;
162 typedef std::map<std::string, ItemBase *> ItemMap;
163 typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
164 typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
167 ItemKeywordSeq keywords;
168 ItemCreatorSeq creators;
171 virtual ~Collection();
174 Adds an object into the collection. If a name collision occurs, an
175 exception is thrown. The collection takes ownership of the object.
178 void add(const std::string &name, T *d)
180 if(items.count(name))
181 throw KeyError("Duplicate key '"+name+"' in collection");
183 items[name]=new Item<T>(d);
187 Gets an object of a specific type from the collection.
190 T &get(const std::string &name) const
192 ItemMap::const_iterator i=items.find(name);
194 throw KeyError("Item '"+name+"' not found in collection");
196 const Item<T> *item=dynamic_cast<const Item<T> *>(i->second);
198 throw TypeError("Item '"+name+"' is not of correct type");
204 Gets an object of a specific type from the collection. If the name is not
205 found in the collection and there is a creator for the item type, it is
209 T &get(const std::string &name)
211 ItemMap::const_iterator i=items.find(name);
214 for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
215 if(T *d=(*j)->create<T>(*this, name))
217 // We already know that the item didn't exist yet
218 items[name]=new Item<T>(d);
221 throw KeyError("Item '"+name+"' not found in collection");
224 const Item<T> *item=dynamic_cast<const Item<T> *>(i->second);
226 throw TypeError("Item '"+name+"' is not of correct type");
232 Returns a list of the names of objects of a specific type in the collection.
235 std::list<std::string> get_names() const
237 std::list<std::string> result;
238 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
239 if(dynamic_cast<const Item<T> *>(i->second))
240 result.push_back(i->first);
245 Checks whether a name exists in the collection. Does not care about the
248 bool contains(const std::string &n) const;
252 Adds a type that can be loaded from datafiles.
255 void add_keyword(const std::string &keyword)
256 { add_keyword<T, T>(keyword); }
259 Adds a type that can be loaded from datafiles, with different storage type.
261 template<typename T, typename S>
262 void add_keyword(const std::string &keyword)
263 { keywords.push_back(new ItemKeyword<T, S>(keyword)); }
266 Adds a type that can be created automatically.
268 template<typename T, typename C>
269 void add_creator(T *(C::*func)(const std::string &))
270 { add_creator<T, T, C>(func); }
272 template<typename T, typename S, typename C>
273 void add_creator(T *(C::*func)(const std::string &))
274 { creators.push_back(new ItemCreator<T, S, C>(func)); }
277 } // namespace DataFile