Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
singleton-subclass-test.cpp
Go to the documentation of this file.
1/*
2 SingletonSubclass(Test) - actually creating a subclass of the Singleton Type
3
4 Copyright (C)
5 2008, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
21#include "lib/format-string.hpp"
22#include "lib/format-cout.hpp"
23#include "lib/util.hpp"
24
25#include "test-target-obj.hpp"
26#include "lib/depend.hpp"
27#include "lib/depend-inject.hpp"
28
29#include <boost/lexical_cast.hpp>
30
31using boost::lexical_cast;
33using util::_Fmt;
34using util::isnil;
35using std::string;
36
37
38namespace lib {
39namespace test{
40
41 using LERR_(LIFECYCLE);
42
43
49 class Interface : public TestTargetObj
50 {
51 public:
52 static int cnt;
53 static void setCountParam (uint c) { Interface::cnt = c; }
54
55 virtual string identify() { return "Interface"; }
56
57 protected:
59 virtual ~Interface() {}
60
62 };
63
64 int Interface::cnt = 0;
65
66
67 class Impl : public Interface
68 {
69 public:
70 virtual string identify() { return "Implementation"; }
71 };
72
73
74 // for checking the safety.....
75 class Impl_XXX : public Impl { };
76 class Unrelated { };
77
78
79
80
81 /***************************************************************/
89 class SingletonSubclass_test : public Test
90 {
91
92 virtual void
93 run(Arg arg)
94 {
95 uint num{firstVal (arg)};
96 cout << _Fmt("using the Singleton should create TargetObj(%d)...\n") % num;
97
99
100 // configuration to use the subclass on demand
102
103 // define an instance of the Singleton factory as always...
104 Depend<Interface> instance;
105
106 // Now use the Singleton factory...
107 // Note: we get the Base type
108 Interface& t1 = instance();
109 Interface& t2 = instance();
110
111 CHECK (isSameObject (t1, t2), "not a Singleton, got two different instances." );
112 CHECK ( INSTANCEOF (Impl,&t1)); // got the subclass as expected
113 CHECK ("Implementation" == t2.identify());
114
115 cout << "calling a non-static method on the Singleton-"
116 << t1.identify() << endl
117 << t1 << endl;
118
120 }
121
122
123
124 void
126 {
128
129 Depend<Interface> newFactory;
130 CHECK ( INSTANCEOF (Impl, &newFactory() )); // works as before
131
133// DependInject<Interface>::useSingleton<Unrelated>();
134 }
135 };
136
137
138
141
142
143
144}} // namespace lib::test
This framework allows to (re)configure the lib::Depend front-end for dependency-injection.
Access point to singletons and other kinds of dependencies designated by type.
Definition depend.hpp:281
Helper to abstract creation and lifecycle of a dependency.
Definition depend.hpp:127
Target object to be instantiated as Singleton Allocates a variable amount of additional heap memory a...
static void setCountParam(uint c)
Target object to be created by Test-Factories or as Singleton.
A front-end for using printf-style formatting.
Per type specific configuration of instances created as service dependencies.
Singleton services and Dependency Injection.
#define LERR_(_NAME_)
Definition error.hpp:45
Automatically use custom string conversion in C++ stream output.
Front-end for printf-style string template interpolation.
unsigned int uint
Definition integral.hpp:29
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee's memory identities.
Definition util.hpp:421
bool isnil(lib::time::Duration const &dur)
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Dummy target object to be created by factory for unit tests Used to verify sane memory management and...
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
#define INSTANCEOF(CLASS, EXPR)
shortcut for subclass test, intended for assertions only.
Definition util.hpp:514