scoped_ptr.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef __AI_BOOST_SCOPED_PTR_INCLUDED
  2. #define __AI_BOOST_SCOPED_PTR_INCLUDED
  3. #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
  4. namespace boost {
  5. // small replacement for boost::scoped_ptr
  6. template <class T>
  7. class scoped_ptr
  8. {
  9. public:
  10. // provide a default construtctor
  11. scoped_ptr()
  12. : ptr(0)
  13. {
  14. }
  15. // construction from an existing heap object of type T
  16. scoped_ptr(T* _ptr)
  17. : ptr(_ptr)
  18. {
  19. }
  20. // automatic destruction of the wrapped object at the
  21. // end of our lifetime
  22. ~scoped_ptr()
  23. {
  24. delete ptr;
  25. }
  26. inline T* get()
  27. {
  28. return ptr;
  29. }
  30. inline operator T*()
  31. {
  32. return ptr;
  33. }
  34. inline T* operator-> ()
  35. {
  36. return ptr;
  37. }
  38. inline void reset (T* t = 0)
  39. {
  40. delete ptr;
  41. ptr = t;
  42. }
  43. void swap(scoped_ptr & b)
  44. {
  45. std::swap(ptr, b.ptr);
  46. }
  47. private:
  48. // encapsulated object pointer
  49. T* ptr;
  50. };
  51. template<class T>
  52. inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b)
  53. {
  54. a.swap(b);
  55. }
  56. } // end of namespace boost
  57. #else
  58. # error "scoped_ptr.h was already included"
  59. #endif
  60. #endif // __AI_BOOST_SCOPED_PTR_INCLUDED