1 #ifndef MARISA_SCOPED_ARRAY_H_ 2 #define MARISA_SCOPED_ARRAY_H_ 3 4 #include "marisa/base.h" 5 6 namespace marisa { 7 8 template <typename T> 9 class scoped_array { 10 public: scoped_array()11 scoped_array() : array_(NULL) {} scoped_array(T * array)12 explicit scoped_array(T *array) : array_(array) {} 13 ~scoped_array()14 ~scoped_array() { 15 delete [] array_; 16 } 17 18 void reset(T *array = NULL) { 19 MARISA_DEBUG_IF((array != NULL) && (array == array_), MARISA_RESET_ERROR); 20 scoped_array(array).swap(*this); 21 } 22 23 T &operator[](std::size_t i) const { 24 MARISA_DEBUG_IF(array_ == NULL, MARISA_STATE_ERROR); 25 return array_[i]; 26 } get()27 T *get() const { 28 return array_; 29 } 30 clear()31 void clear() { 32 scoped_array().swap(*this); 33 } swap(scoped_array & rhs)34 void swap(scoped_array &rhs) { 35 marisa::swap(array_, rhs.array_); 36 } 37 38 private: 39 T *array_; 40 41 // Disallows copy and assignment. 42 scoped_array(const scoped_array &); 43 scoped_array &operator=(const scoped_array &); 44 }; 45 46 } // namespace marisa 47 48 #endif // MARISA_SCOPED_ARRAY_H_ 49