source: rrlib_util/test/test_factory_pattern.cpp @ 39:ad7a09181c77

Last change on this file since 39:ad7a09181c77 was 39:ad7a09181c77, checked in by Tobias Föhst <foehst@…>, 8 years ago

Made singleton threadsafe and renamed GetInstance() to Instance()

File size: 4.7 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
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
9// as published by the Free Software Foundation; either version 2
10// of 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    test_factory_pattern.cpp
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-10-26
27 *
28 */
29//----------------------------------------------------------------------
30
31//----------------------------------------------------------------------
32// External includes (system with <>, local with "")
33//----------------------------------------------------------------------
34#include <cstdlib>
35#include <string>
36#include <iostream>
37
38//----------------------------------------------------------------------
39// Internal includes with ""
40//----------------------------------------------------------------------
41#include "rrlib/util/patterns/factory.h"
42#include "rrlib/util/patterns/singleton.h"
43
44//----------------------------------------------------------------------
45// Debugging
46//----------------------------------------------------------------------
47
48//----------------------------------------------------------------------
49// Namespace usage
50//----------------------------------------------------------------------
51using namespace rrlib::util;
52
53//----------------------------------------------------------------------
54// Forward declarations / typedefs / enums
55//----------------------------------------------------------------------
56
57//----------------------------------------------------------------------
58// Const values
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Implementation
63//----------------------------------------------------------------------
64
65
66struct tShape
67{
68  tShape() {}
69  virtual ~tShape() {}
70};
71
72struct tLine : public tShape
73{
74  tLine()
75  {
76    std::cout << "Creating tLine" << std::endl;
77  }
78};
79
80struct tRectangle : public tShape
81{
82  tRectangle()
83  {
84    std::cout << "Creating tRectangle" << std::endl;
85  }
86};
87
88struct tCircle : public tShape
89{
90  tCircle()
91  {
92    std::cout << "Creating tCircle" << std::endl;
93  }
94};
95
96struct tEllipse : public tShape
97{
98  tEllipse()
99  {
100    std::cout << "Creating tEllipse" << std::endl;
101  }
102};
103
104
105tLine *CloneLine(const tShape &)
106{
107  std::cout << "Cloning a line" << std::endl;
108  return 0;
109}
110tRectangle *CloneRectangle(const tShape &)
111{
112  std::cout << "Cloning a rectangle" << std::endl;
113  return 0;
114}
115tCircle *CloneCircle(const tShape &)
116{
117  std::cout << "Cloning a circle" << std::endl;
118  return 0;
119}
120
121typedef tSingletonHolder<tFactory<tShape, std::string>> tShapeFactory;
122typedef tSingletonHolder<tCloneFactory<tShape>> tShapeCloneFactory;
123
124int main(int argc, char **argv)
125{
126  std::cout << "Testing factory" << std::endl;
127
128  tShapeFactory::Instance().Register<tLine>("line");
129  tShapeFactory::Instance().Register<tRectangle>("line");
130  tShapeFactory::Instance().Register<tRectangle>("rectangle");
131  tShapeFactory::Instance().Register<tCircle>("circle");
132  tShapeFactory::Instance().Register<tEllipse>("ellipse");
133
134  tShape *line = tShapeFactory::Instance().Create("line");
135  tShape *rectangle = tShapeFactory::Instance().Create("rectangle");
136  tShape *circle = tShapeFactory::Instance().Create("circle");
137  tShape *ellipse = tShapeFactory::Instance().Create("ellipse");
138
139  std::cout << "Testing clone factory" << std::endl;
140
141  tShapeCloneFactory::Instance().Register(*line, &CloneLine);
142  tShapeCloneFactory::Instance().Register(rectangle, &CloneRectangle);
143  tShapeCloneFactory::Instance().Register(*circle, &CloneCircle);
144
145  tShapeCloneFactory::Instance().Create(*line);
146  tShapeCloneFactory::Instance().Create(*rectangle);
147  tShapeCloneFactory::Instance().Create(*circle);
148//  tShapeCloneFactory::Instance().Create(*ellipse);
149
150  tShapeCloneFactory::Instance().Create(line);
151  tShapeCloneFactory::Instance().Create(rectangle);
152  tShapeCloneFactory::Instance().Create(circle);
153
154
155
156
157//  tShape *foo = tShapeFactory::GetInstance().Create("foo");
158
159
160  delete line;
161  delete rectangle;
162  delete circle;
163  delete ellipse;
164
165  return EXIT_SUCCESS;
166}
Note: See TracBrowser for help on using the repository browser.