You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
976 B
53 lines
976 B
#ifndef MARISA_SCOPED_PTR_H_
|
|
#define MARISA_SCOPED_PTR_H_
|
|
|
|
#include "marisa/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_DEBUG_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_
|