1 #ifndef MSP_DATAFILE_COLLECTION_H_
2 #define MSP_DATAFILE_COLLECTION_H_
4 #include <msp/core/meta.h>
5 #include <msp/core/refptr.h>
12 Helper struct to determine whether a Loader has a Collection typedef.
15 struct NeedsCollection
17 struct Yes { char c[2]; };
18 struct No { char c; };
21 static Yes f(typename U::Collection *);
25 enum { result=(sizeof(f<T>(0))==sizeof(Yes)) };
29 A collection of objects that can be loaded from a datafile. Each object is
30 identified by a name, which must be unique across the entire collection.
38 /* XXX I don't really like sticking all this stuff in here, but there's some
39 complex inter-class relationships, especially between ItemKeyword and
40 Collection::Loader. */
44 virtual ~ItemBase() { }
48 struct Item: public ItemBase
52 Item(T *d): data(d) { }
53 ~Item() { delete data; }
57 Used to store keywords for types that can be loaded.
59 struct ItemKeywordBase
61 virtual ~ItemKeywordBase() { }
62 virtual void add_to_loader(Loader &) const { };
65 template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
66 struct ItemKeyword: public ItemKeywordBase
70 ItemKeyword(const std::string &kw): keyword(kw) { }
72 void add_to_loader(Loader &ldr) const
73 { ldr.add(keyword, &Loader::item<T, S>); }
76 template<typename T, typename S>
77 struct ItemKeyword<T, S, true>: public ItemKeywordBase
81 ItemKeyword(const std::string &kw): keyword(kw) { }
83 virtual void add_to_loader(Loader &ldr) const
84 { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
88 Used to store types that can be created automatically.
90 struct ItemCreatorBase
92 virtual ~ItemCreatorBase() { }
95 bool create(Collection &coll, const std::string &name, S *&ptr)
97 ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
100 ptr=creator->create(coll, name);
108 struct ItemCreatorBridge: public ItemCreatorBase
110 virtual S *create(Collection &, const std::string &) const = 0;
113 template<typename T, typename S, typename C>
114 struct ItemCreator: public ItemCreatorBridge<S>
116 typedef T *(C::*fCreate)(const std::string &);
120 ItemCreator(fCreate cf): create_func(cf) { }
121 virtual S *create(Collection &coll, const std::string &name) const
122 { return (dynamic_cast<C &>(coll).*create_func)(name); }
127 Loads objects into a Collection.
129 class Loader: public DataFile::Loader
135 Loader(Collection &);
136 Collection &get_object() const { return coll; }
138 template<typename T, typename S, typename C>
139 void coll_item(const std::string &n)
142 load_sub(*it, dynamic_cast<C &>(coll));
143 coll.add<S>(n, it.get());
147 template<typename T, typename S>
148 void item(const std::string &n)
152 coll.add<S>(n, it.get());
156 template<typename, typename, bool> friend class ItemKeyword;
160 typedef std::map<std::string, ItemBase *> ItemMap;
161 typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
162 typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
165 ItemKeywordSeq keywords;
166 ItemCreatorSeq creators;
168 Collection(const Collection &);
169 Collection &operator=(const Collection &);
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 in collection", name);
184 items[name]=new Item<typename RemoveConst<T>::Type>(d);
188 Gets an object of a specific type from the collection.
191 T *get(const std::string &name) const
193 typedef typename RemoveConst<T>::Type NCT;
195 ItemMap::const_iterator i=items.find(name);
197 throw KeyError("Item not found in collection", name);
199 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
201 throw TypeError("Type mismatch on item '"+name+"'");
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
212 T *get(const std::string &name)
214 typedef typename RemoveConst<T>::Type NCT;
216 ItemMap::const_iterator i=items.find(name);
219 for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
222 if((*j)->create(*this, name, d))
224 // We already know that the item didn't exist yet
225 items[name]=new Item<NCT>(d);
229 throw KeyError("Item not found in collection", name);
232 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i->second);
234 throw TypeError("Type mismatch on item '"+name+"'");
240 Returns a list of the names of objects of a specific type in the collection.
243 std::list<std::string> get_names() const
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);
253 Returns a list of objects of a specific type in the collection.
256 std::list<T *> get_list() const
258 typedef typename RemoveConst<T>::Type NCT;
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);
268 Checks whether a name exists in the collection. Does not care about the
271 bool contains(const std::string &n) const;
274 Returns the name of an item in the collection.
277 const std::string &get_name(T *d) const
279 typedef typename RemoveConst<T>::Type NCT;
281 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
282 if(Item<NCT> *item=dynamic_cast<Item<NCT> *>(i->second))
286 throw KeyError("Item not found in collection");
291 Adds a type that can be loaded from datafiles.
294 void add_keyword(const std::string &keyword)
295 { add_keyword<T, T>(keyword); }
298 Adds a type that can be loaded from datafiles, with different storage type.
300 template<typename T, typename S>
301 void add_keyword(const std::string &keyword)
302 { keywords.push_back(new ItemKeyword<T, S>(keyword)); }
305 Adds a type that can be created automatically.
307 template<typename T, typename C>
308 void add_creator(T *(C::*func)(const std::string &))
309 { add_creator<T, T, C>(func); }
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)); }
316 } // namespace DataFile