Commit 4038192a by Derek Mauro Committed by Copybara-Service

Remove workaround for older versions MSVC that did not support

constexpr Flag construction. These versions of MSVC are no longer
supported.

PiperOrigin-RevId: 595195773
Change-Id: Iaf2f01a2255fb24f3aadc26f09600543fffa9da5
parent 9028b812
......@@ -229,10 +229,6 @@ cc_library(
cc_library(
name = "flag",
srcs = [
"flag.cc",
"internal/flag_msvc.inc",
],
hdrs = [
"declare.h",
"flag.h",
......
......@@ -201,12 +201,9 @@ absl_cc_library(
absl_cc_library(
NAME
flags
SRCS
"flag.cc"
HDRS
"declare.h"
"flag.h"
"internal/flag_msvc.inc"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
......
......@@ -40,13 +40,8 @@ class Flag;
// Flag
//
// Forward declaration of the `absl::Flag` type for use in defining the macro.
#if defined(_MSC_VER) && !defined(__clang__)
template <typename T>
class Flag;
#else
template <typename T>
using Flag = flags_internal::Flag<T>;
#endif
ABSL_NAMESPACE_END
} // namespace absl
......
//
// 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.
#include "absl/flags/flag.h"
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
// This global mutex protects on-demand construction of flag objects in MSVC
// builds.
#if defined(_MSC_VER) && !defined(__clang__)
namespace flags_internal {
ABSL_CONST_INIT static absl::Mutex construction_guard(absl::kConstInit);
absl::Mutex* GetGlobalConstructionGuard() { return &construction_guard; }
} // namespace flags_internal
#endif
ABSL_NAMESPACE_END
} // namespace absl
......@@ -71,12 +71,9 @@ ABSL_NAMESPACE_BEGIN
// For type support of Abseil Flags, see the marshalling.h header file, which
// discusses supported standard types, optional flags, and additional Abseil
// type support.
#if !defined(_MSC_VER) || defined(__clang__)
template <typename T>
using Flag = flags_internal::Flag<T>;
#else
#include "absl/flags/internal/flag_msvc.inc"
#endif
// GetFlag()
//
......@@ -196,18 +193,12 @@ ABSL_NAMESPACE_END
// -----------------------------------------------------------------------------
// ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_NAMES
#if !defined(_MSC_VER) || defined(__clang__)
#define ABSL_FLAG_IMPL_FLAG_PTR(flag) flag
#define ABSL_FLAG_IMPL_HELP_ARG(name) \
absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>( \
FLAGS_help_storage_##name)
#define ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name) \
absl::flags_internal::DefaultArg<Type, AbslFlagDefaultGenFor##name>(0)
#else
#define ABSL_FLAG_IMPL_FLAG_PTR(flag) flag.GetImpl()
#define ABSL_FLAG_IMPL_HELP_ARG(name) &AbslFlagHelpGenFor##name::NonConst
#define ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name) &AbslFlagDefaultGenFor##name::Gen
#endif
#if ABSL_FLAGS_STRIP_NAMES
#define ABSL_FLAG_IMPL_FLAGNAME(txt) ""
......
......@@ -144,7 +144,6 @@ using String = std::string;
using int128 = absl::int128;
using uint128 = absl::uint128;
#if !defined(_MSC_VER) || defined(__clang__)
#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
constexpr flags::FlagDefaultArg f1default##T{ \
flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
......@@ -157,16 +156,6 @@ using uint128 = absl::uint128;
flags::FlagDefaultKind::kGenFunc \
} \
}
#else
#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
constexpr flags::FlagDefaultArg f1default##T{ \
flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
constexpr absl::Flag<T> f1##T{"f1", "file", &TestLiteralHelpMsg, \
&TestMakeDflt<T>}; \
ABSL_CONST_INIT absl::Flag<T> f2##T { \
"f2", "file", &TestHelpMsg, &TestMakeDflt<T> \
}
#endif
DEFINE_CONSTRUCTED_FLAG(bool, true, kOneWord);
DEFINE_CONSTRUCTED_FLAG(int16_t, 1, kOneWord);
......
......@@ -54,13 +54,8 @@ template <typename T>
class Flag;
} // namespace flags_internal
#if defined(_MSC_VER) && !defined(__clang__)
template <typename T>
class Flag;
#else
template <typename T>
using Flag = flags_internal::Flag<T>;
#endif
template <typename T>
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag);
......
//
// Copyright 2021 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.
// Do not include this file directly.
// Include absl/flags/flag.h instead.
// 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 recommendation here:
// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html?childToView=648454#comment-648454
namespace flags_internal {
absl::Mutex* GetGlobalConstructionGuard();
} // namespace flags_internal
// Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
// See https://abseil.io/docs/cpp/guides/flags
template <typename T>
class Flag {
public:
// 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
constexpr Flag(const char* name, const char* filename,
const flags_internal::HelpGenFunc help_gen,
const flags_internal::FlagDfltGenFunc default_value_gen)
: name_(name),
filename_(filename),
help_gen_(help_gen),
default_value_gen_(default_value_gen),
inited_(false),
impl_(nullptr) {}
#endif
flags_internal::Flag<T>& GetImpl() const {
if (!inited_.load(std::memory_order_acquire)) {
absl::MutexLock l(flags_internal::GetGlobalConstructionGuard());
if (inited_.load(std::memory_order_acquire)) {
return *impl_;
}
impl_ = new flags_internal::Flag<T>(
name_, filename_,
{flags_internal::FlagHelpMsg(help_gen_),
flags_internal::FlagHelpKind::kGenFunc},
{flags_internal::FlagDefaultSrc(default_value_gen_),
flags_internal::FlagDefaultKind::kGenFunc});
inited_.store(true, std::memory_order_release);
}
return *impl_;
}
// Public methods of `absl::Flag<T>` are NOT part of the Abseil Flags API.
// See https://abseil.io/docs/cpp/guides/flags
bool IsRetired() const { return GetImpl().IsRetired(); }
absl::string_view Name() const { return GetImpl().Name(); }
std::string Help() const { return GetImpl().Help(); }
bool IsModified() const { return GetImpl().IsModified(); }
bool IsSpecifiedOnCommandLine() const {
return GetImpl().IsSpecifiedOnCommandLine();
}
std::string Filename() const { return GetImpl().Filename(); }
std::string DefaultValue() const { return GetImpl().DefaultValue(); }
std::string CurrentValue() const { return GetImpl().CurrentValue(); }
template <typename U>
inline bool IsOfType() const {
return GetImpl().template IsOfType<U>();
}
T Get() const {
return flags_internal::FlagImplPeer::InvokeGet<T>(GetImpl());
}
void Set(const T& v) {
flags_internal::FlagImplPeer::InvokeSet(GetImpl(), v);
}
void InvokeCallback() { GetImpl().InvokeCallback(); }
const CommandLineFlag& Reflect() const {
return flags_internal::FlagImplPeer::InvokeReflect(GetImpl());
}
// The data members are logically private, but they need to be public for
// this to be an aggregate type.
const char* name_;
const char* filename_;
const flags_internal::HelpGenFunc help_gen_;
const flags_internal::FlagDfltGenFunc default_value_gen_;
mutable std::atomic<bool> inited_;
mutable flags_internal::Flag<T>* impl_;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment