| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/Result.h> |
| 8 |
#include <Jolt/Core/StreamIn.h> |
| 9 |
#include <Jolt/Core/StreamOut.h> |
| 10 |
#include <Jolt/Core/UnorderedMap.h> |
| 11 |
#include <Jolt/Core/Factory.h> |
| 12 |
|
| 13 |
JPH_NAMESPACE_BEGIN |
| 14 |
|
| 15 |
namespace StreamUtils { |
| 16 |
|
| 17 |
template <class Type> |
| 18 |
using ObjectToIDMap = UnorderedMap<const Type *, uint32>; |
| 19 |
|
| 20 |
template <class Type> |
| 21 |
using IDToObjectMap = Array<Ref<Type>>; |
| 22 |
|
| 23 |
|
| 24 |
template <class Type> |
| 25 |
Result<Ref<Type>> RestoreObject(StreamIn &inStream, void (Type::*inRestoreBinaryStateFunction)(StreamIn &)) |
| 26 |
{ |
| 27 |
Result<Ref<Type>> result; |
| 28 |
|
| 29 |
|
| 30 |
uint32 hash; |
| 31 |
inStream.Read(hash); |
| 32 |
if (inStream.IsEOF() || inStream.IsFailed()) |
| 33 |
{ |
| 34 |
result.SetError("Failed to read type hash"); |
| 35 |
return result; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
const RTTI *rtti = Factory::sInstance->Find(hash); |
| 40 |
if (rtti == nullptr) |
| 41 |
{ |
| 42 |
result.SetError("Failed to create instance of type"); |
| 43 |
return result; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
Ref<Type> object = reinterpret_cast<Type *>(rtti->CreateObject()); |
| 48 |
(object->*inRestoreBinaryStateFunction)(inStream); |
| 49 |
if (inStream.IsEOF() || inStream.IsFailed()) |
| 50 |
{ |
| 51 |
result.SetError("Failed to restore object"); |
| 52 |
return result; |
| 53 |
} |
| 54 |
|
| 55 |
result.Set(object); |
| 56 |
return result; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
template <class Type> |
| 61 |
void SaveObjectReference(StreamOut &inStream, const Type *inObject, ObjectToIDMap<Type> *ioObjectToIDMap) |
| 62 |
{ |
| 63 |
if (ioObjectToIDMap == nullptr || inObject == nullptr) |
| 64 |
{ |
| 65 |
|
| 66 |
inStream.Write(~uint32(0)); |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
typename ObjectToIDMap<Type>::const_iterator id = ioObjectToIDMap->find(inObject); |
| 71 |
if (id != ioObjectToIDMap->end()) |
| 72 |
{ |
| 73 |
|
| 74 |
inStream.Write(id->second); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
|
| 79 |
uint32 new_id = uint32(ioObjectToIDMap->size()); |
| 80 |
(*ioObjectToIDMap)[inObject] = new_id; |
| 81 |
inStream.Write(new_id); |
| 82 |
|
| 83 |
|
| 84 |
inObject->SaveBinaryState(inStream); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
template <class Type> |
| 91 |
Result<Ref<Type>> RestoreObjectReference(StreamIn &inStream, IDToObjectMap<Type> &ioIDToObjectMap) |
| 92 |
{ |
| 93 |
Result<Ref<Type>> result; |
| 94 |
|
| 95 |
|
| 96 |
uint32 id = ~uint32(0); |
| 97 |
inStream.Read(id); |
| 98 |
|
| 99 |
|
| 100 |
if (id == ~uint32(0)) |
| 101 |
{ |
| 102 |
result.Set(nullptr); |
| 103 |
return result; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
if (id >= ioIDToObjectMap.size()) |
| 108 |
{ |
| 109 |
|
| 110 |
result = Type::sRestoreFromBinaryState(inStream); |
| 111 |
if (result.HasError()) |
| 112 |
return result; |
| 113 |
JPH_ASSERT(id == ioIDToObjectMap.size()); |
| 114 |
ioIDToObjectMap.push_back(result.Get()); |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
|
| 119 |
result.Set(ioIDToObjectMap[id].GetPtr()); |
| 120 |
} |
| 121 |
|
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
template <class ArrayType, class ValueType> |
| 127 |
void SaveObjectArray(StreamOut &inStream, const ArrayType &inArray, ObjectToIDMap<ValueType> *ioObjectToIDMap) |
| 128 |
{ |
| 129 |
uint32 len = uint32(inArray.size()); |
| 130 |
inStream.Write(len); |
| 131 |
for (const ValueType *value: inArray) |
| 132 |
SaveObjectReference(inStream, value, ioObjectToIDMap); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
template <class ArrayType, class ValueType> |
| 137 |
Result<ArrayType> RestoreObjectArray(StreamIn &inStream, IDToObjectMap<ValueType> &ioIDToObjectMap) |
| 138 |
{ |
| 139 |
Result<ArrayType> result; |
| 140 |
|
| 141 |
uint32 len; |
| 142 |
inStream.Read(len); |
| 143 |
if (inStream.IsEOF() || inStream.IsFailed()) |
| 144 |
{ |
| 145 |
result.SetError("Failed to read stream"); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
ArrayType values; |
| 150 |
values.reserve(len); |
| 151 |
for (size_t i = 0; i < len; ++i) |
| 152 |
{ |
| 153 |
Result value = RestoreObjectReference(inStream, ioIDToObjectMap); |
| 154 |
if (value.HasError()) |
| 155 |
{ |
| 156 |
result.SetError(value.GetError()); |
| 157 |
return result; |
| 158 |
} |
| 159 |
values.push_back(std::move(value.Get())); |
| 160 |
} |
| 161 |
|
| 162 |
result.Set(values); |
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
JPH_NAMESPACE_END |