From 698d4ae13da87b7c53aa96bca99c3deaff397eec Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 3 Dec 2012 23:20:02 +0200 Subject: [PATCH] Move most of Collection::get implementation to collection.cpp --- source/collection.cpp | 24 ++++++++++++++++++++++++ source/collection.h | 42 +++++++++++++----------------------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/source/collection.cpp b/source/collection.cpp index bf1c5ed..87982e8 100644 --- a/source/collection.cpp +++ b/source/collection.cpp @@ -11,6 +11,30 @@ Collection::~Collection() delete *i; } +const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type) +{ + ItemMap::iterator i = items.find(name); + if(i!=items.end()) + return i->second; + + if(type) + { + 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); +} + void Collection::add_source(CollectionSource &s) { sources.push_back(&s); diff --git a/source/collection.h b/source/collection.h index ec7cde3..be9ae88 100644 --- a/source/collection.h +++ b/source/collection.h @@ -109,16 +109,27 @@ public: template T &get(const std::string &name) const { - return *get_item(items, name).value::Type> >(); + return extract(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 - T &get(const std::string &); + T &get(const std::string &name) + { + return extract(get_var(name, get_type())); + } private: + const Variant &get_var(const std::string &, const CollectionItemTypeBase *); + + template + T &extract(const Variant &var) const + { + return *var.value::Type> >(); + } + template void collect_items(std::list *objects, std::list *names, std::list *future_names) const { @@ -450,33 +461,6 @@ public: }; -template -T &Collection::get(const std::string &name) -{ - typedef RefPtr::Type> RPNCT; - - ItemMap::iterator i = items.find(name); - if(i!=items.end()) - return *i->second.value(); - - if(CollectionItemTypeBase *type = get_type()) - { - 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(); -} - template CollectionItemType &Collection::add_type() { -- 2.45.2