template<typename T>
T &get(const std::string &name) const
{
- return *get_item(items, name).value<RefPtr<typename RemoveConst<T>::Type> >();
+ return extract<T>(get_item(items, name));
}
/** Gets a typed object from the collection. If the name is not found,
automatic creation with the type's creator function (if defined) or from
sources (if present) is attempted. */
template<typename T>
- T &get(const std::string &);
+ T &get(const std::string &name)
+ {
+ return extract<T>(get_var(name, get_type<T>()));
+ }
private:
+ 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> >();
+ }
+
template<typename T>
void collect_items(std::list<T *> *objects, std::list<std::string> *names, std::list<std::string> *future_names) const
{
class CollectionItemTypeBase
{
protected:
- class TagBase
+ struct TagBase
{
- protected:
- TagBase() { }
- public:
virtual ~TagBase() { }
};
template<typename T>
- class Tag: public TagBase
+ struct Tag: TagBase
{ };
std::string kwd;
class CollectionItemType: public CollectionItemTypeBase
{
private:
- class CreatorBase
+ struct CreatorBase
{
- protected:
- CreatorBase() { }
- public:
virtual ~CreatorBase() { }
virtual T *create(Collection &, const std::string &) const = 0;
};
template<typename C>
- class Creator: public CreatorBase
+ struct Creator: CreatorBase
{
- public:
typedef T *(C::*FuncPtr)(const std::string &);
- private:
FuncPtr func;
- public:
Creator(FuncPtr f): func(f) { }
virtual T *create(Collection &coll, const std::string &name) const
{ return (static_cast<C &>(coll).*func)(name); }
};
- class StoreBase
+ struct StoreBase
{
- protected:
- StoreBase() { }
- public:
virtual ~StoreBase() { }
virtual void store(Collection &, const std::string &, T *) = 0;
};
template<typename S>
- class Store: public StoreBase
+ struct Store: StoreBase
{
- public:
virtual void store(Collection &coll, const std::string &name, T *obj)
{ coll.add(name, static_cast<S *>(obj)); }
};
-template<typename T>
-T &Collection::get(const std::string &name)
-{
- typedef RefPtr<typename RemoveConst<T>::Type> RPNCT;
-
- ItemMap::iterator i = items.find(name);
- if(i!=items.end())
- return *i->second.value<RPNCT>();
-
- if(CollectionItemTypeBase *type = get_type<T>())
- {
- bool loaded = false;
- if(type->can_create())
- {
- type->create_item(*this, name);
- loaded = items.count(name);
- }
- for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
- {
- (*j)->load(*this, *type, name);
- loaded = items.count(name);
- }
- }
-
- return *get_item(items, name).value<RPNCT>();
-}
-
template<typename T>
CollectionItemType<T> &Collection::add_type()
{