StreamOut.h 1.9 KB

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