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>
29 void Read(T &outT)
30 {
31 ReadBytes(&outT, sizeof(outT));
32 }
33
35 template <class T, class A>
36 void Read(std::vector<T, A> &outT)
37 {
38 typename Array<T>::size_type len = 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 for (typename Array<T>::size_type i = 0; i < len; ++i)
44 Read(outT[i]);
45 }
46 else
47 outT.clear();
48 }
49
51 template <class Type, class Traits, class Allocator>
52 void Read(std::basic_string<Type, Traits, Allocator> &outString)
53 {
54 typename std::basic_string<Type, Traits, Allocator>::size_type len = 0;
55 Read(len);
56 if (!IsEOF() && !IsFailed())
57 {
58 outString.resize(len);
59 ReadBytes(outString.data(), len * sizeof(Type));
60 }
61 else
62 outString.clear();
63 }
64
66 template <class T, class A, typename F>
67 void Read(std::vector<T, A> &outT, const F &inReadElement)
68 {
69 typename Array<T>::size_type len = outT.size(); // Initialize to previous array size, this is used for validation in the StateRecorder class
70 Read(len);
71 if (!IsEOF() && !IsFailed())
72 {
73 outT.resize(len);
74 for (typename Array<T>::size_type i = 0; i < len; ++i)
75 inReadElement(*this, outT[i]);
76 }
77 else
78 outT.clear();
79 }
80
82 void Read(Vec3 &outVec)
83 {
84 ReadBytes(&outVec, 3 * sizeof(float));
85 outVec = Vec3::sFixW(outVec.mValue);
86 }
87
89 void Read(DVec3 &outVec)
90 {
91 ReadBytes(&outVec, 3 * sizeof(double));
92 outVec = DVec3::sFixW(outVec.mValue);
93 }
94
96 void Read(DMat44 &outVec)
97 {
98 Vec4 x, y, z;
99 Read(x);
100 Read(y);
101 Read(z);
102
103 DVec3 t;
104 Read(t);
105
106 outVec = DMat44(x, y, z, t);
107 }
108};
109
#define JPH_EXPORT
Definition: Core.h:227
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
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(std::vector< T, A > &outT)
Read a vector of primitives from the binary stream.
Definition: StreamIn.h:36
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:52
void Read(std::vector< 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:67
void Read(Vec3 &outVec)
Read a Vec3 (don't read W)
Definition: StreamIn.h:82
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:96
void Read(DVec3 &outVec)
Read a DVec3 (don't read W)
Definition: StreamIn.h:89
Definition: Vec3.h:16
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:285
Definition: Vec4.h:14