2019-05-07 21:56:42 +02:00
|
|
|
//
|
|
|
|
// Copyright 2019 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// File: flag.h
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This header file defines the `absl::Flag<T>` type for holding command-line
|
|
|
|
// flag data, and abstractions to create, get and set such flag data.
|
|
|
|
//
|
|
|
|
// It is important to note that this type is **unspecified** (an implementation
|
|
|
|
// detail) and you do not construct or manipulate actual `absl::Flag<T>`
|
|
|
|
// instances. Instead, you define and declare flags using the
|
|
|
|
// `ABSL_FLAG()` and `ABSL_DECLARE_FLAG()` macros, and get and set flag values
|
|
|
|
// using the `absl::GetFlag()` and `absl::SetFlag()` functions.
|
|
|
|
|
|
|
|
#ifndef ABSL_FLAGS_FLAG_H_
|
|
|
|
#define ABSL_FLAGS_FLAG_H_
|
|
|
|
|
2020-01-10 17:42:35 +01:00
|
|
|
#include <string>
|
2020-01-09 18:58:48 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/base/attributes.h"
|
|
|
|
#include "absl/base/casts.h"
|
2020-01-10 17:42:35 +01:00
|
|
|
#include "absl/base/config.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/flags/config.h"
|
|
|
|
#include "absl/flags/declare.h"
|
|
|
|
#include "absl/flags/internal/commandlineflag.h"
|
|
|
|
#include "absl/flags/internal/flag.h"
|
2020-01-10 17:42:35 +01:00
|
|
|
#include "absl/flags/internal/registry.h"
|
2019-05-07 21:56:42 +02:00
|
|
|
#include "absl/flags/marshalling.h"
|
|
|
|
|
|
|
|
namespace absl {
|
2019-12-12 19:36:03 +01:00
|
|
|
ABSL_NAMESPACE_BEGIN
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// Flag
|
|
|
|
//
|
|
|
|
// An `absl::Flag` holds a command-line flag value, providing a runtime
|
|
|
|
// parameter to a binary. Such flags should be defined in the global namespace
|
|
|
|
// and (preferably) in the module containing the binary's `main()` function.
|
|
|
|
//
|
|
|
|
// You should not construct and cannot use the `absl::Flag` type directly;
|
|
|
|
// instead, you should declare flags using the `ABSL_DECLARE_FLAG()` macro
|
|
|
|
// within a header file, and define your flag using `ABSL_FLAG()` within your
|
|
|
|
// header's associated `.cc` file. Such flags will be named `FLAGS_name`.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// .h file
|
|
|
|
//
|
|
|
|
// // Declares usage of a flag named "FLAGS_count"
|
|
|
|
// ABSL_DECLARE_FLAG(int, count);
|
|
|
|
//
|
|
|
|
// .cc file
|
|
|
|
//
|
|
|
|
// // Defines a flag named "FLAGS_count" with a default `int` value of 0.
|
|
|
|
// ABSL_FLAG(int, count, 0, "Count of items to process");
|
|
|
|
//
|
|
|
|
// No public methods of `absl::Flag<T>` are part of the Abseil Flags API.
|
2019-10-21 19:21:03 +02:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2019-05-07 21:56:42 +02:00
|
|
|
template <typename T>
|
|
|
|
using Flag = flags_internal::Flag<T>;
|
2019-08-23 20:38:04 +02:00
|
|
|
#else
|
2019-10-31 19:37:35 +01:00
|
|
|
// MSVC debug builds do not implement initialization with constexpr constructors
|
|
|
|
// correctly. To work around this we add a level of indirection, so that the
|
|
|
|
// class `absl::Flag` contains an `internal::Flag*` (instead of being an alias
|
|
|
|
// to that class) and dynamically allocates an instance when necessary. We also
|
|
|
|
// forward all calls to internal::Flag methods via trampoline methods. In this
|
|
|
|
// setup the `absl::Flag` class does not have constructor and virtual methods,
|
|
|
|
// all the data members are public and thus MSVC is able to initialize it at
|
|
|
|
// link time. To deal with multiple threads accessing the flag for the first
|
|
|
|
// time concurrently we use an atomic boolean indicating if flag object is
|
|
|
|
// initialized. We also employ the double-checked locking pattern where the
|
|
|
|
// second level of protection is a global Mutex, so if two threads attempt to
|
|
|
|
// construct the flag concurrently only one wins.
|
|
|
|
// This solution is based on a recomendation here:
|
|
|
|
// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html?childToView=648454#comment-648454
|
2019-08-23 20:38:04 +02:00
|
|
|
|
|
|
|
namespace flags_internal {
|
Export of internal Abseil changes
--
f13697e3d33803f9667d124072da4f6dd8bfbf85 by Andy Soffer <asoffer@google.com>:
Addressing https://github.com/abseil/abseil-cpp/issues/314, fixing
CMakeLists.txt to reference ABSL_TEST_COPTS rather than ABSL_DEFAULT_COPTS.
ABSL_TEST_COPTS should be preferred for all tests so that they are configured consistently (moreover, CMake should agree with Bazel).
PiperOrigin-RevId: 274932312
--
c31c24a1fa6bb98136adf51ef37c0818ac366690 by Derek Mauro <dmauro@google.com>:
Silence MSAN in the stack consumption test utility
PiperOrigin-RevId: 274912950
--
2412913c05a246cd527cd4c31452f126e9129f3a by CJ Johnson <johnsoncj@google.com>:
Internal change
PiperOrigin-RevId: 274847103
--
75e984a93b5760873501b96ac3229ccfd955daf8 by Abseil Team <absl-team@google.com>:
Reformat BUILD file to current standards.
PiperOrigin-RevId: 274815392
--
a2780e085f1df1e4ca2c814a58c893d1b78a1d9c by Samuel Benzaquen <sbenza@google.com>:
Fix invalid result regarding leading zeros in the exponent.
PiperOrigin-RevId: 274808017
--
dd402e1cb5c4ebacb576372ae24bf289d729d323 by Samuel Benzaquen <sbenza@google.com>:
Make string_view's relational operators constexpr when possible.
PiperOrigin-RevId: 274807873
--
b4ef32565653a5da1cb8bb8d0351586d23519658 by Abseil Team <absl-team@google.com>:
Internal rework.
PiperOrigin-RevId: 274787159
--
70d81971c5914e6785b8e8a9d4f6eb2655dd62c0 by Gennadiy Rozental <rogeeff@google.com>:
Internal rework.
PiperOrigin-RevId: 274715557
--
14f5b0440e353b899cafaaa15b53e77f98f401af by Gennadiy Rozental <rogeeff@google.com>:
Make deprecated statements about ParseFLag/UnparseFlag consistent in a file.
PiperOrigin-RevId: 274668123
--
2e85adbdbb92612e4d750bc34fbca3333128b42d by Abseil Team <absl-team@google.com>:
Allow absl::c_equal to be used with arrays.
This is achieved by allowing container size computation for arrays.
PiperOrigin-RevId: 274426830
--
219719f107226d328773e6cec99fb473f5d3119c by Gennadiy Rozental <rogeeff@google.com>:
Release correct extension interfaces to support usage of absl::Time and absl::Duration as ABSL_FLAG
PiperOrigin-RevId: 274273788
--
47a77f93fda23b69b4a6bdbd506fe643c69a5579 by Gennadiy Rozental <rogeeff@google.com>:
Rework of flags persistence/FlagSaver internals.
PiperOrigin-RevId: 274225213
--
7807be3fe757c19e3b0c487298387683d4c9f5b3 by Abseil Team <absl-team@google.com>:
Switch reference to sdkddkver.h to lowercase, matching conventions used in the Windows SDK and other uses. This helps to avoid confusion on case-sensitive filesystems.
PiperOrigin-RevId: 274061877
--
561304090087a19f1d10f0475f564fe132ebf06e by Andy Getzendanner <durandal@google.com>:
Fix ABSL_WAITER_MODE detection for mingw
Import of https://github.com/abseil/abseil-cpp/pull/342
PiperOrigin-RevId: 274030071
--
9b3caac2cf202b9d440dfa1b4ffd538ac4bf715b by Derek Mauro <dmauro@google.com>:
Support using Abseil with the musl libc implementation.
Only test changes were required:
* Workaround for a bug in sigaltstack() on musl
* printf-style pointer formatting (%p) is implementation defined,
so verify StrFromat produces something compatible
* Fix detection of feenableexcept()
PiperOrigin-RevId: 274011666
--
73e8a938fc139e1cc8670d4513a445bacc855539 by Abseil Team <absl-team@google.com>:
nvcc workaround: explicitly specify the definition of node_handle::Base
PiperOrigin-RevId: 274011392
--
ab9cc6d042aca7d48e16c504ab10eab39433f4b2 by Andy Soffer <asoffer@google.com>:
Internal change
PiperOrigin-RevId: 273996318
--
e567c4979ca99c7e71821ec1523b8f5edd2c76ac by Abseil Team <absl-team@google.com>:
Introduce a type alias to work around an nvcc bug.
On the previous code, nvcc gets confused thinking that T has to be a parameter
pack, as IsDecomposable accepts one.
PiperOrigin-RevId: 273980472
--
105b6e6339b77a32f4432de05f44cd3f9c436751 by Eric Fiselier <ericwf@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 273955589
--
8feb87ff1d7e721fe094855e67c19539d5e582b7 by Abseil Team <absl-team@google.com>:
Avoid dual-exporting scheduling_mode.h
PiperOrigin-RevId: 273825112
--
fbc37854776d295dae98fb9d06a541f296daab95 by Andy Getzendanner <durandal@google.com>:
Fix ABSL_HAVE_ALARM check on mingw
Import of https://github.com/abseil/abseil-cpp/pull/341
PiperOrigin-RevId: 273817839
--
6aedcd63a735b9133e143b043744ba0a25407f6f by Andy Soffer <asoffer@google.com>:
Remove bit_gen_view.h now that all callers have been migrated to bit_gen_ref.h
Tested:
TGP - https://test.corp.google.com/ui#id=OCL:273762409:BASE:273743370:1570639020744:3001bcb5
PiperOrigin-RevId: 273810331
--
6573de24a66ba715c579f7f32b5c48a1d743c7f8 by Abseil Team <absl-team@google.com>:
Internal change.
PiperOrigin-RevId: 273589963
--
91c8c28b6dca26d98b39e8e06a8ed17c701ff793 by Abseil Team <absl-team@google.com>:
Update macro name for `ABSL_GUARDED_BY()` in the example section.
PiperOrigin-RevId: 273286983
--
0ff7d1a93d70f8ecd693f8dbb98b7a4a016ca2a4 by Abseil Team <absl-team@google.com>:
Fix potential integer overflow in the absl time library.
In absl::FromTM, the tm.tm_year is added by 1900 regarding that tm.tm_year represents the years since 1900. This change checks integer overflow before doing the arithmetic operation.
PiperOrigin-RevId: 273092952
--
b41c2a1310086807be09a833099ae6d4009f037c by Gennadiy Rozental <rogeeff@google.com>:
Correctly Unlock the global mutex in case of concurrent flag initialization.
Fixes #386
PiperOrigin-RevId: 272979749
--
c53103e71b2a6063af3c6d4ff68aa2d8f9ae9e06 by Abseil Team <absl-team@google.com>:
Try to become idle only when there is no wakeup.
Immediately after waking up (when futex wait returns), the current thread tries
to become idle doing bunch of memory loads and a branch. Problem is that there
is a good chance that we woke up due to a wakeup, especially for actively used
threads. For such wakeups, calling MaybeBecomeIdle() would be a waste of
cycles.
Instead, call MaybeBecomeIdle() only when we are sure there is no wakeup. For
idle threads the net effect should be the same. For active, threads this will
be more efficient.
Moreover, since MaybeBecomeIdle() is called before waiting on the futex, the
current thread will try to become idle before sleeping. This should result
in more accurate idleness and more efficient release of thread resources.
PiperOrigin-RevId: 272940381
GitOrigin-RevId: f13697e3d33803f9667d124072da4f6dd8bfbf85
Change-Id: I36de05aec12595183725652dd362dfa58fb095d0
2019-10-16 03:18:40 +02:00
|
|
|
absl::Mutex* GetGlobalConstructionGuard();
|
2019-08-23 20:38:04 +02:00
|
|
|
} // namespace flags_internal
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Flag {
|
|
|
|
public:
|
2019-10-31 19:37:35 +01:00
|
|
|
// No constructor and destructor to ensure this is an aggregate type.
|
|
|
|
// Visual Studio 2015 still requires the constructor for class to be
|
|
|
|
// constexpr initializable.
|
|
|
|
#if _MSC_VER <= 1900
|
2020-01-03 17:41:10 +01:00
|
|
|
constexpr Flag(const char* name, const char* filename,
|
|
|
|
const flags_internal::HelpGenFunc help_gen,
|
2019-12-02 19:41:53 +01:00
|
|
|
const flags_internal::FlagDfltGenFunc default_value_gen)
|
2019-08-23 20:38:04 +02:00
|
|
|
: name_(name),
|
|
|
|
filename_(filename),
|
2020-01-03 17:41:10 +01:00
|
|
|
help_gen_(help_gen),
|
2019-12-02 19:41:53 +01:00
|
|
|
default_value_gen_(default_value_gen),
|
2019-10-31 19:37:35 +01:00
|
|
|
inited_(false),
|
|
|
|
impl_(nullptr) {}
|
|
|
|
#endif
|
2019-08-23 20:38:04 +02:00
|
|
|
|
|
|
|
flags_internal::Flag<T>* GetImpl() const {
|
|
|
|
if (!inited_.load(std::memory_order_acquire)) {
|
Export of internal Abseil changes
--
f13697e3d33803f9667d124072da4f6dd8bfbf85 by Andy Soffer <asoffer@google.com>:
Addressing https://github.com/abseil/abseil-cpp/issues/314, fixing
CMakeLists.txt to reference ABSL_TEST_COPTS rather than ABSL_DEFAULT_COPTS.
ABSL_TEST_COPTS should be preferred for all tests so that they are configured consistently (moreover, CMake should agree with Bazel).
PiperOrigin-RevId: 274932312
--
c31c24a1fa6bb98136adf51ef37c0818ac366690 by Derek Mauro <dmauro@google.com>:
Silence MSAN in the stack consumption test utility
PiperOrigin-RevId: 274912950
--
2412913c05a246cd527cd4c31452f126e9129f3a by CJ Johnson <johnsoncj@google.com>:
Internal change
PiperOrigin-RevId: 274847103
--
75e984a93b5760873501b96ac3229ccfd955daf8 by Abseil Team <absl-team@google.com>:
Reformat BUILD file to current standards.
PiperOrigin-RevId: 274815392
--
a2780e085f1df1e4ca2c814a58c893d1b78a1d9c by Samuel Benzaquen <sbenza@google.com>:
Fix invalid result regarding leading zeros in the exponent.
PiperOrigin-RevId: 274808017
--
dd402e1cb5c4ebacb576372ae24bf289d729d323 by Samuel Benzaquen <sbenza@google.com>:
Make string_view's relational operators constexpr when possible.
PiperOrigin-RevId: 274807873
--
b4ef32565653a5da1cb8bb8d0351586d23519658 by Abseil Team <absl-team@google.com>:
Internal rework.
PiperOrigin-RevId: 274787159
--
70d81971c5914e6785b8e8a9d4f6eb2655dd62c0 by Gennadiy Rozental <rogeeff@google.com>:
Internal rework.
PiperOrigin-RevId: 274715557
--
14f5b0440e353b899cafaaa15b53e77f98f401af by Gennadiy Rozental <rogeeff@google.com>:
Make deprecated statements about ParseFLag/UnparseFlag consistent in a file.
PiperOrigin-RevId: 274668123
--
2e85adbdbb92612e4d750bc34fbca3333128b42d by Abseil Team <absl-team@google.com>:
Allow absl::c_equal to be used with arrays.
This is achieved by allowing container size computation for arrays.
PiperOrigin-RevId: 274426830
--
219719f107226d328773e6cec99fb473f5d3119c by Gennadiy Rozental <rogeeff@google.com>:
Release correct extension interfaces to support usage of absl::Time and absl::Duration as ABSL_FLAG
PiperOrigin-RevId: 274273788
--
47a77f93fda23b69b4a6bdbd506fe643c69a5579 by Gennadiy Rozental <rogeeff@google.com>:
Rework of flags persistence/FlagSaver internals.
PiperOrigin-RevId: 274225213
--
7807be3fe757c19e3b0c487298387683d4c9f5b3 by Abseil Team <absl-team@google.com>:
Switch reference to sdkddkver.h to lowercase, matching conventions used in the Windows SDK and other uses. This helps to avoid confusion on case-sensitive filesystems.
PiperOrigin-RevId: 274061877
--
561304090087a19f1d10f0475f564fe132ebf06e by Andy Getzendanner <durandal@google.com>:
Fix ABSL_WAITER_MODE detection for mingw
Import of https://github.com/abseil/abseil-cpp/pull/342
PiperOrigin-RevId: 274030071
--
9b3caac2cf202b9d440dfa1b4ffd538ac4bf715b by Derek Mauro <dmauro@google.com>:
Support using Abseil with the musl libc implementation.
Only test changes were required:
* Workaround for a bug in sigaltstack() on musl
* printf-style pointer formatting (%p) is implementation defined,
so verify StrFromat produces something compatible
* Fix detection of feenableexcept()
PiperOrigin-RevId: 274011666
--
73e8a938fc139e1cc8670d4513a445bacc855539 by Abseil Team <absl-team@google.com>:
nvcc workaround: explicitly specify the definition of node_handle::Base
PiperOrigin-RevId: 274011392
--
ab9cc6d042aca7d48e16c504ab10eab39433f4b2 by Andy Soffer <asoffer@google.com>:
Internal change
PiperOrigin-RevId: 273996318
--
e567c4979ca99c7e71821ec1523b8f5edd2c76ac by Abseil Team <absl-team@google.com>:
Introduce a type alias to work around an nvcc bug.
On the previous code, nvcc gets confused thinking that T has to be a parameter
pack, as IsDecomposable accepts one.
PiperOrigin-RevId: 273980472
--
105b6e6339b77a32f4432de05f44cd3f9c436751 by Eric Fiselier <ericwf@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 273955589
--
8feb87ff1d7e721fe094855e67c19539d5e582b7 by Abseil Team <absl-team@google.com>:
Avoid dual-exporting scheduling_mode.h
PiperOrigin-RevId: 273825112
--
fbc37854776d295dae98fb9d06a541f296daab95 by Andy Getzendanner <durandal@google.com>:
Fix ABSL_HAVE_ALARM check on mingw
Import of https://github.com/abseil/abseil-cpp/pull/341
PiperOrigin-RevId: 273817839
--
6aedcd63a735b9133e143b043744ba0a25407f6f by Andy Soffer <asoffer@google.com>:
Remove bit_gen_view.h now that all callers have been migrated to bit_gen_ref.h
Tested:
TGP - https://test.corp.google.com/ui#id=OCL:273762409:BASE:273743370:1570639020744:3001bcb5
PiperOrigin-RevId: 273810331
--
6573de24a66ba715c579f7f32b5c48a1d743c7f8 by Abseil Team <absl-team@google.com>:
Internal change.
PiperOrigin-RevId: 273589963
--
91c8c28b6dca26d98b39e8e06a8ed17c701ff793 by Abseil Team <absl-team@google.com>:
Update macro name for `ABSL_GUARDED_BY()` in the example section.
PiperOrigin-RevId: 273286983
--
0ff7d1a93d70f8ecd693f8dbb98b7a4a016ca2a4 by Abseil Team <absl-team@google.com>:
Fix potential integer overflow in the absl time library.
In absl::FromTM, the tm.tm_year is added by 1900 regarding that tm.tm_year represents the years since 1900. This change checks integer overflow before doing the arithmetic operation.
PiperOrigin-RevId: 273092952
--
b41c2a1310086807be09a833099ae6d4009f037c by Gennadiy Rozental <rogeeff@google.com>:
Correctly Unlock the global mutex in case of concurrent flag initialization.
Fixes #386
PiperOrigin-RevId: 272979749
--
c53103e71b2a6063af3c6d4ff68aa2d8f9ae9e06 by Abseil Team <absl-team@google.com>:
Try to become idle only when there is no wakeup.
Immediately after waking up (when futex wait returns), the current thread tries
to become idle doing bunch of memory loads and a branch. Problem is that there
is a good chance that we woke up due to a wakeup, especially for actively used
threads. For such wakeups, calling MaybeBecomeIdle() would be a waste of
cycles.
Instead, call MaybeBecomeIdle() only when we are sure there is no wakeup. For
idle threads the net effect should be the same. For active, threads this will
be more efficient.
Moreover, since MaybeBecomeIdle() is called before waiting on the futex, the
current thread will try to become idle before sleeping. This should result
in more accurate idleness and more efficient release of thread resources.
PiperOrigin-RevId: 272940381
GitOrigin-RevId: f13697e3d33803f9667d124072da4f6dd8bfbf85
Change-Id: I36de05aec12595183725652dd362dfa58fb095d0
2019-10-16 03:18:40 +02:00
|
|
|
absl::MutexLock l(flags_internal::GetGlobalConstructionGuard());
|
2019-08-23 20:38:04 +02:00
|
|
|
|
|
|
|
if (inited_.load(std::memory_order_acquire)) {
|
|
|
|
return impl_;
|
|
|
|
}
|
|
|
|
|
2020-02-05 23:38:00 +01:00
|
|
|
impl_ =
|
2020-02-14 18:41:25 +01:00
|
|
|
new flags_internal::Flag<T>(name_, filename_,
|
2020-02-05 23:38:00 +01:00
|
|
|
{flags_internal::FlagHelpMsg(help_gen_),
|
|
|
|
flags_internal::FlagHelpKind::kGenFunc},
|
|
|
|
default_value_gen_);
|
2019-08-23 20:38:04 +02:00
|
|
|
inited_.store(true, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
return impl_;
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:37:35 +01:00
|
|
|
// Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
|
|
|
|
// See https://abseil.io/docs/cpp/guides/flags
|
2019-08-23 20:38:04 +02:00
|
|
|
bool IsRetired() const { return GetImpl()->IsRetired(); }
|
|
|
|
bool IsAbseilFlag() const { return GetImpl()->IsAbseilFlag(); }
|
|
|
|
absl::string_view Name() const { return GetImpl()->Name(); }
|
|
|
|
std::string Help() const { return GetImpl()->Help(); }
|
|
|
|
bool IsModified() const { return GetImpl()->IsModified(); }
|
|
|
|
bool IsSpecifiedOnCommandLine() const {
|
2019-10-17 00:51:11 +02:00
|
|
|
return GetImpl()->IsSpecifiedOnCommandLine();
|
2019-08-23 20:38:04 +02:00
|
|
|
}
|
|
|
|
absl::string_view Typename() const { return GetImpl()->Typename(); }
|
|
|
|
std::string Filename() const { return GetImpl()->Filename(); }
|
|
|
|
std::string DefaultValue() const { return GetImpl()->DefaultValue(); }
|
|
|
|
std::string CurrentValue() const { return GetImpl()->CurrentValue(); }
|
2019-10-31 19:37:35 +01:00
|
|
|
template <typename U>
|
2019-08-23 20:38:04 +02:00
|
|
|
inline bool IsOfType() const {
|
2019-10-31 19:37:35 +01:00
|
|
|
return GetImpl()->template IsOfType<U>();
|
2019-08-23 20:38:04 +02:00
|
|
|
}
|
|
|
|
T Get() const { return GetImpl()->Get(); }
|
|
|
|
void Set(const T& v) { GetImpl()->Set(v); }
|
2020-02-05 23:38:00 +01:00
|
|
|
void SetCallback(const flags_internal::FlagCallbackFunc mutation_callback) {
|
2019-08-23 20:38:04 +02:00
|
|
|
GetImpl()->SetCallback(mutation_callback);
|
|
|
|
}
|
|
|
|
void InvokeCallback() { GetImpl()->InvokeCallback(); }
|
|
|
|
|
2019-10-31 19:37:35 +01:00
|
|
|
// The data members are logically private, but they need to be public for
|
|
|
|
// this to be an aggregate type.
|
2019-08-23 20:38:04 +02:00
|
|
|
const char* name_;
|
|
|
|
const char* filename_;
|
2020-01-03 17:41:10 +01:00
|
|
|
const flags_internal::HelpGenFunc help_gen_;
|
2019-12-02 19:41:53 +01:00
|
|
|
const flags_internal::FlagDfltGenFunc default_value_gen_;
|
2019-08-23 20:38:04 +02:00
|
|
|
|
|
|
|
mutable std::atomic<bool> inited_;
|
2019-10-31 19:37:35 +01:00
|
|
|
mutable flags_internal::Flag<T>* impl_;
|
2019-08-23 20:38:04 +02:00
|
|
|
};
|
|
|
|
#endif
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// GetFlag()
|
|
|
|
//
|
|
|
|
// Returns the value (of type `T`) of an `absl::Flag<T>` instance, by value. Do
|
|
|
|
// not construct an `absl::Flag<T>` directly and call `absl::GetFlag()`;
|
|
|
|
// instead, refer to flag's constructed variable name (e.g. `FLAGS_name`).
|
|
|
|
// Because this function returns by value and not by reference, it is
|
|
|
|
// thread-safe, but note that the operation may be expensive; as a result, avoid
|
|
|
|
// `absl::GetFlag()` within any tight loops.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// // FLAGS_count is a Flag of type `int`
|
|
|
|
// int my_count = absl::GetFlag(FLAGS_count);
|
|
|
|
//
|
|
|
|
// // FLAGS_firstname is a Flag of type `std::string`
|
|
|
|
// std::string first_name = absl::GetFlag(FLAGS_firstname);
|
2020-02-04 23:18:00 +01:00
|
|
|
template <typename T>
|
|
|
|
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
|
|
|
|
return flag.Get();
|
|
|
|
}
|
|
|
|
|
2019-05-07 21:56:42 +02:00
|
|
|
// SetFlag()
|
|
|
|
//
|
|
|
|
// Sets the value of an `absl::Flag` to the value `v`. Do not construct an
|
|
|
|
// `absl::Flag<T>` directly and call `absl::SetFlag()`; instead, use the
|
|
|
|
// flag's variable name (e.g. `FLAGS_name`). This function is
|
|
|
|
// thread-safe, but is potentially expensive. Avoid setting flags in general,
|
|
|
|
// but especially within performance-critical code.
|
|
|
|
template <typename T>
|
|
|
|
void SetFlag(absl::Flag<T>* flag, const T& v) {
|
2019-07-17 22:35:47 +02:00
|
|
|
flag->Set(v);
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overload of `SetFlag()` to allow callers to pass in a value that is
|
|
|
|
// convertible to `T`. E.g., use this overload to pass a "const char*" when `T`
|
|
|
|
// is `std::string`.
|
|
|
|
template <typename T, typename V>
|
|
|
|
void SetFlag(absl::Flag<T>* flag, const V& v) {
|
|
|
|
T value(v);
|
2019-07-17 22:35:47 +02:00
|
|
|
flag->Set(value);
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 19:36:03 +01:00
|
|
|
ABSL_NAMESPACE_END
|
2019-05-07 21:56:42 +02:00
|
|
|
} // namespace absl
|
|
|
|
|
|
|
|
|
|
|
|
// ABSL_FLAG()
|
|
|
|
//
|
|
|
|
// This macro defines an `absl::Flag<T>` instance of a specified type `T`:
|
|
|
|
//
|
|
|
|
// ABSL_FLAG(T, name, default_value, help);
|
|
|
|
//
|
|
|
|
// where:
|
|
|
|
//
|
2019-06-25 03:35:20 +02:00
|
|
|
// * `T` is a supported flag type (see the list of types in `marshalling.h`),
|
2019-05-07 21:56:42 +02:00
|
|
|
// * `name` designates the name of the flag (as a global variable
|
|
|
|
// `FLAGS_name`),
|
|
|
|
// * `default_value` is an expression holding the default value for this flag
|
|
|
|
// (which must be implicitly convertible to `T`),
|
|
|
|
// * `help` is the help text, which can also be an expression.
|
|
|
|
//
|
|
|
|
// This macro expands to a flag named 'FLAGS_name' of type 'T':
|
|
|
|
//
|
|
|
|
// absl::Flag<T> FLAGS_name = ...;
|
|
|
|
//
|
|
|
|
// Note that all such instances are created as global variables.
|
|
|
|
//
|
|
|
|
// For `ABSL_FLAG()` values that you wish to expose to other translation units,
|
|
|
|
// it is recommended to define those flags within the `.cc` file associated with
|
|
|
|
// the header where the flag is declared.
|
|
|
|
//
|
|
|
|
// Note: do not construct objects of type `absl::Flag<T>` directly. Only use the
|
|
|
|
// `ABSL_FLAG()` macro for such construction.
|
|
|
|
#define ABSL_FLAG(Type, name, default_value, help) \
|
|
|
|
ABSL_FLAG_IMPL(Type, name, default_value, help)
|
|
|
|
|
|
|
|
// ABSL_FLAG().OnUpdate()
|
|
|
|
//
|
|
|
|
// Defines a flag of type `T` with a callback attached:
|
|
|
|
//
|
|
|
|
// ABSL_FLAG(T, name, default_value, help).OnUpdate(callback);
|
|
|
|
//
|
|
|
|
// After any setting of the flag value, the callback will be called at least
|
|
|
|
// once. A rapid sequence of changes may be merged together into the same
|
|
|
|
// callback. No concurrent calls to the callback will be made for the same
|
|
|
|
// flag. Callbacks are allowed to read the current value of the flag but must
|
|
|
|
// not mutate that flag.
|
|
|
|
//
|
|
|
|
// The update mechanism guarantees "eventual consistency"; if the callback
|
|
|
|
// derives an auxiliary data structure from the flag value, it is guaranteed
|
|
|
|
// that eventually the flag value and the derived data structure will be
|
|
|
|
// consistent.
|
|
|
|
//
|
|
|
|
// Note: ABSL_FLAG.OnUpdate() does not have a public definition. Hence, this
|
|
|
|
// comment serves as its API documentation.
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Implementation details below this section
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_NAMES
|
|
|
|
|
|
|
|
#if ABSL_FLAGS_STRIP_NAMES
|
|
|
|
#define ABSL_FLAG_IMPL_FLAGNAME(txt) ""
|
|
|
|
#define ABSL_FLAG_IMPL_FILENAME() ""
|
2019-10-21 19:21:03 +02:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2019-05-07 21:56:42 +02:00
|
|
|
#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
|
|
|
|
absl::flags_internal::FlagRegistrar<T, false>(&flag)
|
|
|
|
#else
|
2019-08-23 20:38:04 +02:00
|
|
|
#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
|
|
|
|
absl::flags_internal::FlagRegistrar<T, false>(flag.GetImpl())
|
|
|
|
#endif
|
|
|
|
#else
|
2019-05-07 21:56:42 +02:00
|
|
|
#define ABSL_FLAG_IMPL_FLAGNAME(txt) txt
|
|
|
|
#define ABSL_FLAG_IMPL_FILENAME() __FILE__
|
2019-10-21 19:21:03 +02:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2019-05-07 21:56:42 +02:00
|
|
|
#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
|
|
|
|
absl::flags_internal::FlagRegistrar<T, true>(&flag)
|
2019-08-23 20:38:04 +02:00
|
|
|
#else
|
|
|
|
#define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
|
|
|
|
absl::flags_internal::FlagRegistrar<T, true>(flag.GetImpl())
|
|
|
|
#endif
|
2019-05-07 21:56:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_HELP
|
|
|
|
|
|
|
|
#if ABSL_FLAGS_STRIP_HELP
|
|
|
|
#define ABSL_FLAG_IMPL_FLAGHELP(txt) absl::flags_internal::kStrippedFlagHelp
|
|
|
|
#else
|
|
|
|
#define ABSL_FLAG_IMPL_FLAGHELP(txt) txt
|
|
|
|
#endif
|
|
|
|
|
2019-11-18 20:02:26 +01:00
|
|
|
// AbslFlagHelpGenFor##name is used to encapsulate both immediate (method Const)
|
|
|
|
// and lazy (method NonConst) evaluation of help message expression. We choose
|
|
|
|
// between the two via the call to HelpArg in absl::Flag instantiation below.
|
|
|
|
// If help message expression is constexpr evaluable compiler will optimize
|
|
|
|
// away this whole struct.
|
|
|
|
#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
|
|
|
|
struct AbslFlagHelpGenFor##name { \
|
|
|
|
template <typename T = void> \
|
|
|
|
static constexpr const char* Const() { \
|
|
|
|
return absl::flags_internal::HelpConstexprWrap( \
|
|
|
|
ABSL_FLAG_IMPL_FLAGHELP(txt)); \
|
|
|
|
} \
|
|
|
|
static std::string NonConst() { return ABSL_FLAG_IMPL_FLAGHELP(txt); } \
|
2019-05-07 21:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
|
|
|
|
static void* AbslFlagsInitFlag##name() { \
|
|
|
|
return absl::flags_internal::MakeFromDefaultValue<Type>(default_value); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// ABSL_FLAG_IMPL
|
|
|
|
//
|
|
|
|
// Note: Name of registrar object is not arbitrary. It is used to "grab"
|
|
|
|
// global name for FLAGS_no<flag_name> symbol, thus preventing the possibility
|
|
|
|
// of defining two flags with names foo and nofoo.
|
2019-10-31 19:37:35 +01:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2019-07-19 00:07:49 +02:00
|
|
|
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
|
2019-08-07 00:11:42 +02:00
|
|
|
namespace absl /* block flags in namespaces */ {} \
|
2019-07-19 00:07:49 +02:00
|
|
|
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
|
2019-11-18 20:02:26 +01:00
|
|
|
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
|
2019-10-31 19:37:35 +01:00
|
|
|
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
|
2020-01-03 17:41:10 +01:00
|
|
|
ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
|
|
|
|
absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>(0), \
|
2019-10-31 19:37:35 +01:00
|
|
|
&AbslFlagsInitFlag##name}; \
|
|
|
|
extern bool FLAGS_no##name; \
|
|
|
|
bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
|
|
|
|
#else
|
2019-11-18 20:02:26 +01:00
|
|
|
// MSVC version uses aggregate initialization. We also do not try to
|
|
|
|
// optimize away help wrapper.
|
2020-01-03 17:41:10 +01:00
|
|
|
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
|
|
|
|
namespace absl /* block flags in namespaces */ {} \
|
|
|
|
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
|
|
|
|
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
|
|
|
|
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
|
|
|
|
ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
|
|
|
|
&AbslFlagHelpGenFor##name::NonConst, &AbslFlagsInitFlag##name}; \
|
|
|
|
extern bool FLAGS_no##name; \
|
2019-05-07 21:56:42 +02:00
|
|
|
bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
|
2019-10-31 19:37:35 +01:00
|
|
|
#endif
|
2019-05-07 21:56:42 +02:00
|
|
|
|
|
|
|
// ABSL_RETIRED_FLAG
|
|
|
|
//
|
|
|
|
// Designates the flag (which is usually pre-existing) as "retired." A retired
|
|
|
|
// flag is a flag that is now unused by the program, but may still be passed on
|
|
|
|
// the command line, usually by production scripts. A retired flag is ignored
|
|
|
|
// and code can't access it at runtime.
|
|
|
|
//
|
|
|
|
// This macro registers a retired flag with given name and type, with a name
|
|
|
|
// identical to the name of the original flag you are retiring. The retired
|
|
|
|
// flag's type can change over time, so that you can retire code to support a
|
|
|
|
// custom flag type.
|
|
|
|
//
|
|
|
|
// This macro has the same signature as `ABSL_FLAG`. To retire a flag, simply
|
|
|
|
// replace an `ABSL_FLAG` definition with `ABSL_RETIRED_FLAG`, leaving the
|
|
|
|
// arguments unchanged (unless of course you actually want to retire the flag
|
|
|
|
// type at this time as well).
|
|
|
|
//
|
|
|
|
// `default_value` is only used as a double check on the type. `explanation` is
|
|
|
|
// unused.
|
|
|
|
// TODO(rogeeff): Return an anonymous struct instead of bool, and place it into
|
|
|
|
// the unnamed namespace.
|
|
|
|
#define ABSL_RETIRED_FLAG(type, flagname, default_value, explanation) \
|
2020-01-03 17:41:10 +01:00
|
|
|
ABSL_ATTRIBUTE_UNUSED static const bool ignored_##flagname = \
|
|
|
|
([] { return type(default_value); }, \
|
2019-05-07 21:56:42 +02:00
|
|
|
absl::flags_internal::RetiredFlag<type>(#flagname))
|
|
|
|
|
|
|
|
#endif // ABSL_FLAGS_FLAG_H_
|