All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
envSetting.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_ENV_SETTING_H
25 #define PXR_BASE_TF_ENV_SETTING_H
26 
116 
117 #include "pxr/pxr.h"
118 #include "pxr/base/arch/hints.h"
120 
121 #include <atomic>
122 #include <string>
123 
124 PXR_NAMESPACE_OPEN_SCOPE
125 
126 // POD, statically initialized.
127 //
128 // We store the atomic_value separately and refer to it via pointer because we
129 // cannot use aggregate-initialization on a struct holding an atomic, but we
130 // can value-initialize a single std::atomic.
131 template <class T>
132 struct TfEnvSetting
133 {
134  std::atomic<T*> *_value;
135  T _default;
136  char const * _name;
137  char const * _description;
138 };
139 
140 // Specialize for string, default is stored as char const * (pointing to a
141 // literal).
142 template <>
143 struct TfEnvSetting<std::string>
144 {
145  std::atomic<std::string*> *_value;
146  char const * _default;
147  char const * _name;
148  char const * _description;
149 };
150 
151 template <class T>
152 void Tf_InitializeEnvSetting(TfEnvSetting<T> *);
153 
156 template <class T>
157 inline T const &
158 TfGetEnvSetting(TfEnvSetting<T>& setting) {
159  extern void Tf_InitEnvSettings();
160  Tf_InitEnvSettings();
161 
162  T *val = setting._value->load();
163  if (ARCH_UNLIKELY(!val)) {
164  Tf_InitializeEnvSetting(&setting);
165  val = setting._value->load();
166  }
167  return *val;
168 }
169 
170 // Ensure that we only allow bool, int, and string, and map char * and char
171 // array to string.
172 
173 bool Tf_ChooseEnvSettingType(bool);
174 int Tf_ChooseEnvSettingType(int);
175 std::string Tf_ChooseEnvSettingType(char const *);
176 
177 class Tf_EnvSettingRegistry;
178 
182 #define TF_DEFINE_ENV_SETTING(envVar, defValue, description) \
183  std::atomic< decltype(Tf_ChooseEnvSettingType(defValue))*> \
184  envVar##_value; \
185  TfEnvSetting<decltype(Tf_ChooseEnvSettingType(defValue))> envVar = { \
186  &envVar##_value, defValue, #envVar, description }; \
187  TF_REGISTRY_FUNCTION_WITH_TAG(Tf_EnvSettingRegistry, envVar) { \
188  (void)TfGetEnvSetting(envVar); \
189  }
190 
191 PXR_NAMESPACE_CLOSE_SCOPE
192 
193 #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:158
Compiler hints.