aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/singleton.h
blob: 5a31a17f4c8ce27969126b23a2d8a77355d90dc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/09/05
 * @version $Id$
 * @file    singleton.h
 * @brief   Singleton pattern.
 */


#pragma once

#include <list>

#include "mutex.h"


namespace fsa {

// {{{ class SingletonExitHandler

/**
 * @class SingletonExitHandler
 * @brief %Singleton exit handler.
 *
 * %Singleton exit handler. Uses the atexit() librarary call to
 * destory all Singleton objects in reverse order as they were
 * created. It is also a singleton self.
 */
class SingletonExitHandler
{
private:

  /** Default constructor */
  SingletonExitHandler();

  /** Method to call at exit, destroys all Singletons. */
  static void atExit();

  /** Instance pointer */
  static SingletonExitHandler* _instance;

  /** Destroy method -  does the dirty work */
  void destroy();


  using FunctionList = std::list<void(*)()>;
  using FunctionListIterator = std::list<void(*)()>::iterator;

  /** List of Singleton destroy functions */
  FunctionList _functionList;

public:

  /** Destructor */
  virtual ~SingletonExitHandler();

  /**
   * @brief Get instance pointer.
   *
   * @return pointer to instance.
   */
  static SingletonExitHandler* instance();

  /**
   * @brief Register a singleton.
   *
   * @param p Pointer to destroy function of the singleton.
   */
  void registerSingletonDestroyer(void (*p)());

};

// }}}

// {{{ class Singleton

/**
 * @class Singleton
 * @brief %Singleton template.
 *
 * %Singleton template (from Design Patterns by Gamma et al.). To use
 * it, subclass as follows, and make constructors private:
 *
 *  class MyClass : public Singleton<MyClass> {
 *    friend class Singleton<MyClass>;
 *  private:
 *    MyClass();
 *  public:
 *    void MyMethod();
 *  ...
 *  }
 *
 * and then call MyMethod as:
 *
 *   MyClass::instance().MyMethod();
 *
 */
template<typename T>
class Singleton
{
  /** SingletonExitHandler handles destruction. */
  friend class SingletonExitHandler;

public:
  /** Destructor */
  virtual ~Singleton();

  /**
   * @brief Get reference to the instance.
   *
   * Get reference to the instance. The first call of this method will
   * create the instance, and register the destroy function with the
   * exit handler.
   *
   * @return Reference to the instance.
   */
  static T& instance();

protected:

  /** Explicit constructor (to avoid implicit conversion). */
  explicit Singleton();

private:

  /** Copy constructor (unimplemented) */
  Singleton(const Singleton&);
  /** Assignment operator (unimplemented) */
  Singleton& operator=(const Singleton&);

  /** Destroy function - this will be registered with the exit handler. */
  static void destroy();

  static Mutex _lock;  /**< Mutex for synchronization. */

  static T* _instance; /**< Instance pointer.          */
};


template<typename T> Singleton<T>::Singleton() {}

template<typename T> Singleton<T>::~Singleton() {}

template<typename T> void Singleton<T>::destroy()
{
  delete _instance;
  _instance = NULL;
}

template<typename T> T& Singleton<T>::instance()
{
  if (_instance == NULL) {
    _lock.lock();
    if (_instance == NULL) {
      SingletonExitHandler::instance()->registerSingletonDestroyer(&destroy);
      _instance = new T();
    }
    _lock.unlock();
  }

  return *_instance;
}

template<typename T> T* Singleton<T>::_instance = NULL;

template<typename T> Mutex Singleton<T>::_lock;

// }}}

} // namespace fsa