source: rrlib_util/test/test_singleton_pattern.cpp @ 44:b8711bbdd13e

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

Added lifetime policy Longevity for ordered destruction of singletons

File size: 4.3 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};
81//typedef tSingletonHolder<tLogImplementation, singleton::PhoenixSingleton> tLog;
82//typedef tSingletonHolder<tLogImplementation, singleton::NoDestruction> tLog;
83typedef tSingletonHolder<tLogImplementation, singleton::Longevity> tLog;
84unsigned int GetLongevity(tLogImplementation *)
85{
86  return 2;
87}
88
89struct tKeyboardImplementation
90{
91  tKeyboardImplementation()
92  {
93    std::cout << "Keyboard ctor" << std::endl;
94  }
95  ~tKeyboardImplementation()
96  {
97    std::cout << "Keyboard dtor" << std::endl;
98    tLog::Instance().Print("Keyboard destroyed.");
99  }
100
101  void Type(const std::string &message) const
102  {
103    std::cout << "keyboard: " << message << std::endl;
104  };
105};
106//typedef rrlib::util::tSingletonHolder<tKeyboardImplementation> tKeyboard;
107typedef rrlib::util::tSingletonHolder<tKeyboardImplementation, singleton::Longevity> tKeyboard;
108unsigned int GetLongevity(tKeyboardImplementation *)
109{
110  return 1;
111}
112
113struct tDisplayImplementation
114{
115  tDisplayImplementation()
116  {
117    std::cout << "Display ctor" << std::endl;
118  }
119  ~tDisplayImplementation()
120  {
121    std::cout << "Display dtor" << std::endl;
122    tLog::Instance().Print("Display destroyed.");
123  }
124
125  void Show(const std::string &message) const
126  {
127    std::cout << "display: " << message << std::endl;
128  };
129};
130//typedef rrlib::util::tSingletonHolder<tDisplayImplementation> tDisplay;
131typedef rrlib::util::tSingletonHolder<tDisplayImplementation, singleton::Longevity> tDisplay;
132unsigned int GetLongevity(tDisplayImplementation *)
133{
134  return 1;
135}
136
137int main(int argc, char **argv)
138{
139  tKeyboard::Instance().Type("foo");
140  tDisplay::Instance().Show("bar");
141
142  return EXIT_SUCCESS;
143}
Note: See TracBrowser for help on using the repository browser.