scoped_array.hpp 1.1 KB

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