Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
StreamIn.h
Go to the documentation of this file.
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
8
10
13{
14public:
16 virtual ~StreamIn() = default;
17
19 virtual void ReadBytes(void *outData, size_t inNumBytes) = 0;
20
22 virtual bool IsEOF() const = 0;
23
25 virtual bool IsFailed() const = 0;
26
28 template <class T, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true>
29 void Read(T &outT)
30 {
31 ReadBytes(&outT, sizeof(outT));
32 }
33
35 template <class T, class A, std::enable_if_t<std::is_trivially_copyable_v<T>, bool> = true>
36 void Read(Array<T, A> &outT)
37 {
38 uint32 len = uint32(outT.size()); // Initialize to previous array size, this is used for validation in the StateRecorder class
39 Read(len);
40 if (!IsEOF() && !IsFailed())
41 {
42 outT.resize(len);
43 if constexpr (std::is_same_v<T, Vec3> || std::is_same_v<T, DVec3> || std::is_same_v<T, DMat44>)
44 {
45 // These types have unused components that we don't want to read
46 for (typename Array<T, A>::size_type i = 0; i < len; ++i)
47 Read(outT[i]);
48 }
49 else
50 {
51 // Read all elements at once
52 ReadBytes(outT.data(), len * sizeof(T));
53 }
54 }
55 else
56 outT.clear();
57 }
58
60 template <class Type, class Traits, class Allocator>
61 void Read(std::basic_string<Type, Traits, Allocator> &outString)
62 {
63 uint32 len = 0;
64 Read(len);
65 if (!IsEOF() && !IsFailed())
66 {
67 outString.resize(len);
68 ReadBytes(outString.data(), len * sizeof(Type));
69 }
70 else
71 outString.clear();
72 }
73
75 template <class T, class A, typename F>
76 void Read(Array<T, A> &outT, const F &inReadElement)
77 {
78 uint32 len = uint32(outT.size()); // Initialize to previous array size, this is used for validation in the StateRecorder class
79 Read(len);
80 if (!IsEOF() && !IsFailed())
81 {
82 outT.resize(len);
83 for (typename Array<T, A>::size_type i = 0; i < len; ++i)
84 inReadElement(*this, outT[i]);
85 }
86 else
87 outT.clear();
88 }
89
91 void Read(Vec3 &outVec)
92 {
93 ReadBytes(&outVec, 3 * sizeof(float));
94 outVec = Vec3::sFixW(outVec.mValue);
95 }
96
98 void Read(DVec3 &outVec)
99 {
100 ReadBytes(&outVec, 3 * sizeof(double));
101 outVec = DVec3::sFixW(outVec.mValue);
102 }
103
105 void Read(DMat44 &outVec)
106 {
107 Vec4 x, y, z;
108 Read(x);
109 Read(y);
110 Read(z);
111
112 DVec3 t;
113 Read(t);
114
115 outVec = DMat44(x, y, z, t);
116 }
117};
118
#define JPH_EXPORT
Definition Core.h:271
#define JPH_NAMESPACE_END
Definition Core.h:414
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
Definition Array.h:36
void resize(size_type inNewSize)
Resize array to new length.
Definition Array.h:120
size_t size_type
Definition Array.h:40
const T * data() const
Definition Array.h:444
size_type size() const
Returns amount of elements in the array.
Definition Array.h:320
void clear()
Destruct all elements and set length to zero.
Definition Array.h:145
Holds a 4x4 matrix of floats with the last column consisting of doubles.
Definition DMat44.h:13
Definition DVec3.h:14
static JPH_INLINE Type sFixW(TypeArg inValue)
Internal helper function that ensures that the Z component is replicated to the W component to preven...
Definition DVec3.inl:92
Type mValue
Definition DVec3.h:279
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Simple binary input stream.
Definition StreamIn.h:13
virtual ~StreamIn()=default
Virtual destructor.
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition StreamIn.h:29
void Read(Array< T, A > &outT, const F &inReadElement)
Read a vector of primitives from the binary stream using a custom function to read the elements.
Definition StreamIn.h:76
void Read(std::basic_string< Type, Traits, Allocator > &outString)
Read a string from the binary stream (reads the number of characters and then the characters)
Definition StreamIn.h:61
void Read(Vec3 &outVec)
Read a Vec3 (don't read W)
Definition StreamIn.h:91
virtual void ReadBytes(void *outData, size_t inNumBytes)=0
Read a string of bytes from the binary stream.
virtual bool IsEOF() const =0
Returns true when an attempt has been made to read past the end of the file.
virtual bool IsFailed() const =0
Returns true if there was an IO failure.
void Read(DMat44 &outVec)
Read a DMat44 (don't read W component of translation)
Definition StreamIn.h:105
void Read(Array< T, A > &outT)
Read a vector of primitives from the binary stream.
Definition StreamIn.h:36
void Read(DVec3 &outVec)
Read a DVec3 (don't read W)
Definition StreamIn.h:98
Definition Vec3.h:17
static JPH_INLINE Type sFixW(Type inValue)
Internal helper function that ensures that the Z component is replicated to the W component to preven...
Type mValue
Definition Vec3.h:286
Definition Vec4.h:14