📄 scoped_ptr.h
字号:
/* CRF++ -- Yet Another CRF toolkit $Id: scoped_ptr.h 1528 2006-08-07 02:39:50Z taku $; Copyright(C) 2005 Taku Kudo <taku@chasen.org> This is free software with ABSOLUTELY NO WARRANTY. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or(at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#ifndef _CRFPP_SCOPED_PTR_H#define _CRFPP_SCOPED_PTR_H#include <cstring>#include <string>namespace CRFPP { template<class T> class scoped_ptr { private: T * ptr_; scoped_ptr(scoped_ptr const &); scoped_ptr & operator=(scoped_ptr const &); typedef scoped_ptr<T> this_type; public: typedef T element_type; explicit scoped_ptr(T * p = 0): ptr_(p) {} virtual ~scoped_ptr() { delete ptr_; } void reset(T * p = 0) { delete ptr_; ptr_ = p; } T & operator*() const { return *ptr_; } T * operator->() const { return ptr_; } T * get() const { return ptr_; } }; template<class T> class scoped_array { private: T * ptr_; scoped_array(scoped_array const &); scoped_array & operator=(scoped_array const &); typedef scoped_array<T> this_type; public: typedef T element_type; explicit scoped_array(T * p = 0): ptr_(p) {} virtual ~scoped_array() { delete [] ptr_; } void reset(T * p = 0) { delete [] ptr_; ptr_ = p; } T & operator*() const { return *ptr_; } T * operator->() const { return ptr_; } T * get() const { return ptr_; } T & operator[](size_t i) const { return ptr_[i]; } }; class scoped_string: public scoped_array<char> { public: explicit scoped_string() { reset_string(""); } explicit scoped_string(const std::string &str) { reset_string(str); } void reset_string(const std::string &str) { char *p = new char[str.size() + 1]; strcpy(p, str.c_str()); reset(p); } void reset_string(const char *str) { char *p = new char[strlen(str) + 1]; strcpy(p, str); reset(p); } };}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -