| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/NonCopyable.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
|
| 12 |
class JPH_EXPORT StreamOut : public NonCopyable |
| 13 |
{ |
| 14 |
public: |
| 15 |
|
| 16 |
virtual ~StreamOut() = default; |
| 17 |
|
| 18 |
|
| 19 |
virtual void WriteBytes(const void *inData, size_t inNumBytes) = 0; |
| 20 |
|
| 21 |
|
| 22 |
virtual bool IsFailed() const = 0; |
| 23 |
|
| 24 |
|
| 25 |
template <class T, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true> |
| 26 |
void Write(const T &inT) |
| 27 |
{ |
| 28 |
WriteBytes(&inT, sizeof(inT)); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
template <class T, class A, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true> |
| 33 |
void Write(const Array<T, A> &inT) |
| 34 |
{ |
| 35 |
uint32 len = uint32(inT.size()); |
| 36 |
Write(len); |
| 37 |
if (!IsFailed()) |
| 38 |
{ |
| 39 |
if constexpr (std::is_same_v<T, Vec3> || std::is_same_v<T, DVec3> || std::is_same_v<T, DMat44>) |
| 40 |
{ |
| 41 |
|
| 42 |
for (typename Array<T, A>::size_type i = 0; i < len; ++i) |
| 43 |
Write(inT[i]); |
| 44 |
} |
| 45 |
else |
| 46 |
{ |
| 47 |
|
| 48 |
WriteBytes(inT.data(), len * sizeof(T)); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
template <class Type, class Traits, class Allocator> |
| 55 |
void Write(const std::basic_string<Type, Traits, Allocator> &inString) |
| 56 |
{ |
| 57 |
uint32 len = uint32(inString.size()); |
| 58 |
Write(len); |
| 59 |
if (!IsFailed()) |
| 60 |
WriteBytes(inString.data(), len * sizeof(Type)); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
template <class T, class A, typename F> |
| 65 |
void Write(const Array<T, A> &inT, const F &inWriteElement) |
| 66 |
{ |
| 67 |
uint32 len = uint32(inT.size()); |
| 68 |
Write(len); |
| 69 |
if (!IsFailed()) |
| 70 |
for (typename Array<T, A>::size_type i = 0; i < len; ++i) |
| 71 |
inWriteElement(inT[i], *this); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
void Write(const Vec3 &inVec) |
| 76 |
{ |
| 77 |
WriteBytes(&inVec, 3 * sizeof(float)); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
void Write(const DVec3 &inVec) |
| 82 |
{ |
| 83 |
WriteBytes(&inVec, 3 * sizeof(double)); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
void Write(const DMat44 &inVec) |
| 88 |
{ |
| 89 |
Write(inVec.GetColumn4(0)); |
| 90 |
Write(inVec.GetColumn4(1)); |
| 91 |
Write(inVec.GetColumn4(2)); |
| 92 |
|
| 93 |
Write(inVec.GetTranslation()); |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
JPH_NAMESPACE_END |