lumiera_/tests/library/multifact-singleton-test.cpp

135 lines
3.8 KiB
C++
Raw Normal View History

/*
MultiFactSingleton(Test) - using MultiFact to manage a family of singletons
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
Copyright (C)
2009, Hermann Vosseler <Ichthyostega@web.de>
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
  **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.
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
* *****************************************************************/
/** @file multifact-singleton-test.cpp
** unit test \ref MultiFactSingleton_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/multifact.hpp"
#include "lib/format-cout.hpp"
#include "lib/util.hpp"
#include <boost/lexical_cast.hpp>
#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 LERR_(INVALID);
namespace { // hierarchy of test dummy objects
struct Interface
{
virtual ~Interface() {};
virtual operator string () const =0;
};
enum theID
{ ONE = 1
, TWO
, THR
, FOU
};
typedef factory::MultiFact<Interface&, theID> TestFactory;
template<theID ii>
class Implementation
: public Interface
{
operator string() const override
{
return "Impl-"+lexical_cast<string> (ii);
}
};
/** Factory instance for the tests... */
TestFactory theFact;
// Configure the products to be fabricated....
2016-12-23 04:23:03 +01:00
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 the use of the MultiFact template to access Singletons.
* While generally speaking the MultiFact allows us to address
* and invoke several "production lines" by ID, an obvious
* use case would be to access a "family" of singletons
* through this mechanism. And indeed, \c MultiFact::Singleton
* is a preconfigured shortcut for this use case. The actual
* singleton access factories are placed into a static context
* (here in the anonymous namespace above) and their access
* operation is wired as "factory function".
* - we use a hierarchy of test dummy objects
* - we set up a singleton factory for several subclasses
* - the registration happens automatically in the ctor
* - we verify that we indeed get the correct 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) );
2016-12-23 04:23:03 +01:00
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