OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / third_party / mecab-0.996 / src / scoped_ptr.h
1 //  MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
2 //
3 //
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
8
9 #include <cstring>
10 #include <string>
11
12 namespace MeCab {
13
14 template<class T> class scoped_ptr {
15  private:
16   T * ptr_;
17   scoped_ptr(scoped_ptr const &);
18   scoped_ptr & operator= (scoped_ptr const &);
19   typedef scoped_ptr<T> this_type;
20
21  public:
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) {
26     delete ptr_;
27     ptr_ = p;
28   }
29   T & operator*() const   { return *ptr_; }
30   T * operator->() const  { return ptr_;  }
31   T * get() const         { return ptr_;  }
32 };
33
34 template<class T> class scoped_array {
35  private:
36   T * ptr_;
37   scoped_array(scoped_array const &);
38   scoped_array & operator= (scoped_array const &);
39   typedef scoped_array<T> this_type;
40
41  public:
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) {
46     delete [] ptr_;
47     ptr_ = p;
48   }
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]; }
53 };
54
55 template<class T, int N> class scoped_fixed_array {
56  private:
57   T * ptr_;
58   size_t size_;
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;
62
63  public:
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]; }
73 };
74
75 class scoped_string: public scoped_array<char> {
76  public:
77   explicit scoped_string() { reset_string(""); }
78   explicit scoped_string(const std::string &str) {
79     reset_string(str);
80   }
81
82   void reset_string(const std::string &str) {
83     char *p = new char[str.size() + 1];
84     std::strcpy(p, str.c_str());
85     reset(p);
86   }
87
88   void reset_string(const char *str) {
89     char *p = new char[std::strlen(str) + 1];
90     std::strcpy(p, str);
91     reset(p);
92   }
93 };
94 }
95 #endif