RSC  0.17.1
Singleton.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010, 2011 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <boost/noncopyable.hpp>
30 #include <boost/shared_ptr.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/bind.hpp>
33 
34 namespace rsc {
35 namespace patterns {
36 
50 template<typename T>
51 class Singleton: private boost::noncopyable {
52 public:
53 
54  virtual ~Singleton();
55 
61  static T& getInstance();
62 
72  static void killInstance();
73 private:
74  static boost::shared_ptr<T>& getStorage();
75 
76  static boost::mutex& getInstanceMutex();
77 
78  static void createMutex(boost::mutex*& destination);
79 
80 };
81 
82 // Singleton implementation
83 
84 template<typename T>
85 void Singleton<T>::createMutex(boost::mutex*& destination) {
86  destination = new boost::mutex();
87 }
88 
89 template<typename T>
91  static boost::mutex* instanceMutex;
92  static boost::once_flag instanceMutexOnceFlag = BOOST_ONCE_INIT;
93  boost::call_once(instanceMutexOnceFlag,
94  boost::bind(&Singleton::createMutex, boost::ref(instanceMutex)));
95  return *instanceMutex;
96 }
97 
98 template<typename T>
100  boost::mutex& instanceMutex = getInstanceMutex();
101  boost::mutex::scoped_lock lock(instanceMutex);
102 
103  boost::shared_ptr<T>& instance = getStorage();
104 
105  if (!instance) {
106  instance = boost::shared_ptr<T>(new T());
107  }
108 
109  return *instance;
110 }
111 
112 template<typename T>
114 }
115 
116 template<typename T>
118  boost::mutex& instanceMutex = getInstanceMutex();
119  boost::mutex::scoped_lock lock(instanceMutex);
120 
121  boost::shared_ptr<T>& instance = getStorage();
122 
123  if (instance) {
124  instance.reset();
125  }
126 }
127 
128 template<typename T>
129 boost::shared_ptr<T>& Singleton<T>::getStorage() {
130  static boost::shared_ptr<T> instance = boost::shared_ptr<T>();
131 
132  return instance;
133 }
134 
135 }
136 }
This template class implements the singleton pattern.
Definition: Singleton.h:51
static boost::mutex & getInstanceMutex()
Definition: Singleton.h:90
static boost::shared_ptr< T > & getStorage()
Definition: Singleton.h:129
static void createMutex(boost::mutex *&destination)
Definition: Singleton.h:85
static void killInstance()
This function can be used to make sure the instance is deleted at a particular time.
Definition: Singleton.h:117
static T & getInstance()
Retrieve the singleton instance, creating it if necessary.
Definition: Singleton.h:99