aboutsummaryrefslogtreecommitdiffhomepage
path: root/absl/flags/internal/flag.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-02-10 10:18:03 -0800
committerGravatar Mark Barolak <mbar@google.com>2020-02-10 13:55:42 -0500
commitbf78e977309c4cb946914b456404141ddac1c302 (patch)
tree3d4c99e9bccb4c0cb19a5be2eaf65bb9c81f1c34 /absl/flags/internal/flag.cc
parentd95d1567165d449e4c213ea31a15cbb112a9865f (diff)
Export of internal Abseil changes
-- 803abc2dcad8b2354c988e9bf58dac4a17683832 by Gennadiy Rozental <rogeeff@google.com>: Avoid warning when RTTI is not enabled. PiperOrigin-RevId: 294247546 -- 5a7b0b4d07d1d6e56fbb0b0ffbf4f8fcab772dbf by Derek Mauro <dmauro@google.com>: Add a public Abseil FAQ PiperOrigin-RevId: 294226960 -- 6945c4a6df7d7679711fea31aacf4fba6ac7baa1 by Gennadiy Rozental <rogeeff@google.com>: Re-enable type mismatch check, which works in all the cases including shared libraries. We will use RTTI in case when our hand written approximation of it reports a type mismatch. This way we can ensure that if a flag is defined in one shared object and referenced in another we do not report spurious errors. PiperOrigin-RevId: 293905563 GitOrigin-RevId: 803abc2dcad8b2354c988e9bf58dac4a17683832 Change-Id: I1a23776d227ed2734c2e7183323786b7a95c3cc7
Diffstat (limited to 'absl/flags/internal/flag.cc')
-rw-r--r--absl/flags/internal/flag.cc45
1 files changed, 23 insertions, 22 deletions
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 6ce7def..cfc0cf4 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -56,6 +56,14 @@ bool ShouldValidateFlagValue(FlagOpFn flag_type_id) {
return true;
}
+#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
+bool MatchRuntimeTypeId(FlagOpFn lhs_type_id, FlagOpFn rhs_type_id) {
+ return RuntimeTypeId(lhs_type_id) == RuntimeTypeId(rhs_type_id);
+}
+#else
+bool MatchRuntimeTypeId(FlagOpFn, FlagOpFn) { return true; }
+#endif
+
// RAII helper used to temporarily unlock and relock `absl::Mutex`.
// This is used when we need to ensure that locks are released while
// invoking user supplied callbacks and then reacquired, since callbacks may
@@ -133,6 +141,18 @@ void FlagImpl::Destroy() {
is_data_guard_inited_ = false;
}
+void FlagImpl::AssertValidType(const flags_internal::FlagOpFn op) const {
+ // `op` is the unmarshaling operation corresponding to the declaration
+ // visibile at the call site. `op_` is the Flag's defined unmarshalling
+ // operation. They must match for this operation to be well-defined.
+ if (ABSL_PREDICT_FALSE(op != op_) && !MatchRuntimeTypeId(op, op_)) {
+ ABSL_INTERNAL_LOG(
+ FATAL,
+ absl::StrCat("Flag '", Name(),
+ "' is defined as one type and declared as another"));
+ }
+}
+
std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
void* res = nullptr;
if (DefaultKind() == FlagDefaultKind::kDynamicValue) {
@@ -219,7 +239,7 @@ bool FlagImpl::RestoreState(const void* value, bool modified,
if (counter_ == counter) return false;
}
- Write(value, op_);
+ Write(value);
{
absl::MutexLock l(DataGuard());
@@ -254,18 +274,9 @@ bool FlagImpl::TryParse(void** dst, absl::string_view value,
return true;
}
-void FlagImpl::Read(void* dst, const flags_internal::FlagOpFn dst_op) const {
+void FlagImpl::Read(void* dst) const {
absl::ReaderMutexLock l(DataGuard());
- // `dst_op` is the unmarshaling operation corresponding to the declaration
- // visibile at the call site. `op` is the Flag's defined unmarshalling
- // operation. They must match for this operation to be well-defined.
- if (ABSL_PREDICT_FALSE(dst_op != op_)) {
- ABSL_INTERNAL_LOG(
- ERROR,
- absl::StrCat("Flag '", Name(),
- "' is defined as one type and declared as another"));
- }
CopyConstruct(op_, value_.dynamic, dst);
}
@@ -286,19 +297,9 @@ void FlagImpl::StoreAtomic() {
#endif
}
-void FlagImpl::Write(const void* src, const flags_internal::FlagOpFn src_op) {
+void FlagImpl::Write(const void* src) {
absl::MutexLock l(DataGuard());
- // `src_op` is the marshalling operation corresponding to the declaration
- // visible at the call site. `op` is the Flag's defined marshalling operation.
- // They must match for this operation to be well-defined.
- if (ABSL_PREDICT_FALSE(src_op != op_)) {
- ABSL_INTERNAL_LOG(
- ERROR,
- absl::StrCat("Flag '", Name(),
- "' is defined as one type and declared as another"));
- }
-
if (ShouldValidateFlagValue(op_)) {
void* obj = Clone(op_, src);
std::string ignored_error;