This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
104template <class T>
106public:
120 inline static T& GetInstance() {
121 // Suppress undefined-var-template warnings from clang; _instance
122 // is expected to be instantiated in another translation unit via
123 // the TF_INSTANTIATE_SINGLETON macro.
124 ARCH_PRAGMA_PUSH
125 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
126 T *p = _instance.load();
127 if (!p) {
128 p = _CreateInstance(_instance);
129 }
130 ARCH_PRAGMA_POP
131 return *p;
132 }
133
138 inline static bool CurrentlyExists() {
139 // Suppress undefined-var-template warnings from clang; _instance
140 // is expected to be instantiated in another translation unit via
141 // the TF_INSTANTIATE_SINGLETON macro.
142 ARCH_PRAGMA_PUSH
143 ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
144 return static_cast<bool>(_instance.load());
145 ARCH_PRAGMA_POP
146 }
147
166 inline static void SetInstanceConstructed(T& instance);
167
176 inline static void DeleteInstance();
177
178private:
179 static T *_CreateInstance(std::atomic<T *> &instance);
180
181 static std::atomic<T *> _instance;
182};
183
184PXR_NAMESPACE_CLOSE_SCOPE
185
186#endif
Manage a single instance of an object (see.
Definition: singleton.h:105
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:138
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:120
Pragmas for controlling compiler-specific behaviors.