From 60e8000566e2b1510ccd52697793ab049b62b07c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 27 Sep 2012 16:11:56 +0300 Subject: [PATCH] Simplify loading collection items The ItemLoader wrapper from DirectoryCollection can be used in the base Collection::Loader as well. This removes the need for the kludgy struct Add and the need for two separate loader functions. As an added bonus, the ItemLoader can still be used in DirectoryCollection and future derived classes as well, further reducing duplication. --- source/collection.h | 42 ++++++++++++++++-------------------- source/directorycollection.h | 21 ------------------ 2 files changed, 19 insertions(+), 44 deletions(-) diff --git a/source/collection.h b/source/collection.h index b2c91d6..3d5b7f0 100644 --- a/source/collection.h +++ b/source/collection.h @@ -55,34 +55,27 @@ public: template friend class CollectionItemType; private: - template::value> - struct Add; - Collection &coll; public: Loader(Collection &); Collection &get_object() const { return coll; } private: - template - void coll_item(const std::string &n) - { - RefPtr it = new T; - load_sub(*it, dynamic_cast(coll)); - coll.add(n, it.get()); - it.release(); - } - template void item(const std::string &n) { RefPtr it = new T; - load_sub(*it); + ItemLoader ldr(*it, coll); + load_sub_with(ldr); coll.add(n, it.get()); it.release(); } }; +protected: + template::value> + class ItemLoader; + private: typedef std::map ItemMap; typedef std::list TypeList; @@ -272,19 +265,22 @@ protected: CollectionItemType &add_type(); }; - -template -struct Collection::Loader::Add +template +class Collection::ItemLoader: public T::Loader { - static void add(Loader &loader, const std::string &kwd) - { loader.add(kwd, &Loader::item); } +public: + ItemLoader(T &o, Collection &): + T::Loader(o) + { } }; -template -struct Collection::Loader::Add +template +class Collection::ItemLoader: public T::Loader { - static void add(Loader &loader, const std::string &kwd) - { loader.add(kwd, &Loader::coll_item); } +public: + ItemLoader(T &o, Collection &c): + T::Loader(o, dynamic_cast(c)) + { } }; @@ -383,7 +379,7 @@ private: { return RefPtr(0); } virtual void add_to_loader(Collection::Loader &loader, const std::string &kwd) - { Collection::Loader::Add::add(loader, kwd); } + { loader.add(kwd, &Collection::Loader::item); } }; CreatorBase *creat; diff --git a/source/directorycollection.h b/source/directorycollection.h index cbaffd6..a796054 100644 --- a/source/directorycollection.h +++ b/source/directorycollection.h @@ -13,9 +13,6 @@ A Collection that can automatically load items from files in a directory. class DirectoryCollection: public Collection { private: - template::value> - class ItemLoader; - std::list dirs; public: @@ -57,24 +54,6 @@ protected: bool lookup_file(const std::string &, FS::Path &) const; }; -template -class DirectoryCollection::ItemLoader: public T::Loader -{ -public: - ItemLoader(T &o, Collection &): - T::Loader(o) - { } -}; - -template -class DirectoryCollection::ItemLoader: public T::Loader -{ -public: - ItemLoader(T &o, Collection &c): - T::Loader(o, dynamic_cast(c)) - { } -}; - } // namespace DataFile } // namespace Msp -- 2.43.0