Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
multifact-argument-test.cpp
Go to the documentation of this file.
1/*
2 MultiFactArgument(Test) - passing additional invocation arguments to registered factory functions
3
4 Copyright (C)
5 2009, 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-cout.hpp"
22#include "lib/multifact.hpp"
23
24#include <functional>
25
26
27
28namespace lib {
29namespace test{
30
31 using std::bind;
32 using std::function;
33 using std::placeholders::_1;
35
36
37
38 namespace { // dummy fabrication function, creating wrapped numbers, controlled by an additional argument
39
40 enum prodID
41 { ONE = 1
43 };
44
45 struct Num { int n_; };
46
53 Num*
54 fabricateNumberz (int base, int offset)
55 {
56 cout << "fabricate("<<base<<", "<<offset<<")" << endl;
57 Num* product = new Num;
58 product->n_ = base*offset;
59 return product;
60 }
61
62
64 typedef factory::MultiFact< Num(int) // nominal signature of fabrication
65 , prodID // select factory function by prodID
66 , factory::BuildRefcountPtr // wrapper: manage product by smart-ptr
68
69 // for reference: type of an equivalent dispatcher table...
70 typedef std::map<prodID, function<Num(int)> > DispatcherMap;
71 }
72
73
74
75
76
77 /***************************************************************/
90 class MultiFactArgument_test : public Test
91 {
92 void
93 run (Arg)
94 {
95 TestFactory theFact;
96 theFact.defineProduction (ONE, bind (&fabricateNumberz, 1, _1 ));
97 theFact.defineProduction (TWO, bind (&fabricateNumberz, 2, _1 ));
98
99 cout << showSizeof (theFact) << endl;
100 CHECK (sizeof(theFact) == sizeof(DispatcherMap));
101
102 typedef TestFactory::Product PP;
103
104 PP p1 = theFact(ONE, 2);
105 PP p2 = theFact(TWO, 3);
106 CHECK (1*2 == p1->n_);
107 CHECK (2*3 == p2->n_);
108 }
109 };
110
111
114
115
116
117}} // namespace lib::test
Factory for creating a family of objects by ID.
void defineProduction(ID id, FUNC &&fun)
to set up a production line, associated with a specific ID
Automatically use custom string conversion in C++ stream output.
Framework for building a configurable factory, to generate families of related objects.
Num * fabricateNumberz(int base, int offset)
dummy "factory" function to be invoked
string showSizeof(size_t siz, string name)
for printing sizeof().
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Wrapper taking ownership, by wrapping into smart-ptr.
A collection of frequently used helper functions to support unit testing.