Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/StreamIn.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Core/NonCopyable.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 /// Simple binary input stream
12 class JPH_EXPORT StreamIn : public NonCopyable
13 {
14 public:
15 /// Virtual destructor
16 virtual ~StreamIn() = default;
17
18 /// Read a string of bytes from the binary stream
19 virtual void ReadBytes(void *outData, size_t inNumBytes) = 0;
20
21 /// Returns true when an attempt has been made to read past the end of the file.
22 /// Note that this follows the convention of std::basic_ios::eof which only returns true when an attempt is made to read past the end, not when the read pointer is at the end.
23 virtual bool IsEOF() const = 0;
24
25 /// Returns true if there was an IO failure
26 virtual bool IsFailed() const = 0;
27
28 /// Read a primitive (e.g. float, int, etc.) from the binary stream
29 template <class T, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true>
30 void Read(T &outT)
31 {
32 ReadBytes(&outT, sizeof(outT));
33 }
34
35 /// Read a vector of primitives from the binary stream
36 template <class T, class A, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true>
37 void Read(Array<T, A> &outT)
38 {
39 uint32 len = uint32(outT.size()); // Initialize to previous array size, this is used for validation in the StateRecorder class
40 Read(len);
41 if (!IsEOF() && !IsFailed())
42 {
43 outT.resize(len);
44 if constexpr (std::is_same_v<T, Vec3> || std::is_same_v<T, DVec3> || std::is_same_v<T, DMat44>)
45 {
46 // These types have unused components that we don't want to read
47 for (typename Array<T, A>::size_type i = 0; i < len; ++i)
48 Read(outT[i]);
49 }
50 else
51 {
52 // Read all elements at once
53 ReadBytes(outT.data(), len * sizeof(T));
54 }
55 }
56 else
57 outT.clear();
58 }
59
60 /// Read a string from the binary stream (reads the number of characters and then the characters)
61 template <class Type, class Traits, class Allocator>
62 void Read(std::basic_string<Type, Traits, Allocator> &outString)
63 {
64 uint32 len = 0;
65 Read(len);
66 if (!IsEOF() && !IsFailed())
67 {
68 outString.resize(len);
69 ReadBytes(outString.data(), len * sizeof(Type));
70 }
71 else
72 outString.clear();
73 }
74
75 /// Read a vector of primitives from the binary stream using a custom function to read the elements
76 template <class T, class A, typename F>
77 void Read(Array<T, A> &outT, const F &inReadElement)
78 {
79 uint32 len = uint32(outT.size()); // Initialize to previous array size, this is used for validation in the StateRecorder class
80 Read(len);
81 if (!IsEOF() && !IsFailed())
82 {
83 outT.resize(len);
84 for (typename Array<T, A>::size_type i = 0; i < len; ++i)
85 inReadElement(*this, outT[i]);
86 }
87 else
88 outT.clear();
89 }
90
91 /// Read a Vec3 (don't read W)
92 void Read(Vec3 &outVec)
93 {
94 ReadBytes(&outVec, 3 * sizeof(float));
95 outVec = Vec3::sFixW(outVec.mValue);
96 }
97
98 /// Read a DVec3 (don't read W)
99 void Read(DVec3 &outVec)
100 {
101 ReadBytes(&outVec, 3 * sizeof(double));
102 outVec = DVec3::sFixW(outVec.mValue);
103 }
104
105 /// Read a DMat44 (don't read W component of translation)
106 void Read(DMat44 &outVec)
107 {
108 Vec4 x, y, z;
109 Read(x);
110 Read(y);
111 Read(z);
112
113 DVec3 t;
114 Read(t);
115
116 outVec = DMat44(x, y, z, t);
117 }
118 };
119
120 JPH_NAMESPACE_END