PropertySerialiser: make into a singleton.

Also update UESaveFile to match.
This commit is contained in:
Guillaume Jacquemin 2022-03-06 13:21:56 +01:00
parent 771e008e62
commit fdb7567aea
4 changed files with 17 additions and 6 deletions

View file

@ -65,6 +65,11 @@ PropertySerialiser::PropertySerialiser() {
arrayAppend(_collectionSerialisers, Containers::pointer<StructSerialiser>()); arrayAppend(_collectionSerialisers, Containers::pointer<StructSerialiser>());
} }
auto PropertySerialiser::instance() -> PropertySerialiser& {
static PropertySerialiser serialiser;
return serialiser;
}
auto PropertySerialiser::read(BinaryReader& reader) -> UnrealPropertyBase::ptr { auto PropertySerialiser::read(BinaryReader& reader) -> UnrealPropertyBase::ptr {
if(reader.peekChar() < 0 || reader.eof()) { if(reader.peekChar() < 0 || reader.eof()) {
return nullptr; return nullptr;
@ -233,8 +238,10 @@ auto PropertySerialiser::writeSet(Containers::ArrayView<UnrealPropertyBase::ptr>
} }
auto PropertySerialiser::getSerialiser(Containers::StringView item_type) -> AbstractUnrealPropertySerialiser* { auto PropertySerialiser::getSerialiser(Containers::StringView item_type) -> AbstractUnrealPropertySerialiser* {
!Utility::Debug{};
for(auto& item : _serialisers) { for(auto& item : _serialisers) {
for(Containers::StringView serialiser_type : item->types()) { for(auto serialiser_type : item->types()) {
Utility::Debug{} << serialiser_type;
if(item_type == serialiser_type) { if(item_type == serialiser_type) {
return item.get(); return item.get();
} }

View file

@ -32,7 +32,7 @@ class BinaryWriter;
class PropertySerialiser { class PropertySerialiser {
public: public:
PropertySerialiser(); static auto instance() -> PropertySerialiser&;
auto read(BinaryReader& reader) -> UnrealPropertyBase::ptr; auto read(BinaryReader& reader) -> UnrealPropertyBase::ptr;
auto readItem(BinaryReader& reader, Containers::String type, UnsignedLong value_length, auto readItem(BinaryReader& reader, Containers::String type, UnsignedLong value_length,
@ -50,6 +50,8 @@ class PropertySerialiser {
UnsignedLong& bytes_written, BinaryWriter& writer) -> bool; UnsignedLong& bytes_written, BinaryWriter& writer) -> bool;
private: private:
PropertySerialiser();
auto getSerialiser(Containers::StringView item_type) -> AbstractUnrealPropertySerialiser*; auto getSerialiser(Containers::StringView item_type) -> AbstractUnrealPropertySerialiser*;
auto getCollectionSerialiser(Containers::StringView item_type) -> AbstractUnrealCollectionPropertySerialiser*; auto getCollectionSerialiser(Containers::StringView item_type) -> AbstractUnrealCollectionPropertySerialiser*;

View file

@ -25,7 +25,8 @@
using namespace Containers::Literals; using namespace Containers::Literals;
UESaveFile::UESaveFile(Containers::String filepath) UESaveFile::UESaveFile(Containers::String filepath):
_propSerialiser{PropertySerialiser::instance()}
{ {
_filepath = std::move(filepath); _filepath = std::move(filepath);
@ -108,7 +109,7 @@ auto UESaveFile::saveToFile() -> bool {
for(auto& prop : _properties) { for(auto& prop : _properties) {
UnsignedLong bytes_written = 0; UnsignedLong bytes_written = 0;
if(!_propSerialiser.write(prop, bytes_written, writer)) { if(!_propSerialiser->write(prop, bytes_written, writer)) {
_lastError = "Couldn't write the property "_s + *prop->name + " to the array."_s; _lastError = "Couldn't write the property "_s + *prop->name + " to the array."_s;
return false; return false;
} }
@ -213,7 +214,7 @@ void UESaveFile::loadData() {
} }
UnrealPropertyBase::ptr prop; UnrealPropertyBase::ptr prop;
while((prop = _propSerialiser.read(reader)) != nullptr) { while((prop = _propSerialiser->read(reader)) != nullptr) {
arrayAppend(_properties, std::move(prop)); arrayAppend(_properties, std::move(prop));
} }

View file

@ -18,6 +18,7 @@
#include <Corrade/Containers/Array.h> #include <Corrade/Containers/Array.h>
#include <Corrade/Containers/GrowableArray.h> #include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Containers/Reference.h>
#include <Corrade/Containers/StaticArray.h> #include <Corrade/Containers/StaticArray.h>
#include <Corrade/Containers/String.h> #include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringView.h> #include <Corrade/Containers/StringView.h>
@ -90,5 +91,5 @@ class UESaveFile {
Containers::Array<UnrealPropertyBase::ptr> _properties; Containers::Array<UnrealPropertyBase::ptr> _properties;
PropertySerialiser _propSerialiser; Containers::Reference<PropertySerialiser> _propSerialiser;
}; };