Loading...
Searching...
No Matches
singleton.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_SINGLETON_H
8#define PXR_BASE_TF_SINGLETON_H
9
89
90#include "pxr/pxr.h"
92
93#include <atomic>
94
95PXR_NAMESPACE_OPEN_SCOPE
96
97struct Tf_SingletonInitState;
98
106template <class T>
108public:
122 inline static T& GetInstance() {
123 // Suppress undefined-var-template warnings from clang; _instance
124 // is expected to be instantiated in another translation unit via
125 // the TF_INSTANTIATE_SINGLETON macro.
126 ARCH_PRAGMA_PUSH
127 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
128 T *p = _instance.load();
129 if (!p) {
130 p = _CreateOrWaitForInstance(_instance);
131 }
132 ARCH_PRAGMA_POP
133 return *p;
134 }
135
140 inline static bool CurrentlyExists() {
141 // Suppress undefined-var-template warnings from clang; _instance
142 // is expected to be instantiated in another translation unit via
143 // the TF_INSTANTIATE_SINGLETON macro.
144 ARCH_PRAGMA_PUSH
145 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
146 return static_cast<bool>(_instance.load());
147 ARCH_PRAGMA_POP
148 }
149
173 inline static void SetInstanceConstructed(T& instance);
174
183 inline static void DeleteInstance();
184
185private:
186 static T *_CreateOrWaitForInstance(std::atomic<T *> &instance);
187
188 static std::atomic<T *> _instance;
189 static std::atomic<Tf_SingletonInitState *> _initState;
190};
191
192PXR_NAMESPACE_CLOSE_SCOPE
193
194#endif
Manage a single instance of an object (see.
Definition: singleton.h:107
static void DeleteInstance()
Destroy the sole instance object of type T, if it exists.
static void SetInstanceConstructed(T &instance)
Indicate that the sole instance object has already been created.
static bool CurrentlyExists()
Return whether or not the single object of type T is currently in existence.
Definition: singleton.h:140
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:122
Pragmas for controlling compiler-specific behaviors.