FCV.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "Constants.hpp"
  27. namespace ZeroTier {
  28. /**
  29. * A really simple fixed capacity vector
  30. *
  31. * This class does no bounds checking, so the user must ensure that
  32. * no more than C elements are ever added and that accesses are in
  33. * bounds.
  34. *
  35. * @tparam T Type to contain
  36. * @tparam C Capacity of vector
  37. */
  38. template<typename T,unsigned long C>
  39. class FCV
  40. {
  41. public:
  42. FCV() : _s(0) {}
  43. ~FCV() { clear(); }
  44. FCV(const FCV &v) :
  45. _s(v._s)
  46. {
  47. for(unsigned long i=0;i<_s;++i) {
  48. new (reinterpret_cast<T *>(_mem + (sizeof(T) * i))) T(reinterpret_cast<const T *>(v._mem)[i]);
  49. }
  50. }
  51. inline FCV &operator=(const FCV &v)
  52. {
  53. clear();
  54. _s = v._s;
  55. for(unsigned long i=0;i<_s;++i) {
  56. new (reinterpret_cast<T *>(_mem + (sizeof(T) * i))) T(reinterpret_cast<const T *>(v._mem)[i]);
  57. }
  58. return *this;
  59. }
  60. typedef T * iterator;
  61. typedef const T * const_iterator;
  62. typedef unsigned long size_type;
  63. inline iterator begin() { return (T *)_mem; }
  64. inline iterator end() { return (T *)(_mem + (sizeof(T) * _s)); }
  65. inline iterator begin() const { return (const T *)_mem; }
  66. inline iterator end() const { return (const T *)(_mem + (sizeof(T) * _s)); }
  67. inline T &operator[](const size_type i) { return reinterpret_cast<T *>(_mem)[i]; }
  68. inline const T &operator[](const size_type i) const { return reinterpret_cast<const T *>(_mem)[i]; }
  69. inline T &front() { return reinterpret_cast<T *>(_mem)[0]; }
  70. inline const T &front() const { return reinterpret_cast<const T *>(_mem)[0]; }
  71. inline T &back() { return reinterpret_cast<T *>(_mem)[_s - 1]; }
  72. inline const T &back() const { return reinterpret_cast<const T *>(_mem)[_s - 1]; }
  73. inline void push_back(const T &v) { new (reinterpret_cast<T *>(_mem + (sizeof(T) * _s++))) T(v); }
  74. inline void pop_back() { reinterpret_cast<T *>(_mem + (sizeof(T) * --_s))->~T(); }
  75. inline size_type size() const { return _s; }
  76. inline size_type capacity() const { return C; }
  77. inline void clear()
  78. {
  79. for(unsigned long i=0;i<_s;++i)
  80. reinterpret_cast<T *>(_mem + (sizeof(T) * i))->~T();
  81. _s = 0;
  82. }
  83. private:
  84. char _mem[sizeof(T) * C];
  85. unsigned long _s;
  86. };
  87. } // namespace ZeroTier