16 #ifndef TENSORFLOW_SERVING_UTIL_ANY_PTR_H_
17 #define TENSORFLOW_SERVING_UTIL_ANY_PTR_H_
22 namespace tensorflow {
68 AnyPtr() : type_id_(FastTypeId<void>()), ptr_(nullptr) {}
76 : type_id_(FastTypeId<T>()),
81 ptr_(const_cast<void*>(reinterpret_cast<const void*>(ptr))) {}
86 if (type_id_ != FastTypeId<T>()) {
89 return reinterpret_cast<T*
>(ptr_);
93 template <
typename Type>
94 static size_t FastTypeId() {
97 return reinterpret_cast<std::size_t
>(&dummy);
101 std::size_t type_id_;
116 template <
typename T>
118 : ptr_(ptr.release()), deleter_(DeleterForType<T>()) {}
135 template <
typename T>
137 return ptr_.
get<T>();
145 swap(ptr_, other.ptr_);
146 swap(deleter_, other.deleter_);
152 using Deleter = void (*)(AnyPtr ptr);
156 template <
typename T>
157 static Deleter DeleterForType() {
158 return [](AnyPtr ptr) {
delete ptr.get<T>(); };
161 static Deleter NoOpDeleter() {
162 return [](AnyPtr ptr) {};
165 AnyPtr ptr_ =
nullptr;
166 Deleter deleter_ = NoOpDeleter();
AnyPtr()
AnyPtr is void and null by default.
T * get() const
Accessor for the underlying pointer if it is of type T, otherwise null.
AnyPtr(T *ptr)
Construct from a pointer to any type.
AnyPtr(std::nullptr_t)
Implicit construction from nullptr.
UniqueAnyPtr()=default
UniqueAnyPtr is void and null by default.
const AnyPtr & as_any_ptr() const
Accessor for the underlying pointer as an AnyPtr.
T * get() const
Accessor for the underlying pointer if it is of type T, otherwise null.
UniqueAnyPtr(std::unique_ptr< T > ptr)
Construct from a unique pointer to any type.