WIP adapt unit test and the argument-accepting MultiFact specialisation

This commit is contained in:
Fischlurch 2009-10-28 04:45:17 +01:00
parent 2530e8c1a1
commit 0cef067c11
5 changed files with 62 additions and 30 deletions

View file

@ -41,6 +41,8 @@
#include "lib/multifact.hpp" #include "lib/multifact.hpp"
#include <tr1/functional>
namespace lib { namespace lib {
@ -54,7 +56,7 @@ namespace lib {
struct FabTraits<TY(ARG)> struct FabTraits<TY(ARG)>
{ {
typedef TY RawProduct; typedef TY RawProduct;
typedef RawProduct FacSig(ARG); typedef RawProduct FabSig(ARG);
typedef ARG Argument; typedef ARG Argument;
}; };

View file

@ -40,6 +40,7 @@
#include "util.hpp" #include "util.hpp"
#include <tr1/functional> #include <tr1/functional>
#include <tr1/memory>
#include <map> #include <map>
@ -55,17 +56,33 @@ namespace lib {
template<typename TAR> template<typename TAR>
struct PassReference struct PassReference
{ {
typedef TAR& RType;
typedef TAR& PType; typedef TAR& PType;
PType wrap (TAR& object) { return object; } PType wrap (RType object) { return object; }
}; };
/**
* Wrapper taking ownership,
* by wrapping into smart-ptr
*/
template<typename TAR>
struct BuildRefcountPtr
{
typedef TAR* RType;
typedef std::tr1::shared_ptr<TAR> PType;
PType wrap (RType ptr) { return PType (ptr); }
};
template<typename TY> template<typename TY>
struct FabTraits struct FabTraits
{ {
typedef TY RawProduct; typedef TY RawProduct;
typedef RawProduct FacSig(void); typedef RawProduct FabSig(void);
}; };
@ -78,7 +95,7 @@ namespace lib {
template<typename TY, typename ID> template<typename TY, typename ID>
struct Fab struct Fab
{ {
typedef typename FabTraits<TY>::FacSig Signature; typedef typename FabTraits<TY>::FabSig Signature;
typedef std::tr1::function<Signature> FactoryFunc; typedef std::tr1::function<Signature> FactoryFunc;
@ -126,15 +143,18 @@ namespace lib {
class MultiFact class MultiFact
: Wrapper<typename FabTraits<TY>::RawProduct> : Wrapper<typename FabTraits<TY>::RawProduct>
{ {
typedef Fab<TY,ID> _Fab; typedef Wrapper<typename FabTraits<TY>::RawProduct> _Wrap;
typedef typename FabTraits<TY>::RawProduct RawType;
typedef typename _Wrap::PType WrappedProduct;
typedef typename _Wrap::RType FabProduct;
typedef Fab<FabProduct,ID> _Fab;
_Fab funcTable_; _Fab funcTable_;
protected:
typedef typename FabTraits<TY>::RawProduct RawType;
typedef typename Wrapper<RawType>::PType Product;
typedef typename _Fab::FactoryFunc Creator;
protected:
typedef typename _Fab::FactoryFunc Creator;
typedef WrappedProduct Product;
Creator& Creator&
selectProducer (ID const& id) selectProducer (ID const& id)

View file

@ -2,6 +2,14 @@ TESTING "Proc Layer config rules Test Suite" ./test-lib --group=query
PLANNED "issuing typed queries" QueryResolver_test <<END
END
PLANNED "sub-extensible ID" SubID_test <<END
END
TEST "simple query" QueryUtils_test Query <<END TEST "simple query" QueryUtils_test Query <<END
out: N7lumiera5query4test5ThingE: I am writing a test sentence. out: N7lumiera5query4test5ThingE: I am writing a test sentence.
END END

View file

@ -71,10 +71,6 @@ PLANNED "Query focus management" QueryFocus_test <<END
END END
PLANNED "issuing typed queries" QueryResolver_test <<END
END
PLANNED "RebuildFixture_test" RebuildFixture_test <<END PLANNED "RebuildFixture_test" RebuildFixture_test <<END
END END

View file

@ -23,7 +23,7 @@
#include "lib/test/run.hpp" #include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp" #include "lib/test/test-helper.hpp"
#include "lib/multifact.hpp" #include "lib/multifact-arg.hpp"
//#include "lib/util.hpp" //#include "lib/util.hpp"
//#include <boost/lexical_cast.hpp> //#include <boost/lexical_cast.hpp>
@ -45,27 +45,27 @@ namespace test{
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::tr1::bind; using std::tr1::bind;
using std::tr1::function;
using std::tr1::placeholders::_1;
// using lumiera::error::LUMIERA_ERROR_INVALID; // using lumiera::error::LUMIERA_ERROR_INVALID;
namespace { // a test-dummy ID type, used to encapsulate additional arguments namespace { // dummy fabrication function, creating wrapped numbers, controlled by an additional argument
enum baseType enum prodID
{ ONE = 1 { ONE = 1
, TWO , TWO
}; };
struct DummyID
{
baseType bas;
int additionalInfo;
};
struct Num { int n_; }; struct Num { int n_; };
/** dummy "factory" function to be invoked */ /** dummy "factory" function to be invoked
* @return pointer to heap allocated product object
* @note this function needs to deliver the product in a form
* which can be accepted by the concrete wrapper, which
* is going to be configured into the factory.
*/
Num* Num*
fabricateNumberz (int base, int offset) fabricateNumberz (int base, int offset)
{ {
@ -76,7 +76,11 @@ namespace test{
} }
typedef factory::MultiFact<Num, DummyID, factory::BuildRefcountPtr> TestFactory; /** the factory instantiation used for this test */
typedef factory::MultiFact< function<Num(int)> // nominal signature of fabrication
, prodID // select factory function by prodID
, factory::BuildRefcountPtr // wrapper: manage product by smart-ptr
> TestFactory;
} }
@ -88,6 +92,11 @@ namespace test{
* @test define a MultiFact (factory with dynamic registration), * @test define a MultiFact (factory with dynamic registration),
* which accepts additional arguments and passes them * which accepts additional arguments and passes them
* through to the registered factory function(s). * through to the registered factory function(s).
* @note we set up fabrication functions by binding such as to match
* the function signature declared in the factory; thereby one
* argument remains unclosed, which is the argument to be
* supplied on each factory invocation by the client code.
*
* @see lib::MultiFact * @see lib::MultiFact
* @see query-resolver.cpp * @see query-resolver.cpp
*/ */
@ -104,11 +113,8 @@ namespace test{
typedef TestFactory::Product PP; typedef TestFactory::Product PP;
DummyID id1 = {ONE, 2}; PP p1 = theFact(ONE, 2);
DummyID id1 = {TWO, 3}; PP p2 = theFact(TWO, 3);
PP p1 = theFact(id1);
PP p2 = theFact(id2);
ASSERT (1*2 == p1->n_); ASSERT (1*2 == p1->n_);
ASSERT (2*3 == p2->n_); ASSERT (2*3 == p2->n_);
} }