Loading...
Searching...
No Matches
registryManager.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_REGISTRY_MANAGER_H
25#define PXR_BASE_TF_REGISTRY_MANAGER_H
26
29
30#include "pxr/pxr.h"
31
33#include "pxr/base/tf/preprocessorUtilsLite.h"
34#include "pxr/base/tf/api.h"
35
36#include <functional>
37#include <typeinfo>
38
39PXR_NAMESPACE_OPEN_SCOPE
40
49 TfRegistryManager(const TfRegistryManager&) = delete;
50 TfRegistryManager& operator=(const TfRegistryManager&) = delete;
51
52public:
53 // The type of a registration function. The arguments are not used.
54 typedef void (*RegistrationFunctionType)(void*, void*);
55 typedef std::function<void ()> UnloadFunctionType;
56
59
67 template <class T>
68 void SubscribeTo() {
69 _SubscribeTo(typeid(T));
70 }
71
76 template <class T>
78 _UnsubscribeFrom(typeid(T));
79 }
80
101 TF_API bool AddFunctionForUnload(const UnloadFunctionType&);
102
113 TF_API static void RunUnloadersAtExit();
114
115private:
116 TF_API TfRegistryManager();
117 TF_API ~TfRegistryManager();
118
119 TF_API void _SubscribeTo(const std::type_info&);
120 TF_API void _UnsubscribeFrom(const std::type_info&);
121};
122
123TF_API void Tf_RegistryInitCtor(char const *name);
124TF_API void Tf_RegistryInitDtor(char const *name);
125
126namespace {
127struct Tf_RegistryStaticInit {
128 Tf_RegistryStaticInit() {
129 Tf_RegistryInitCtor(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME));
130 }
131 ~Tf_RegistryStaticInit() {
132 Tf_RegistryInitDtor(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME));
133 }
134};
135}
136
137// Private class used to indicate the library has finished registering
138// functions, to indicate that the library is being unloaded and to
139// add functions to the registry.
140class Tf_RegistryInit {
141public:
142 TF_API static void Add(const char* libName,
143 TfRegistryManager::RegistrationFunctionType func,
144 const char* typeName);
145 template <class T, class U>
146 static void Add(const char* libName,
147 void (*func)(T*, U*),
148 const char* typeName)
149 {
150 Add(libName,(TfRegistryManager::RegistrationFunctionType)func,typeName);
151 }
152};
153
154// The ARCH_CONSTRUCTOR priority for registering registry functions.
155#define TF_REGISTRY_PRIORITY 100
156
157//
158// Macros for adding registry functions outside class templates.
159//
160
161// Define a registry function outside of a template. Follow the macro with
162// the body of the function inside braces. KEY_TYPE and TAG must be types.
163#define TF_REGISTRY_DEFINE_WITH_TYPE(KEY_TYPE, TAG) \
164 static void _Tf_RegistryFunction(KEY_TYPE*, TAG*); \
165 ARCH_CONSTRUCTOR(TF_PP_CAT(_Tf_RegistryAdd, __LINE__), \
166 TF_REGISTRY_PRIORITY, KEY_TYPE*, TAG*) \
167 { \
168 Tf_RegistryInit::Add(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME), \
169 (void(*)(KEY_TYPE*, TAG*))_Tf_RegistryFunction, \
170 TF_PP_STRINGIZE(KEY_TYPE)); \
171 } \
172 _ARCH_ENSURE_PER_LIB_INIT(Tf_RegistryStaticInit, _tfRegistryInit); \
173 static void _Tf_RegistryFunction(KEY_TYPE*, TAG*)
174
175// Define a registry function outside of a template. Follow the macro with
176// the body of the function inside braces. KEY_TYPE must be a type and NAME
177// must be a valid C++ name.
178#define TF_REGISTRY_DEFINE(KEY_TYPE, NAME) \
179 static void TF_PP_CAT(_Tf_RegistryFunction, NAME)(KEY_TYPE*, void*); \
180 ARCH_CONSTRUCTOR(TF_PP_CAT(_Tf_RegistryAdd, NAME), \
181 TF_REGISTRY_PRIORITY, KEY_TYPE*) \
182 { \
183 Tf_RegistryInit::Add(TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME), \
184 (void(*)(KEY_TYPE*, void*)) \
185 TF_PP_CAT(_Tf_RegistryFunction, NAME), \
186 TF_PP_STRINGIZE(KEY_TYPE)); \
187 } \
188 _ARCH_ENSURE_PER_LIB_INIT(Tf_RegistryStaticInit, _tfRegistryInit); \
189 static void TF_PP_CAT(_Tf_RegistryFunction, NAME)(KEY_TYPE*, void*)
190
191
219#define TF_REGISTRY_FUNCTION(KEY_TYPE) \
220 TF_REGISTRY_DEFINE(KEY_TYPE, __LINE__)
221
260#define TF_REGISTRY_FUNCTION_WITH_TAG(KEY_TYPE, TAG) \
261 TF_REGISTRY_DEFINE(KEY_TYPE, TF_PP_CAT(TAG, __LINE__))
262
263PXR_NAMESPACE_CLOSE_SCOPE
264
265#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.