00001 00012 #ifndef UTILITY_H 00013 #define UTILITY_H 00014 00015 #include <cstddef> 00016 00019 class noncopyable { 00020 protected: 00021 noncopyable() {} 00022 ~noncopyable() {} 00023 private: 00024 noncopyable(noncopyable const &); 00025 noncopyable const & operator=(noncopyable const &); 00026 }; 00027 00028 00029 template<typename T> class scoped_ptr { 00030 public: 00031 explicit scoped_ptr(T * p = 0) : p_(p) {} 00032 ~scoped_ptr() { delete p_; } 00033 00034 void reset(T * p = 0) { 00035 if (p == p_) 00036 return; 00037 delete p_; 00038 p_ = p; 00039 } 00040 00041 T & operator*() const { return *p_; } 00042 T * operator->() const { return p_; } 00043 T * get() const { return p_; } 00044 00045 void swap(scoped_ptr & sp) { 00046 T * tmp = sp.p_; 00047 sp.p_ = p_; 00048 p_ = tmp; 00049 } 00050 00051 private: 00052 scoped_ptr & operator=(scoped_ptr const &); 00053 scoped_ptr(scoped_ptr const &); 00054 T * p_; 00055 }; 00056 00057 template<typename T> class scoped_array { 00058 public: 00059 explicit scoped_array(T * p = 0) : p_(p) {} 00060 ~scoped_array() { delete [] p_; } 00061 00062 void reset(T * p = 0) { 00063 if (p == p_) 00064 return; 00065 delete [] p_; 00066 p_ = p; 00067 } 00068 00069 T & operator[](std::ptrdiff_t i) const { return p_[i]; } 00070 T * get() const { return p_; } 00071 00072 void swap(scoped_array & sp) { 00073 T * tmp = sp.p_; 00074 sp.p_ = p_; 00075 p_ = tmp; 00076 } 00077 00078 private: 00079 scoped_array & operator=(scoped_array const &); 00080 scoped_array(scoped_array const &); 00081 T * p_; 00082 }; 00083 00090 inline double op_ratio(double count, double total) 00091 { 00092 return total == 0 ? 0.0 : (count / total); 00093 } 00094 00095 // Part copyright: 00096 // (C) Copyright boost.org 1999. Permission to copy, use, modify, sell 00097 // and distribute this software is granted provided this copyright 00098 // notice appears in all copies. This software is provided "as is" without 00099 // express or implied warranty, and with no claim as to its suitability for 00100 // any purpose. 00101 00102 #endif /* !UTILITY_H */