Ticket #388: start investigation of MultiFact design
needs overhaul, since current design leads to problems with GCC 4.8 onwards (and is messed up anyway)
This commit is contained in:
parent
b2b75fbe43
commit
a1bb9178f5
4 changed files with 421 additions and 3 deletions
277
src/lib/muttifac.hpp
Normal file
277
src/lib/muttifac.hpp
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
/*
|
||||
MULTIFACT.hpp - flexible family-of-object factory template
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
*/
|
||||
|
||||
/** @file multifact.hpp
|
||||
** Framework for building a configurable factory, to generate families of related objects.
|
||||
** These building blocks are targeted towards the "classical" factory situation: obtaining
|
||||
** objects of various kinds, which are related somehow (usually through an common interface).
|
||||
** The creation of these objects might be non-trivial, while the number of flavours to be
|
||||
** produced and the exact parametrisation isn't known beforehand and needs to be figured out
|
||||
** at runtime. As a solution, thus a number of "fabrication lines" is set up, to be selected
|
||||
** on invocation through an ID (which may be symbolic, hashed or structural).
|
||||
**
|
||||
** Usually, the issue of object and storage management is closely related, while it is
|
||||
** desirable to keep the object production logic clean of these rather technical concerns.
|
||||
** The implementation built here separates the latter into a policy template invoked as a
|
||||
** \em wrapper, accepting the raw product and either registering it, taking ownership, clone
|
||||
** it or use it for more involved wiring. Obviously, the product generated by the installed
|
||||
** "fabrication lines" needs to be delivered in a form acceptable by the concrete wrapper;
|
||||
** mismatch will be spotted by the compiler on registration of the respective fabrication
|
||||
** function.
|
||||
**
|
||||
** \par Singleton generation
|
||||
** For the very common situation of building a family of singleton objects, accessible by ID,
|
||||
** there is a convenience shortcut: The nested MultiFact::Singleton template can be instantiated
|
||||
** within the context providing the objects (usually a static context). In itself a lib::Singleton
|
||||
** factory, it automatically registers the singleton access function as "fabrication" function
|
||||
** into a suitable MultiFact instance passed in as ctor parameter.
|
||||
**
|
||||
** @note there is an extension header, multifact-arg.hpp, which provides template specialisations
|
||||
** for the special case when the fabrication functions need additional invocation arguments.
|
||||
** @todo still way to convoluted design. We can do better //////////TICKET #388
|
||||
**
|
||||
** @see multifact-test.cpp
|
||||
** @see multifact-argument-test.cpp
|
||||
** @see SingletonFactory
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LIB_MUTTIFACT_H
|
||||
#define LIB_MUTTIFACT_H
|
||||
|
||||
|
||||
#include "lib/error.hpp"
|
||||
#include "lib/depend.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace factory {
|
||||
|
||||
// Helpers to wrap the factory's product
|
||||
|
||||
/**
|
||||
* Dummy "wrapper",
|
||||
* just returning a target-ref
|
||||
*/
|
||||
template<typename TAR>
|
||||
struct PassReference
|
||||
{
|
||||
typedef TAR& RType;
|
||||
typedef TAR& PType;
|
||||
|
||||
PType wrap (RType object) { return object; }
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper taking ownership,
|
||||
* by wrapping into smart-ptr
|
||||
*/
|
||||
template<typename TAR>
|
||||
struct BuildRefcountPtr
|
||||
{
|
||||
typedef TAR* RType;
|
||||
typedef std::shared_ptr<TAR> PType;
|
||||
|
||||
PType wrap (RType ptr) { return PType{ptr}; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Table of registered production functions for MultiFact.
|
||||
* Each stored function can be accessed by ID and is able
|
||||
* to fabricate a specific object, which is assignable to
|
||||
* the nominal target type in the MultiFact definition.
|
||||
*/
|
||||
template<typename SIG, typename ID>
|
||||
struct Fab
|
||||
{
|
||||
typedef std::function<SIG> FactoryFunc;
|
||||
|
||||
|
||||
FactoryFunc&
|
||||
select (ID const& id)
|
||||
{
|
||||
if (!contains (id))
|
||||
throw lumiera::error::Invalid("unknown factory product requested.");
|
||||
|
||||
return producerTable_[id];
|
||||
}
|
||||
|
||||
void
|
||||
defineProduction (ID const& id, FactoryFunc fun)
|
||||
{
|
||||
producerTable_[id] = fun;
|
||||
}
|
||||
|
||||
|
||||
/* === diagnostics === */
|
||||
|
||||
bool empty () const { return producerTable_.empty(); }
|
||||
bool contains (ID id) const { return util::contains (producerTable_,id); }
|
||||
|
||||
private:
|
||||
std::map<ID, FactoryFunc> producerTable_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @internal configuration of the elements
|
||||
* to be combined into a MultiFact instance
|
||||
*/
|
||||
template< typename TY
|
||||
, template<class> class Wrapper
|
||||
>
|
||||
struct FabWiring
|
||||
: Wrapper<TY>
|
||||
{
|
||||
typedef typename Wrapper<TY>::PType WrappedProduct;
|
||||
typedef typename Wrapper<TY>::RType FabProduct;
|
||||
typedef FabProduct SIG_Fab(void);
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Factory for creating a family of objects by ID.
|
||||
* The actual factory functions are to be installed
|
||||
* from the usage site through calls to #defineProduction .
|
||||
* Each generated object will be treated by the Wrapper template,
|
||||
* allowing for the generation of smart-ptrs. The embedded class
|
||||
* Singleton allows to build a family of singleton objects; it is
|
||||
* to be instantiated at the call site and acts as singleton factory,
|
||||
* accessible through a MultiFact instance as frontend.
|
||||
*/
|
||||
template< typename TY
|
||||
, typename ID
|
||||
, template<class> class Wrapper
|
||||
>
|
||||
class MuttiFac
|
||||
: public FabWiring<TY,Wrapper>
|
||||
{
|
||||
typedef FabWiring<TY,Wrapper> _Conf;
|
||||
typedef typename _Conf::SIG_Fab SIG_Fab;
|
||||
typedef Fab<SIG_Fab,ID> _Fab;
|
||||
|
||||
_Fab funcTable_;
|
||||
|
||||
|
||||
protected:
|
||||
typedef typename _Fab::FactoryFunc Creator;
|
||||
|
||||
Creator&
|
||||
selectProducer (ID const& id)
|
||||
{
|
||||
return funcTable_.select(id);
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
typedef typename _Conf::WrappedProduct Product;
|
||||
|
||||
Product
|
||||
operator() (ID const& id)
|
||||
{
|
||||
Creator& func = this->selectProducer (id);
|
||||
return this->wrap (func());
|
||||
}
|
||||
|
||||
Product
|
||||
invokeFactory (ID const& id) ///< alias for the function operator
|
||||
{
|
||||
return this->operator() (id);
|
||||
}
|
||||
|
||||
|
||||
/** to set up a production line,
|
||||
* associated with a specific ID
|
||||
*/
|
||||
template<typename FUNC>
|
||||
void
|
||||
defineProduction (ID id, FUNC fun)
|
||||
{
|
||||
funcTable_.defineProduction (id, fun);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convenience shortcut for automatically setting up
|
||||
* a production line, to fabricate a singleton instance
|
||||
* of the given implementation target type (IMP)
|
||||
*/
|
||||
template<class IMP>
|
||||
class Singleton
|
||||
: lib::Depend<IMP>
|
||||
{
|
||||
typedef lib::Depend<IMP> SingFac;
|
||||
|
||||
Creator
|
||||
createSingleton_accessFunction()
|
||||
{
|
||||
return std::bind (&SingFac::operator()
|
||||
, static_cast<SingFac*>(this));
|
||||
}
|
||||
|
||||
public:
|
||||
Singleton (MuttiFac& factory, ID id)
|
||||
{
|
||||
factory.defineProduction(id, createSingleton_accessFunction());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* === diagnostics === */
|
||||
|
||||
bool empty () const { return funcTable_.empty(); }
|
||||
bool contains (ID id) const { return funcTable_.contains (id); }
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace factory
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Standard configuration of the family-of-object factory
|
||||
* @todo this is rather guesswork... find out what the best and most used configuration could be....
|
||||
*/
|
||||
template< typename TY
|
||||
, typename ID
|
||||
>
|
||||
class MuttiFact
|
||||
: public factory::MuttiFac<TY,ID, factory::PassReference>
|
||||
{ };
|
||||
|
||||
|
||||
} // namespace lib
|
||||
#endif
|
||||
|
|
@ -376,7 +376,11 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
TEST "configurable Factory" MultiFact_test <<END
|
||||
PLANNED "configurable Factory" MultiFact_test <<END
|
||||
END
|
||||
|
||||
|
||||
TEST "Family of Singleton Factories" MultiFactSingleton_test <<END
|
||||
out: Impl-1
|
||||
out: Impl-2
|
||||
out: Impl-3
|
||||
|
|
|
|||
137
tests/library/multifact-singleton-test.cpp
Normal file
137
tests/library/multifact-singleton-test.cpp
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
MultiFactSingleton(Test) - using lib::multifact to manage a family of singletons
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/test/test-helper.hpp"
|
||||
#include "lib/multifact.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace test{
|
||||
|
||||
using boost::lexical_cast;
|
||||
using lib::test::showSizeof;
|
||||
using util::isSameObject;
|
||||
using util::isnil;
|
||||
using std::ostream;
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
using lumiera::error::LUMIERA_ERROR_INVALID;
|
||||
|
||||
|
||||
namespace { // hierarchy of test dummy objects
|
||||
|
||||
struct Interface
|
||||
{
|
||||
virtual ~Interface() {};
|
||||
virtual operator string () =0;
|
||||
};
|
||||
|
||||
inline ostream& operator<< (ostream& os, Interface& ifa) { return os << string(ifa); }
|
||||
|
||||
|
||||
enum theID
|
||||
{ ONE = 1
|
||||
, TWO
|
||||
, THR
|
||||
, FOU
|
||||
};
|
||||
|
||||
typedef factory::MultiFact<Interface, theID, factory::PassReference> TestFactory;
|
||||
|
||||
|
||||
template<theID ii>
|
||||
class Implementation
|
||||
: public Interface
|
||||
{
|
||||
operator string()
|
||||
{
|
||||
return "Impl-"+lexical_cast<string> (ii);
|
||||
}
|
||||
|
||||
public:
|
||||
static theID getTypeID() { return ii; }
|
||||
};
|
||||
|
||||
/** Factory instance for the tests... */
|
||||
TestFactory theFact;
|
||||
|
||||
// Configure the products to be fabricated....
|
||||
TestFactory::Singleton<Implementation<ONE> > holder1 (theFact,ONE);
|
||||
TestFactory::Singleton<Implementation<TWO> > holder2 (theFact,TWO);
|
||||
TestFactory::Singleton<Implementation<THR> > holder3 (theFact,THR);
|
||||
TestFactory::Singleton<Implementation<FOU> > holder4 (theFact,FOU);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/***************************************************************//**
|
||||
* @test verify simple setup of the MultiFact template.
|
||||
* Define a hierarchy of test dummy objects, in order to
|
||||
* register them automatically for creation through a suitable
|
||||
* instantiation of MultiFact. Verify we get the correct product
|
||||
* when invoking this MultiFac flavour.
|
||||
* @see lib::MultiFact
|
||||
*/
|
||||
class MultiFactSingleton_test : public Test
|
||||
{
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
cout << theFact(ONE) << endl;
|
||||
cout << theFact(TWO) << endl;
|
||||
cout << theFact(THR) << endl;
|
||||
cout << theFact(FOU) << endl;
|
||||
cout << showSizeof (theFact) << endl;
|
||||
|
||||
Interface & o1 = theFact(ONE);
|
||||
Interface & o2 = theFact(ONE);
|
||||
CHECK (isSameObject(o1,o2));
|
||||
|
||||
TestFactory anotherFact;
|
||||
CHECK (isnil (anotherFact));
|
||||
VERIFY_ERROR (INVALID, anotherFact(ONE) );
|
||||
|
||||
TestFactory::Singleton<Implementation<ONE> > anotherSingletonHolder (anotherFact,ONE);
|
||||
Interface & o3 = anotherFact(ONE);
|
||||
CHECK (isSameObject(o2,o3));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** Register this test class... */
|
||||
LAUNCHER (MultiFactSingleton_test, "unit common");
|
||||
|
||||
|
||||
|
||||
}} // namespace lib::test
|
||||
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/test/test-helper.hpp"
|
||||
#include "lib/multifact.hpp"
|
||||
#include "lib/muttifac.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
|
@ -65,7 +65,7 @@ namespace test{
|
|||
, FOU
|
||||
};
|
||||
|
||||
typedef factory::MultiFact<Interface, theID, factory::PassReference> TestFactory;
|
||||
typedef factory::MuttiFac<Interface, theID, factory::PassReference> TestFactory;
|
||||
|
||||
|
||||
template<theID ii>
|
||||
|
|
|
|||
Loading…
Reference in a new issue