2009-10-26 01:39:25 +01:00
|
|
|
/*
|
2009-10-26 01:42:35 +01:00
|
|
|
MultiFactArgument(Test) - passing additional invocation arguments to registered factory functions
|
2009-10-26 01:39:25 +01:00
|
|
|
|
|
|
|
|
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"
|
2009-10-26 01:42:35 +01:00
|
|
|
//#include "lib/util.hpp"
|
2009-10-26 01:39:25 +01:00
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
//#include <boost/lexical_cast.hpp>
|
2009-10-26 01:39:25 +01:00
|
|
|
#include <iostream>
|
2009-10-26 01:42:35 +01:00
|
|
|
//#include <string>
|
|
|
|
|
#include <tr1/functional>
|
2009-10-26 01:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
// using boost::lexical_cast;
|
2009-10-26 01:39:25 +01:00
|
|
|
using lib::test::showSizeof;
|
2009-10-26 01:42:35 +01:00
|
|
|
// using util::isSameObject;
|
|
|
|
|
// using util::isnil;
|
|
|
|
|
// using std::ostream;
|
|
|
|
|
// using std::string;
|
2009-10-26 01:39:25 +01:00
|
|
|
using std::cout;
|
|
|
|
|
using std::endl;
|
2009-10-26 01:42:35 +01:00
|
|
|
using std::tr1::bind;
|
2009-10-26 01:39:25 +01:00
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
// using lumiera::error::LUMIERA_ERROR_INVALID;
|
2009-10-26 01:39:25 +01:00
|
|
|
|
|
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
namespace { // a test-dummy ID type, used to encapsulate additional arguments
|
2009-10-26 01:39:25 +01:00
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
enum baseType
|
2009-10-26 01:39:25 +01:00
|
|
|
{ ONE = 1
|
|
|
|
|
, TWO
|
|
|
|
|
};
|
|
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
struct DummyID
|
2009-10-26 01:39:25 +01:00
|
|
|
{
|
2009-10-26 01:42:35 +01:00
|
|
|
baseType bas;
|
|
|
|
|
int additionalInfo;
|
2009-10-26 01:39:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
struct Num { int n_; };
|
|
|
|
|
|
|
|
|
|
/** dummy "factory" function to be invoked */
|
|
|
|
|
Num*
|
|
|
|
|
fabricateNumberz (int base, int offset)
|
|
|
|
|
{
|
|
|
|
|
cout << "fabricate("<<base<<", "<<offset<<")" << endl;
|
|
|
|
|
Num* product = new Num;
|
|
|
|
|
product->n_ = base*offset;
|
|
|
|
|
return product;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef factory::MultiFact<Num, DummyID, factory::BuildRefcountPtr> TestFactory;
|
|
|
|
|
|
2009-10-26 01:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
2009-10-26 01:42:35 +01:00
|
|
|
* @test define a MultiFact (factory with dynamic registration),
|
|
|
|
|
* which accepts additional arguments and passes them
|
|
|
|
|
* through to the registered factory function(s).
|
2009-10-26 01:39:25 +01:00
|
|
|
* @see lib::MultiFact
|
2009-10-26 01:42:35 +01:00
|
|
|
* @see query-resolver.cpp
|
2009-10-26 01:39:25 +01:00
|
|
|
*/
|
2009-10-26 01:42:35 +01:00
|
|
|
class MultiFactArgument_test : public Test
|
2009-10-26 01:39:25 +01:00
|
|
|
{
|
|
|
|
|
void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
2009-10-26 01:42:35 +01:00
|
|
|
TestFactory theFact;
|
|
|
|
|
theFact.defineProduction (ONE, bind (&fabricateNumberz, 1, _1));
|
|
|
|
|
theFact.defineProduction (TWO, bind (&fabricateNumberz, 2, _1));
|
|
|
|
|
|
2009-10-26 01:39:25 +01:00
|
|
|
cout << showSizeof (theFact) << endl;
|
|
|
|
|
|
2009-10-27 05:13:38 +01:00
|
|
|
typedef TestFactory::Product PP;
|
2009-10-26 01:39:25 +01:00
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
DummyID id1 = {ONE, 2};
|
|
|
|
|
DummyID id1 = {TWO, 3};
|
2009-10-26 01:39:25 +01:00
|
|
|
|
2009-10-26 01:42:35 +01:00
|
|
|
PP p1 = theFact(id1);
|
|
|
|
|
PP p2 = theFact(id2);
|
|
|
|
|
ASSERT (1*2 == p1->n_);
|
|
|
|
|
ASSERT (2*3 == p2->n_);
|
2009-10-26 01:39:25 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
2009-10-26 01:42:35 +01:00
|
|
|
LAUNCHER (MultiFactArgument_test, "unit common");
|
2009-10-26 01:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|