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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file ScopedPtr.hpp
* Defines class ScopedPtr.
*/
#ifndef __ScopedPtr__
#define __ScopedPtr__
#include "Forwards.hpp"
#include "Debug/Assertion.hpp"
#include "Lib/Allocator.hpp"
namespace Lib
{
/**
* Wrapper containing a pointer to an object which is deleted
* when the wrapper is destroyed
*/
template<typename T>
class ScopedPtr {
private:
ScopedPtr(const ScopedPtr& ptr);
ScopedPtr& operator=(const ScopedPtr& ptr);
public:
inline
ScopedPtr() : _obj(0) {}
/**
* Create a scoped pointer containing pointer @b obj
*/
inline
explicit ScopedPtr(T* obj)
: _obj(obj) {ASS(obj);}
inline
~ScopedPtr()
{
if(_obj) {
checked_delete(_obj);
}
}
void operator=(T* obj)
{
if(_obj) {
checked_delete(_obj);
}
_obj = obj;
}
inline
operator bool() const { return _obj; }
inline
T* operator->() const
{
ASS(_obj);
return _obj;
}
inline
T& operator*() const
{
ASS(_obj);
return *_obj;
}
inline
T* ptr() const { return _obj; }
inline
bool isEmpty() const { return !_obj; }
template<class Target>
inline
Target* pcast() const { return static_cast<Target*>(_obj); }
/** Remove object from the scoped pointer without deleting it */
T* release() {
T* res = _obj;
_obj = 0;
return res;
}
private:
T* _obj;
};
}
#endif // __ScopedPtr__