Array.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Array.h
  3. //
  4. // $Id: //poco/svn/Foundation/include/Poco/Array.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Array
  9. //
  10. // Definition of the Array class
  11. //
  12. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. // ------------------------------------------------------------------------------
  18. // (C) Copyright Nicolai M. Josuttis 2001.
  19. // Permission to copy, use, modify, sell and distribute this software
  20. // is granted provided this copyright notice appears in all copies.
  21. // This software is provided "as is" without express or implied
  22. // warranty, and with no claim as to its suitability for any purpose.
  23. // ------------------------------------------------------------------------------
  24. #ifndef Foundation_Array_INCLUDED
  25. #define Foundation_Array_INCLUDED
  26. #include "Poco/Exception.h"
  27. #include "Poco/Bugcheck.h"
  28. #include <algorithm>
  29. namespace Poco {
  30. template<class T, std::size_t N>
  31. class Array
  32. /// STL container like C-style array replacement class.
  33. ///
  34. /// This implementation is based on the idea of Nicolai Josuttis.
  35. /// His original implementation can be found at http://www.josuttis.com/cppcode/array.html .
  36. {
  37. public:
  38. typedef T value_type;
  39. typedef T* iterator;
  40. typedef const T* const_iterator;
  41. typedef T& reference;
  42. typedef const T& const_reference;
  43. typedef std::size_t size_type;
  44. typedef std::ptrdiff_t difference_type;
  45. iterator begin()
  46. {
  47. return elems;
  48. }
  49. const_iterator begin() const
  50. {
  51. return elems;
  52. }
  53. iterator end()
  54. {
  55. return elems+N;
  56. }
  57. const_iterator end() const
  58. {
  59. return elems+N;
  60. }
  61. typedef std::reverse_iterator<iterator> reverse_iterator;
  62. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  63. reverse_iterator rbegin()
  64. {
  65. return reverse_iterator(end());
  66. }
  67. const_reverse_iterator rbegin() const
  68. {
  69. return const_reverse_iterator(end());
  70. }
  71. reverse_iterator rend()
  72. {
  73. return reverse_iterator(begin());
  74. }
  75. const_reverse_iterator rend() const
  76. {
  77. return const_reverse_iterator(begin());
  78. }
  79. reference operator[](size_type i)
  80. /// Element access without range check. If the index is not small than the given size, the behavior is undefined.
  81. {
  82. poco_assert( i < N && "out of range" );
  83. return elems[i];
  84. }
  85. const_reference operator[](size_type i) const
  86. /// Element access without range check. If the index is not small than the given size, the behavior is undefined.
  87. {
  88. poco_assert( i < N && "out of range" );
  89. return elems[i];
  90. }
  91. reference at(size_type i)
  92. /// Element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
  93. {
  94. if(i>=size())
  95. throw Poco::InvalidArgumentException("Array::at() range check failed: index is over range");
  96. return elems[i];
  97. }
  98. const_reference at(size_type i) const
  99. /// Element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
  100. {
  101. if(i>=size())
  102. throw Poco::InvalidArgumentException("Array::at() range check failed: index is over range");
  103. return elems[i];
  104. }
  105. reference front()
  106. {
  107. return elems[0];
  108. }
  109. const_reference front() const
  110. {
  111. return elems[0];
  112. }
  113. reference back()
  114. {
  115. return elems[N-1];
  116. }
  117. const_reference back() const
  118. {
  119. return elems[N-1];
  120. }
  121. static size_type size()
  122. {
  123. return N;
  124. }
  125. static bool empty()
  126. {
  127. return false;
  128. }
  129. static size_type max_size()
  130. {
  131. return N;
  132. }
  133. enum { static_size = N };
  134. void swap (Array<T,N>& y) {
  135. std::swap_ranges(begin(),end(),y.begin());
  136. }
  137. const T* data() const
  138. /// Direct access to data (read-only)
  139. {
  140. return elems;
  141. }
  142. T* data()
  143. {
  144. return elems;
  145. }
  146. T* c_array(){
  147. /// Use array as C array (direct read/write access to data)
  148. return elems;
  149. }
  150. template <typename Other>
  151. Array<T,N>& operator= (const Array<Other,N>& rhs)
  152. /// Assignment with type conversion
  153. {
  154. std::copy(rhs.begin(),rhs.end(), begin());
  155. return *this;
  156. }
  157. void assign (const T& value)
  158. /// Assign one value to all elements
  159. {
  160. std::fill_n(begin(),size(),value);
  161. }
  162. public:
  163. T elems[N];
  164. /// Fixed-size array of elements of type T, public specifier used to make this class a aggregate.
  165. };
  166. // comparisons
  167. template<class T, std::size_t N>
  168. bool operator== (const Array<T,N>& x, const Array<T,N>& y)
  169. {
  170. return std::equal(x.begin(), x.end(), y.begin());
  171. }
  172. template<class T, std::size_t N>
  173. bool operator< (const Array<T,N>& x, const Array<T,N>& y)
  174. {
  175. return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
  176. }
  177. template<class T, std::size_t N>
  178. bool operator!= (const Array<T,N>& x, const Array<T,N>& y)
  179. {
  180. return !(x==y);
  181. }
  182. template<class T, std::size_t N>
  183. bool operator> (const Array<T,N>& x, const Array<T,N>& y)
  184. {
  185. return y<x;
  186. }
  187. template<class T, std::size_t N>
  188. bool operator<= (const Array<T,N>& x, const Array<T,N>& y)
  189. {
  190. return !(y<x);
  191. }
  192. template<class T, std::size_t N>
  193. bool operator>= (const Array<T,N>& x, const Array<T,N>& y)
  194. {
  195. return !(x<y);
  196. }
  197. template<class T, std::size_t N>
  198. inline void swap (Array<T,N>& x, Array<T,N>& y)
  199. /// global swap()
  200. {
  201. x.swap(y);
  202. }
  203. }// namespace Poco
  204. #endif // Foundation_Array_INCLUDED