source: rrlib_util/test/test_singleton_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: 3.8 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_singleton_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/singleton.h"
42
43//----------------------------------------------------------------------
44// Debugging
45//----------------------------------------------------------------------
46
47//----------------------------------------------------------------------
48// Namespace usage
49//----------------------------------------------------------------------
50using namespace rrlib::util;
51
52//----------------------------------------------------------------------
53// Forward declarations / typedefs / enums
54//----------------------------------------------------------------------
55
56//----------------------------------------------------------------------
57// Const values
58//----------------------------------------------------------------------
59
60//----------------------------------------------------------------------
61// Implementation
62//----------------------------------------------------------------------
63
64
65struct tLogImplementation
66{
67  tLogImplementation()
68  {
69    std::cout << "Log ctor" << std::endl;
70  }
71  ~tLogImplementation()
72  {
73    std::cout << "Log dtor" << std::endl;
74  }
75
76  void Print(const std::string &message) const
77  {
78    std::cout << "log: " << message << std::endl;
79  }
80};
81typedef tSingletonHolder<tLogImplementation, singleton::PhoenixSingleton> tLog;
82//typedef tSingletonHolder<tLogImplementation, singleton::NoDestruction> tLog;
83
84struct tKeyboardImplementation
85{
86  tKeyboardImplementation()
87  {
88    std::cout << "Keyboard ctor" << std::endl;
89  }
90  ~tKeyboardImplementation()
91  {
92    std::cout << "Keyboard dtor" << std::endl;
93    tLog::Instance().Print("Keyboard destroyed.");
94  }
95
96  void Type(const std::string &message) const
97  {
98    std::cout << "keyboard: " << message << std::endl;
99  };
100};
101typedef rrlib::util::tSingletonHolder<tKeyboardImplementation> tKeyboard;
102
103struct tDisplayImplementation
104{
105  tDisplayImplementation()
106  {
107    std::cout << "Display ctor" << std::endl;
108  }
109  ~tDisplayImplementation()
110  {
111    std::cout << "Display dtor" << std::endl;
112    tLog::Instance().Print("Display destroyed.");
113  }
114
115  void Show(const std::string &message) const
116  {
117    std::cout << "display: " << message << std::endl;
118  };
119};
120typedef rrlib::util::tSingletonHolder<tDisplayImplementation> tDisplay;
121
122int main(int argc, char **argv)
123{
124  tKeyboard::Instance().Type("foo");
125  tDisplay::Instance().Show("bar");
126
127  return EXIT_SUCCESS;
128}
Note: See TracBrowser for help on using the repository browser.