TriviallyCopyable.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_TRIVIALLYCOPYABLE_HPP
  14. #define ZT_TRIVIALLYCOPYABLE_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * Classes inheriting from this base class are safe to abuse in C-like ways.
  20. *
  21. * It also includes some static methods to do this conveniently.
  22. */
  23. struct TriviallyCopyable
  24. {
  25. public:
  26. /**
  27. * Zero a TriviallyCopyable object
  28. *
  29. * @tparam T Automatically inferred type of object
  30. * @param obj Any TriviallyCopyable object
  31. */
  32. template<typename T>
  33. static ZT_INLINE void memoryZero(T *obj) noexcept
  34. {
  35. mustBeTriviallyCopyable(obj);
  36. Utils::zero<sizeof(T)>(obj);
  37. }
  38. /**
  39. * Zero a TriviallyCopyable object
  40. *
  41. * @tparam T Automatically inferred type of object
  42. * @param obj Any TriviallyCopyable object
  43. */
  44. template<typename T>
  45. static ZT_INLINE void memoryZero(T &obj) noexcept
  46. {
  47. mustBeTriviallyCopyable(obj);
  48. Utils::zero<sizeof(T)>(&obj);
  49. }
  50. /**
  51. * Copy a TriviallyCopyable object
  52. *
  53. * @tparam T Automatically inferred type of destination
  54. * @param dest Destination TriviallyCopyable object
  55. * @param src Source TriviallyCopyable object
  56. */
  57. template<typename T>
  58. static ZT_INLINE void memoryCopy(T *dest,const T *src) noexcept
  59. {
  60. mustBeTriviallyCopyable(dest);
  61. Utils::copy<sizeof(T)>(dest,src);
  62. }
  63. /**
  64. * Copy a TriviallyCopyable object
  65. *
  66. * @tparam T Automatically inferred type of destination
  67. * @param dest Destination TriviallyCopyable object
  68. * @param src Source TriviallyCopyable object
  69. */
  70. template<typename T>
  71. static ZT_INLINE void memoryCopy(T *dest,const T &src) noexcept
  72. {
  73. mustBeTriviallyCopyable(src);
  74. Utils::copy<sizeof(T)>(dest,&src);
  75. }
  76. /**
  77. * Copy a TriviallyCopyable object
  78. *
  79. * @tparam T Automatically inferred type of destination
  80. * @param dest Destination TriviallyCopyable object
  81. * @param src Source TriviallyCopyable object
  82. */
  83. template<typename T>
  84. static ZT_INLINE void memoryCopy(T &dest,const T *src) noexcept
  85. {
  86. mustBeTriviallyCopyable(dest);
  87. Utils::copy<sizeof(T)>(&dest,src);
  88. }
  89. /**
  90. * Copy a TriviallyCopyable object
  91. *
  92. * @tparam T Automatically inferred type of destination
  93. * @param dest Destination TriviallyCopyable object
  94. * @param src Source TriviallyCopyable object
  95. */
  96. template<typename T>
  97. static ZT_INLINE void memoryCopy(T &dest,const T &src) noexcept
  98. {
  99. mustBeTriviallyCopyable(dest);
  100. Utils::copy<sizeof(T)>(&dest,&src);
  101. }
  102. private:
  103. static ZT_INLINE void mustBeTriviallyCopyable(const TriviallyCopyable &) noexcept {}
  104. static ZT_INLINE void mustBeTriviallyCopyable(const TriviallyCopyable *) noexcept {}
  105. };
  106. } // namespace ZeroTier
  107. #endif