Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSingleton.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 
23 /**
24  @example ExampleSingleton.cpp
25  @relates Singleton
26  @brief Example for Singleton usage
27  @ingroup g_examples
28  @author woelk
29 */
30 
31 #include <Base/Common/Singleton.hh>
32 
33 using namespace BIAS;
34 using namespace std;
35 
36 /** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
37 /* myclass */
38 class MyClass:public Singleton<MyClass> {
39 public:
40  double data;
41 
42  /// make sure that class MyClass cannot be used as non-singleton by
43  /// declaring constructors and copy operator private
44  MyClass() : data(0) {}
45  MyClass(const MyClass &a) : data(a.data) {}
46  MyClass& operator=(const MyClass& a) { data = a.data; return *this; }
47 };
48 
49 typedef Singleton<MyClass> MyClassSingleton;
50 
51 
52 void function()
53 {
54  // set the singleton with 5
55  MyClassSingleton::GetInstance()->data = 5;
56 }
57 /** \endcond */
58 
59 /** @author woelk 10/2007 (c) www.vision-n.de */
60 int main(int argc, char *argv[])
61 {
62  cout << "initial data: " << MyClassSingleton::GetInstance()->data << endl;
63 
64  function();
65 
66  cout << "after function: " << MyClassSingleton::GetInstance()->data << endl;
67 
68  cout << "accessing Singleton<MyClass> is identical to MyClassSingleton: "
69  <<Singleton<MyClass>::GetInstance()->data << endl;
70 
71  // does not compile, because constructor of MyClass is private
72  //MyClass a;
73 
74  return 0;
75 }
static T * GetInstance()
Definition: Singleton.hh:152
Simple singleton implementation for multithreaded applications.
Definition: Singleton.hh:86