1 // MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
4 // Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
5 // Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
6 #ifndef MECAB_SCOPED_PTR_H
7 #define MECAB_SCOPED_PTR_H
14 template<class T> class scoped_ptr {
17 scoped_ptr(scoped_ptr const &);
18 scoped_ptr & operator= (scoped_ptr const &);
19 typedef scoped_ptr<T> this_type;
22 typedef T element_type;
23 explicit scoped_ptr(T * p = 0): ptr_(p) {}
24 virtual ~scoped_ptr() { delete ptr_; }
25 void reset(T * p = 0) {
29 T & operator*() const { return *ptr_; }
30 T * operator->() const { return ptr_; }
31 T * get() const { return ptr_; }
34 template<class T> class scoped_array {
37 scoped_array(scoped_array const &);
38 scoped_array & operator= (scoped_array const &);
39 typedef scoped_array<T> this_type;
42 typedef T element_type;
43 explicit scoped_array(T * p = 0): ptr_(p) {}
44 virtual ~scoped_array() { delete [] ptr_; }
45 void reset(T * p = 0) {
49 T & operator*() const { return *ptr_; }
50 T * operator->() const { return ptr_; }
51 T * get() const { return ptr_; }
52 T & operator[](size_t i) const { return ptr_[i]; }
55 template<class T, int N> class scoped_fixed_array {
59 scoped_fixed_array(scoped_fixed_array const &);
60 scoped_fixed_array & operator= (scoped_fixed_array const &);
61 typedef scoped_fixed_array<T, N> this_type;
64 typedef T element_type;
65 explicit scoped_fixed_array()
66 : ptr_(new T[N]), size_(N) {}
67 virtual ~scoped_fixed_array() { delete [] ptr_; }
68 size_t size() const { return size_; }
69 T & operator*() const { return *ptr_; }
70 T * operator->() const { return ptr_; }
71 T * get() const { return ptr_; }
72 T & operator[](size_t i) const { return ptr_[i]; }
75 class scoped_string: public scoped_array<char> {
77 explicit scoped_string() { reset_string(""); }
78 explicit scoped_string(const std::string &str) {
82 void reset_string(const std::string &str) {
83 char *p = new char[str.size() + 1];
84 std::strcpy(p, str.c_str());
88 void reset_string(const char *str) {
89 char *p = new char[std::strlen(str) + 1];