Lumiera  0.pre.03
»edit your freedom«
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) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/format-cout.hpp"
31 #include "lib/multifact.hpp"
32 
33 #include <functional>
34 
35 
36 
37 namespace lib {
38 namespace test{
39 
40  using std::bind;
41  using std::function;
42  using std::placeholders::_1;
43  using lib::test::showSizeof;
44 
45 
46 
47  namespace { // dummy fabrication function, creating wrapped numbers, controlled by an additional argument
48 
49  enum prodID
50  { ONE = 1
51  , TWO
52  };
53 
54  struct Num { int n_; };
55 
62  Num*
63  fabricateNumberz (int base, int offset)
64  {
65  cout << "fabricate("<<base<<", "<<offset<<")" << endl;
66  Num* product = new Num;
67  product->n_ = base*offset;
68  return product;
69  }
70 
71 
73  typedef factory::MultiFact< Num(int) // nominal signature of fabrication
74  , prodID // select factory function by prodID
75  , factory::BuildRefcountPtr // wrapper: manage product by smart-ptr
77 
78  // for reference: type of an equivalent dispatcher table...
79  typedef std::map<prodID, function<Num(int)> > DispatcherMap;
80  }
81 
82 
83 
84 
85 
86  /***************************************************************/
99  class MultiFactArgument_test : public Test
100  {
101  void
102  run (Arg)
103  {
105  theFact.defineProduction (ONE, bind (&fabricateNumberz, 1, _1 ));
106  theFact.defineProduction (TWO, bind (&fabricateNumberz, 2, _1 ));
107 
108  cout << showSizeof (theFact) << endl;
109  CHECK (sizeof(theFact) == sizeof(DispatcherMap));
110 
111  typedef TestFactory::Product PP;
112 
113  PP p1 = theFact(ONE, 2);
114  PP p2 = theFact(TWO, 3);
115  CHECK (1*2 == p1->n_);
116  CHECK (2*3 == p2->n_);
117  }
118  };
119 
120 
122  LAUNCHER (MultiFactArgument_test, "unit common");
123 
124 
125 
126 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
factory::MultiFact< Num(int), prodID, factory::BuildRefcountPtr > TestFactory
the factory instantiation used for this test
Definition: run.hpp:49
Framework for building a configurable factory, to generate families of related objects.
string showSizeof(size_t siz, string name)
for printing sizeof().
Definition: test-helper.cpp:57
Implementation namespace for support and library code.
Num * fabricateNumberz(int base, int offset)
dummy "factory" function to be invoked
Factory for creating a family of objects by ID.
Definition: multifact.hpp:262
Simple test class runner.
A collection of frequently used helper functions to support unit testing.
void defineProduction(ID id, FUNC &&fun)
to set up a production line, associated with a specific ID
Definition: multifact.hpp:320
Wrapper taking ownership, by wrapping into smart-ptr.
Definition: multifact.hpp:108