TriviallyCopyable.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: 2025-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. private:
  51. static ZT_INLINE void mustBeTriviallyCopyable(const TriviallyCopyable &) noexcept {}
  52. static ZT_INLINE void mustBeTriviallyCopyable(const TriviallyCopyable *) noexcept {}
  53. };
  54. } // namespace ZeroTier
  55. #endif