annotate clang/test/Analysis/os_smart_ptr.h @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900 (2021-07-18)
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 #ifndef _OS_SMART_POINTER_H
anatofuz
parents:
diff changeset
2 #define _OS_SMART_POINTER_H
anatofuz
parents:
diff changeset
3
anatofuz
parents:
diff changeset
4 #include "os_object_base.h"
anatofuz
parents:
diff changeset
5
anatofuz
parents:
diff changeset
6 namespace os {
anatofuz
parents:
diff changeset
7
anatofuz
parents:
diff changeset
8 template<class T>
anatofuz
parents:
diff changeset
9 struct smart_ptr {
anatofuz
parents:
diff changeset
10 smart_ptr() : pointer(nullptr) {}
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 explicit smart_ptr(T *&p) : pointer(p) {
anatofuz
parents:
diff changeset
13 if (pointer) {
anatofuz
parents:
diff changeset
14 _retain(pointer);
anatofuz
parents:
diff changeset
15 }
anatofuz
parents:
diff changeset
16 }
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 smart_ptr(smart_ptr const &rhs) : pointer(rhs.pointer) {
anatofuz
parents:
diff changeset
19 if (pointer) {
anatofuz
parents:
diff changeset
20 _retain(pointer);
anatofuz
parents:
diff changeset
21 }
anatofuz
parents:
diff changeset
22 }
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 smart_ptr & operator=(T *&rhs) {
anatofuz
parents:
diff changeset
25 smart_ptr(rhs).swap(*this);
anatofuz
parents:
diff changeset
26 return *this;
anatofuz
parents:
diff changeset
27 }
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 smart_ptr & operator=(smart_ptr &rhs) {
anatofuz
parents:
diff changeset
30 smart_ptr(rhs).swap(*this);
anatofuz
parents:
diff changeset
31 return *this;
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 ~smart_ptr() {
anatofuz
parents:
diff changeset
35 if (pointer) {
anatofuz
parents:
diff changeset
36 _release(pointer);
anatofuz
parents:
diff changeset
37 }
anatofuz
parents:
diff changeset
38 }
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 void reset() {
anatofuz
parents:
diff changeset
41 smart_ptr().swap(*this);
anatofuz
parents:
diff changeset
42 }
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 T *get() const {
anatofuz
parents:
diff changeset
45 return pointer;
anatofuz
parents:
diff changeset
46 }
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 T ** get_for_out_param() {
anatofuz
parents:
diff changeset
49 reset();
anatofuz
parents:
diff changeset
50 return &pointer;
anatofuz
parents:
diff changeset
51 }
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 T * operator->() const {
anatofuz
parents:
diff changeset
54 return pointer;
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 explicit
anatofuz
parents:
diff changeset
58 operator bool() const {
anatofuz
parents:
diff changeset
59 return pointer != nullptr;
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 inline void
anatofuz
parents:
diff changeset
63 swap(smart_ptr &p) {
anatofuz
parents:
diff changeset
64 T *temp = pointer;
anatofuz
parents:
diff changeset
65 pointer = p.pointer;
anatofuz
parents:
diff changeset
66 p.pointer = temp;
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 static inline void
anatofuz
parents:
diff changeset
70 _retain(T *obj) {
anatofuz
parents:
diff changeset
71 obj->retain();
anatofuz
parents:
diff changeset
72 }
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 static inline void
anatofuz
parents:
diff changeset
75 _release(T *obj) {
anatofuz
parents:
diff changeset
76 obj->release();
anatofuz
parents:
diff changeset
77 }
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 static inline T *
anatofuz
parents:
diff changeset
80 _alloc() {
anatofuz
parents:
diff changeset
81 return new T;
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 T *pointer;
anatofuz
parents:
diff changeset
85 };
anatofuz
parents:
diff changeset
86 } // namespace os
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 #endif /* _OS_SMART_POINTER_H */