|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 | + * http://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 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <type_traits> |
| 21 | +#include <typeinfo> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include <folly/Traits.h> |
| 25 | +#include <folly/lang/SafeAssert.h> |
| 26 | + |
| 27 | +namespace apache::thrift::util { |
| 28 | + |
| 29 | +/** |
| 30 | + * A type-safe type-erased const reference to an object. |
| 31 | + * |
| 32 | + * Unlike TypeErasedValue, the referred-to object is not owned by this class. |
| 33 | + * The user must ensure that the aforementioned object outlives any access |
| 34 | + * through an object of this class. |
| 35 | + * |
| 36 | + * Access is provided through the templated value<T>() method which checks that |
| 37 | + * the referred-to object is indeed of type T. This is the main difference |
| 38 | + * between TypeErasedRef and const void* — the latter has no safety checks. |
| 39 | + * |
| 40 | + * TypeErasedRef is copyable. The copy points to the same object as the |
| 41 | + * original. |
| 42 | + */ |
| 43 | +class TypeErasedRef { |
| 44 | + public: |
| 45 | + const std::type_info& type() const noexcept { return *typeInfo_; } |
| 46 | + const void* ptr() const noexcept { return ptr_; } |
| 47 | + |
| 48 | + template <class T> |
| 49 | + bool holds_alternative() const noexcept { |
| 50 | + return type() == typeid(T); |
| 51 | + } |
| 52 | + |
| 53 | + template <class T> |
| 54 | + const folly::remove_cvref_t<T>& value() const { |
| 55 | + if (!holds_alternative<T>()) { |
| 56 | + throw std::bad_cast(); |
| 57 | + } |
| 58 | + return value_unchecked<T>(); |
| 59 | + } |
| 60 | + |
| 61 | + template <class T> |
| 62 | + const folly::remove_cvref_t<T>& value_unchecked() const noexcept { |
| 63 | + FOLLY_SAFE_DCHECK( |
| 64 | + holds_alternative<T>(), |
| 65 | + "Tried to call value_unchecked() on TypeErasedRef with incompatible type"); |
| 66 | + return *reinterpret_cast<const folly::remove_cvref_t<T>*>(ptr_); |
| 67 | + } |
| 68 | + |
| 69 | + TypeErasedRef(const TypeErasedRef& other) noexcept = default; |
| 70 | + TypeErasedRef& operator=(const TypeErasedRef& other) noexcept = default; |
| 71 | + |
| 72 | + template <class T> |
| 73 | + static TypeErasedRef of(folly::remove_cvref_t<T>&&) = delete; |
| 74 | + |
| 75 | + template <class T> |
| 76 | + static TypeErasedRef of(const folly::remove_cvref_t<T>& object) noexcept { |
| 77 | + return fromTypeInfoUnchecked( |
| 78 | + static_cast<const void*>(std::addressof(object)), typeid(T)); |
| 79 | + } |
| 80 | + |
| 81 | + static TypeErasedRef fromTypeInfoUnchecked( |
| 82 | + const void* ptr, const std::type_info& typeInfo) noexcept { |
| 83 | + return TypeErasedRef(ptr, typeInfo); |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + TypeErasedRef(const void* ptr, const std::type_info& typeInfo) noexcept |
| 88 | + : ptr_(ptr), typeInfo_(&typeInfo) {} |
| 89 | + |
| 90 | + const void* ptr_; |
| 91 | + const std::type_info* typeInfo_; |
| 92 | +}; |
| 93 | + |
| 94 | +} // namespace apache::thrift::util |
0 commit comments