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)
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"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/format-cout.hpp"
22 #include "lib/multifact.hpp"
23 
24 #include <functional>
25 
26 
27 
28 namespace lib {
29 namespace test{
30 
31  using std::bind;
32  using std::function;
33  using std::placeholders::_1;
34  using lib::test::showSizeof;
35 
36 
37 
38  namespace { // dummy fabrication function, creating wrapped numbers, controlled by an additional argument
39 
40  enum prodID
41  { ONE = 1
42  , TWO
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  {
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 
113  LAUNCHER (MultiFactArgument_test, "unit common");
114 
115 
116 
117 }} // 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:40
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:49
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:253
Simplistic 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:311
Wrapper taking ownership, by wrapping into smart-ptr.
Definition: multifact.hpp:99