Blob.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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_BLOB_HPP
  14. #define ZT_BLOB_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * Container for arbitrary bytes for use in collections
  20. *
  21. * @tparam S Size of container in bytes
  22. */
  23. template<unsigned int S>
  24. struct Blob
  25. {
  26. uint8_t data[S];
  27. ZT_INLINE Blob() noexcept { Utils::zero<S>(data); }
  28. explicit ZT_INLINE Blob(const void *const d) noexcept { Utils::copy<S>(data,d); }
  29. ZT_INLINE operator bool() const noexcept { return !Utils::allZero(data); }
  30. ZT_INLINE bool operator==(const Blob &b) const noexcept { return (memcmp(data,b.data,S) == 0); }
  31. ZT_INLINE bool operator!=(const Blob &b) const noexcept { return (memcmp(data,b.data,S) != 0); }
  32. ZT_INLINE bool operator<(const Blob &b) const noexcept { return (memcmp(data,b.data,S) < 0); }
  33. ZT_INLINE bool operator>(const Blob &b) const noexcept { return (memcmp(data,b.data,S) > 0); }
  34. ZT_INLINE bool operator<=(const Blob &b) const noexcept { return (memcmp(data,b.data,S) <= 0); }
  35. ZT_INLINE bool operator>=(const Blob &b) const noexcept { return (memcmp(data,b.data,S) >= 0); }
  36. };
  37. } // namespace ZeroTier
  38. #endif