Ocean
Singleton.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_SINGLETON_H
9 #define META_OCEAN_BASE_SINGLETON_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Lock.h"
14 
15 #include <cstdlib>
16 
17 namespace Ocean
18 {
19 
20 /**
21  * This template class is the base class for all singleton objects.
22  * Each object derived from this class can only have at most one instance at the same time within the same process unless standalone dynamic libraries attached to the process.<br>
23  * By default, in the case a standalone dynamic library is attached, this library has it's own global variables and thus also provides own singleton objects.<br>
24  * If only one unique process-wide singleton is desired for applications with standalone dynamic libraries, the OceanManager object needs to be initialized accordingly directly at process beginning and directly during initialization of the dynamic library.<br>
25  *
26  * Beware: The derived class must not be accomplished entirely with inline functions.<br>
27  * Further, the derived class has to defined the Singleton base class as friend class.<br>
28  * @tparam T Class type that will be extended as singleton
29  * @see OceanManager.
30  *
31  * See this tutorial:
32  * @code
33  * // Any class using the thread class as base class.
34  * class DerivedClass : public Singleton<DerivedClass>
35  * {
36  * friend class Singleton<DerivedClass>;
37  *
38  * public:
39  *
40  * // Any function.
41  * void anyFunction()
42  * {
43  * // do something here
44  * }
45  *
46  * private:
47  *
48  * // Private constructor ensuring that this object can only be created by the singleton mechanism.
49  * DerivedClass()
50  * {
51  * // initialize something here
52  * }
53  *
54  * // Private destructor ensuring that this object can only be disposed by the singleton mechanism.
55  * ~DerivedClass()
56  * {
57  * // release something here
58  * }
59  * };
60  *
61  * void anywhereInYourCode()
62  * {
63  * // accessing the singleton object anywhere in your code
64  * DerivedClass::get().anyFunction();
65  * }
66  * @endcode
67  * @ingroup base
68  */
69 template <typename T>
70 class Singleton
71 {
72  public:
73 
74  /**
75  * Returns a reference to the unique object.
76  * @return Reference to the object
77  */
78  static T& get();
79 
80  protected:
81 
82  /**
83  * Default constructor.
84  */
85  Singleton() = default;
86 
87  private:
88 
89  /**
90  * Disabled copy constructor.
91  * @param singleton The singleton object that would be copied
92  */
93  Singleton(const Singleton<T>& singleton) = delete;
94 
95  /**
96  * Returns a pointer to the unique object.
97  * @return Pointer to the object
98  */
99  static T* internalGet();
100 
101  /**
102  * Releases the singleton object.
103  * Beware: This function should be call by the OceanManager only.
104  */
105  static void releaseSingleton();
106 
107  /**
108  * Disabled assign operator.
109  * @param singleton The singleton object that would be assigned
110  */
111  Singleton& operator=(const Singleton<T>& singleton) = delete;
112 };
113 
114 template <typename T>
116 {
117  return *internalGet();
118 }
119 
120 template <typename T>
122 {
123  static T* singleton = nullptr;
124 
125  if (!singleton)
126  {
127  static Lock lock;
128  const ScopedLock scopedLock(lock);
129 
130  if (!singleton)
131  {
132  singleton = new T();
133  ocean_assert(singleton);
134 
135  OceanManager::get().registerSingleton(releaseSingleton);
136  }
137  }
138 
139  ocean_assert(singleton);
140  return singleton;
141 }
142 
143 template <typename T>
145 {
146  ocean_assert(internalGet() != nullptr);
147  delete internalGet();
148 }
149 
150 }
151 
152 #endif // FACEBOOK_SINGLETON_H
This class implements a recursive lock object.
Definition: Lock.h:31
void registerSingleton(const SingletonDestroyFunction &singletonDestroyFunction)
Registers a new singleton object.
static OceanManager & get()
Returns a reference to the OceanManager object.
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
static void releaseSingleton()
Releases the singleton object.
Definition: Singleton.h:144
static T & get()
Returns a reference to the unique object.
Definition: Singleton.h:115
Singleton()=default
Default constructor.
static T * internalGet()
Returns a pointer to the unique object.
Definition: Singleton.h:121
Singleton & operator=(const Singleton< T > &singleton)=delete
Disabled assign operator.
Singleton(const Singleton< T > &singleton)=delete
Disabled copy constructor.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15