configurable factory? test-driven brainstorming
This commit is contained in:
parent
8c21f21acc
commit
8ee76b1bfd
4 changed files with 250 additions and 1 deletions
120
src/lib/multifact.hpp
Normal file
120
src/lib/multifact.hpp
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
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
|
||||
** Building blocks of a configurable factory, generating families of related objects.
|
||||
**
|
||||
** @todo WIP-WIP-WIP ... currently collecting various bits of implementation here. TICKET #277
|
||||
**
|
||||
** @see SingletonFactory
|
||||
** @see lib::factory::Factory
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LIB_MULTIFACT_H
|
||||
#define LIB_MULTIFACT_H
|
||||
|
||||
|
||||
#include "lib/error.hpp"
|
||||
//#include <tr1/memory>
|
||||
|
||||
#include <tr1/functional>
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace factory {
|
||||
|
||||
/**
|
||||
* Repository of registered production lines.
|
||||
* @todo write type comment
|
||||
*/
|
||||
template<typename TY, typename ID>
|
||||
class Fab
|
||||
{
|
||||
typedef TY& RawProduct;
|
||||
typedef std::tr1::function<RawProduct(void)> & FactoryFunc;
|
||||
|
||||
public:
|
||||
static FactoryFunc
|
||||
select (ID id)
|
||||
{
|
||||
UNIMPLEMENTED ("how to store/select the production line");
|
||||
}
|
||||
|
||||
static void
|
||||
defineProduction (ID id, FactoryFunc fun)
|
||||
{
|
||||
UNIMPLEMENTED ("how to store/select the production line");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @todo write type comment
|
||||
*/
|
||||
template< typename TY
|
||||
, typename ID
|
||||
, template<class> class Wrapper
|
||||
>
|
||||
class MultiFact
|
||||
: Wrapper<TY>
|
||||
{
|
||||
typedef Fab<TY,ID> _Fab;
|
||||
typedef typename _Fab::FactoryFunc Creator;
|
||||
typedef typename WR::PType Product;
|
||||
|
||||
public:
|
||||
Product
|
||||
operator() (ID id)
|
||||
{
|
||||
Creator func = _Fab::select(id);
|
||||
return wrap (func());
|
||||
}
|
||||
|
||||
/**
|
||||
* to set up a production line,
|
||||
* associated with a specific ID
|
||||
*/
|
||||
struct Produce
|
||||
: Creator
|
||||
{
|
||||
template<typename FUNC>
|
||||
Produce (ID id, FUNC fun)
|
||||
: Creator(fun)
|
||||
{
|
||||
_Fab::defineProduction (id, *this);
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace factory
|
||||
|
||||
//using factory::Factory;
|
||||
|
||||
|
||||
} // namespace lib
|
||||
#endif
|
||||
|
|
@ -284,6 +284,16 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
PLANNED "configurable Factory" MultiFact_test <<END
|
||||
out: Impl-1
|
||||
out: Impl-2
|
||||
out: Impl-3
|
||||
out: Impl-4
|
||||
out: sizeof\( .+ \) = 1
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "inline type erasure" OpaqueHolder_test <<END
|
||||
out: 1
|
||||
out: 3
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ test_lib_LDADD = \
|
|||
$(NOBUGMT_LUMIERA_LIBS) -ldl -lboost_program_options-mt -lboost_regex-mt
|
||||
|
||||
test_lib_SOURCES = \
|
||||
$(testlib_srcdir)/mainsuite.cpp \
|
||||
$(testlib_srcdir)/allocationclustertest.cpp \
|
||||
$(testlib_srcdir)/appconfigtest.cpp \
|
||||
$(testlib_srcdir)/customsharedptrtest.cpp \
|
||||
|
|
@ -48,8 +49,8 @@ test_lib_SOURCES = \
|
|||
$(testlib_srcdir)/hash-indexed-test.cpp \
|
||||
$(testlib_srcdir)/helloworldtest.cpp \
|
||||
$(testlib_srcdir)/lifecycletest.cpp \
|
||||
$(testlib_srcdir)/multifact-test.cpp \
|
||||
$(testlib_srcdir)/iter-adapter-test.cpp \
|
||||
$(testlib_srcdir)/mainsuite.cpp \
|
||||
$(testlib_srcdir)/meta/typelist-test.cpp \
|
||||
$(testlib_srcdir)/meta/typelist-manip-test.cpp \
|
||||
$(testlib_srcdir)/meta/typeseq-manip-test.cpp \
|
||||
|
|
|
|||
118
tests/lib/multifact-test.cpp
Normal file
118
tests/lib/multifact-test.cpp
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
MultiFact(Test) - unittest for the configurable object-family creating factory
|
||||
|
||||
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/multifact.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace test{
|
||||
|
||||
using boost::lexical_cast;
|
||||
//using util::isnil;
|
||||
using util::isSameObject;
|
||||
using std::string;
|
||||
using std::cout;
|
||||
|
||||
|
||||
namespace { // hierarchy of test dummy objects
|
||||
|
||||
struct Interface
|
||||
{
|
||||
virtual ~Interface() {};
|
||||
virtual operator string ();
|
||||
};
|
||||
|
||||
enum theID
|
||||
{ ONE = 1
|
||||
, TWO
|
||||
, THR
|
||||
, FOU
|
||||
};
|
||||
|
||||
typedef MultiFact<Interface, theID, PassReference> TestFactory;
|
||||
|
||||
|
||||
template<theID ii>
|
||||
class Implementation
|
||||
: public Interface
|
||||
{
|
||||
operator string()
|
||||
{
|
||||
return "Impl-"+lexical_cast<string> (ii);
|
||||
}
|
||||
};
|
||||
|
||||
TestFactory::ProduceSingleton<Implementation<ONE> > _register_for_fabrication_ONE;
|
||||
TestFactory::ProduceSingleton<Implementation<TWO> > _register_for_fabrication_TWO;
|
||||
TestFactory::ProduceSingleton<Implementation<THR> > _register_for_fabrication_THR;
|
||||
TestFactory::ProduceSingleton<Implementation<FOU> > _register_for_fabrication_FOU;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*******************************************************************
|
||||
* @test verify simple setup of the MultiFact template.
|
||||
* Define a hierarchy of test dummy objects, such as 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 MultiFact_test : public Test
|
||||
{
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
TestFactory theFact;
|
||||
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);
|
||||
ASSERT (isSameObject(o1,o2));
|
||||
|
||||
TestFactory anotherFact;
|
||||
Interface & o3 = anotherFact(ONE);
|
||||
ASSERT (isSameObject(o2,o3));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** Register this test class... */
|
||||
LAUNCHER (MultiFact_test, "unit common");
|
||||
|
||||
|
||||
|
||||
}} // namespace lib::test
|
||||
Loading…
Reference in a new issue