dynarray.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dynarray.h
  3. // Purpose: interface of wxArray<T>
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. This section describes the so called @e "dynamic arrays". This is a C
  9. array-like type safe data structure i.e. the member access time is constant
  10. (and not linear according to the number of container elements as for linked
  11. lists). However, these arrays are dynamic in the sense that they will
  12. automatically allocate more memory if there is not enough of it for adding
  13. a new element. They also perform range checking on the index values but in
  14. debug mode only, so please be sure to compile your application in debug
  15. mode to use it (see @ref overview_debugging for details). So, unlike the
  16. arrays in some other languages, attempt to access an element beyond the
  17. arrays bound doesn't automatically expand the array but provokes an
  18. assertion failure instead in debug build and does nothing (except possibly
  19. crashing your program) in the release build.
  20. The array classes were designed to be reasonably efficient, both in terms
  21. of run-time speed and memory consumption and the executable size. The speed
  22. of array item access is, of course, constant (independent of the number of
  23. elements) making them much more efficient than linked lists (wxList).
  24. Adding items to the arrays is also implemented in more or less constant
  25. time, but the price is preallocating the memory in advance. In the
  26. "memory management" function section, you may find some useful hints about
  27. optimizing wxArray memory usage. As for executable size, all wxArray
  28. functions are inline, so they do not take @e any space at all.
  29. wxWidgets has three different kinds of array. All of them derive from
  30. wxBaseArray class which works with untyped data and cannot be used
  31. directly. The standard macros WX_DEFINE_ARRAY(), WX_DEFINE_SORTED_ARRAY()
  32. and WX_DEFINE_OBJARRAY() are used to define a new class deriving from it.
  33. The classes declared will be called in this documentation wxArray,
  34. wxSortedArray and wxObjArray but you should keep in mind that no classes
  35. with such names actually exist, each time you use one of the
  36. WX_DEFINE_XXXARRAY() macros, you define a class with a new name. In fact,
  37. these names are "template" names and each usage of one of the macros
  38. mentioned above creates a template specialization for the given element
  39. type.
  40. wxArray is suitable for storing integer types and pointers which it does
  41. not treat as objects in any way, i.e. the element pointed to by the pointer
  42. is not deleted when the element is removed from the array. It should be
  43. noted that all of wxArray's functions are inline, so it costs strictly
  44. nothing to define as many array types as you want (either in terms of the
  45. executable size or the speed) as long as at least one of them is defined
  46. and this is always the case because wxArrays are used by wxWidgets
  47. internally. This class has one serious limitation: it can only be used for
  48. storing integral types (bool, char, short, int, long and their unsigned
  49. variants) or pointers (of any kind). An attempt to use with objects of
  50. @c sizeof() greater than @c sizeof(long) will provoke a runtime assertion
  51. failure, however declaring a wxArray of floats will not (on the machines
  52. where @c "sizeof(float) <= sizeof(long)"), yet it will @b not work, please
  53. use wxObjArray for storing floats and doubles.
  54. wxSortedArray is a wxArray variant which should be used when searching in
  55. the array is a frequently used operation. It requires you to define an
  56. additional function for comparing two elements of the array element type
  57. and always stores its items in the sorted order (according to this
  58. function). Thus, its Index() function execution time is @c "O(log(N))"
  59. instead of @c "O(N)" for the usual arrays but the Add() method is slower:
  60. it is @c "O(log(N))" instead of constant time (neglecting time spent in
  61. memory allocation routine). However, in a usual situation elements are
  62. added to an array much less often than searched inside it, so wxSortedArray
  63. may lead to huge performance improvements compared to wxArray. Finally, it
  64. should be noticed that, as wxArray, wxSortedArray can be only used for
  65. storing integral types or pointers.
  66. wxObjArray class treats its elements like "objects". It may delete them
  67. when they are removed from the array (invoking the correct destructor) and
  68. copies them using the objects copy constructor. In order to implement this
  69. behaviour the definition of the wxObjArray arrays is split in two parts:
  70. first, you should declare the new wxObjArray class using the
  71. WX_DECLARE_OBJARRAY() macro and then you must include the file defining the
  72. implementation of template type: @<wx/arrimpl.cpp@> and define the array
  73. class with the WX_DEFINE_OBJARRAY() macro from a point where the full (as
  74. opposed to 'forward') declaration of the array elements class is in scope.
  75. As it probably sounds very complicated here is an example:
  76. @code
  77. #include <wx/dynarray.h>
  78. // We must forward declare the array because it is used
  79. // inside the class declaration.
  80. class MyDirectory;
  81. class MyFile;
  82. // This defines two new types: ArrayOfDirectories and ArrayOfFiles which
  83. // can be now used as shown below.
  84. WX_DECLARE_OBJARRAY(MyDirectory, ArrayOfDirectories);
  85. WX_DECLARE_OBJARRAY(MyFile, ArrayOfFiles);
  86. class MyDirectory
  87. {
  88. // ...
  89. ArrayOfDirectories m_subdirectories; // All subdirectories
  90. ArrayOfFiles m_files; // All files in this directory
  91. };
  92. // ...
  93. // Now that we have MyDirectory declaration in scope we may finish the
  94. // definition of ArrayOfDirectories -- note that this expands into some C++
  95. // code and so should only be compiled once (i.e., don't put this in the
  96. // header, but into a source file or you will get linking errors)
  97. #include <wx/arrimpl.cpp> // This is a magic incantation which must be done!
  98. WX_DEFINE_OBJARRAY(ArrayOfDirectories);
  99. // that's all!
  100. @endcode
  101. It is not as elegant as writing this:
  102. @code
  103. typedef std::vector<MyDirectory> ArrayOfDirectories;
  104. @endcode
  105. But is not that complicated and allows the code to be compiled with any,
  106. however dumb, C++ compiler in the world.
  107. Remember to include @<wx/arrimpl.cpp@> just before each
  108. WX_DEFINE_OBJARRAY() occurrence in your code, even if you have several in
  109. the same file.
  110. Things are much simpler for wxArray and wxSortedArray however: it is enough
  111. just to write:
  112. @code
  113. WX_DEFINE_ARRAY_INT(int, ArrayOfInts);
  114. WX_DEFINE_SORTED_ARRAY_INT(int, ArrayOfSortedInts);
  115. @endcode
  116. There is only one @c DEFINE macro and no need for separate @c DECLARE one.
  117. For the arrays of the primitive types, the macros
  118. @c WX_DEFINE_ARRAY_CHAR/SHORT/INT/SIZE_T/LONG/DOUBLE should be used
  119. depending on the sizeof of the values (notice that storing values of
  120. smaller type, e.g. shorts, in an array of larger one, e.g. @c ARRAY_INT,
  121. does not work on all architectures!).
  122. @section array_macros Macros for Template Array Definition
  123. To use an array you must first define the array class. This is done with
  124. the help of the macros in this section. The class of array elements must be
  125. (at least) forward declared for WX_DEFINE_ARRAY(), WX_DEFINE_SORTED_ARRAY()
  126. and WX_DECLARE_OBJARRAY() macros and must be fully declared before you use
  127. WX_DEFINE_OBJARRAY() macro.
  128. - WX_DEFINE_ARRAY()
  129. - WX_DEFINE_EXPORTED_ARRAY()
  130. - WX_DEFINE_USER_EXPORTED_ARRAY()
  131. - WX_DEFINE_SORTED_ARRAY()
  132. - WX_DEFINE_SORTED_EXPORTED_ARRAY()
  133. - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY()
  134. - WX_DECLARE_EXPORTED_OBJARRAY()
  135. - WX_DECLARE_USER_EXPORTED_OBJARRAY()
  136. - WX_DEFINE_OBJARRAY()
  137. - WX_DEFINE_EXPORTED_OBJARRAY()
  138. - WX_DEFINE_USER_EXPORTED_OBJARRAY()
  139. To slightly complicate the matters even further, the operator "->" defined
  140. by default for the array iterators by these macros only makes sense if the
  141. array element type is not a pointer itself and, although it still works,
  142. this provokes warnings from some compilers and to avoid them you should use
  143. the @c _PTR versions of the macros above. For example, to define an array
  144. of pointers to @c double you should use:
  145. @code
  146. WX_DEFINE_ARRAY_PTR(double *, MyArrayOfDoublePointers);
  147. @endcode
  148. Note that the above macros are generally only useful for wxObject types.
  149. There are separate macros for declaring an array of a simple type, such as
  150. an int.
  151. The following simple types are supported:
  152. - @c int
  153. - @c long
  154. - @c size_t
  155. - @c double
  156. To create an array of a simple type, simply append the type you want in
  157. CAPS to the array definition.
  158. For example, you'd use one of the following variants for an integer array:
  159. - WX_DEFINE_ARRAY_INT()
  160. - WX_DEFINE_EXPORTED_ARRAY_INT()
  161. - WX_DEFINE_USER_EXPORTED_ARRAY_INT()
  162. - WX_DEFINE_SORTED_ARRAY_INT()
  163. - WX_DEFINE_SORTED_EXPORTED_ARRAY_INT()
  164. - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT()
  165. @section array_predef Predefined array types
  166. wxWidgets defines the following dynamic array types:
  167. - ::wxArrayShort
  168. - ::wxArrayInt
  169. - ::wxArrayDouble
  170. - ::wxArrayLong
  171. - ::wxArrayPtrVoid
  172. To use them you don't need any macro; you just need to include @c dynarray.h.
  173. @library{wxbase}
  174. @category{containers}
  175. @see @ref overview_container, wxList<T>, wxVector<T>
  176. */
  177. template <typename T>
  178. class wxArray<T>
  179. {
  180. public:
  181. /**
  182. @name Constructors and Destructors
  183. Array classes are 100% C++ objects and as such they have the
  184. appropriate copy constructors and assignment operators. Copying wxArray
  185. just copies the elements but copying wxObjArray copies the arrays
  186. items. However, for memory-efficiency sake, neither of these classes
  187. has virtual destructor. It is not very important for wxArray which has
  188. trivial destructor anyhow, but it does mean that you should avoid
  189. deleting wxObjArray through a wxBaseArray pointer (as you would never
  190. use wxBaseArray anyhow it shouldn't be a problem) and that you should
  191. not derive your own classes from the array classes.
  192. */
  193. //@{
  194. /**
  195. Default constructor.
  196. */
  197. wxArray();
  198. /**
  199. Default constructor initializes an empty array object.
  200. */
  201. wxObjArray();
  202. /**
  203. There is no default constructor for wxSortedArray classes - you must
  204. initialize it with a function to use for item comparison. It is a
  205. function which is passed two arguments of type @c T where @c T is the
  206. array element type and which should return a negative, zero or positive
  207. value according to whether the first element passed to it is less than,
  208. equal to or greater than the second one.
  209. */
  210. wxSortedArray(int (*)(T first, T second)compareFunction);
  211. /**
  212. Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to
  213. even if the source array contains the items of pointer type).
  214. */
  215. wxArray(const wxArray& array);
  216. /**
  217. Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to
  218. even if the source array contains the items of pointer type).
  219. */
  220. wxSortedArray(const wxSortedArray& array);
  221. /**
  222. Performs a deep copy (i.e.\ the array element are copied too).
  223. */
  224. wxObjArray(const wxObjArray& array);
  225. /**
  226. Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to
  227. even if the source array contains the items of pointer type).
  228. */
  229. wxArray& operator=(const wxArray& array);
  230. /**
  231. Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to
  232. even if the source array contains the items of pointer type).
  233. */
  234. wxSortedArray& operator=(const wxSortedArray& array);
  235. /**
  236. Performs a deep copy (i.e.\ the array element are copied too).
  237. */
  238. wxObjArray& operator=(const wxObjArray& array);
  239. /**
  240. This destructor does not delete all the items owned by the array, you
  241. may use the WX_CLEAR_ARRAY() macro for this.
  242. */
  243. ~wxArray();
  244. /**
  245. This destructor does not delete all the items owned by the array, you
  246. may use the WX_CLEAR_ARRAY() macro for this.
  247. */
  248. ~wxSortedArray();
  249. /**
  250. This destructor deletes all the items owned by the array.
  251. */
  252. ~wxObjArray();
  253. //@}
  254. /**
  255. @name Memory Management
  256. Automatic array memory management is quite trivial: the array starts by
  257. preallocating some minimal amount of memory (defined by
  258. @c WX_ARRAY_DEFAULT_INITIAL_SIZE) and when further new items exhaust
  259. already allocated memory it reallocates it adding 50% of the currently
  260. allocated amount, but no more than some maximal number which is defined
  261. by the @c ARRAY_MAXSIZE_INCREMENT constant. Of course, this may lead to
  262. some memory being wasted (@c ARRAY_MAXSIZE_INCREMENT in the worst case,
  263. i.e. 4Kb in the current implementation), so the Shrink() function is
  264. provided to deallocate the extra memory. The Alloc() function can also
  265. be quite useful if you know in advance how many items you are going to
  266. put in the array and will prevent the array code from reallocating the
  267. memory more times than needed.
  268. */
  269. //@{
  270. /**
  271. Preallocates memory for a given number of array elements. It is worth
  272. calling when the number of items which are going to be added to the
  273. array is known in advance because it will save unneeded memory
  274. reallocation. If the array already has enough memory for the given
  275. number of items, nothing happens. In any case, the existing contents of
  276. the array is not modified.
  277. */
  278. void Alloc(size_t count);
  279. /**
  280. Frees all memory unused by the array. If the program knows that no new
  281. items will be added to the array it may call Shrink() to reduce its
  282. memory usage. However, if a new item is added to the array, some extra
  283. memory will be allocated again.
  284. */
  285. void Shrink();
  286. //@}
  287. /**
  288. @name Number of Elements and Simple Item Access
  289. Functions in this section return the total number of array elements and
  290. allow to retrieve them - possibly using just the C array indexing []
  291. operator which does exactly the same as the Item() method.
  292. */
  293. //@{
  294. /**
  295. Return the number of items in the array.
  296. */
  297. size_t GetCount() const;
  298. /**
  299. Returns @true if the array is empty, @false otherwise.
  300. */
  301. bool IsEmpty() const;
  302. /**
  303. Returns the item at the given position in the array. If @a index is out
  304. of bounds, an assert failure is raised in the debug builds but nothing
  305. special is done in the release build.
  306. The returned value is of type "reference to the array element type" for
  307. all of the array classes.
  308. */
  309. T& Item(size_t index) const;
  310. /**
  311. Returns the last element in the array, i.e.\ is the same as calling
  312. "Item(GetCount() - 1)". An assert failure is raised in the debug mode
  313. if the array is empty.
  314. The returned value is of type "reference to the array element type" for
  315. all of the array classes.
  316. */
  317. T& Last() const;
  318. //@}
  319. /**
  320. @name Adding Items
  321. */
  322. //@{
  323. /**
  324. Appends the given number of @a copies of the @a item to the array
  325. consisting of the elements of type @c T.
  326. This version is used with wxArray.
  327. You may also use WX_APPEND_ARRAY() macro to append all elements of one
  328. array to another one but it is more efficient to use the @a copies
  329. parameter and modify the elements in place later if you plan to append
  330. a lot of items.
  331. */
  332. void Add(T item, size_t copies = 1);
  333. /**
  334. Appends the @a item to the array consisting of the elements of type
  335. @c T.
  336. This version is used with wxSortedArray, returning the index where
  337. @a item is stored.
  338. */
  339. size_t Add(T item);
  340. /**
  341. Appends the @a item to the array consisting of the elements of type
  342. @c T.
  343. This version is used with wxObjArray. The array will take ownership of
  344. the @a item, deleting it when the item is deleted from the array. Note
  345. that you cannot append more than one pointer as reusing it would lead
  346. to deleting it twice (or more) resulting in a crash.
  347. You may also use WX_APPEND_ARRAY() macro to append all elements of one
  348. array to another one but it is more efficient to use the @a copies
  349. parameter and modify the elements in place later if you plan to append
  350. a lot of items.
  351. */
  352. void Add(T* item);
  353. /**
  354. Appends the given number of @a copies of the @a item to the array
  355. consisting of the elements of type @c T.
  356. This version is used with wxObjArray. The array will make a copy of the
  357. item and will not take ownership of the original item.
  358. You may also use WX_APPEND_ARRAY() macro to append all elements of one
  359. array to another one but it is more efficient to use the @a copies
  360. parameter and modify the elements in place later if you plan to append
  361. a lot of items.
  362. */
  363. void Add(T& item, size_t copies = 1);
  364. /**
  365. Inserts the given @a item into the array in the specified @e index
  366. position.
  367. Be aware that you will set out the order of the array if you give a
  368. wrong position.
  369. This function is useful in conjunction with IndexForInsert() for a
  370. common operation of "insert only if not found".
  371. */
  372. void AddAt(T item, size_t index);
  373. /**
  374. Insert the given number of @a copies of the @a item into the array
  375. before the existing item @a n - thus, @e Insert(something, 0u) will
  376. insert an item in such way that it will become the first array element.
  377. wxSortedArray doesn't have this function because inserting in wrong
  378. place would break its sorted condition.
  379. Please see Add() for an explanation of the differences between the
  380. overloaded versions of this function.
  381. */
  382. void Insert(T item, size_t n, size_t copies = 1);
  383. /**
  384. Insert the @a item into the array before the existing item @a n - thus,
  385. @e Insert(something, 0u) will insert an item in such way that it will
  386. become the first array element.
  387. wxSortedArray doesn't have this function because inserting in wrong
  388. place would break its sorted condition.
  389. Please see Add() for an explanation of the differences between the
  390. overloaded versions of this function.
  391. */
  392. void Insert(T* item, size_t n);
  393. /**
  394. Insert the given number of @a copies of the @a item into the array
  395. before the existing item @a n - thus, @e Insert(something, 0u) will
  396. insert an item in such way that it will become the first array element.
  397. wxSortedArray doesn't have this function because inserting in wrong
  398. place would break its sorted condition.
  399. Please see Add() for an explanation of the differences between the
  400. overloaded versions of this function.
  401. */
  402. void Insert(T& item, size_t n, size_t copies = 1);
  403. /**
  404. This function ensures that the number of array elements is at least
  405. @a count. If the array has already @a count or more items, nothing is
  406. done. Otherwise, @a count - GetCount() elements are added and
  407. initialized to the value @a defval.
  408. @see GetCount()
  409. */
  410. void SetCount(size_t count, T defval = T(0));
  411. //@}
  412. /**
  413. @name Removing Items
  414. */
  415. //@{
  416. /**
  417. This function does the same as Empty() and additionally frees the
  418. memory allocated to the array.
  419. */
  420. void Clear();
  421. /**
  422. Removes the element from the array, but unlike Remove(), it doesn't
  423. delete it. The function returns the pointer to the removed element.
  424. */
  425. T* Detach(size_t index);
  426. /**
  427. Empties the array. For wxObjArray classes, this destroys all of the
  428. array elements. For wxArray and wxSortedArray this does nothing except
  429. marking the array of being empty - this function does not free the
  430. allocated memory, use Clear() for this.
  431. */
  432. void Empty();
  433. /**
  434. Removes an element from the array by value: the first item of the array
  435. equal to @a item is removed, an assert failure will result from an
  436. attempt to remove an item which doesn't exist in the array.
  437. When an element is removed from wxObjArray it is deleted by the array -
  438. use Detach() if you don't want this to happen. On the other hand, when
  439. an object is removed from a wxArray nothing happens - you should delete
  440. it manually if required:
  441. @code
  442. T *item = array[n];
  443. array.Remove(item);
  444. delete item;
  445. @endcode
  446. See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray
  447. (supposed to contain pointers).
  448. Notice that for sorted arrays this method uses binary search to find
  449. the item so it doesn't necessarily remove the first matching item, but
  450. the first one found by the binary search.
  451. @see RemoveAt()
  452. */
  453. void Remove(T item);
  454. /**
  455. Removes @a count elements starting at @a index from the array. When an
  456. element is removed from wxObjArray it is deleted by the array - use
  457. Detach() if you don't want this to happen. On the other hand, when an
  458. object is removed from a wxArray nothing happens - you should delete it
  459. manually if required:
  460. @code
  461. T *item = array[n];
  462. delete item;
  463. array.RemoveAt(n);
  464. @endcode
  465. See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray
  466. (supposed to contain pointers).
  467. */
  468. void RemoveAt(size_t index, size_t count = 1);
  469. //@}
  470. /**
  471. @name Searching and Sorting
  472. */
  473. //@{
  474. /**
  475. This version of Index() is for wxArray and wxObjArray only.
  476. Searches the element in the array, starting from either beginning or
  477. the end depending on the value of @a searchFromEnd parameter.
  478. @c wxNOT_FOUND is returned if the element is not found, otherwise the
  479. index of the element is returned.
  480. @note Even for wxObjArray classes, the operator "==" of the elements in
  481. the array is @b not used by this function. It searches exactly
  482. the given element in the array and so will only succeed if this
  483. element had been previously added to the array, but fail even if
  484. another, identical, element is in the array.
  485. */
  486. int Index(T& item, bool searchFromEnd = false) const;
  487. /**
  488. This version of Index() is for wxSortedArray only.
  489. Searches for the element in the array, using binary search.
  490. @c wxNOT_FOUND is returned if the element is not found, otherwise the
  491. index of the element is returned.
  492. */
  493. int Index(T& item) const;
  494. /**
  495. Search for a place to insert @a item into the sorted array (binary
  496. search). The index returned is just before the first existing item that
  497. is greater or equal (according to the compare function) to the given
  498. @a item.
  499. You have to do extra work to know if the @a item already exists in
  500. array.
  501. This function is useful in conjunction with AddAt() for a common
  502. operation of "insert only if not found".
  503. */
  504. size_t IndexForInsert(T item) const;
  505. /**
  506. The notation @c "CMPFUNCT<T>" should be read as if we had the following
  507. declaration:
  508. @code
  509. template int CMPFUNC(T *first, T *second);
  510. @endcode
  511. Where @e T is the type of the array elements. I.e. it is a function
  512. returning @e int which is passed two arguments of type @e T*.
  513. Sorts the array using the specified compare function: this function
  514. should return a negative, zero or positive value according to whether
  515. the first element passed to it is less than, equal to or greater than
  516. the second one.
  517. wxSortedArray doesn't have this function because it is always sorted.
  518. */
  519. void Sort(CMPFUNC<T> compareFunction);
  520. //@}
  521. };
  522. /**
  523. This macro may be used to append all elements of the @a wxArray_arrayToBeAppended
  524. array to the @a wxArray_arrayToModify. The two arrays must be of the same type.
  525. */
  526. #define WX_APPEND_ARRAY(wxArray_arrayToModify, wxArray_arrayToBeAppended)
  527. /**
  528. This macro may be used to delete all elements of the array before emptying
  529. it. It cannot be used with wxObjArrays - but they will delete their
  530. elements anyway when you call Empty().
  531. */
  532. #define WX_CLEAR_ARRAY(wxArray_arrayToBeCleared)
  533. //@{
  534. /**
  535. This macro declares a new object array class named @a name and containing
  536. the elements of type @e T.
  537. An exported array is used when compiling wxWidgets as a DLL under Windows
  538. and the array needs to be visible outside the DLL. An user exported array
  539. needed for exporting an array from a user DLL.
  540. Example:
  541. @code
  542. class MyClass;
  543. WX_DECLARE_OBJARRAY(MyClass, wxArrayOfMyClass); // note: not "MyClass *"!
  544. @endcode
  545. You must use WX_DEFINE_OBJARRAY() macro to define the array class,
  546. otherwise you would get link errors.
  547. */
  548. #define WX_DECLARE_OBJARRAY(T, name)
  549. #define WX_DECLARE_EXPORTED_OBJARRAY(T, name)
  550. #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name)
  551. //@}
  552. //@{
  553. /**
  554. This macro defines a new array class named @a name and containing the
  555. elements of type @a T.
  556. An exported array is used when compiling wxWidgets as a DLL under Windows
  557. and the array needs to be visible outside the DLL. An user exported array
  558. needed for exporting an array from a user DLL.
  559. Example:
  560. @code
  561. WX_DEFINE_ARRAY_INT(int, MyArrayInt);
  562. class MyClass;
  563. WX_DEFINE_ARRAY(MyClass *, ArrayOfMyClass);
  564. @endcode
  565. Note that wxWidgets predefines the following standard array classes:
  566. @b wxArrayInt, @b wxArrayLong, @b wxArrayShort, @b wxArrayDouble,
  567. @b wxArrayPtrVoid.
  568. */
  569. #define WX_DEFINE_ARRAY(T, name)
  570. #define WX_DEFINE_EXPORTED_ARRAY(T, name)
  571. #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, exportspec)
  572. //@}
  573. //@{
  574. /**
  575. This macro defines the methods of the array class @a name not defined by
  576. the WX_DECLARE_OBJARRAY() macro. You must include the file
  577. @<wx/arrimpl.cpp@> before using this macro and you must have the full
  578. declaration of the class of array elements in scope! If you forget to do
  579. the first, the error will be caught by the compiler, but, unfortunately,
  580. many compilers will not give any warnings if you forget to do the second -
  581. but the objects of the class will not be copied correctly and their real
  582. destructor will not be called.
  583. An exported array is used when compiling wxWidgets as a DLL under Windows
  584. and the array needs to be visible outside the DLL. An user exported array
  585. needed for exporting an array from a user DLL.
  586. Example of usage:
  587. @code
  588. // first declare the class!
  589. class MyClass
  590. {
  591. public:
  592. MyClass(const MyClass&);
  593. // ...
  594. virtual ~MyClass();
  595. };
  596. #include <wx/arrimpl.cpp>
  597. WX_DEFINE_OBJARRAY(wxArrayOfMyClass);
  598. @endcode
  599. */
  600. #define WX_DEFINE_OBJARRAY(name)
  601. #define WX_DEFINE_EXPORTED_OBJARRAY(name)
  602. #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name)
  603. //@}
  604. //@{
  605. /**
  606. This macro defines a new sorted array class named @a name and containing
  607. the elements of type @e T.
  608. An exported array is used when compiling wxWidgets as a DLL under Windows
  609. and the array needs to be visible outside the DLL. An user exported array
  610. needed for exporting an array from a user DLL.
  611. Example:
  612. @code
  613. WX_DEFINE_SORTED_ARRAY_INT(int, MySortedArrayInt);
  614. class MyClass;
  615. WX_DEFINE_SORTED_ARRAY(MyClass *, ArrayOfMyClass);
  616. @endcode
  617. You will have to initialize the objects of this class by passing a
  618. comparison function to the array object constructor like this:
  619. @code
  620. int CompareInts(int n1, int n2)
  621. {
  622. return n1 - n2;
  623. }
  624. MySortedArrayInt sorted(CompareInts);
  625. int CompareMyClassObjects(MyClass *item1, MyClass *item2)
  626. {
  627. // sort the items by their address...
  628. return Stricmp(item1->GetAddress(), item2->GetAddress());
  629. }
  630. ArrayOfMyClass another(CompareMyClassObjects);
  631. @endcode
  632. */
  633. #define WX_DEFINE_SORTED_ARRAY(T, name)
  634. #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name)
  635. #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name)
  636. //@}
  637. /**
  638. This macro may be used to prepend all elements of the @a wxArray_arrayToBePrepended
  639. array to the @a wxArray_arrayToModify. The two arrays must be of the same type.
  640. */
  641. #define WX_PREPEND_ARRAY(wxArray_arrayToModify, wxArray_arrayToBePrepended)
  642. //@{
  643. /**
  644. Predefined specialization of wxArray<T> for standard types.
  645. */
  646. typedef wxArray<int> wxArrayInt;
  647. typedef wxArray<long> wxArrayLong;
  648. typedef wxArray<short> wxArrayShort;
  649. typedef wxArray<double> wxArrayDouble;
  650. typedef wxArray<void*> wxArrayPtrVoid;
  651. //@}