blob: 9a9c44735339d3e7995c5d0f24fd37d880751e22 (
plain) (
blame)
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
|
#pragma once
#ifndef MARISA_SCOPED_PTR_H_
#define MARISA_SCOPED_PTR_H_
#include "base.h"
namespace marisa {
template <typename T>
class scoped_ptr {
public:
scoped_ptr() : ptr_(NULL) {}
explicit scoped_ptr(T *ptr) : ptr_(ptr) {}
~scoped_ptr() {
delete ptr_;
}
void reset(T *ptr = NULL) {
MARISA_THROW_IF((ptr != NULL) && (ptr == ptr_), MARISA_RESET_ERROR);
scoped_ptr(ptr).swap(*this);
}
T &operator*() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return *ptr_;
}
T *operator->() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return ptr_;
}
T *get() const {
return ptr_;
}
void clear() {
scoped_ptr().swap(*this);
}
void swap(scoped_ptr &rhs) {
marisa::swap(ptr_, rhs.ptr_);
}
private:
T *ptr_;
// Disallows copy and assignment.
scoped_ptr(const scoped_ptr &);
scoped_ptr &operator=(const scoped_ptr &);
};
} // namespace marisa
#endif // MARISA_SCOPED_PTR_H_
|