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
11{
12public:
14 virtual ~StreamIn() = default;
15
17 virtual void ReadBytes(void *outData, size_t inNumBytes) = 0;
18
20 virtual bool IsEOF() const = 0;
21
23 virtual bool IsFailed() const = 0;
24
26 template <class T>
27 void Read(T &outT)
28 {
29 ReadBytes(&outT, sizeof(outT));
30 }
31
33 template <class T, class A>
34 void Read(std::vector<T, A> &outT)
35 {
36 typename Array<T>::size_type len = outT.size(); // Initialize to previous array size, this is used for validation in the StateRecorder class
37 Read(len);
38 if (!IsEOF() && !IsFailed())
39 {
40 outT.resize(len);
41 for (typename Array<T>::size_type i = 0; i < len; ++i)
42 Read(outT[i]);
43 }
44 else
45 outT.clear();
46 }
47
49 template <class Type, class Traits, class Allocator>
50 void Read(std::basic_string<Type, Traits, Allocator> &outString)
51 {
52 typename std::basic_string<Type, Traits, Allocator>::size_type len = 0;
53 Read(len);
54 if (!IsEOF() && !IsFailed())
55 {
56 outString.resize(len);
57 ReadBytes(outString.data(), len * sizeof(Type));
58 }
59 else
60 outString.clear();
61 }
62
64 void Read(Vec3 &outVec)
65 {
66 ReadBytes(&outVec, 3 * sizeof(float));
67 outVec = Vec3::sFixW(outVec.mValue);
68 }
69
71 void Read(DVec3 &outVec)
72 {
73 ReadBytes(&outVec, 3 * sizeof(double));
74 outVec = DVec3::sFixW(outVec.mValue);
75 }
76
78 void Read(DMat44 &outVec)
79 {
80 Vec4 x, y, z;
81 Read(x);
82 Read(y);
83 Read(z);
84
85 DVec3 t;
86 Read(t);
87
88 outVec = DMat44(x, y, z, t);
89 }
90};
91
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
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:275
Simple binary input stream.
Definition: StreamIn.h:11
virtual ~StreamIn()=default
Virtual destructor.
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition: StreamIn.h:27
void Read(std::vector< T, A > &outT)
Read a vector of primitives from the binary stream.
Definition: StreamIn.h:34
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:50
void Read(Vec3 &outVec)
Read a Vec3 (don't read W)
Definition: StreamIn.h:64
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:78
void Read(DVec3 &outVec)
Read a DVec3 (don't read W)
Definition: StreamIn.h:71
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:281
Definition: Vec4.h:14