scoped_array.hpp 1.2 KB

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