/src/abseil-cpp/absl/flags/internal/flag.h
Line  | Count  | Source  | 
1  |  | //  | 
2  |  | // Copyright 2019 The Abseil Authors.  | 
3  |  | //  | 
4  |  | // Licensed under the Apache License, Version 2.0 (the "License");  | 
5  |  | // you may not use this file except in compliance with the License.  | 
6  |  | // You may obtain a copy of the License at  | 
7  |  | //  | 
8  |  | //      https://www.apache.org/licenses/LICENSE-2.0  | 
9  |  | //  | 
10  |  | // Unless required by applicable law or agreed to in writing, software  | 
11  |  | // distributed under the License is distributed on an "AS IS" BASIS,  | 
12  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
13  |  | // See the License for the specific language governing permissions and  | 
14  |  | // limitations under the License.  | 
15  |  |  | 
16  |  | #ifndef ABSL_FLAGS_INTERNAL_FLAG_H_  | 
17  |  | #define ABSL_FLAGS_INTERNAL_FLAG_H_  | 
18  |  |  | 
19  |  | #include <stddef.h>  | 
20  |  | #include <stdint.h>  | 
21  |  |  | 
22  |  | #include <atomic>  | 
23  |  | #include <cstring>  | 
24  |  | #include <memory>  | 
25  |  | #include <string>  | 
26  |  | #include <type_traits>  | 
27  |  | #include <typeinfo>  | 
28  |  |  | 
29  |  | #include "absl/base/attributes.h"  | 
30  |  | #include "absl/base/call_once.h"  | 
31  |  | #include "absl/base/casts.h"  | 
32  |  | #include "absl/base/config.h"  | 
33  |  | #include "absl/base/optimization.h"  | 
34  |  | #include "absl/base/thread_annotations.h"  | 
35  |  | #include "absl/flags/commandlineflag.h"  | 
36  |  | #include "absl/flags/config.h"  | 
37  |  | #include "absl/flags/internal/commandlineflag.h"  | 
38  |  | #include "absl/flags/internal/registry.h"  | 
39  |  | #include "absl/flags/internal/sequence_lock.h"  | 
40  |  | #include "absl/flags/marshalling.h"  | 
41  |  | #include "absl/meta/type_traits.h"  | 
42  |  | #include "absl/strings/string_view.h"  | 
43  |  | #include "absl/synchronization/mutex.h"  | 
44  |  | #include "absl/utility/utility.h"  | 
45  |  |  | 
46  |  | namespace absl { | 
47  |  | ABSL_NAMESPACE_BEGIN  | 
48  |  |  | 
49  |  | ///////////////////////////////////////////////////////////////////////////////  | 
50  |  | // Forward declaration of absl::Flag<T> public API.  | 
51  |  | namespace flags_internal { | 
52  |  | template <typename T>  | 
53  |  | class Flag;  | 
54  |  | }  // namespace flags_internal  | 
55  |  |  | 
56  |  | template <typename T>  | 
57  |  | using Flag = flags_internal::Flag<T>;  | 
58  |  |  | 
59  |  | template <typename T>  | 
60  |  | [[nodiscard]] T GetFlag(const absl::Flag<T>& flag);  | 
61  |  |  | 
62  |  | template <typename T>  | 
63  |  | void SetFlag(absl::Flag<T>* flag, const T& v);  | 
64  |  |  | 
65  |  | template <typename T, typename V>  | 
66  |  | void SetFlag(absl::Flag<T>* flag, const V& v);  | 
67  |  |  | 
68  |  | template <typename U>  | 
69  |  | const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<U>& f);  | 
70  |  |  | 
71  |  | ///////////////////////////////////////////////////////////////////////////////  | 
72  |  | // Flag value type operations, eg., parsing, copying, etc. are provided  | 
73  |  | // by function specific to that type with a signature matching FlagOpFn.  | 
74  |  |  | 
75  |  | namespace flags_internal { | 
76  |  |  | 
77  |  | enum class FlagOp { | 
78  |  |   kAlloc,  | 
79  |  |   kDelete,  | 
80  |  |   kCopy,  | 
81  |  |   kCopyConstruct,  | 
82  |  |   kSizeof,  | 
83  |  |   kFastTypeId,  | 
84  |  |   kRuntimeTypeId,  | 
85  |  |   kParse,  | 
86  |  |   kUnparse,  | 
87  |  |   kValueOffset,  | 
88  |  | };  | 
89  |  | using FlagOpFn = void* (*)(FlagOp, const void*, void*, void*);  | 
90  |  |  | 
91  |  | // Forward declaration for Flag value specific operations.  | 
92  |  | template <typename T>  | 
93  |  | void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3);  | 
94  |  |  | 
95  |  | // Allocate aligned memory for a flag value.  | 
96  | 0  | inline void* Alloc(FlagOpFn op) { | 
97  | 0  |   return op(FlagOp::kAlloc, nullptr, nullptr, nullptr);  | 
98  | 0  | }  | 
99  |  | // Deletes memory interpreting obj as flag value type pointer.  | 
100  | 0  | inline void Delete(FlagOpFn op, void* obj) { | 
101  | 0  |   op(FlagOp::kDelete, nullptr, obj, nullptr);  | 
102  | 0  | }  | 
103  |  | // Copies src to dst interpreting as flag value type pointers.  | 
104  | 0  | inline void Copy(FlagOpFn op, const void* src, void* dst) { | 
105  | 0  |   op(FlagOp::kCopy, src, dst, nullptr);  | 
106  | 0  | }  | 
107  |  | // Construct a copy of flag value in a location pointed by dst  | 
108  |  | // based on src - pointer to the flag's value.  | 
109  | 0  | inline void CopyConstruct(FlagOpFn op, const void* src, void* dst) { | 
110  | 0  |   op(FlagOp::kCopyConstruct, src, dst, nullptr);  | 
111  | 0  | }  | 
112  |  | // Makes a copy of flag value pointed by obj.  | 
113  | 0  | inline void* Clone(FlagOpFn op, const void* obj) { | 
114  | 0  |   void* res = flags_internal::Alloc(op);  | 
115  | 0  |   flags_internal::CopyConstruct(op, obj, res);  | 
116  | 0  |   return res;  | 
117  | 0  | }  | 
118  |  | // Returns true if parsing of input text is successful.  | 
119  |  | inline bool Parse(FlagOpFn op, absl::string_view text, void* dst,  | 
120  | 0  |                   std::string* error) { | 
121  | 0  |   return op(FlagOp::kParse, &text, dst, error) != nullptr;  | 
122  | 0  | }  | 
123  |  | // Returns string representing supplied value.  | 
124  | 0  | inline std::string Unparse(FlagOpFn op, const void* val) { | 
125  | 0  |   std::string result;  | 
126  | 0  |   op(FlagOp::kUnparse, val, &result, nullptr);  | 
127  | 0  |   return result;  | 
128  | 0  | }  | 
129  |  | // Returns size of flag value type.  | 
130  | 2  | inline size_t Sizeof(FlagOpFn op) { | 
131  |  |   // This sequence of casts reverses the sequence from  | 
132  |  |   // `flags_internal::FlagOps()`  | 
133  | 2  |   return static_cast<size_t>(reinterpret_cast<intptr_t>(  | 
134  | 2  |       op(FlagOp::kSizeof, nullptr, nullptr, nullptr)));  | 
135  | 2  | }  | 
136  |  | // Returns fast type id corresponding to the value type.  | 
137  | 5.93k  | inline FlagFastTypeId FastTypeId(FlagOpFn op) { | 
138  | 5.93k  |   return reinterpret_cast<FlagFastTypeId>(  | 
139  | 5.93k  |       op(FlagOp::kFastTypeId, nullptr, nullptr, nullptr));  | 
140  | 5.93k  | }  | 
141  |  | // Returns fast type id corresponding to the value type.  | 
142  | 0  | inline const std::type_info* RuntimeTypeId(FlagOpFn op) { | 
143  | 0  |   return reinterpret_cast<const std::type_info*>(  | 
144  | 0  |       op(FlagOp::kRuntimeTypeId, nullptr, nullptr, nullptr));  | 
145  | 0  | }  | 
146  |  | // Returns offset of the field value_ from the field impl_ inside of  | 
147  |  | // absl::Flag<T> data. Given FlagImpl pointer p you can get the  | 
148  |  | // location of the corresponding value as:  | 
149  |  | //      reinterpret_cast<char*>(p) + ValueOffset().  | 
150  | 2  | inline ptrdiff_t ValueOffset(FlagOpFn op) { | 
151  |  |   // This sequence of casts reverses the sequence from  | 
152  |  |   // `flags_internal::FlagOps()`  | 
153  | 2  |   return static_cast<ptrdiff_t>(reinterpret_cast<intptr_t>(  | 
154  | 2  |       op(FlagOp::kValueOffset, nullptr, nullptr, nullptr)));  | 
155  | 2  | }  | 
156  |  |  | 
157  |  | // Returns an address of RTTI's typeid(T).  | 
158  |  | template <typename T>  | 
159  |  | inline const std::type_info* GenRuntimeTypeId() { | 
160  |  | #ifdef ABSL_INTERNAL_HAS_RTTI  | 
161  |  |   return &typeid(T);  | 
162  |  | #else  | 
163  |  |   return nullptr;  | 
164  |  | #endif  | 
165  |  | }  | 
166  |  |  | 
167  |  | ///////////////////////////////////////////////////////////////////////////////  | 
168  |  | // Flag help auxiliary structs.  | 
169  |  |  | 
170  |  | // This is help argument for absl::Flag encapsulating the string literal pointer  | 
171  |  | // or pointer to function generating it as well as enum descriminating two  | 
172  |  | // cases.  | 
173  |  | using HelpGenFunc = std::string (*)();  | 
174  |  |  | 
175  |  | template <size_t N>  | 
176  |  | struct FixedCharArray { | 
177  |  |   char value[N];  | 
178  |  |  | 
179  |  |   template <size_t... I>  | 
180  |  |   static constexpr FixedCharArray<N> FromLiteralString(  | 
181  |  |       absl::string_view str, absl::index_sequence<I...>) { | 
182  |  |     return (void)str, FixedCharArray<N>({{str[I]..., '\0'}}); | 
183  |  |   }  | 
184  |  | };  | 
185  |  |  | 
186  |  | template <typename Gen, size_t N = Gen::Value().size()>  | 
187  |  | constexpr FixedCharArray<N + 1> HelpStringAsArray(int) { | 
188  |  |   return FixedCharArray<N + 1>::FromLiteralString(  | 
189  |  |       Gen::Value(), absl::make_index_sequence<N>{}); | 
190  |  | }  | 
191  |  |  | 
192  |  | template <typename Gen>  | 
193  |  | constexpr std::false_type HelpStringAsArray(char) { | 
194  |  |   return std::false_type{}; | 
195  |  | }  | 
196  |  |  | 
197  |  | union FlagHelpMsg { | 
198  | 0  |   constexpr explicit FlagHelpMsg(const char* help_msg) : literal(help_msg) {} | 
199  | 0  |   constexpr explicit FlagHelpMsg(HelpGenFunc help_gen) : gen_func(help_gen) {} | 
200  |  |  | 
201  |  |   const char* literal;  | 
202  |  |   HelpGenFunc gen_func;  | 
203  |  | };  | 
204  |  |  | 
205  |  | enum class FlagHelpKind : uint8_t { kLiteral = 0, kGenFunc = 1 }; | 
206  |  |  | 
207  |  | struct FlagHelpArg { | 
208  |  |   FlagHelpMsg source;  | 
209  |  |   FlagHelpKind kind;  | 
210  |  | };  | 
211  |  |  | 
212  |  | extern const char kStrippedFlagHelp[];  | 
213  |  |  | 
214  |  | // These two HelpArg overloads allows us to select at compile time one of two  | 
215  |  | // way to pass Help argument to absl::Flag. We'll be passing  | 
216  |  | // AbslFlagHelpGenFor##name as Gen and integer 0 as a single argument to prefer  | 
217  |  | // first overload if possible. If help message is evaluatable on constexpr  | 
218  |  | // context We'll be able to make FixedCharArray out of it and we'll choose first  | 
219  |  | // overload. In this case the help message expression is immediately evaluated  | 
220  |  | // and is used to construct the absl::Flag. No additional code is generated by  | 
221  |  | // ABSL_FLAG Otherwise SFINAE kicks in and first overload is dropped from the  | 
222  |  | // consideration, in which case the second overload will be used. The second  | 
223  |  | // overload does not attempt to evaluate the help message expression  | 
224  |  | // immediately and instead delays the evaluation by returning the function  | 
225  |  | // pointer (&T::NonConst) generating the help message when necessary. This is  | 
226  |  | // evaluatable in constexpr context, but the cost is an extra function being  | 
227  |  | // generated in the ABSL_FLAG code.  | 
228  |  | template <typename Gen, size_t N>  | 
229  |  | constexpr FlagHelpArg HelpArg(const FixedCharArray<N>& value) { | 
230  |  |   return {FlagHelpMsg(value.value), FlagHelpKind::kLiteral}; | 
231  |  | }  | 
232  |  |  | 
233  |  | template <typename Gen>  | 
234  |  | constexpr FlagHelpArg HelpArg(std::false_type) { | 
235  |  |   return {FlagHelpMsg(&Gen::NonConst), FlagHelpKind::kGenFunc}; | 
236  |  | }  | 
237  |  |  | 
238  |  | ///////////////////////////////////////////////////////////////////////////////  | 
239  |  | // Flag default value auxiliary structs.  | 
240  |  |  | 
241  |  | // Signature for the function generating the initial flag value (usually  | 
242  |  | // based on default value supplied in flag's definition)  | 
243  |  | using FlagDfltGenFunc = void (*)(void*);  | 
244  |  |  | 
245  |  | union FlagDefaultSrc { | 
246  |  |   constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg)  | 
247  | 0  |       : gen_func(gen_func_arg) {} | 
248  |  |  | 
249  |  | #define ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE(T, name) \  | 
250  |  |   T name##_value;                                  \  | 
251  | 0  |   constexpr explicit FlagDefaultSrc(T value) : name##_value(value) {}  // NOLINTUnexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(bool) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(short) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(unsigned short) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(int) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(unsigned int) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(long) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(unsigned long) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(long long) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(unsigned long long) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(double) Unexecuted instantiation: absl::flags_internal::FlagDefaultSrc::FlagDefaultSrc(float)  | 
252  |  |   ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE)  | 
253  |  | #undef ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE  | 
254  |  |  | 
255  |  |   void* dynamic_value;  | 
256  |  |   FlagDfltGenFunc gen_func;  | 
257  |  | };  | 
258  |  |  | 
259  |  | enum class FlagDefaultKind : uint8_t { | 
260  |  |   kDynamicValue = 0,  | 
261  |  |   kGenFunc = 1,  | 
262  |  |   kOneWord = 2  // for default values UP to one word in size  | 
263  |  | };  | 
264  |  |  | 
265  |  | struct FlagDefaultArg { | 
266  |  |   FlagDefaultSrc source;  | 
267  |  |   FlagDefaultKind kind;  | 
268  |  | };  | 
269  |  |  | 
270  |  | // This struct and corresponding overload to InitDefaultValue are used to  | 
271  |  | // facilitate usage of {} as default value in ABSL_FLAG macro. | 
272  |  | // TODO(rogeeff): Fix handling types with explicit constructors.  | 
273  |  | struct EmptyBraces {}; | 
274  |  |  | 
275  |  | template <typename T>  | 
276  |  | constexpr T InitDefaultValue(T t) { | 
277  |  |   return t;  | 
278  |  | }  | 
279  |  |  | 
280  |  | template <typename T>  | 
281  |  | constexpr T InitDefaultValue(EmptyBraces) { | 
282  |  |   return T{}; | 
283  |  | }  | 
284  |  |  | 
285  |  | template <typename ValueT, typename GenT,  | 
286  |  |           typename std::enable_if<std::is_integral<ValueT>::value, int>::type =  | 
287  |  |               ((void)GenT{}, 0)> | 
288  |  | constexpr FlagDefaultArg DefaultArg(int) { | 
289  |  |   return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord}; | 
290  |  | }  | 
291  |  |  | 
292  |  | template <typename ValueT, typename GenT>  | 
293  |  | constexpr FlagDefaultArg DefaultArg(char) { | 
294  |  |   return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc}; | 
295  |  | }  | 
296  |  |  | 
297  |  | ///////////////////////////////////////////////////////////////////////////////  | 
298  |  | // Flag storage selector traits. Each trait indicates what kind of storage kind  | 
299  |  | // to use for the flag value.  | 
300  |  |  | 
301  |  | template <typename T>  | 
302  |  | using FlagUseValueAndInitBitStorage =  | 
303  |  |     std::integral_constant<bool, std::is_trivially_copyable<T>::value &&  | 
304  |  |                                      std::is_default_constructible<T>::value &&  | 
305  |  |                                      (sizeof(T) < 8)>;  | 
306  |  |  | 
307  |  | template <typename T>  | 
308  |  | using FlagUseOneWordStorage =  | 
309  |  |     std::integral_constant<bool, std::is_trivially_copyable<T>::value &&  | 
310  |  |                                      (sizeof(T) <= 8)>;  | 
311  |  |  | 
312  |  | template <class T>  | 
313  |  | using FlagUseSequenceLockStorage =  | 
314  |  |     std::integral_constant<bool, std::is_trivially_copyable<T>::value &&  | 
315  |  |                                      (sizeof(T) > 8)>;  | 
316  |  |  | 
317  |  | enum class FlagValueStorageKind : uint8_t { | 
318  |  |   kValueAndInitBit = 0,  | 
319  |  |   kOneWordAtomic = 1,  | 
320  |  |   kSequenceLocked = 2,  | 
321  |  |   kHeapAllocated = 3,  | 
322  |  | };  | 
323  |  |  | 
324  |  | // This constexpr function returns the storage kind for the given flag value  | 
325  |  | // type.  | 
326  |  | template <typename T>  | 
327  |  | static constexpr FlagValueStorageKind StorageKind() { | 
328  |  |   return FlagUseValueAndInitBitStorage<T>::value  | 
329  |  |              ? FlagValueStorageKind::kValueAndInitBit  | 
330  |  |          : FlagUseOneWordStorage<T>::value  | 
331  |  |              ? FlagValueStorageKind::kOneWordAtomic  | 
332  |  |          : FlagUseSequenceLockStorage<T>::value  | 
333  |  |              ? FlagValueStorageKind::kSequenceLocked  | 
334  |  |              : FlagValueStorageKind::kHeapAllocated;  | 
335  |  | }  | 
336  |  |  | 
337  |  | // This is a base class for the storage classes used by kOneWordAtomic and  | 
338  |  | // kValueAndInitBit storage kinds. It literally just stores the one word value  | 
339  |  | // as an atomic. By default, it is initialized to a magic value that is unlikely  | 
340  |  | // a valid value for the flag value type.  | 
341  |  | struct FlagOneWordValue { | 
342  | 0  |   constexpr static int64_t Uninitialized() { | 
343  | 0  |     return static_cast<int64_t>(0xababababababababll);  | 
344  | 0  |   }  | 
345  |  |  | 
346  | 0  |   constexpr FlagOneWordValue() : value(Uninitialized()) {} | 
347  | 0  |   constexpr explicit FlagOneWordValue(int64_t v) : value(v) {} | 
348  |  |   std::atomic<int64_t> value;  | 
349  |  | };  | 
350  |  |  | 
351  |  | // This class represents a memory layout used by kValueAndInitBit storage kind.  | 
352  |  | template <typename T>  | 
353  |  | struct alignas(8) FlagValueAndInitBit { | 
354  |  |   T value;  | 
355  |  |   // Use an int instead of a bool to guarantee that a non-zero value has  | 
356  |  |   // a bit set.  | 
357  |  |   uint8_t init;  | 
358  |  | };  | 
359  |  |  | 
360  |  | // This class implements an aligned pointer with two options stored via masks  | 
361  |  | // in unused bits of the pointer value (due to alignment requirement).  | 
362  |  | //  - IsUnprotectedReadCandidate - indicates that the value can be switched to  | 
363  |  | //    unprotected read without a lock.  | 
364  |  | //  - HasBeenRead - indicates that the value has been read at least once.  | 
365  |  | //  - AllowsUnprotectedRead - combination of the two options above and indicates  | 
366  |  | //    that the value can now be read without a lock.  | 
367  |  | // Further details of these options and their use is covered in the description  | 
368  |  | // of the FlagValue<T, FlagValueStorageKind::kHeapAllocated> specialization.  | 
369  |  | class MaskedPointer { | 
370  |  |  public:  | 
371  |  |   using mask_t = uintptr_t;  | 
372  |  |   using ptr_t = void*;  | 
373  |  |  | 
374  | 0  |   static constexpr int RequiredAlignment() { return 4; } | 
375  |  |  | 
376  | 0  |   constexpr MaskedPointer() : ptr_(nullptr) {} | 
377  | 0  |   constexpr explicit MaskedPointer(ptr_t rhs) : ptr_(rhs) {} | 
378  |  |   MaskedPointer(ptr_t rhs, bool is_candidate);  | 
379  |  |  | 
380  |  |   MaskedPointer(const MaskedPointer& rhs) = default;  | 
381  |  |   MaskedPointer& operator=(const MaskedPointer& rhs) = default;  | 
382  |  |  | 
383  | 0  |   void* Ptr() const { | 
384  | 0  |     return reinterpret_cast<void*>(reinterpret_cast<mask_t>(ptr_) &  | 
385  | 0  |                                    kPtrValueMask);  | 
386  | 0  |   }  | 
387  | 0  |   bool AllowsUnprotectedRead() const { | 
388  | 0  |     return (reinterpret_cast<mask_t>(ptr_) & kAllowsUnprotectedRead) ==  | 
389  | 0  |            kAllowsUnprotectedRead;  | 
390  | 0  |   }  | 
391  |  |   bool IsUnprotectedReadCandidate() const;  | 
392  |  |   bool HasBeenRead() const;  | 
393  |  |  | 
394  |  |   void Set(FlagOpFn op, const void* src, bool is_candidate);  | 
395  |  |   void MarkAsRead();  | 
396  |  |  | 
397  |  |  private:  | 
398  |  |   // Masks  | 
399  |  |   // Indicates that the flag value either default or originated from command  | 
400  |  |   // line.  | 
401  |  |   static constexpr mask_t kUnprotectedReadCandidate = 0x1u;  | 
402  |  |   // Indicates that flag has been read.  | 
403  |  |   static constexpr mask_t kHasBeenRead = 0x2u;  | 
404  |  |   static constexpr mask_t kAllowsUnprotectedRead =  | 
405  |  |       kUnprotectedReadCandidate | kHasBeenRead;  | 
406  |  |   static constexpr mask_t kPtrValueMask = ~kAllowsUnprotectedRead;  | 
407  |  |  | 
408  |  |   void ApplyMask(mask_t mask);  | 
409  |  |   bool CheckMask(mask_t mask) const;  | 
410  |  |  | 
411  |  |   ptr_t ptr_;  | 
412  |  | };  | 
413  |  |  | 
414  |  | // This class implements a type erased storage of the heap allocated flag value.  | 
415  |  | // It is used as a base class for the storage class for kHeapAllocated storage  | 
416  |  | // kind. The initial_buffer is expected to have an alignment of at least  | 
417  |  | // MaskedPointer::RequiredAlignment(), so that the bits used by the  | 
418  |  | // MaskedPointer to store masks are set to 0. This guarantees that value starts  | 
419  |  | // in an uninitialized state.  | 
420  |  | struct FlagMaskedPointerValue { | 
421  |  |   constexpr explicit FlagMaskedPointerValue(MaskedPointer::ptr_t initial_buffer)  | 
422  | 0  |       : value(MaskedPointer(initial_buffer)) {} | 
423  |  |  | 
424  |  |   std::atomic<MaskedPointer> value;  | 
425  |  | };  | 
426  |  |  | 
427  |  | // This is the forward declaration for the template that represents a storage  | 
428  |  | // for the flag values. This template is expected to be explicitly specialized  | 
429  |  | // for each storage kind and it does not have a generic default  | 
430  |  | // implementation.  | 
431  |  | template <typename T,  | 
432  |  |           FlagValueStorageKind Kind = flags_internal::StorageKind<T>()>  | 
433  |  | struct FlagValue;  | 
434  |  |  | 
435  |  | // This specialization represents the storage of flag values types with the  | 
436  |  | // kValueAndInitBit storage kind. It is based on the FlagOneWordValue class  | 
437  |  | // and relies on memory layout in FlagValueAndInitBit<T> to indicate that the  | 
438  |  | // value has been initialized or not.  | 
439  |  | template <typename T>  | 
440  |  | struct FlagValue<T, FlagValueStorageKind::kValueAndInitBit> : FlagOneWordValue { | 
441  |  |   constexpr FlagValue() : FlagOneWordValue(0) {} | 
442  |  |   bool Get(const SequenceLock&, T& dst) const { | 
443  |  |     int64_t storage = value.load(std::memory_order_acquire);  | 
444  |  |     if (ABSL_PREDICT_FALSE(storage == 0)) { | 
445  |  |       // This assert is to ensure that the initialization inside FlagImpl::Init  | 
446  |  |       // is able to set init member correctly.  | 
447  |  |       static_assert(offsetof(FlagValueAndInitBit<T>, init) == sizeof(T),  | 
448  |  |                     "Unexpected memory layout of FlagValueAndInitBit");  | 
449  |  |       return false;  | 
450  |  |     }  | 
451  |  |     dst = absl::bit_cast<FlagValueAndInitBit<T>>(storage).value;  | 
452  |  |     return true;  | 
453  |  |   }  | 
454  |  | };  | 
455  |  |  | 
456  |  | // This specialization represents the storage of flag values types with the  | 
457  |  | // kOneWordAtomic storage kind. It is based on the FlagOneWordValue class  | 
458  |  | // and relies on the magic uninitialized state of default constructed instead of  | 
459  |  | // FlagOneWordValue to indicate that the value has been initialized or not.  | 
460  |  | template <typename T>  | 
461  |  | struct FlagValue<T, FlagValueStorageKind::kOneWordAtomic> : FlagOneWordValue { | 
462  |  |   constexpr FlagValue() : FlagOneWordValue() {} | 
463  |  |   bool Get(const SequenceLock&, T& dst) const { | 
464  |  |     int64_t one_word_val = value.load(std::memory_order_acquire);  | 
465  |  |     if (ABSL_PREDICT_FALSE(one_word_val == FlagOneWordValue::Uninitialized())) { | 
466  |  |       return false;  | 
467  |  |     }  | 
468  |  |     std::memcpy(&dst, static_cast<const void*>(&one_word_val), sizeof(T));  | 
469  |  |     return true;  | 
470  |  |   }  | 
471  |  | };  | 
472  |  |  | 
473  |  | // This specialization represents the storage of flag values types with the  | 
474  |  | // kSequenceLocked storage kind. This storage is used by trivially copyable  | 
475  |  | // types with size greater than 8 bytes. This storage relies on uninitialized  | 
476  |  | // state of the SequenceLock to indicate that the value has been initialized or  | 
477  |  | // not. This storage also provides lock-free read access to the underlying  | 
478  |  | // value once it is initialized.  | 
479  |  | template <typename T>  | 
480  |  | struct FlagValue<T, FlagValueStorageKind::kSequenceLocked> { | 
481  |  |   bool Get(const SequenceLock& lock, T& dst) const { | 
482  |  |     return lock.TryRead(&dst, value_words, sizeof(T));  | 
483  |  |   }  | 
484  |  |  | 
485  |  |   static constexpr int kNumWords =  | 
486  |  |       flags_internal::AlignUp(sizeof(T), sizeof(uint64_t)) / sizeof(uint64_t);  | 
487  |  |  | 
488  |  |   alignas(T) alignas(  | 
489  |  |       std::atomic<uint64_t>) std::atomic<uint64_t> value_words[kNumWords];  | 
490  |  | };  | 
491  |  |  | 
492  |  | // This specialization represents the storage of flag values types with the  | 
493  |  | // kHeapAllocated storage kind. This is a storage of last resort and is used  | 
494  |  | // if none of other storage kinds are applicable.  | 
495  |  | //  | 
496  |  | // Generally speaking the values with this storage kind can't be accessed  | 
497  |  | // atomically and thus can't be read without holding a lock. If we would ever  | 
498  |  | // want to avoid the lock, we'd need to leak the old value every time new flag  | 
499  |  | // value is being set (since we are in danger of having a race condition  | 
500  |  | // otherwise).  | 
501  |  | //  | 
502  |  | // Instead of doing that, this implementation attempts to cater to some common  | 
503  |  | // use cases by allowing at most 2 values to be leaked - default value and  | 
504  |  | // value set from the command line.  | 
505  |  | //  | 
506  |  | // This specialization provides an initial buffer for the first flag value. This  | 
507  |  | // is where the default value is going to be stored. We attempt to reuse this  | 
508  |  | // buffer if possible, including storing the value set from the command line  | 
509  |  | // there.  | 
510  |  | //  | 
511  |  | // As long as we only read this value, we can access it without a lock (in  | 
512  |  | // practice we still use the lock for the very first read to be able set  | 
513  |  | // "has been read" option on this flag).  | 
514  |  | //  | 
515  |  | // If flag is specified on the command line we store the parsed value either  | 
516  |  | // in the internal buffer (if the default value never been read) or we leak the  | 
517  |  | // default value and allocate the new storage for the parse value. This value is  | 
518  |  | // also a candidate for an unprotected read. If flag is set programmatically  | 
519  |  | // after the command line is parsed, the storage for this value is going to be  | 
520  |  | // leaked. Note that in both scenarios we are not going to have a real leak.  | 
521  |  | // Instead we'll store the leaked value pointers in the internal freelist to  | 
522  |  | // avoid triggering the memory leak checker complains.  | 
523  |  | //  | 
524  |  | // If the flag is ever set programmatically, it stops being the candidate for an  | 
525  |  | // unprotected read, and any follow up access to the flag value requires a lock.  | 
526  |  | // Note that if the value if set programmatically before the command line is  | 
527  |  | // parsed, we can switch back to enabling unprotected reads for that value.  | 
528  |  | template <typename T>  | 
529  |  | struct FlagValue<T, FlagValueStorageKind::kHeapAllocated>  | 
530  |  |     : FlagMaskedPointerValue { | 
531  |  |   // We const initialize the value with unmasked pointer to the internal buffer,  | 
532  |  |   // making sure it is not a candidate for unprotected read. This way we can  | 
533  |  |   // ensure Init is done before any access to the flag value.  | 
534  |  |   constexpr FlagValue() : FlagMaskedPointerValue(&buffer[0]) {} | 
535  |  |  | 
536  |  |   bool Get(const SequenceLock&, T& dst) const { | 
537  |  |     MaskedPointer ptr_value = value.load(std::memory_order_acquire);  | 
538  |  |  | 
539  |  |     if (ABSL_PREDICT_TRUE(ptr_value.AllowsUnprotectedRead())) { | 
540  |  |       ::new (static_cast<void*>(&dst)) T(*static_cast<T*>(ptr_value.Ptr()));  | 
541  |  |       return true;  | 
542  |  |     }  | 
543  |  |     return false;  | 
544  |  |   }  | 
545  |  |  | 
546  |  |   alignas(MaskedPointer::RequiredAlignment()) alignas(  | 
547  |  |       T) char buffer[sizeof(T)]{}; | 
548  |  | };  | 
549  |  |  | 
550  |  | ///////////////////////////////////////////////////////////////////////////////  | 
551  |  | // Flag callback auxiliary structs.  | 
552  |  |  | 
553  |  | // Signature for the mutation callback used by watched Flags  | 
554  |  | // The callback is noexcept.  | 
555  |  | // TODO(rogeeff): add noexcept after C++17 support is added.  | 
556  |  | using FlagCallbackFunc = void (*)();  | 
557  |  |  | 
558  |  | struct FlagCallback { | 
559  |  |   FlagCallbackFunc func;  | 
560  |  |   absl::Mutex guard;  // Guard for concurrent callback invocations.  | 
561  |  | };  | 
562  |  |  | 
563  |  | ///////////////////////////////////////////////////////////////////////////////  | 
564  |  | // Flag implementation, which does not depend on flag value type.  | 
565  |  | // The class encapsulates the Flag's data and access to it.  | 
566  |  |  | 
567  |  | struct DynValueDeleter { | 
568  |  |   explicit DynValueDeleter(FlagOpFn op_arg = nullptr);  | 
569  |  |   void operator()(void* ptr) const;  | 
570  |  |  | 
571  |  |   FlagOpFn op;  | 
572  |  | };  | 
573  |  |  | 
574  |  | class FlagState;  | 
575  |  |  | 
576  |  | // These are only used as constexpr global objects.  | 
577  |  | // They do not use a virtual destructor to simplify their implementation.  | 
578  |  | // They are not destroyed except at program exit, so leaks do not matter.  | 
579  |  | #if defined(__GNUC__) && !defined(__clang__)  | 
580  |  | #pragma GCC diagnostic push  | 
581  |  | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"  | 
582  |  | #endif  | 
583  |  | class FlagImpl final : public CommandLineFlag { | 
584  |  |  public:  | 
585  |  |   constexpr FlagImpl(const char* name, const char* type_name,  | 
586  |  |                      const char* filename, FlagOpFn op, FlagHelpArg help,  | 
587  |  |                      FlagValueStorageKind value_kind,  | 
588  |  |                      FlagDefaultArg default_arg)  | 
589  |  |       : name_(name),  | 
590  |  |         type_name_(type_name),  | 
591  |  |         filename_(filename),  | 
592  |  |         op_(op),  | 
593  |  |         help_(help.source),  | 
594  |  |         help_source_kind_(static_cast<uint8_t>(help.kind)),  | 
595  |  |         value_storage_kind_(static_cast<uint8_t>(value_kind)),  | 
596  |  |         def_kind_(static_cast<uint8_t>(default_arg.kind)),  | 
597  |  |         modified_(false),  | 
598  |  |         on_command_line_(false),  | 
599  |  |         callback_(nullptr),  | 
600  |  |         default_value_(default_arg.source),  | 
601  | 0  |         data_guard_{} {} | 
602  |  |  | 
603  |  |   // Constant access methods  | 
604  |  |   int64_t ReadOneWord() const ABSL_LOCKS_EXCLUDED(DataGuard());  | 
605  |  |   bool ReadOneBool() const ABSL_LOCKS_EXCLUDED(DataGuard());  | 
606  |  |   void Read(void* dst) const override ABSL_LOCKS_EXCLUDED(DataGuard());  | 
607  | 0  |   void Read(bool* value) const ABSL_LOCKS_EXCLUDED(DataGuard()) { | 
608  | 0  |     *value = ReadOneBool();  | 
609  | 0  |   }  | 
610  |  |   template <typename T,  | 
611  |  |             absl::enable_if_t<flags_internal::StorageKind<T>() ==  | 
612  |  |                                   FlagValueStorageKind::kOneWordAtomic,  | 
613  |  |                               int> = 0>  | 
614  |  |   void Read(T* value) const ABSL_LOCKS_EXCLUDED(DataGuard()) { | 
615  |  |     int64_t v = ReadOneWord();  | 
616  |  |     std::memcpy(value, static_cast<const void*>(&v), sizeof(T));  | 
617  |  |   }  | 
618  |  |   template <typename T,  | 
619  |  |             typename std::enable_if<flags_internal::StorageKind<T>() ==  | 
620  |  |                                         FlagValueStorageKind::kValueAndInitBit,  | 
621  |  |                                     int>::type = 0>  | 
622  |  |   void Read(T* value) const ABSL_LOCKS_EXCLUDED(DataGuard()) { | 
623  |  |     *value = absl::bit_cast<FlagValueAndInitBit<T>>(ReadOneWord()).value;  | 
624  |  |   }  | 
625  |  |  | 
626  |  |   // Mutating access methods  | 
627  |  |   void Write(const void* src) ABSL_LOCKS_EXCLUDED(DataGuard());  | 
628  |  |  | 
629  |  |   // Interfaces to operate on callbacks.  | 
630  |  |   void SetCallback(const FlagCallbackFunc mutation_callback)  | 
631  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
632  |  |   void InvokeCallback() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(DataGuard());  | 
633  |  |  | 
634  |  |   // Used in read/write operations to validate source/target has correct type.  | 
635  |  |   // For example if flag is declared as absl::Flag<int> FLAGS_foo, a call to  | 
636  |  |   // absl::GetFlag(FLAGS_foo) validates that the type of FLAGS_foo is indeed  | 
637  |  |   // int. To do that we pass the assumed type id (which is deduced from type  | 
638  |  |   // int) as an argument `type_id`, which is in turn is validated against the  | 
639  |  |   // type id stored in flag object by flag definition statement.  | 
640  |  |   void AssertValidType(FlagFastTypeId type_id,  | 
641  |  |                        const std::type_info* (*gen_rtti)()) const;  | 
642  |  |  | 
643  |  |  private:  | 
644  |  |   template <typename T>  | 
645  |  |   friend class Flag;  | 
646  |  |   friend class FlagState;  | 
647  |  |  | 
648  |  |   // Ensures that `data_guard_` is initialized and returns it.  | 
649  |  |   absl::Mutex& DataGuard() const  | 
650  |  |       ABSL_LOCK_RETURNED(reinterpret_cast<absl::Mutex*>(data_guard_));  | 
651  |  |   // Returns heap allocated value of type T initialized with default value.  | 
652  |  |   std::unique_ptr<void, DynValueDeleter> MakeInitValue() const  | 
653  |  |       ABSL_EXCLUSIVE_LOCKS_REQUIRED(DataGuard());  | 
654  |  |   // Flag initialization called via absl::call_once.  | 
655  |  |   void Init();  | 
656  |  |  | 
657  |  |   // Offset value access methods. One per storage kind. These methods to not  | 
658  |  |   // respect const correctness, so be very careful using them.  | 
659  |  |  | 
660  |  |   // This is a shared helper routine which encapsulates most of the magic. Since  | 
661  |  |   // it is only used inside the three routines below, which are defined in  | 
662  |  |   // flag.cc, we can define it in that file as well.  | 
663  |  |   template <typename StorageT>  | 
664  |  |   StorageT* OffsetValue() const;  | 
665  |  |  | 
666  |  |   // The same as above, but used for sequencelock-protected storage.  | 
667  |  |   std::atomic<uint64_t>* AtomicBufferValue() const;  | 
668  |  |  | 
669  |  |   // This is an accessor for a value stored as one word atomic. Returns a  | 
670  |  |   // mutable reference to an atomic value.  | 
671  |  |   std::atomic<int64_t>& OneWordValue() const;  | 
672  |  |  | 
673  |  |   std::atomic<MaskedPointer>& PtrStorage() const;  | 
674  |  |  | 
675  |  |   // Attempts to parse supplied `value` string. If parsing is successful,  | 
676  |  |   // returns new value. Otherwise returns nullptr.  | 
677  |  |   std::unique_ptr<void, DynValueDeleter> TryParse(absl::string_view value,  | 
678  |  |                                                   std::string& err) const  | 
679  |  |       ABSL_EXCLUSIVE_LOCKS_REQUIRED(DataGuard());  | 
680  |  |   // Stores the flag value based on the pointer to the source.  | 
681  |  |   void StoreValue(const void* src, ValueSource source)  | 
682  |  |       ABSL_EXCLUSIVE_LOCKS_REQUIRED(DataGuard());  | 
683  |  |  | 
684  |  |   // Copy the flag data, protected by `seq_lock_` into `dst`.  | 
685  |  |   //  | 
686  |  |   // REQUIRES: ValueStorageKind() == kSequenceLocked.  | 
687  |  |   void ReadSequenceLockedData(void* dst) const ABSL_LOCKS_EXCLUDED(DataGuard());  | 
688  |  |  | 
689  | 0  |   FlagHelpKind HelpSourceKind() const { | 
690  | 0  |     return static_cast<FlagHelpKind>(help_source_kind_);  | 
691  | 0  |   }  | 
692  | 8  |   FlagValueStorageKind ValueStorageKind() const { | 
693  | 8  |     return static_cast<FlagValueStorageKind>(value_storage_kind_);  | 
694  | 8  |   }  | 
695  |  |   FlagDefaultKind DefaultKind() const  | 
696  | 0  |       ABSL_EXCLUSIVE_LOCKS_REQUIRED(DataGuard()) { | 
697  | 0  |     return static_cast<FlagDefaultKind>(def_kind_);  | 
698  | 0  |   }  | 
699  |  |  | 
700  |  |   // CommandLineFlag interface implementation  | 
701  |  |   absl::string_view Name() const override;  | 
702  |  |   absl::string_view TypeName() const override;  | 
703  |  |   std::string Filename() const override;  | 
704  |  |   std::string Help() const override;  | 
705  |  |   FlagFastTypeId TypeId() const override;  | 
706  |  |   bool IsSpecifiedOnCommandLine() const override  | 
707  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
708  |  |   std::string DefaultValue() const override ABSL_LOCKS_EXCLUDED(DataGuard());  | 
709  |  |   std::string CurrentValue() const override ABSL_LOCKS_EXCLUDED(DataGuard());  | 
710  |  |   bool ValidateInputValue(absl::string_view value) const override  | 
711  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
712  |  |   void CheckDefaultValueParsingRoundtrip() const override  | 
713  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
714  |  |  | 
715  |  |   int64_t ModificationCount() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(DataGuard());  | 
716  |  |  | 
717  |  |   // Interfaces to save and restore flags to/from persistent state.  | 
718  |  |   // Returns current flag state or nullptr if flag does not support  | 
719  |  |   // saving and restoring a state.  | 
720  |  |   std::unique_ptr<FlagStateInterface> SaveState() override  | 
721  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
722  |  |  | 
723  |  |   // Restores the flag state to the supplied state object. If there is  | 
724  |  |   // nothing to restore returns false. Otherwise returns true.  | 
725  |  |   bool RestoreState(const FlagState& flag_state)  | 
726  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
727  |  |  | 
728  |  |   bool ParseFrom(absl::string_view value, FlagSettingMode set_mode,  | 
729  |  |                  ValueSource source, std::string& error) override  | 
730  |  |       ABSL_LOCKS_EXCLUDED(DataGuard());  | 
731  |  |  | 
732  |  |   // Immutable flag's state.  | 
733  |  |  | 
734  |  |   // Flags name passed to ABSL_FLAG as second arg.  | 
735  |  |   const char* const name_;  | 
736  |  |  | 
737  |  |   // Flags type passed to ABSL_FLAG as first arg.  | 
738  |  |   const char* const type_name_;  | 
739  |  |  | 
740  |  |   // The file name where ABSL_FLAG resides.  | 
741  |  |   const char* const filename_;  | 
742  |  |   // Type-specific operations vtable.  | 
743  |  |   const FlagOpFn op_;  | 
744  |  |   // Help message literal or function to generate it.  | 
745  |  |   const FlagHelpMsg help_;  | 
746  |  |   // Indicates if help message was supplied as literal or generator func.  | 
747  |  |   const uint8_t help_source_kind_ : 1;  | 
748  |  |   // Kind of storage this flag is using for the flag's value.  | 
749  |  |   const uint8_t value_storage_kind_ : 2;  | 
750  |  |  | 
751  |  |   uint8_t : 0;  // The bytes containing the const bitfields must not be  | 
752  |  |                 // shared with bytes containing the mutable bitfields.  | 
753  |  |  | 
754  |  |   // Mutable flag's state (guarded by `data_guard_`).  | 
755  |  |  | 
756  |  |   // def_kind_ is not guard by DataGuard() since it is accessed in Init without  | 
757  |  |   // locks.  | 
758  |  |   uint8_t def_kind_ : 2;  | 
759  |  |   // Has this flag's value been modified?  | 
760  |  |   bool modified_ : 1 ABSL_GUARDED_BY(DataGuard());  | 
761  |  |   // Has this flag been specified on command line.  | 
762  |  |   bool on_command_line_ : 1 ABSL_GUARDED_BY(DataGuard());  | 
763  |  |  | 
764  |  |   // Unique tag for absl::call_once call to initialize this flag.  | 
765  |  |   absl::once_flag init_control_;  | 
766  |  |  | 
767  |  |   // Sequence lock / mutation counter.  | 
768  |  |   flags_internal::SequenceLock seq_lock_;  | 
769  |  |  | 
770  |  |   // Optional flag's callback and absl::Mutex to guard the invocations.  | 
771  |  |   FlagCallback* callback_ ABSL_GUARDED_BY(DataGuard());  | 
772  |  |   // Either a pointer to the function generating the default value based on the  | 
773  |  |   // value specified in ABSL_FLAG or pointer to the dynamically set default  | 
774  |  |   // value via SetCommandLineOptionWithMode. def_kind_ is used to distinguish  | 
775  |  |   // these two cases.  | 
776  |  |   FlagDefaultSrc default_value_;  | 
777  |  |  | 
778  |  |   // This is reserved space for an absl::Mutex to guard flag data. It will be  | 
779  |  |   // initialized in FlagImpl::Init via placement new.  | 
780  |  |   // We can't use "absl::Mutex data_guard_", since this class is not literal.  | 
781  |  |   // We do not want to use "absl::Mutex* data_guard_", since this would require  | 
782  |  |   // heap allocation during initialization, which is both slows program startup  | 
783  |  |   // and can fail. Using reserved space + placement new allows us to avoid both  | 
784  |  |   // problems.  | 
785  |  |   alignas(absl::Mutex) mutable unsigned char data_guard_[sizeof(absl::Mutex)];  | 
786  |  | };  | 
787  |  | #if defined(__GNUC__) && !defined(__clang__)  | 
788  |  | #pragma GCC diagnostic pop  | 
789  |  | #endif  | 
790  |  |  | 
791  |  | ///////////////////////////////////////////////////////////////////////////////  | 
792  |  | // The Flag object parameterized by the flag's value type. This class implements  | 
793  |  | // flag reflection handle interface.  | 
794  |  |  | 
795  |  | template <typename T>  | 
796  |  | class Flag { | 
797  |  |  public:  | 
798  |  |   constexpr Flag(const char* name, const char* type_name, const char* filename,  | 
799  |  |                  FlagHelpArg help, const FlagDefaultArg default_arg)  | 
800  |  |       : impl_(name, type_name, filename, &FlagOps<T>, help,  | 
801  |  |               flags_internal::StorageKind<T>(), default_arg),  | 
802  |  |         value_() {} | 
803  |  |  | 
804  |  |   // CommandLineFlag interface  | 
805  |  |   absl::string_view Name() const { return impl_.Name(); } | 
806  |  |   std::string Filename() const { return impl_.Filename(); } | 
807  |  |   std::string Help() const { return impl_.Help(); } | 
808  |  |   // Do not use. To be removed.  | 
809  |  |   bool IsSpecifiedOnCommandLine() const { | 
810  |  |     return impl_.IsSpecifiedOnCommandLine();  | 
811  |  |   }  | 
812  |  |   std::string DefaultValue() const { return impl_.DefaultValue(); } | 
813  |  |   std::string CurrentValue() const { return impl_.CurrentValue(); } | 
814  |  |  | 
815  |  |  private:  | 
816  |  |   template <typename, bool>  | 
817  |  |   friend class FlagRegistrar;  | 
818  |  |   friend class FlagImplPeer;  | 
819  |  |  | 
820  |  |   T Get() const { | 
821  |  |     // See implementation notes in CommandLineFlag::Get().  | 
822  |  |     union U { | 
823  |  |       T value;  | 
824  |  |       U() {} | 
825  |  |       ~U() { value.~T(); } | 
826  |  |     };  | 
827  |  |     U u;  | 
828  |  |  | 
829  |  | #if !defined(NDEBUG)  | 
830  |  |     impl_.AssertValidType(absl::FastTypeId<T>(), &GenRuntimeTypeId<T>);  | 
831  |  | #endif  | 
832  |  |  | 
833  |  |     if (ABSL_PREDICT_FALSE(!value_.Get(impl_.seq_lock_, u.value))) { | 
834  |  |       impl_.Read(&u.value);  | 
835  |  |     }  | 
836  |  |     return std::move(u.value);  | 
837  |  |   }  | 
838  |  |   void Set(const T& v) { | 
839  |  |     impl_.AssertValidType(absl::FastTypeId<T>(), &GenRuntimeTypeId<T>);  | 
840  |  |     impl_.Write(&v);  | 
841  |  |   }  | 
842  |  |  | 
843  |  |   // Access to the reflection.  | 
844  |  |   const CommandLineFlag& Reflect() const { return impl_; } | 
845  |  |  | 
846  |  |   // Flag's data  | 
847  |  |   // The implementation depends on value_ field to be placed exactly after the  | 
848  |  |   // impl_ field, so that impl_ can figure out the offset to the value and  | 
849  |  |   // access it.  | 
850  |  |   FlagImpl impl_;  | 
851  |  |   FlagValue<T> value_;  | 
852  |  | };  | 
853  |  |  | 
854  |  | ///////////////////////////////////////////////////////////////////////////////  | 
855  |  | // Trampoline for friend access  | 
856  |  |  | 
857  |  | class FlagImplPeer { | 
858  |  |  public:  | 
859  |  |   template <typename T, typename FlagType>  | 
860  |  |   static T InvokeGet(const FlagType& flag) { | 
861  |  |     return flag.Get();  | 
862  |  |   }  | 
863  |  |   template <typename FlagType, typename T>  | 
864  |  |   static void InvokeSet(FlagType& flag, const T& v) { | 
865  |  |     flag.Set(v);  | 
866  |  |   }  | 
867  |  |   template <typename FlagType>  | 
868  |  |   static const CommandLineFlag& InvokeReflect(const FlagType& f) { | 
869  |  |     return f.Reflect();  | 
870  |  |   }  | 
871  |  | };  | 
872  |  |  | 
873  |  | ///////////////////////////////////////////////////////////////////////////////  | 
874  |  | // Implementation of Flag value specific operations routine.  | 
875  |  | template <typename T>  | 
876  |  | void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) { | 
877  |  |   struct AlignedSpace { | 
878  |  |     alignas(MaskedPointer::RequiredAlignment()) alignas(  | 
879  |  |         T) unsigned char buf[sizeof(T)];  | 
880  |  |   };  | 
881  |  |   using Allocator = std::allocator<AlignedSpace>;  | 
882  |  |   switch (op) { | 
883  |  |     case FlagOp::kAlloc: { | 
884  |  |       Allocator alloc;  | 
885  |  |       return std::allocator_traits<Allocator>::allocate(alloc, 1);  | 
886  |  |     }  | 
887  |  |     case FlagOp::kDelete: { | 
888  |  |       T* p = static_cast<T*>(v2);  | 
889  |  |       p->~T();  | 
890  |  |       Allocator alloc;  | 
891  |  |       std::allocator_traits<Allocator>::deallocate(  | 
892  |  |           alloc, reinterpret_cast<AlignedSpace*>(p), 1);  | 
893  |  |       return nullptr;  | 
894  |  |     }  | 
895  |  |     case FlagOp::kCopy:  | 
896  |  |       *static_cast<T*>(v2) = *static_cast<const T*>(v1);  | 
897  |  |       return nullptr;  | 
898  |  |     case FlagOp::kCopyConstruct:  | 
899  |  |       new (v2) T(*static_cast<const T*>(v1));  | 
900  |  |       return nullptr;  | 
901  |  |     case FlagOp::kSizeof:  | 
902  |  |       return reinterpret_cast<void*>(static_cast<uintptr_t>(sizeof(T)));  | 
903  |  |     case FlagOp::kFastTypeId:  | 
904  |  |       return const_cast<void*>(absl::FastTypeId<T>());  | 
905  |  |     case FlagOp::kRuntimeTypeId:  | 
906  |  |       return const_cast<std::type_info*>(GenRuntimeTypeId<T>());  | 
907  |  |     case FlagOp::kParse: { | 
908  |  |       // Initialize the temporary instance of type T based on current value in  | 
909  |  |       // destination (which is going to be flag's default value).  | 
910  |  |       T temp(*static_cast<T*>(v2));  | 
911  |  |       if (!absl::ParseFlag<T>(*static_cast<const absl::string_view*>(v1), &temp,  | 
912  |  |                               static_cast<std::string*>(v3))) { | 
913  |  |         return nullptr;  | 
914  |  |       }  | 
915  |  |       *static_cast<T*>(v2) = std::move(temp);  | 
916  |  |       return v2;  | 
917  |  |     }  | 
918  |  |     case FlagOp::kUnparse:  | 
919  |  |       *static_cast<std::string*>(v2) =  | 
920  |  |           absl::UnparseFlag<T>(*static_cast<const T*>(v1));  | 
921  |  |       return nullptr;  | 
922  |  |     case FlagOp::kValueOffset: { | 
923  |  |       // Round sizeof(FlagImp) to a multiple of alignof(FlagValue<T>) to get the  | 
924  |  |       // offset of the data.  | 
925  |  |       size_t round_to = alignof(FlagValue<T>);  | 
926  |  |       size_t offset = (sizeof(FlagImpl) + round_to - 1) / round_to * round_to;  | 
927  |  |       return reinterpret_cast<void*>(offset);  | 
928  |  |     }  | 
929  |  |   }  | 
930  |  |   return nullptr;  | 
931  |  | }  | 
932  |  |  | 
933  |  | ///////////////////////////////////////////////////////////////////////////////  | 
934  |  | // This class facilitates Flag object registration and tail expression-based  | 
935  |  | // flag definition, for example:  | 
936  |  | // ABSL_FLAG(int, foo, 42, "Foo help").OnUpdate(NotifyFooWatcher);  | 
937  |  | struct FlagRegistrarEmpty {}; | 
938  |  | template <typename T, bool do_register>  | 
939  |  | class FlagRegistrar { | 
940  |  |  public:  | 
941  |  |   constexpr explicit FlagRegistrar(Flag<T>& flag, const char* filename)  | 
942  |  |       : flag_(flag) { | 
943  |  |     if (do_register)  | 
944  |  |       flags_internal::RegisterCommandLineFlag(flag_.impl_, filename);  | 
945  |  |   }  | 
946  |  |  | 
947  |  |   FlagRegistrar OnUpdate(FlagCallbackFunc cb) && { | 
948  |  |     flag_.impl_.SetCallback(cb);  | 
949  |  |     return *this;  | 
950  |  |   }  | 
951  |  |  | 
952  |  |   // Makes the registrar die gracefully as an empty struct on a line where  | 
953  |  |   // registration happens. Registrar objects are intended to live only as  | 
954  |  |   // temporary.  | 
955  |  |   constexpr operator FlagRegistrarEmpty() const { return {}; }  // NOLINT | 
956  |  |  | 
957  |  |  private:  | 
958  |  |   Flag<T>& flag_;  // Flag being registered (not owned).  | 
959  |  | };  | 
960  |  |  | 
961  |  | ///////////////////////////////////////////////////////////////////////////////  | 
962  |  | // Test only API  | 
963  |  | uint64_t NumLeakedFlagValues();  | 
964  |  |  | 
965  |  | }  // namespace flags_internal  | 
966  |  | ABSL_NAMESPACE_END  | 
967  |  | }  // namespace absl  | 
968  |  |  | 
969  |  | #endif  // ABSL_FLAGS_INTERNAL_FLAG_H_  |