CppParser
Loading...
Searching...
No Matches
cppeasyptr.h
Go to the documentation of this file.
1/*
2 The MIT License (MIT)
3
4 Copyright (c) 2018 Satya Das
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#pragma once
25
26#include "cppconst.h"
27
28#include <memory>
29
30struct CppObj;
31
32CppObjType objType(const CppObj* cppObj);
33
42template <typename CppObjT>
44{
45public:
46 CppEasyPtr(CppObjT* rawPtr)
47 : ptr_(rawPtr)
48 {
49 }
50 CppEasyPtr(const std::unique_ptr<CppObjT>& ptr)
51 : ptr_(ptr.get())
52 {
53 }
54
55 template <typename = std::enable_if<!std::is_const<CppObjT>::value>>
57 : ptr_(nullptr)
58 {
59 if ((objType(rawPtr) == CppObjT::kObjectType))
60 ptr_ = static_cast<CppObjT*>(rawPtr);
61 }
62
63 template <typename = std::enable_if<std::is_const<CppObjT>::value>>
64 CppEasyPtr(const CppObj* rawPtr)
65 : ptr_(nullptr)
66 {
67 if ((objType(rawPtr) == CppObjT::kObjectType))
68 ptr_ = static_cast<CppObjT*>(rawPtr);
69 }
70
71 template <typename _UCppObj>
72 CppEasyPtr(const std::unique_ptr<_UCppObj>& ptr)
73 : ptr_(nullptr)
74 {
75 if (ptr && (ptr->objType_ == CppObjT::kObjectType))
76 ptr_ = static_cast<CppObjT*>(ptr.get());
77 }
78
79 CppObjT* operator->() const
80 {
81 return ptr_;
82 }
83
84 operator bool() const
85 {
86 return ptr_ != nullptr;
87 }
88
89 operator CppObjT*() const
90 {
91 return ptr_;
92 }
93
94 operator CppObj*() const
95 {
96 return ptr_;
97 }
98
99 operator const CppObj*() const
100 {
101 return ptr_;
102 }
103
104 CppObjT& operator*() const
105 {
106 return *ptr_;
107 }
108
109 template <typename _UCppObj>
110 operator _UCppObj*() const
111 {
112 return _UCppObj::kObjectType == CppObjT::kObjectType ? static_cast<_UCppObj*>(ptr_) : nullptr;
113 }
114
115 CppObjT* get() const
116 {
117 return ptr_;
118 }
119
120private:
121 CppObjT* ptr_;
122};
Helps working with raw or unique_ptr of CppObj in a uniform way.
Definition: cppeasyptr.h:44
CppEasyPtr(const CppObj *rawPtr)
Definition: cppeasyptr.h:64
CppEasyPtr(CppObj *rawPtr)
Definition: cppeasyptr.h:56
CppObjT * operator->() const
Definition: cppeasyptr.h:79
CppObjT & operator*() const
Definition: cppeasyptr.h:104
CppEasyPtr(const std::unique_ptr< _UCppObj > &ptr)
Definition: cppeasyptr.h:72
CppEasyPtr(const std::unique_ptr< CppObjT > &ptr)
Definition: cppeasyptr.h:50
CppEasyPtr(CppObjT *rawPtr)
Definition: cppeasyptr.h:46
CppObjT * ptr_
Definition: cppeasyptr.h:121
CppObjT * get() const
Definition: cppeasyptr.h:115
CppObjType
Definition: cppconst.h:36
CppObjType objType(const CppObj *cppObj)
An abstract class that is used as base class of all other classes.
Definition: cppast.h:134