+
+/**
+Describes a type of item that can be loaded by a Collection. These are created
+by Collection::add_type.
+*/
+template<typename T>
+class CollectionItemType: public CollectionItemTypeBase
+{
+private:
+ struct CreatorBase
+ {
+ virtual ~CreatorBase() { }
+
+ virtual T *create(Collection &, const std::string &) const = 0;
+ };
+
+ template<typename C>
+ struct Creator: CreatorBase
+ {
+ typedef T *(C::*FuncPtr)(const std::string &);
+
+ FuncPtr func;
+
+ Creator(FuncPtr f): func(f) { }
+
+ virtual T *create(Collection &coll, const std::string &name) const
+ { return (dynamic_cast<C &>(coll).*func)(name); }
+ };
+
+ template<typename B>
+ struct Extractor: CollectionItemTypeBase::Extractor<B>
+ {
+ virtual B &extract(const Variant &var) const
+ { return *var.value<RefPtr<T> >(); }
+ };
+
+ CreatorBase *creat;
+
+public:
+ CollectionItemType():
+ creat(0)
+ { }
+
+ ~CollectionItemType()
+ {
+ delete creat;
+ }
+
+ /** Sets a datafile keyword for this item type. The Collection's loader
+ will accept a statement with this keyword and a single string argument - the
+ item's name. */
+ CollectionItemType &keyword(const std::string &k)
+ {
+ set_keyword(k);
+ return *this;
+ }
+
+ /** Adds a suffix that is used to match names when looking for future
+ objects. There is no implied separator; a name matches if it ends with the
+ suffix. If a keyword is defined before any suffixes, then "."+keyword is
+ added as a suffix. */
+ CollectionItemType &suffix(const std::string &s)
+ {
+ add_suffix(s);
+ return *this;
+ }
+
+ /** Attaches a creator function to this item type. If an item is not found
+ in the Collection, the creator function for its type is called to create it.
+ The function must be a member of the Collection subclass containing the
+ type. It must return the created object, or null if it could not be
+ created. It's also permissible to load the item via other means and then
+ return null. */
+ template<typename C>
+ CollectionItemType &creator(T *(C::*func)(const std::string &))
+ {
+ delete creat;
+ creat = new Creator<C>(func);
+ return *this;
+ }
+
+ /** Makes items of this type available through a base class. */
+ template<typename B>
+ CollectionItemType &base()
+ {
+ extractors.push_back(new Extractor<B>);
+ return *this;
+ }
+
+ virtual bool is_same_type(const CollectionItemTypeBase &other) const
+ { return dynamic_cast<const CollectionItemType<T> *>(&other); }
+
+ virtual bool check_item_type(const Variant &var) const
+ { return var.check_type<RefPtr<T> >(); }
+
+ virtual void add_to_loader(Collection::Loader &loader) const
+ { loader.add(kwd, &Collection::Loader::item<T, T>); }
+
+ virtual bool can_create() const
+ { return creat!=0; }
+
+ virtual void create_item(Collection &coll, const std::string &name) const
+ {
+ if(!creat)
+ throw std::runtime_error("no creator");
+ T *obj = creat->create(coll, name);
+ if(obj)
+ coll.add(name, obj);
+ }
+
+ virtual void load_item(Collection &coll, Parser &parser, const std::string &name) const
+ {
+ RefPtr<T> obj = new T;
+ Collection::ItemLoader<T> ldr(*obj, coll);
+ ldr.load(parser);
+ coll.add(name, obj.get());
+ obj.release();
+ }
+};
+
+
+template<typename T>
+T &Collection::extract(const Variant &var) const
+{
+ if(!var.check_type<RefPtr<T> >())
+ if(CollectionItemTypeBase *type = get_type_for_item(var))
+ if(T *item = type->extract<T>(var))
+ return *item;
+
+ return *var.value<RefPtr<T> >();
+}
+
+template<typename T>
+CollectionItemType<T> &Collection::add_type()
+{
+ CollectionItemType<T> *type = new CollectionItemType<T>;
+ types.push_back(type);
+ return *type;
+}
+
+template<typename T>
+CollectionItemTypeBase *Collection::get_type() const
+{
+ for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
+ if(dynamic_cast<CollectionItemType<T> *>(*j))
+ return *j;
+ for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
+ if((*j)->can_extract<T>())
+ return *j;
+ return 0;
+}
+