Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/StateRecorderImpl.cpp

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 #include <Jolt/Jolt.h>
6
7 #include <Jolt/Physics/StateRecorderImpl.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 void StateRecorderImpl::WriteBytes(const void *inData, size_t inNumBytes)
12 {
13 mStream.write((const char *)inData, inNumBytes);
14 }
15
16 void StateRecorderImpl::Rewind()
17 {
18 mStream.seekg(0, std::stringstream::beg);
19 }
20
21 void StateRecorderImpl::Clear()
22 {
23 mStream.clear();
24 mStream.str({});
25 }
26
27 void StateRecorderImpl::ReadBytes(void *outData, size_t inNumBytes)
28 {
29 if (IsValidating())
30 {
31 // Read data in temporary buffer to compare with current value
32 void *data = JPH_STACK_ALLOC(inNumBytes);
33 mStream.read((char *)data, inNumBytes);
34 if (memcmp(data, outData, inNumBytes) != 0)
35 {
36 // Mismatch, print error
37 Trace("Mismatch reading %u bytes", (uint)inNumBytes);
38 for (size_t i = 0; i < inNumBytes; ++i)
39 {
40 int b1 = reinterpret_cast<uint8 *>(outData)[i];
41 int b2 = reinterpret_cast<uint8 *>(data)[i];
42 if (b1 != b2)
43 Trace("Offset %d: %02X -> %02X", i, b1, b2);
44 }
45 JPH_BREAKPOINT;
46 }
47
48 // Copy the temporary data to the final destination
49 memcpy(outData, data, inNumBytes);
50 return;
51 }
52
53 mStream.read((char *)outData, inNumBytes);
54 }
55
56 bool StateRecorderImpl::IsEqual(StateRecorderImpl &inReference)
57 {
58 // Get length of new state
59 mStream.seekg(0, std::stringstream::end);
60 std::streamoff this_len = mStream.tellg();
61 mStream.seekg(0, std::stringstream::beg);
62
63 // Get length of old state
64 inReference.mStream.seekg(0, std::stringstream::end);
65 std::streamoff reference_len = inReference.mStream.tellg();
66 inReference.mStream.seekg(0, std::stringstream::beg);
67
68 // Compare size
69 bool fail = reference_len != this_len;
70 if (fail)
71 {
72 Trace("Failed to properly recover state, different stream length!");
73 return false;
74 }
75
76 // Compare byte by byte
77 for (std::streamoff i = 0, l = this_len; !fail && i < l; ++i)
78 {
79 fail = inReference.mStream.get() != mStream.get();
80 if (fail)
81 {
82 Trace("Failed to properly recover state, different at offset %d!", (int)i);
83 return false;
84 }
85 }
86
87 return true;
88 }
89
90 JPH_NAMESPACE_END