lumiera_/tests/library/multifact-argument-test.cpp
Ichthyostega 806db414dd Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
 * there is no entity "Lumiera.org" which holds any copyrights
 * Lumiera source code is provided under the GPL Version 2+

== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''

The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!

The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00

117 lines
3.5 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
MultiFactArgument(Test) - passing additional invocation arguments to registered factory functions
Copyright (C)
2009, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** 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. See the file COPYING for further details.
* *****************************************************************/
/** @file multifact-argument-test.cpp
** unit test \ref MultiFactArgument_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/format-cout.hpp"
#include "lib/multifact.hpp"
#include <functional>
namespace lib {
namespace test{
using std::bind;
using std::function;
using std::placeholders::_1;
using lib::test::showSizeof;
namespace { // dummy fabrication function, creating wrapped numbers, controlled by an additional argument
enum prodID
{ ONE = 1
, TWO
};
struct Num { int n_; };
/** 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*
fabricateNumberz (int base, int offset)
{
cout << "fabricate("<<base<<", "<<offset<<")" << endl;
Num* product = new Num;
product->n_ = base*offset;
return product;
}
/** the factory instantiation used for this test */
typedef factory::MultiFact< Num(int) // nominal signature of fabrication
, prodID // select factory function by prodID
, factory::BuildRefcountPtr // wrapper: manage product by smart-ptr
> TestFactory;
// for reference: type of an equivalent dispatcher table...
typedef std::map<prodID, function<Num(int)> > DispatcherMap;
}
/***************************************************************//**
* @test define a MultiFact (factory with dynamic registration),
* which accepts additional arguments and passes them
* through to the registered factory function(s).
* @note we set up fabrication functions by binding the functions
* in such a way 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 query-resolver.cpp
*/
class MultiFactArgument_test : public Test
{
void
run (Arg)
{
TestFactory theFact;
theFact.defineProduction (ONE, bind (&fabricateNumberz, 1, _1 ));
theFact.defineProduction (TWO, bind (&fabricateNumberz, 2, _1 ));
cout << showSizeof (theFact) << endl;
CHECK (sizeof(theFact) == sizeof(DispatcherMap));
typedef TestFactory::Product PP;
PP p1 = theFact(ONE, 2);
PP p2 = theFact(TWO, 3);
CHECK (1*2 == p1->n_);
CHECK (2*3 == p2->n_);
}
};
/** Register this test class... */
LAUNCHER (MultiFactArgument_test, "unit common");
}} // namespace lib::test