source: rrlib_util/patterns/singleton/tSingletonHolder.h @ 38:8433bff5a75a

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

Changed policy order in tSingletonHolder as it seems more likely that someone would want to change lifetime instead of creation

File size: 4.0 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    tSingletonHolder.h
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-10-26
27 *
28 * \brief Contains tSingletonHolder
29 *
30 * \b tSingletonHolder
31 *
32 */
33//----------------------------------------------------------------------
34#ifndef __rrlib__util__patterns__singleton__tSingletonHolder_h__
35#define __rrlib__util__patterns__singleton__tSingletonHolder_h__
36
37//----------------------------------------------------------------------
38// External includes (system with <>, local with "")
39//----------------------------------------------------------------------
40#include <boost/noncopyable.hpp>
41
42//----------------------------------------------------------------------
43// Internal includes with ""
44//----------------------------------------------------------------------
45
46//----------------------------------------------------------------------
47// Debugging
48//----------------------------------------------------------------------
49#include <cassert>
50
51//----------------------------------------------------------------------
52// Namespace declaration
53//----------------------------------------------------------------------
54namespace rrlib
55{
56namespace util
57{
58
59//----------------------------------------------------------------------
60// Forward declarations / typedefs / enums
61//----------------------------------------------------------------------
62
63//----------------------------------------------------------------------
64// Class declaration
65//----------------------------------------------------------------------
66//!
67/*!
68 *
69 */
70template <
71typename T,
72template <typename> class TLifetimePolicy = singleton::DefaultLifetime,
73template <typename> class TCreationPolicy = singleton::CreateUsingNew
74>
75class tSingletonHolder : public boost::noncopyable
76{
77
78//----------------------------------------------------------------------
79// Public methods and typedefs
80//----------------------------------------------------------------------
81public:
82
83  static T &GetInstance()
84  {
85    if (!tSingletonHolder::Instance())
86    {
87      if (tSingletonHolder::Destroyed())
88      {
89        TLifetimePolicy<T>::OnDeadReference();
90        tSingletonHolder::Destroyed() = false;
91      }
92      tSingletonHolder::Instance() = TCreationPolicy<T>::Create();
93      TLifetimePolicy<T>::ScheduleDestruction(&tSingletonHolder::DestroyInstance);
94    }
95    return *tSingletonHolder::Instance();
96  }
97
98//----------------------------------------------------------------------
99// Private fields and methods
100//----------------------------------------------------------------------
101private:
102
103  tSingletonHolder();
104
105  static void DestroyInstance()
106  {
107    assert(!tSingletonHolder::Destroyed());
108    TCreationPolicy<T>::Destroy(tSingletonHolder::Instance());
109    tSingletonHolder::Instance() = 0;
110    tSingletonHolder::Destroyed() = true;
111  }
112
113  static T *&Instance()
114  {
115    static T *instance = 0;
116    return instance;
117  }
118  static bool &Destroyed()
119  {
120    static bool destroyed = false;
121    return destroyed;
122  }
123
124};
125
126//----------------------------------------------------------------------
127// End of namespace declaration
128//----------------------------------------------------------------------
129}
130}
131
132#endif
Note: See TracBrowser for help on using the repository browser.