From c20bf355152d9a715dae3c19777c3d4a1046174b Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Sat, 8 Feb 2025 10:40:44 -0800 Subject: [SharedCache] Make MetadataSerializable::Load static `MetadataSerializable` is updated to allow a subclass to optionally specify the return type of `Load`. If not specified it defaults to the subclass itself. `SharedCache` specifies `std::optional` to represent the case where the serialized metadata is in a format or version it does not recognize. By making `Load` a static member function and having it return a new object it becomes easier to reason about the state of objects when a deserialization failure occurs. It can return `std::optional` to indicate failure and there is no object left in an unknown state. Prior to this, `Load` worked on an existing instance of an object. It was unclear what state the object would be in if a deserialization failure occurred (its original state prior to `Load`? some partially-loaded state? a null state?). The failure itself also had to be communicated out of band. --- view/sharedcache/api/sharedcache.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'view/sharedcache/api/sharedcache.cpp') diff --git a/view/sharedcache/api/sharedcache.cpp b/view/sharedcache/api/sharedcache.cpp index e764531c..472bb024 100644 --- a/view/sharedcache/api/sharedcache.cpp +++ b/view/sharedcache/api/sharedcache.cpp @@ -204,9 +204,8 @@ namespace SharedCacheAPI { BNFreeString(outputStr); if (output.empty()) return {}; - SharedCacheMachOHeader header; - header.LoadFromString(output); - return header; + + return SharedCacheMachOHeader::LoadFromString(output); } std::optional SharedCache::GetMachOHeaderForAddress(uint64_t address) @@ -218,9 +217,7 @@ namespace SharedCacheAPI { BNFreeString(outputStr); if (output.empty()) return {}; - SharedCacheMachOHeader header; - header.LoadFromString(output); - return header; + return SharedCacheMachOHeader::LoadFromString(output); } BNDSCViewState SharedCache::GetState() -- cgit v1.3.1