StreamOut.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. };
  46. JPH_NAMESPACE_END