scopedarray.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/scopedarray.h
  3. // Purpose: interface of wxScopedArray
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxScopedArray
  9. This is a simple scoped smart pointer array implementation that is similar to
  10. the Boost smart pointers (see http://www.boost.org/) but rewritten to
  11. use macros instead.
  12. @b Example:
  13. Below is an example of using a wxWidgets scoped smart pointer and pointer array.
  14. @code
  15. class MyClass { ... };
  16. // declare a smart pointer to a MyClass called wxMyClassPtr
  17. wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
  18. // declare a smart pointer to an array of chars
  19. wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
  20. ...
  21. // define the first pointer class, must be complete
  22. wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
  23. // define the second pointer class
  24. wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
  25. // create an object with a new pointer to MyClass
  26. wxMyClassPtr theObj(new MyClass());
  27. // reset the pointer (deletes the previous one)
  28. theObj.reset(new MyClass());
  29. // access the pointer
  30. theObj->MyFunc();
  31. // create an object with a new array of chars
  32. wxCharArray theCharObj(new char[100]);
  33. // access the array
  34. theCharObj[0] = "!";
  35. @endcode
  36. <b>Declaring new smart pointer types:</b>
  37. @code
  38. wxDECLAR_SCOPED_ARRAY( TYPE, // type of the values
  39. CLASSNAME ); // name of the class
  40. @endcode
  41. A smart pointer holds a pointer to an object (which must be complete when
  42. wxDEFINE_SCOPED_ARRAY() is called).
  43. The memory used by the object is deleted when the smart pointer goes out of
  44. scope. The first argument of the macro is the pointer type, the second is the
  45. name of the new smart pointer class being created. Below we will use wxScopedArray
  46. to represent the scoped pointer array class, but the user may create the class with
  47. any legal name.
  48. @library{wxbase}
  49. @category{smartpointers}
  50. @see wxScopedPtr
  51. */
  52. class wxScopedArray
  53. {
  54. public:
  55. /**
  56. Creates the smart pointer with the given pointer or none if @NULL. On
  57. compilers that support it, this uses the explicit keyword.
  58. */
  59. wxScopedArray(type* T = NULL);
  60. /**
  61. This operator gets the pointer stored in the smart pointer or returns @NULL if
  62. there is none.
  63. */
  64. const T* get();
  65. /**
  66. This operator acts like the standard [] indexing operator for C++ arrays. The
  67. function does not do bounds checking.
  68. */
  69. const T& operator [](long int i);
  70. /**
  71. Deletes the currently held pointer and sets it to 'p' or to @NULL if no
  72. arguments are specified. This function does check to make sure that the
  73. pointer you are assigning is not the same pointer that is already stored.
  74. */
  75. reset(T* p = NULL);
  76. /**
  77. Swap the pointer inside the smart pointer with @a ot. The pointer being swapped
  78. must be of the same type (hence the same class name).
  79. */
  80. swap(wxScopedArray& ot);
  81. };
  82. /**
  83. A scoped array template class.
  84. This class is similar to boost scoped_array class:
  85. http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/scoped_array.htm
  86. Notice that objects of this class intentionally cannot be copied.
  87. @library{wxbase}
  88. @category{smartpointers}
  89. */
  90. template <class T>
  91. class wxScopedArray
  92. {
  93. public:
  94. /// The type of the array elements.
  95. typedef T element_type;
  96. /**
  97. Constructor takes ownership of the given array.
  98. If @a array is @NULL, reset() must presumably be called later.
  99. @param array
  100. An array allocated using @c new[] or @NULL.
  101. */
  102. explicit wxScopedArray(T * array = NULL);
  103. /// Destructor destroy the array.
  104. ~wxScopedArray();
  105. /**
  106. Conversion to a boolean expression (in a variant which is not
  107. convertible to anything but a boolean expression).
  108. If this class contains a valid array it will return @true, if it contains
  109. a @NULL pointer it will return @false.
  110. */
  111. operator unspecified_bool_type() const;
  112. /**
  113. Change the array pointer stored.
  114. The previously stored array is deleted.
  115. @param array
  116. An array allocated using @c new[] or @NULL.
  117. */
  118. void reset(T *array = NULL);
  119. /**
  120. Return the n-th element of the array.
  121. Must not be called if the array has no valid pointer.
  122. */
  123. T& operator[](size_t n) const;
  124. /**
  125. Return the array pointer.
  126. The returned pointer may be @NULL. It must not be deleted by the
  127. caller, call @c reset(NULL) instead.
  128. */
  129. T *get() const;
  130. /// Swaps the contents of this array with another one.
  131. void swap(wxScopedArray &other);
  132. };