-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_entity_ptr.h
More file actions
84 lines (68 loc) · 1.31 KB
/
Copy pathcpp_entity_ptr.h
File metadata and controls
84 lines (68 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef B3AB0DD0_7FBB_4455_8B74_3DB850581596
#define B3AB0DD0_7FBB_4455_8B74_3DB850581596
#include <type_traits>
namespace cppast::helper {
/**
* @brief A convinient class to work with CppEntity derived classes.
*
* @note Its not an RAII class and takes no responsibility of life cycle management.
*
* @tparam T CppEntity derived class.
*/
template <typename T>
class CppEntityPtr
{
public:
CppEntityPtr(T* ptr)
: ptr_(ptr)
{
}
CppEntityPtr(CppEntity* entityPtr)
: ptr_(dynamic_cast<T*>(entityPtr))
{
}
CppEntityPtr(const CppEntity* entityPtr)
: ptr_(std::is_const_v<T> ? dynamic_cast<T*>(entityPtr) : nullptr)
{
}
template <typename U>
CppEntityPtr(const CppEntityPtr<U>& other)
: CppEntityPtr(other.get())
{
}
//*/
T* operator->() const
{
return ptr_;
}
operator bool() const
{
return ptr_ != nullptr;
}
operator T*() const
{
return ptr_;
}
operator CppEntity*() const
{
return ptr_;
}
operator const CppEntity*() const
{
return ptr_;
}
T& operator*() const
{
return *ptr_;
}
T* get() const
{
return ptr_;
}
private:
T* ptr_;
};
} // namespace cppast::helper
#endif /* B3AB0DD0_7FBB_4455_8B74_3DB850581596 */