-
Notifications
You must be signed in to change notification settings - Fork 101
/
support-common.h
94 lines (77 loc) · 1.33 KB
/
support-common.h
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
#pragma once
#ifdef __cplusplus
#define DllImport __declspec(dllimport)
#define DllExport __declspec(dllexport)
#else
#define DllImport
#define DllExport
#endif
#ifdef BuildDLL
#define Caffe_API DllExport
#else
#ifdef ImportDLL
#define Caffe_API DllImport
#else
#define Caffe_API
#endif
#endif
#ifdef __cplusplus
template<typename Dtype>
class WPtr{
typedef Dtype* DtypePtr;
template<typename T>
struct ptrInfo{
T ptr;
int refCount;
ptrInfo(T p) :ptr(p), refCount(1){}
void addRef(){ refCount++; }
bool releaseRef(){ return --refCount <= 0; }
};
public:
WPtr() :ptr(0){};
WPtr(DtypePtr p){
ptr = new ptrInfo<DtypePtr>(p);
}
WPtr(const WPtr& other) :ptr(0){
operator=(other);
}
~WPtr(){
releaseRef();
}
void release(DtypePtr ptr);
DtypePtr operator->(){
return get();
}
operator DtypePtr(){
return get();
}
WPtr& operator=(const WPtr& other){
releaseRef();
this->ptr = other.ptr;
addRef();
return *this;
}
DtypePtr get(){
if (this->ptr)
return ptr->ptr;
return 0;
}
void addRef(){
if (this->ptr)
this->ptr->addRef();
}
void releaseRef(){
if (this->ptr && this->ptr->releaseRef()){
release(this->ptr->ptr);
delete ptr;
ptr = 0;
}
}
private:
ptrInfo<DtypePtr>* ptr;
};
template<typename DtypePtr>
inline void WPtr<DtypePtr>::release(DtypePtr p){
if (p) delete p;
}
#endif