2009-07-19 05:47:36 +02:00
|
|
|
/*
|
|
|
|
|
SingletonSubclass(Test) - actually creating a subclass of the Singleton Type
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
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.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
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.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2009-09-30 00:27:14 +02:00
|
|
|
#include "lib/test/test-helper.hpp"
|
2013-09-01 17:36:05 +02:00
|
|
|
#include "lib/format-string.hpp"
|
2016-01-07 03:58:29 +01:00
|
|
|
#include "lib/format-cout.hpp"
|
2009-07-19 05:47:36 +02:00
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
2013-10-14 01:18:56 +02:00
|
|
|
#include "test-target-obj.hpp"
|
2013-10-20 03:19:36 +02:00
|
|
|
#include "lib/depend.hpp"
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
|
|
using boost::lexical_cast;
|
2013-10-20 03:19:36 +02:00
|
|
|
using util::isSameObject;
|
2013-09-01 17:36:05 +02:00
|
|
|
using util::_Fmt;
|
2009-07-19 05:47:36 +02:00
|
|
|
using util::isnil;
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
|
2009-09-30 00:27:14 +02:00
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
2009-07-19 05:47:36 +02:00
|
|
|
|
2013-10-20 03:19:36 +02:00
|
|
|
using lumiera::error::LUMIERA_ERROR_LIFECYCLE;
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Target object to be instantiated as Singleton
|
|
|
|
|
* Allocates a variable amount of additional heap memory
|
|
|
|
|
* and prints diagnostic messages.
|
|
|
|
|
*/
|
|
|
|
|
class Interface : public TestTargetObj
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static int cnt;
|
|
|
|
|
static void setCountParam (uint c) { Interface::cnt = c; }
|
|
|
|
|
|
|
|
|
|
virtual string identify() { return "Interface"; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Interface () : TestTargetObj(cnt) {}
|
|
|
|
|
virtual ~Interface() {}
|
|
|
|
|
|
2013-10-20 21:51:28 +02:00
|
|
|
friend class lib::DependencyFactory;
|
2009-07-19 05:47:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int Interface::cnt = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Impl : public Interface
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual string identify() { return "Implementation"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for checking the safety.....
|
|
|
|
|
class Impl_XXX : public Impl { };
|
|
|
|
|
class Unrelated { };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
/***************************************************************//**
|
2009-07-19 05:47:36 +02:00
|
|
|
* @test specialised variant of the Singleton Factory, for creating
|
2010-12-10 02:55:40 +01:00
|
|
|
* subclasses (implementation classes) without coupling the
|
2009-07-19 05:47:36 +02:00
|
|
|
* caller to the concrete class type.
|
|
|
|
|
* Expected results: an instance of the subclass is created.
|
2013-10-20 03:19:36 +02:00
|
|
|
* @see lib::Depend
|
|
|
|
|
* @see lib::buildSingleton()
|
|
|
|
|
* @see lib/dependency-factory.hpp
|
2009-07-19 05:47:36 +02:00
|
|
|
*/
|
|
|
|
|
class SingletonSubclass_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
2010-12-17 22:51:27 +01:00
|
|
|
virtual void
|
|
|
|
|
run(Arg arg)
|
2009-07-19 05:47:36 +02:00
|
|
|
{
|
|
|
|
|
uint num= isnil(arg)? 1 : lexical_cast<uint>(arg[1]);
|
|
|
|
|
|
2013-09-01 17:36:05 +02:00
|
|
|
cout << _Fmt("using the Singleton should create TargetObj(%d)...\n") % num;
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
Interface::setCountParam(num);
|
|
|
|
|
|
|
|
|
|
// marker to declare the concrete type to be created
|
2013-10-20 03:19:36 +02:00
|
|
|
DependencyFactory::InstanceConstructor factoryFunction = buildSingleton<Impl>();
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
// define an instance of the Singleton factory,
|
2013-09-01 17:36:05 +02:00
|
|
|
// specialised to create the concrete Type passed in
|
2013-10-20 03:19:36 +02:00
|
|
|
Depend<Interface> instance (factoryFunction);
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
// Now use the Singleton factory...
|
|
|
|
|
// Note: we get the Base type
|
|
|
|
|
Interface& t1 = instance();
|
|
|
|
|
Interface& t2 = instance();
|
|
|
|
|
|
2013-10-20 03:19:36 +02:00
|
|
|
CHECK (isSameObject (t1, t2), "not a Singleton, got two different instances." );
|
2009-07-19 05:47:36 +02:00
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
cout << "calling a non-static method on the Singleton-"
|
2016-01-07 03:58:29 +01:00
|
|
|
<< t1.identify() << endl
|
|
|
|
|
<< t1 << endl;
|
2009-07-19 05:47:36 +02:00
|
|
|
|
2013-10-20 03:19:36 +02:00
|
|
|
verify_error_detection ();
|
2010-12-10 02:55:40 +01:00
|
|
|
}
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-17 22:51:27 +01:00
|
|
|
void
|
|
|
|
|
verify_error_detection ()
|
2009-07-19 05:47:36 +02:00
|
|
|
{
|
2013-10-20 03:19:36 +02:00
|
|
|
VERIFY_ERROR (LIFECYCLE, Depend<Interface> instance (buildSingleton<Impl_XXX>()) );
|
|
|
|
|
VERIFY_ERROR (LIFECYCLE, Depend<Interface> instance (buildSingleton<Unrelated>()) );
|
2009-07-19 05:47:36 +02:00
|
|
|
|
2013-10-20 03:19:36 +02:00
|
|
|
Depend<Interface> newFactory;
|
|
|
|
|
CHECK ( INSTANCEOF (Impl, &newFactory() )); // works as before
|
2009-07-19 05:47:36 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (SingletonSubclass_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-30 00:27:14 +02:00
|
|
|
}} // namespace lib::test
|