scoped_array.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 T* operator-> ()
  32. {
  33. return ptr;
  34. }
  35. inline void reset (T* t = 0)
  36. {
  37. delete[] ptr;
  38. ptr = t;
  39. }
  40. T & operator[](std::ptrdiff_t i) const
  41. {
  42. return ptr[i];
  43. }
  44. void swap(scoped_array & b)
  45. {
  46. std::swap(ptr, b.ptr);
  47. }
  48. private:
  49. // encapsulated object pointer
  50. T* ptr;
  51. };
  52. template<class T>
  53. inline void swap(scoped_array<T> & a, scoped_array<T> & b)
  54. {
  55. a.swap(b);
  56. }
  57. } // end of namespace boost
  58. #else
  59. # error "scoped_array.h was already included"
  60. #endif
  61. #endif // __AI_BOOST_SCOPED_ARRAY_INCLUDED