reinterpret_bytes.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_UTIL_MISC_REINTERPRET_BYTES_H_
  15. #define CRASHPAD_UTIL_MISC_REINTERPRET_BYTES_H_
  16. #include <stddef.h>
  17. namespace crashpad {
  18. namespace internal {
  19. bool ReinterpretBytesImpl(const char* from,
  20. size_t from_size,
  21. char* to,
  22. size_t to_size);
  23. } // namespace internal
  24. //! \brief Copies the bytes of \a from to \a to.
  25. //!
  26. //! This function is similar to `bit_cast`, except that it can operate on
  27. //! differently sized types.
  28. //!
  29. //! \return `true` if the copy is possible without information loss, otherwise
  30. //! `false` with a message logged.
  31. template <typename From, typename To>
  32. bool ReinterpretBytes(const From& from, To* to) {
  33. return internal::ReinterpretBytesImpl(reinterpret_cast<const char*>(&from),
  34. sizeof(From),
  35. reinterpret_cast<char*>(to),
  36. sizeof(To));
  37. }
  38. } // namespace crashpad
  39. #endif // CRASHPAD_UTIL_MISC_REINTERPRET_BYTES_H_