retdec
smart_ptr.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_SUPPORT_SMART_PTR_H
8 #define RETDEC_LLVMIR2HLL_SUPPORT_SMART_PTR_H
9 
10 #include <cstddef>
11 #include <memory>
12 
13 namespace retdec {
14 namespace llvmir2hll {
15 
17 template<typename T>
18 using ShPtr = std::shared_ptr<T>;
19 
21 template<typename T>
22 using WkPtr = std::weak_ptr<T>;
23 
25 template<typename T>
26 using UPtr = std::unique_ptr<T>;
27 
43 template<class ForClass>
44 class SharableFromThis: public std::enable_shared_from_this<ForClass> {
45 protected:
46  // Change the visibility of shared_from_this() from public to protected to
47  // prevent unintentional use of it outside of the class.
48  using std::enable_shared_from_this<ForClass>::shared_from_this;
49 };
50 
62 template<typename To, typename From>
63 ShPtr<To> cast(const ShPtr<From> &ptr) noexcept {
64  return std::dynamic_pointer_cast<To>(ptr);
65 }
66 
81 template<typename To, typename From>
82 ShPtr<To> ucast(const ShPtr<From> &ptr) noexcept {
83  return std::static_pointer_cast<To>(ptr);
84 }
85 
107 template<typename To, typename From>
108 bool isa(const ShPtr<From> &ptr) noexcept {
109  return cast<To>(ptr) != nullptr;
110 }
111 
115 template<typename T>
117 public:
119 
120  bool operator()(const WkPtr<T> &otherPtr) const {
121  return ptr.lock() == otherPtr.lock();
122  }
123 
124 private:
125  const WkPtr<T> &ptr;
126 };
127 
128 } // namespace llvmir2hll
129 } // namespace retdec
130 
131 #endif
Enables shared_from_this() in the inheriting class.
Definition: smart_ptr.h:44
A predicate for checking the equality of two weak pointers.
Definition: smart_ptr.h:116
WkPtrEqPredicate(const WkPtr< T > &ptr)
Definition: smart_ptr.h:118
bool operator()(const WkPtr< T > &otherPtr) const
Definition: smart_ptr.h:120
const WkPtr< T > & ptr
Definition: smart_ptr.h:125
A library providing API for working with back-end IR.
std::weak_ptr< T > WkPtr
An alias for a weak pointer.
Definition: smart_ptr.h:22
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
std::unique_ptr< T > UPtr
An alias for a unique pointer.
Definition: smart_ptr.h:26
ShPtr< To > cast(const ShPtr< From > &ptr) noexcept
Equivalent of dynamic_cast<> for shared pointers.
Definition: smart_ptr.h:63
ShPtr< To > ucast(const ShPtr< From > &ptr) noexcept
Equivalent of static_cast<> for shared pointers (unchecked cast).
Definition: smart_ptr.h:82
bool isa(const ShPtr< From > &ptr) noexcept
Returns true if ptr is of type To or can be casted from From to To, false otherwise.
Definition: smart_ptr.h:108
Definition: archive_wrapper.h:19