Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / os_smart_ptr.h
blob48a5ef3df042f49cb84b0c49b4db32b704d6f782
1 #ifndef _OS_SMART_POINTER_H
2 #define _OS_SMART_POINTER_H
4 #include "os_object_base.h"
6 namespace os {
8 template<class T>
9 struct smart_ptr {
10 smart_ptr() : pointer(nullptr) {}
12 explicit smart_ptr(T *&p) : pointer(p) {
13 if (pointer) {
14 _retain(pointer);
18 smart_ptr(smart_ptr const &rhs) : pointer(rhs.pointer) {
19 if (pointer) {
20 _retain(pointer);
24 smart_ptr & operator=(T *&rhs) {
25 smart_ptr(rhs).swap(*this);
26 return *this;
29 smart_ptr & operator=(smart_ptr &rhs) {
30 smart_ptr(rhs).swap(*this);
31 return *this;
34 ~smart_ptr() {
35 if (pointer) {
36 _release(pointer);
40 void reset() {
41 smart_ptr().swap(*this);
44 T *get() const {
45 return pointer;
48 T ** get_for_out_param() {
49 reset();
50 return &pointer;
53 T * operator->() const {
54 return pointer;
57 explicit
58 operator bool() const {
59 return pointer != nullptr;
62 inline void
63 swap(smart_ptr &p) {
64 T *temp = pointer;
65 pointer = p.pointer;
66 p.pointer = temp;
69 static inline void
70 _retain(T *obj) {
71 obj->retain();
74 static inline void
75 _release(T *obj) {
76 obj->release();
79 static inline T *
80 _alloc() {
81 return new T;
84 T *pointer;
86 } // namespace os
88 #endif /* _OS_SMART_POINTER_H */