All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
envSetting.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_ENV_SETTING_H
8#define PXR_BASE_TF_ENV_SETTING_H
9
99
100#include "pxr/pxr.h"
101#include "pxr/base/arch/hints.h"
103
104#include <atomic>
105#include <string>
106
107PXR_NAMESPACE_OPEN_SCOPE
108
109// POD, statically initialized.
110//
111// We store the atomic_value separately and refer to it via pointer because we
112// cannot use aggregate-initialization on a struct holding an atomic, but we
113// can value-initialize a single std::atomic.
114template <class T>
115struct TfEnvSetting
116{
117 std::atomic<T*> *_value;
118 T _default;
119 char const * _name;
120 char const * _description;
121};
122
123// Specialize for string, default is stored as char const * (pointing to a
124// literal).
125template <>
126struct TfEnvSetting<std::string>
127{
128 std::atomic<std::string*> *_value;
129 char const * _default;
130 char const * _name;
131 char const * _description;
132};
133
134template <class T>
135void Tf_InitializeEnvSetting(TfEnvSetting<T> *);
136
139template <class T>
140inline T const &
141TfGetEnvSetting(TfEnvSetting<T>& setting) {
142 T *val = setting._value->load();
143 if (ARCH_UNLIKELY(!val)) {
144 Tf_InitializeEnvSetting(&setting);
145 val = setting._value->load();
146 }
147 return *val;
148}
149
150// Ensure that we only allow bool, int, and string, and map char * and char
151// array to string.
152
153bool Tf_ChooseEnvSettingType(bool);
154int Tf_ChooseEnvSettingType(int);
155std::string Tf_ChooseEnvSettingType(char const *);
156
157class Tf_EnvSettingRegistry;
158
162#define TF_DEFINE_ENV_SETTING(envVar, defValue, description) \
163 std::atomic< decltype(Tf_ChooseEnvSettingType(defValue))*> \
164 envVar##_value; \
165 TfEnvSetting<decltype(Tf_ChooseEnvSettingType(defValue))> envVar = { \
166 &envVar##_value, defValue, #envVar, description }; \
167 TF_REGISTRY_FUNCTION_WITH_TAG(Tf_EnvSettingRegistry, envVar) { \
168 (void)TfGetEnvSetting(envVar); \
169 }
170
171PXR_NAMESPACE_CLOSE_SCOPE
172
173#endif // PXR_BASE_TF_ENV_SETTING_H
T const & TfGetEnvSetting(TfEnvSetting< T > &setting)
Returns the value of the specified env setting, registered using TF_DEFINE_ENV_SETTING.
Definition: envSetting.h:141
Compiler hints.
STL namespace.