Loading...
Searching...
No Matches
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
124PXR_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.
131template <class T>
132struct 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).
142template <>
143struct TfEnvSetting<std::string>
144{
145 std::atomic<std::string*> *_value;
146 char const * _default;
147 char const * _name;
148 char const * _description;
149};
150
151template <class T>
152void Tf_InitializeEnvSetting(TfEnvSetting<T> *);
153
156template <class T>
157inline T const &
158TfGetEnvSetting(TfEnvSetting<T>& setting) {
159 T *val = setting._value->load();
160 if (ARCH_UNLIKELY(!val)) {
161 Tf_InitializeEnvSetting(&setting);
162 val = setting._value->load();
163 }
164 return *val;
165}
166
167// Ensure that we only allow bool, int, and string, and map char * and char
168// array to string.
169
170bool Tf_ChooseEnvSettingType(bool);
171int Tf_ChooseEnvSettingType(int);
172std::string Tf_ChooseEnvSettingType(char const *);
173
174class Tf_EnvSettingRegistry;
175
179#define TF_DEFINE_ENV_SETTING(envVar, defValue, description) \
180 std::atomic< decltype(Tf_ChooseEnvSettingType(defValue))*> \
181 envVar##_value; \
182 TfEnvSetting<decltype(Tf_ChooseEnvSettingType(defValue))> envVar = { \
183 &envVar##_value, defValue, #envVar, description }; \
184 TF_REGISTRY_FUNCTION_WITH_TAG(Tf_EnvSettingRegistry, envVar) { \
185 (void)TfGetEnvSetting(envVar); \
186 }
187
188PXR_NAMESPACE_CLOSE_SCOPE
189
190#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.
STL namespace.