| 1 |
#ifndef mock_types_1103988513531 |
| 2 |
#define mock_types_1103988513531 |
| 3 |
|
| 4 |
template<typename T> |
| 5 |
struct RawPtrTraits { |
| 6 |
using StorageType = T*; |
| 7 |
|
| 8 |
template<typename U> |
| 9 |
static T* exchange(StorageType& ptr, U&& newValue) |
| 10 |
{ |
| 11 |
StorageType oldValue = static_cast<StorageType&&>(ptr); |
| 12 |
ptr = static_cast<U&&>(newValue); |
| 13 |
return oldValue; |
| 14 |
} |
| 15 |
|
| 16 |
static void swap(StorageType& a, StorageType& b) |
| 17 |
{ |
| 18 |
StorageType temp = static_cast<StorageType&&>(a); |
| 19 |
a = static_cast<StorageType&&>(b); |
| 20 |
b = static_cast<StorageType&&>(temp); |
| 21 |
} |
| 22 |
static T* unwrap(const StorageType& ptr) { return ptr; } |
| 23 |
}; |
| 24 |
|
| 25 |
template<typename T> struct DefaultRefDerefTraits { |
| 26 |
static T* refIfNotNull(T* ptr) |
| 27 |
{ |
| 28 |
if (ptr) |
| 29 |
ptr->ref(); |
| 30 |
return ptr; |
| 31 |
} |
| 32 |
|
| 33 |
static T& ref(T& ref) |
| 34 |
{ |
| 35 |
ref.ref(); |
| 36 |
return ref; |
| 37 |
} |
| 38 |
|
| 39 |
static void derefIfNotNull(T* ptr) |
| 40 |
{ |
| 41 |
if (ptr) |
| 42 |
ptr->deref(); |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
template <typename T, typename PtrTraits = RawPtrTraits<T>, typename RefDerefTraits = DefaultRefDerefTraits<T>> struct Ref { |
| 47 |
typename PtrTraits::StorageType t; |
| 48 |
|
| 49 |
Ref() : t{} {}; |
| 50 |
Ref(T &t) : t(&RefDerefTraits::ref(t)) { } |
| 51 |
Ref(const Ref& o) : t(RefDerefTraits::refIfNotNull(PtrTraits::unwrap(o.t))) { } |
| 52 |
~Ref() { RefDerefTraits::derefIfNotNull(PtrTraits::exchange(t, nullptr)); } |
| 53 |
T &get() { return *PtrTraits::unwrap(t); } |
| 54 |
T *ptr() { return PtrTraits::unwrap(t); } |
| 55 |
T *operator->() { return PtrTraits::unwrap(t); } |
| 56 |
operator const T &() const { return *PtrTraits::unwrap(t); } |
| 57 |
operator T &() { return *PtrTraits::unwrap(t); } |
| 58 |
T* leakRef() { return PtrTraits::exchange(t, nullptr); } |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename T> struct RefPtr { |
| 62 |
T *t; |
| 63 |
|
| 64 |
RefPtr() : t(new T) {} |
| 65 |
RefPtr(T *t) |
| 66 |
: t(t) { |
| 67 |
if (t) |
| 68 |
t->ref(); |
| 69 |
} |
| 70 |
RefPtr(Ref<T>&& o) |
| 71 |
: t(o.leakRef()) |
| 72 |
{ } |
| 73 |
~RefPtr() { |
| 74 |
if (t) |
| 75 |
t->deref(); |
| 76 |
} |
| 77 |
T *get() { return t; } |
| 78 |
T *operator->() { return t; } |
| 79 |
const T *operator->() const { return t; } |
| 80 |
T &operator*() { return *t; } |
| 81 |
RefPtr &operator=(T *) { return *this; } |
| 82 |
operator bool() const { return t; } |
| 83 |
}; |
| 84 |
|
| 85 |
template <typename T> bool operator==(const RefPtr<T> &, const RefPtr<T> &) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
template <typename T> bool operator==(const RefPtr<T> &, T *) { return false; } |
| 90 |
|
| 91 |
template <typename T> bool operator==(const RefPtr<T> &, T &) { return false; } |
| 92 |
|
| 93 |
template <typename T> bool operator!=(const RefPtr<T> &, const RefPtr<T> &) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
template <typename T> bool operator!=(const RefPtr<T> &, T *) { return false; } |
| 98 |
|
| 99 |
template <typename T> bool operator!=(const RefPtr<T> &, T &) { return false; } |
| 100 |
|
| 101 |
struct RefCountable { |
| 102 |
static Ref<RefCountable> create(); |
| 103 |
void ref() {} |
| 104 |
void deref() {} |
| 105 |
void method(); |
| 106 |
int trivial() { return 123; } |
| 107 |
}; |
| 108 |
|
| 109 |
template <typename T> T *downcast(T *t) { return t; } |
| 110 |
|
| 111 |
#endif |