StreamOut.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// Simple binary output stream
  6. class StreamOut
  7. {
  8. public:
  9. /// Virtual destructor
  10. virtual ~StreamOut() = default;
  11. /// Write a string of bytes to the binary stream
  12. virtual void WriteBytes(const void *inData, size_t inNumBytes) = 0;
  13. /// Returns true if there was an IO failure
  14. virtual bool IsFailed() const = 0;
  15. /// Write a primitive (e.g. float, int, etc.) to the binary stream
  16. template <class T>
  17. void Write(const T &inT)
  18. {
  19. WriteBytes(&inT, sizeof(inT));
  20. }
  21. /// Write a vector of primitives from the binary stream
  22. template <class T, class A>
  23. void Write(const std::vector<T, A> &inT)
  24. {
  25. typename Array<T>::size_type len = inT.size();
  26. Write(len);
  27. if (!IsFailed())
  28. for (typename Array<T>::size_type i = 0; i < len; ++i)
  29. Write(inT[i]);
  30. }
  31. /// Write a string to the binary stream (writes the number of characters and then the characters)
  32. template <class Type, class Traits, class Allocator>
  33. void Write(const std::basic_string<Type, Traits, Allocator> &inString)
  34. {
  35. typename std::basic_string<Type, Traits, Allocator>::size_type len = inString.size();
  36. Write(len);
  37. if (!IsFailed())
  38. WriteBytes(inString.data(), len * sizeof(Type));
  39. }
  40. /// Write a Vec3 (don't write W)
  41. void Write(const Vec3 &inVec)
  42. {
  43. WriteBytes(&inVec, 3 * sizeof(float));
  44. }
  45. /// Write a DVec3 (don't write W)
  46. void Write(const DVec3 &inVec)
  47. {
  48. WriteBytes(&inVec, 3 * sizeof(double));
  49. }
  50. /// Write a DMat44 (don't write W component of translation)
  51. void Write(const DMat44 &inVec)
  52. {
  53. Write(inVec.GetAxisX());
  54. Write(inVec.GetAxisY());
  55. Write(inVec.GetAxisZ());
  56. Write(inVec.GetTranslation());
  57. }
  58. };
  59. JPH_NAMESPACE_END