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
registryManager.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_REGISTRY_MANAGER_H
8#define PXR_BASE_TF_REGISTRY_MANAGER_H
9
12
13#include "pxr/pxr.h"
14
16#include "pxr/base/tf/preprocessorUtilsLite.h"
17#include "pxr/base/tf/api.h"
18
19#include <functional>
20#include <typeinfo>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
32 TfRegistryManager(const TfRegistryManager&) = delete;
33 TfRegistryManager& operator=(const TfRegistryManager&) = delete;
34
35public:
36 // The type of a registration function. The arguments are not used.
37 typedef void (*RegistrationFunctionType)(void*, void*);
38 typedef std::function<void ()> UnloadFunctionType;
39
42
50 template <class T>
51 void SubscribeTo() {
52 _SubscribeTo(typeid(T));
53 }
54
59 template <class T>
61 _UnsubscribeFrom(typeid(T));
62 }
63
84 TF_API bool AddFunctionForUnload(const UnloadFunctionType&);
85
96 TF_API static void RunUnloadersAtExit();
97
98private:
99 TF_API TfRegistryManager();
100 TF_API ~TfRegistryManager();
101
102 TF_API void _SubscribeTo(const std::type_info&);
103 TF_API void _UnsubscribeFrom(const std::type_info&);
104};
105
106TF_API void Tf_RegistryInitCtor(char const *name);
107TF_API void Tf_RegistryInitDtor(char const *name);
108
109namespace {
110struct Tf_RegistryStaticInit {
111 Tf_RegistryStaticInit() {
112 Tf_RegistryInitCtor(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME));
113 }
114 ~Tf_RegistryStaticInit() {
115 Tf_RegistryInitDtor(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME));
116 }
117};
118}
119
120// Private class used to indicate the library has finished registering
121// functions, to indicate that the library is being unloaded and to
122// add functions to the registry.
123class Tf_RegistryInit {
124public:
125 TF_API static void Add(const char* libName,
126 TfRegistryManager::RegistrationFunctionType func,
127 const char* typeName);
128 template <class T, class U>
129 static void Add(const char* libName,
130 void (*func)(T*, U*),
131 const char* typeName)
132 {
133 Add(libName,(TfRegistryManager::RegistrationFunctionType)func,typeName);
134 }
135};
136
137// The ARCH_CONSTRUCTOR priority for registering registry functions.
138#define TF_REGISTRY_PRIORITY 100
139
140//
141// Macros for adding registry functions outside class templates.
142//
143
144// Define a registry function outside of a template. Follow the macro with
145// the body of the function inside braces. KEY_TYPE and TAG must be types.
146#define TF_REGISTRY_DEFINE_WITH_TYPE(KEY_TYPE, TAG) \
147 static void _Tf_RegistryFunction(KEY_TYPE*, TAG*); \
148 ARCH_CONSTRUCTOR(TF_PP_CAT(_Tf_RegistryAdd, __LINE__), \
149 TF_REGISTRY_PRIORITY, KEY_TYPE*, TAG*) \
150 { \
151 Tf_RegistryInit::Add(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME), \
152 (void(*)(KEY_TYPE*, TAG*))_Tf_RegistryFunction, \
153 TF_PP_STRINGIZE(KEY_TYPE)); \
154 } \
155 _ARCH_ENSURE_PER_LIB_INIT(Tf_RegistryStaticInit, _tfRegistryInit); \
156 static void _Tf_RegistryFunction(KEY_TYPE*, TAG*)
157
158// Define a registry function outside of a template. Follow the macro with
159// the body of the function inside braces. KEY_TYPE must be a type and NAME
160// must be a valid C++ name.
161#define TF_REGISTRY_DEFINE(KEY_TYPE, NAME) \
162 static void TF_PP_CAT(_Tf_RegistryFunction, NAME)(KEY_TYPE*, void*); \
163 ARCH_CONSTRUCTOR(TF_PP_CAT(_Tf_RegistryAdd, NAME), \
164 TF_REGISTRY_PRIORITY, KEY_TYPE*) \
165 { \
166 Tf_RegistryInit::Add(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME), \
167 (void(*)(KEY_TYPE*, void*)) \
168 TF_PP_CAT(_Tf_RegistryFunction, NAME), \
169 TF_PP_STRINGIZE(KEY_TYPE)); \
170 } \
171 _ARCH_ENSURE_PER_LIB_INIT(Tf_RegistryStaticInit, _tfRegistryInit); \
172 static void TF_PP_CAT(_Tf_RegistryFunction, NAME)(KEY_TYPE*, void*)
173
174
202#define TF_REGISTRY_FUNCTION(KEY_TYPE) \
203 TF_REGISTRY_DEFINE(KEY_TYPE, __LINE__)
204
243#define TF_REGISTRY_FUNCTION_WITH_TAG(KEY_TYPE, TAG) \
244 TF_REGISTRY_DEFINE(KEY_TYPE, TF_PP_CAT(TAG, __LINE__))
245
246PXR_NAMESPACE_CLOSE_SCOPE
247
248#endif // PXR_BASE_TF_REGISTRY_MANAGER_H
Define function attributes.
Manage initialization of registries.
TF_API bool AddFunctionForUnload(const UnloadFunctionType &)
Add an action to be performed at code unload time.
static TF_API void RunUnloadersAtExit()
Run unload functions program exit time.
void SubscribeTo()
Request that any initialization for service T be performed.
void UnsubscribeFrom()
Cancel any previous subscriptions to service T.
static TF_API TfRegistryManager & GetInstance()
Return the singleton TfRegistryManager instance.