FCV.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_FCV_HPP
  14. #define ZT_FCV_HPP
  15. #include "Constants.hpp"
  16. #include <iterator>
  17. #include <algorithm>
  18. #include <memory>
  19. #include <cstring>
  20. #include <cstdlib>
  21. namespace ZeroTier {
  22. /**
  23. * FCV is a Fixed Capacity Vector
  24. *
  25. * Attempts to resize, push, or access this vector beyond its capacity will
  26. * silently fail. The [] operator is NOT bounds checked!
  27. *
  28. * This doesn't implement everything in std::vector, just what we need. It
  29. * also adds a few special things for use in ZT core code.
  30. *
  31. * Note that an FCV will be TriviallyCopyable IF and only if its contained
  32. * type is TriviallyCopyable. There's a const static checker for this.
  33. *
  34. * @tparam T Type to contain
  35. * @tparam C Maximum capacity of vector
  36. */
  37. template<typename T,unsigned int C>
  38. class FCV
  39. {
  40. public:
  41. typedef T * iterator;
  42. typedef const T * const_iterator;
  43. /**
  44. * @return True if this FCV is trivially copyable, which means its type is also.
  45. */
  46. static constexpr bool isTriviallyCopyable() noexcept { return isTriviallyCopyable(reinterpret_cast<const T *>(0)); }
  47. ZT_INLINE FCV() noexcept : _s(0) {} // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  48. ZT_INLINE FCV(const FCV &v) : _s(0) { *this = v; } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  49. template<typename I>
  50. ZT_INLINE FCV(I i,I end) : // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  51. _s(0)
  52. {
  53. while (i != end) {
  54. push_back(*i);
  55. ++i;
  56. }
  57. }
  58. ZT_INLINE ~FCV() { this->clear(); }
  59. ZT_INLINE FCV &operator=(const FCV &v)
  60. {
  61. if (&v != this) {
  62. this->clear();
  63. const unsigned int s = v._s;
  64. _s = s;
  65. for (unsigned int i=0;i<s;++i)
  66. new(reinterpret_cast<T *>(_m) + i) T(*(reinterpret_cast<const T *>(v._m) + i));
  67. }
  68. return *this;
  69. }
  70. /**
  71. * Clear this vector, destroying all content objects
  72. */
  73. ZT_INLINE void clear()
  74. {
  75. const unsigned int s = _s;
  76. _s = 0;
  77. for(unsigned int i=0;i<s;++i)
  78. (reinterpret_cast<T *>(_m) + i)->~T();
  79. }
  80. /**
  81. * Clear without calling destructors (same as unsafeResize(0))
  82. */
  83. ZT_INLINE void unsafeClear() noexcept { _s = 0; }
  84. /**
  85. * This does a straight copy of one vector's data to another
  86. *
  87. * @tparam C2 Inferred capacity of other vector
  88. * @param v Other vector to copy to this one
  89. */
  90. template<unsigned int C2>
  91. ZT_INLINE void unsafeAssign(const FCV<T,C2> &v) noexcept
  92. {
  93. _s = ((C2 > C)&&(v._s > C)) ? C : v._s;
  94. Utils::copy(_m,v._m,_s * sizeof(T));
  95. }
  96. /**
  97. * Move contents from this vector to another and clear this vector
  98. *
  99. * @param v Target vector
  100. */
  101. ZT_INLINE void unsafeMoveTo(FCV &v) noexcept
  102. {
  103. Utils::copy(v._m,_m,(v._s = _s) * sizeof(T));
  104. _s = 0;
  105. }
  106. ZT_INLINE iterator begin() noexcept { return reinterpret_cast<T *>(_m); }
  107. ZT_INLINE const_iterator begin() const noexcept { return reinterpret_cast<const T *>(_m); }
  108. ZT_INLINE iterator end() noexcept { return reinterpret_cast<T *>(_m) + _s; }
  109. ZT_INLINE const_iterator end() const noexcept { return reinterpret_cast<const T *>(_m) + _s; }
  110. ZT_INLINE T &operator[](const unsigned int i) noexcept { return reinterpret_cast<T *>(_m)[i]; }
  111. ZT_INLINE const T &operator[](const unsigned int i) const noexcept { return reinterpret_cast<T *>(_m)[i]; }
  112. ZT_INLINE unsigned int size() const noexcept { return _s; }
  113. ZT_INLINE bool empty() const noexcept { return (_s == 0); }
  114. static constexpr unsigned int capacity() noexcept { return C; }
  115. /**
  116. * Push a value onto the back of this vector
  117. *
  118. * If the vector is at capacity this silently fails.
  119. *
  120. * @param v Value to push
  121. */
  122. ZT_INLINE void push_back(const T &v)
  123. {
  124. if (_s < C)
  125. new (reinterpret_cast<T *>(_m) + _s++) T(v);
  126. }
  127. /**
  128. * Push a new value onto the vector and return it, or return last item if capacity is reached
  129. *
  130. * @return Reference to new item
  131. */
  132. ZT_INLINE T &push()
  133. {
  134. if (_s < C) {
  135. return *(new(reinterpret_cast<T *>(_m) + _s++) T());
  136. } else {
  137. return *(reinterpret_cast<T *>(_m) + (C - 1));
  138. }
  139. }
  140. /**
  141. * Push a new value onto the vector and return it, or return last item if capacity is reached
  142. *
  143. * @return Reference to new item
  144. */
  145. ZT_INLINE T &push(const T &v)
  146. {
  147. if (_s < C) {
  148. return *(new(reinterpret_cast<T *>(_m) + _s++) T(v));
  149. } else {
  150. T &tmp = *(reinterpret_cast<T *>(_m) + (C - 1));
  151. tmp = v;
  152. return tmp;
  153. }
  154. }
  155. /**
  156. * Remove the last element if this vector is not empty
  157. */
  158. ZT_INLINE void pop_back()
  159. {
  160. if (_s != 0)
  161. (reinterpret_cast<T *>(_m) + --_s)->~T();
  162. }
  163. /**
  164. * Resize vector
  165. *
  166. * @param ns New size (clipped to C if larger than capacity)
  167. */
  168. ZT_INLINE void resize(unsigned int ns)
  169. {
  170. if (ns > C)
  171. ns = C;
  172. unsigned int s = _s;
  173. while (s < ns)
  174. new(reinterpret_cast<T *>(_m) + s++) T();
  175. while (s > ns)
  176. (reinterpret_cast<T *>(_m) + --s)->~T();
  177. _s = s;
  178. }
  179. /**
  180. * Resize without calling any constructors or destructors on T
  181. *
  182. * This must only be called if T is a primitive type or is TriviallyCopyable and
  183. * safe to initialize from undefined contents.
  184. *
  185. * @param ns New size (clipped to C if larger than capacity)
  186. */
  187. ZT_INLINE void unsafeResize(const unsigned int ns) noexcept { _s = (ns > C) ? C : ns; }
  188. /**
  189. * This is a bounds checked auto-resizing variant of the [] operator
  190. *
  191. * If 'i' is out of bounds vs the current size of the vector, the vector is
  192. * resized. If that size would exceed C (capacity), 'i' is clipped to C-1.
  193. *
  194. * @param i Index to obtain as a reference, resizing if needed
  195. * @return Reference to value at this index
  196. */
  197. ZT_INLINE T &at(unsigned int i)
  198. {
  199. if (i >= _s) {
  200. if (unlikely(i >= C))
  201. i = C - 1;
  202. do {
  203. new(reinterpret_cast<T *>(_m) + _s++) T();
  204. } while (i >= _s);
  205. }
  206. return *(reinterpret_cast<T *>(_m) + i);
  207. }
  208. /**
  209. * Assign this vector's contents from a range of pointers or iterators
  210. *
  211. * If the range is larger than C it is truncated at C.
  212. *
  213. * @tparam X Inferred type of interators or pointers
  214. * @param start Starting iterator
  215. * @param end Ending iterator (must be greater than start)
  216. */
  217. template<typename X>
  218. ZT_INLINE void assign(X start,const X &end)
  219. {
  220. const int l = std::min((int)std::distance(start,end),(int)C);
  221. if (l > 0) {
  222. this->resize((unsigned int)l);
  223. for(int i=0;i<l;++i)
  224. reinterpret_cast<T *>(_m)[i] = *(start++);
  225. } else {
  226. this->clear();
  227. }
  228. }
  229. ZT_INLINE bool operator==(const FCV &v) const noexcept
  230. {
  231. if (_s == v._s) {
  232. for(unsigned int i=0;i<_s;++i) {
  233. if (!(*(reinterpret_cast<const T *>(_m) + i) == *(reinterpret_cast<const T *>(v._m) + i)))
  234. return false;
  235. }
  236. return true;
  237. }
  238. return false;
  239. }
  240. ZT_INLINE bool operator!=(const FCV &v) const noexcept { return (!(*this == v)); }
  241. ZT_INLINE bool operator<(const FCV &v) const noexcept { return std::lexicographical_compare(begin(),end(),v.begin(),v.end()); }
  242. ZT_INLINE bool operator>(const FCV &v) const noexcept { return (v < *this); }
  243. ZT_INLINE bool operator<=(const FCV &v) const noexcept { return !(v < *this); }
  244. ZT_INLINE bool operator>=(const FCV &v) const noexcept { return !(*this < v); }
  245. private:
  246. unsigned int _s;
  247. uint8_t _m[sizeof(T) * C];
  248. };
  249. } // namespace ZeroTier
  250. #endif