TriviallyCopyable.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include <cstring>
  18. #include <cstdlib>
  19. namespace ZeroTier {
  20. /**
  21. * Classes inheriting from this base class are safe to abuse in C-like ways.
  22. *
  23. * It also includes some static methods to do this conveniently.
  24. */
  25. ZT_PACKED_STRUCT(struct TriviallyCopyable
  26. {
  27. /**
  28. * Zero a TriviallyCopyable object
  29. *
  30. * @tparam T Automatically inferred type of object
  31. * @param obj Any TriviallyCopyable object
  32. */
  33. template<typename T>
  34. static ZT_INLINE void memoryZero(T *obj) noexcept
  35. {
  36. TriviallyCopyable *const tmp = obj;
  37. Utils::zero<sizeof(T)>(tmp);
  38. }
  39. /**
  40. * Zero a TriviallyCopyable object
  41. *
  42. * @tparam T Automatically inferred type of object
  43. * @param obj Any TriviallyCopyable object
  44. */
  45. template<typename T>
  46. static ZT_INLINE void memoryZero(T &obj) noexcept
  47. {
  48. TriviallyCopyable *const tmp = &obj;
  49. Utils::zero<sizeof(T)>(tmp);
  50. }
  51. /**
  52. * Copy any memory over a TriviallyCopyable object
  53. *
  54. * @tparam T Automatically inferred type of destination
  55. * @param dest Any TriviallyCopyable object
  56. * @param src Source memory of same size or less than sizeof(dest)
  57. */
  58. template<typename T>
  59. static ZT_INLINE void memoryCopyUnsafe(T *dest,const void *src) noexcept
  60. {
  61. TriviallyCopyable *const tmp = dest;
  62. Utils::copy<sizeof(T)>(tmp,src);
  63. }
  64. /**
  65. * Copy any memory over a TriviallyCopyable object
  66. *
  67. * @tparam T Automatically inferred type of destination
  68. * @param dest Any TriviallyCopyable object
  69. * @param src Source memory of same size or less than sizeof(dest)
  70. */
  71. template<typename T>
  72. static ZT_INLINE void memoryCopyUnsafe(T &dest,const void *src) noexcept
  73. {
  74. TriviallyCopyable *const tmp = &dest;
  75. Utils::copy<sizeof(T)>(tmp,src);
  76. }
  77. /**
  78. * Copy a TriviallyCopyable object
  79. *
  80. * @tparam T Automatically inferred type of destination
  81. * @param dest Destination TriviallyCopyable object
  82. * @param src Source TriviallyCopyable object
  83. */
  84. template<typename T>
  85. static ZT_INLINE void memoryCopy(T *dest,const T *src) noexcept
  86. {
  87. TriviallyCopyable *const tmp = dest;
  88. Utils::copy<sizeof(T)>(tmp,src);
  89. }
  90. /**
  91. * Copy a TriviallyCopyable object
  92. *
  93. * @tparam T Automatically inferred type of destination
  94. * @param dest Destination TriviallyCopyable object
  95. * @param src Source TriviallyCopyable object
  96. */
  97. template<typename T>
  98. static ZT_INLINE void memoryCopy(T *dest,const T &src) noexcept
  99. {
  100. TriviallyCopyable *const tmp = dest;
  101. Utils::copy<sizeof(T)>(tmp,&src);
  102. }
  103. /**
  104. * Copy a TriviallyCopyable object
  105. *
  106. * @tparam T Automatically inferred type of destination
  107. * @param dest Destination TriviallyCopyable object
  108. * @param src Source TriviallyCopyable object
  109. */
  110. template<typename T>
  111. static ZT_INLINE void memoryCopy(T &dest,const T *src) noexcept
  112. {
  113. TriviallyCopyable *const tmp = &dest;
  114. Utils::copy<sizeof(T)>(tmp,src);
  115. }
  116. /**
  117. * Copy a TriviallyCopyable object
  118. *
  119. * @tparam T Automatically inferred type of destination
  120. * @param dest Destination TriviallyCopyable object
  121. * @param src Source TriviallyCopyable object
  122. */
  123. template<typename T>
  124. static ZT_INLINE void memoryCopy(T &dest,const T &src) noexcept
  125. {
  126. TriviallyCopyable *const tmp = &dest;
  127. Utils::copy<sizeof(T)>(tmp,&src);
  128. }
  129. });
  130. static constexpr bool isTriviallyCopyable(const TriviallyCopyable *const anything) noexcept { return true; }
  131. static constexpr bool isTriviallyCopyable(const void *const anything) noexcept { return false; }
  132. template<typename T>
  133. static constexpr bool isTriviallyCopyable(const T &anything) noexcept { return isTriviallyCopyable(&anything); }
  134. } // namespace ZeroTier
  135. #endif