1 #ifndef MSP_DATAFILE_COLLECTION_H_
2 #define MSP_DATAFILE_COLLECTION_H_
4 #include <msp/core/meta.h>
5 #include <msp/core/maputils.h>
6 #include <msp/core/refptr.h>
13 Helper struct to determine whether a Loader has a Collection typedef.
16 struct NeedsCollection
18 struct Yes { char c[2]; };
19 struct No { char c; };
22 static Yes f(typename U::Collection *);
26 enum { result=(sizeof(f<T>(0))==sizeof(Yes)) };
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.
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. */
45 virtual ~ItemBase() { }
49 struct Item: public ItemBase
53 Item(T *d): data(d) { }
54 ~Item() { delete data; }
58 Used to store keywords for types that can be loaded.
60 struct ItemKeywordBase
62 virtual ~ItemKeywordBase() { }
63 virtual void add_to_loader(Loader &) const { };
66 template<typename T, typename S, bool need_coll=NeedsCollection<typename T::Loader>::result>
67 struct ItemKeyword: public ItemKeywordBase
71 ItemKeyword(const std::string &kw): keyword(kw) { }
73 void add_to_loader(Loader &ldr) const
74 { ldr.add(keyword, &Loader::item<T, S>); }
77 template<typename T, typename S>
78 struct ItemKeyword<T, S, true>: public ItemKeywordBase
82 ItemKeyword(const std::string &kw): keyword(kw) { }
84 virtual void add_to_loader(Loader &ldr) const
85 { ldr.add(keyword, &Loader::coll_item<T, S, typename T::Loader::Collection>); }
89 Used to store types that can be created automatically.
91 struct ItemCreatorBase
93 virtual ~ItemCreatorBase() { }
96 bool create(Collection &coll, const std::string &name, S *&ptr)
98 ItemCreatorBridge<S> *creator=dynamic_cast<ItemCreatorBridge<S> *>(this);
101 ptr=creator->create(coll, name);
109 struct ItemCreatorBridge: public ItemCreatorBase
111 virtual S *create(Collection &, const std::string &) const = 0;
114 template<typename T, typename S, typename C>
115 struct ItemCreator: public ItemCreatorBridge<S>
117 typedef T *(C::*fCreate)(const std::string &);
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); }
128 Loads objects into a Collection.
130 class Loader: public DataFile::Loader
136 Loader(Collection &);
137 Collection &get_object() const { return coll; }
139 template<typename T, typename S, typename C>
140 void coll_item(const std::string &n)
143 load_sub(*it, dynamic_cast<C &>(coll));
144 coll.add<S>(n, it.get());
148 template<typename T, typename S>
149 void item(const std::string &n)
153 coll.add<S>(n, it.get());
157 template<typename, typename, bool> friend class ItemKeyword;
161 typedef std::map<std::string, ItemBase *> ItemMap;
162 typedef std::list<ItemKeywordBase *> ItemKeywordSeq;
163 typedef std::list<ItemCreatorBase *> ItemCreatorSeq;
166 ItemKeywordSeq keywords;
167 ItemCreatorSeq creators;
169 Collection(const Collection &);
170 Collection &operator=(const Collection &);
173 virtual ~Collection();
176 Adds an object into the collection. If a name collision occurs, an
177 exception is thrown. The collection takes ownership of the object.
180 void add(const std::string &name, T *d)
182 typedef typename RemoveConst<T>::Type NCT;
184 RefPtr<Item<NCT> > i=new Item<NCT>(d);
185 insert_unique(items, i.get());
190 Gets an object of a specific type from the collection.
193 T *get(const std::string &name) const
195 typedef typename RemoveConst<T>::Type NCT;
197 ItemBase *i=get_item(items, name);
199 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i);
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 if(!items.count(name))
218 for(ItemCreatorSeq::iterator j=creators.begin(); j!=creators.end(); ++j)
221 if((*j)->create(*this, name, d))
223 // We already know that the item didn't exist yet
224 items[name]=new Item<NCT>(d);
230 ItemBase *i=get_item(items, name);
232 const Item<NCT> *item=dynamic_cast<const Item<NCT> *>(i);
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