const Variant &get_var(const std::string &, const CollectionItemTypeBase *);
template<typename T>
- T &extract(const Variant &var) const
- {
- return *var.value<RefPtr<typename RemoveConst<T>::Type> >();
- }
+ T &extract(const Variant &var) const;
template<typename T>
void collect_items(std::list<T *> *objects, std::list<std::string> *names, std::list<std::string> *future_names) const
template<typename T>
CollectionItemType<T> &add_type();
- /** Returns the descriptor for a type, or null if one isn't defined. */
+ /// Returns the descriptor for a type, or null if one isn't defined.
template<typename T>
CollectionItemTypeBase *get_type() const;
+ /// Returns the descriptor for an item, or null if it's of an unknown type.
+ CollectionItemTypeBase *get_type_for_item(const Variant &) const;
+
void add_source(CollectionSource &);
};
class CollectionItemTypeBase
{
protected:
- struct TagBase
+ struct ExtractorBase
{
- virtual ~TagBase() { }
+ virtual ~ExtractorBase() { }
};
template<typename T>
- struct Tag: TagBase
- { };
+ struct Extractor: ExtractorBase
+ {
+ virtual T &extract(const Variant &) const = 0;
+ };
std::string kwd;
std::vector<std::string> suffixes;
- TagBase *tag;
+ std::vector<ExtractorBase *> extractors;
- CollectionItemTypeBase();
+ CollectionItemTypeBase() { }
public:
virtual ~CollectionItemTypeBase();
const std::string &get_keyword() const { return kwd; }
void add_suffix(const std::string &);
bool match_name(const std::string &) const;
+ virtual bool check_item_type(const Variant &) const = 0;
virtual void add_to_loader(Collection::Loader &) const = 0;
virtual bool can_create() const = 0;
virtual void create_item(Collection &, const std::string &) const = 0;
virtual void load_item(Collection &, Parser &, const std::string &) const = 0;
template<typename T>
- bool check_type() const
- { return dynamic_cast<Tag<T> *>(tag); }
+ bool can_extract() const
+ {
+ for(std::vector<ExtractorBase *>::const_iterator i=extractors.begin(); i!=extractors.end(); ++i)
+ if(dynamic_cast<Extractor<T> *>(*i))
+ return true;
+ return false;
+ }
+
+ template<typename T>
+ T *extract(const Variant &var) const
+ {
+ for(std::vector<ExtractorBase *>::const_iterator i=extractors.begin(); i!=extractors.end(); ++i)
+ if(Extractor<T> *ex = dynamic_cast<Extractor<T> *>(*i))
+ return &ex->extract(var);
+ return 0;
+ }
};
{ return (static_cast<C &>(coll).*func)(name); }
};
- struct StoreBase
+ template<typename B>
+ struct Extractor: CollectionItemTypeBase::Extractor<B>
{
- virtual ~StoreBase() { }
-
- virtual void store(Collection &, const std::string &, T *) = 0;
-
- virtual void add_to_loader(Collection::Loader &, const std::string &) = 0;
- };
-
- template<typename S>
- struct Store: StoreBase
- {
- virtual void store(Collection &coll, const std::string &name, T *obj)
- { coll.add(name, static_cast<S *>(obj)); }
-
- virtual void add_to_loader(Collection::Loader &loader, const std::string &kwd)
- { loader.add(kwd, &Collection::Loader::item<T, S>); }
+ virtual B &extract(const Variant &var) const
+ { return *var.value<RefPtr<T> >(); }
};
CreatorBase *creat;
- StoreBase *store;
public:
CollectionItemType():
- creat(0), store(new Store<T>)
- { tag = new Tag<T>; }
+ creat(0)
+ { }
~CollectionItemType()
{
delete creat;
- delete store;
}
/** Sets a datafile keyword for this item type. The Collection's loader
return *this;
}
- /** Specifies the storage type for items of this type. It must be a base
- class of the actual type. */
- template<typename S>
- CollectionItemType &store_as()
+ /** Makes items of this type available through a base class. */
+ template<typename B>
+ CollectionItemType &base()
{
- delete tag;
- tag = new Tag<S>;
- delete store;
- store = new Store<S>;
+ extractors.push_back(new Extractor<B>);
return *this;
}
+ virtual bool check_item_type(const Variant &var) const
+ { return var.check_type<RefPtr<T> >(); }
+
virtual void add_to_loader(Collection::Loader &loader) const
- { store->add_to_loader(loader, kwd); }
+ { loader.add(kwd, &Collection::Loader::item<T, T>); }
virtual bool can_create() const
{ return creat!=0; }
throw std::runtime_error("no creator");
T *obj = creat->create(coll, name);
if(obj)
- store->store(coll, name, 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);
- store->store(coll, name, obj.get());
- obj.release();
+ // Collection::add will delete the object if it fails
+ coll.add(name, obj.release());
}
};
+template<typename T>
+T &Collection::extract(const Variant &var) const
+{
+ typedef RefPtr<typename RemoveConst<T>::Type> RPNCT;
+
+ if(!var.check_type<RPNCT>())
+ if(CollectionItemTypeBase *type = get_type_for_item(var))
+ if(T *item = type->extract<T>(var))
+ return *item;
+
+ return *var.value<RPNCT>();
+}
+
template<typename T>
CollectionItemType<T> &Collection::add_type()
{
CollectionItemTypeBase *Collection::get_type() const
{
for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
- if((*j)->check_type<typename RemoveConst<T>::Type>())
+ if(dynamic_cast<CollectionItemType<typename RemoveConst<T>::Type> *>(*j))
+ return *j;
+ for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
+ if((*j)->can_extract<T>())
return *j;
return 0;
}