[LYN-9039] Object stream write response change (#7085)
* Updated the ObjectStreamWriteOverride Callback to return a ObjectStreamWriteOverrideResponse. The ObjectStreamWriteOverrideResponse allows the callback to indicate that the default ObjectStream::WriteElement behavior should occur if the callback hasn't implemented the write itself. Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> * Fixed forward declaration not matching actual enum class name Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com> * Add unit tests Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com> * Remove unused member Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com> * Add virtual destructor for test class Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com> * Change string literal to not have tabs in it Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com> Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
This commit is contained in:
@@ -1668,9 +1668,23 @@ namespace AZ
|
||||
SerializeContext::ENUM_ACCESS_FOR_READ,
|
||||
&m_errorLogger
|
||||
);
|
||||
if (objectStreamWriteOverrideCB.Invoke<void>(callContext, objectPtr, *classData, classElement))
|
||||
if (ObjectStreamWriteOverrideResponse writeResponse;
|
||||
objectStreamWriteOverrideCB.Read<ObjectStreamWriteOverrideResponse>(writeResponse, callContext, objectPtr, *classData, classElement))
|
||||
{
|
||||
return false;
|
||||
switch (writeResponse)
|
||||
{
|
||||
case ObjectStreamWriteOverrideResponse::FallbackToDefaultWrite:
|
||||
break;
|
||||
case ObjectStreamWriteOverrideResponse::AbortWrite:
|
||||
m_errorLogger.ReportError(AZStd::string::format("ObjectStream Write Element Override callback has aborted the write for class data %s",
|
||||
classData->m_name).c_str());
|
||||
[[fallthrough]];
|
||||
case ObjectStreamWriteOverrideResponse::CompletedWrite:
|
||||
return false;
|
||||
default:
|
||||
AZ_Error("Serialize", false, "Invalid Response %d returned from the ObjectStream Write Element Override callback", static_cast<int>(writeResponse));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -49,14 +49,24 @@ namespace AZ
|
||||
static const AZ::Crc32 ObjectStreamWriteElementOverride = AZ_CRC("ObjectStreamWriteElementOverride", 0x35eb659f);
|
||||
}
|
||||
|
||||
enum class ObjectStreamWriteOverrideResponse
|
||||
{
|
||||
CompletedWrite,
|
||||
FallbackToDefaultWrite,
|
||||
AbortWrite
|
||||
};
|
||||
AZ_TYPE_INFO_SPECIALIZE(ObjectStreamWriteOverrideResponse, "{BDF960A8-0F18-4E9D-96DA-F800A122C42D}");
|
||||
|
||||
///< Callback that the object stream invokes to override saving an instance of the registered class
|
||||
///< @param callContext EnumerateInstanceCallContext which contains the WriteElement BeingElemCB and the CloseElement EndElemCB
|
||||
///< the callContext parameter can be passed to the SerializeContext::EnumerateInstance to continue object stream writing
|
||||
///< @param classPtr class type which is of pointer to the type represented by the m_typeId value
|
||||
///< @param classData reference to this instance Class Data that will be supplied to the callback
|
||||
///< @param classElement class element pointer which contains information about the element being serialized.
|
||||
///< root elements do not not have a valid class element pointer
|
||||
using ObjectStreamWriteOverrideCB = AZStd::function<void(SerializeContext::EnumerateInstanceCallContext& callContext,
|
||||
///< root elements have a nullptr classElement
|
||||
///< @return enum to indicate that the override has saved the registered class and that the default writing should be skipped.
|
||||
///< Returning false will have the WriteElement code fallback to using the default logic
|
||||
using ObjectStreamWriteOverrideCB = AZStd::function<ObjectStreamWriteOverrideResponse(SerializeContext::EnumerateInstanceCallContext& callContext,
|
||||
const void* classPtr, const SerializeContext::ClassData& classData, const SerializeContext::ClassElement* classElement)>;
|
||||
|
||||
AZ_TYPE_INFO_SPECIALIZE(ObjectStreamWriteOverrideCB, "{87B1A36B-8C8A-42B6-A0B5-E770D9FDBAD4}");
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
enum class ObjectStreamWriteOverrideResponse;
|
||||
|
||||
namespace VariantSerializationInternal
|
||||
{
|
||||
template <class ValueType>
|
||||
@@ -480,7 +482,7 @@ namespace AZ
|
||||
}
|
||||
}
|
||||
private:
|
||||
static void ObjectStreamWriter(SerializeContext::EnumerateInstanceCallContext& callContext, const void* variantPtr,
|
||||
static ObjectStreamWriteOverrideResponse ObjectStreamWriter(SerializeContext::EnumerateInstanceCallContext& callContext, const void* variantPtr,
|
||||
[[maybe_unused]] const SerializeContext::ClassData& variantClassData, const SerializeContext::ClassElement* variantClassElement)
|
||||
{
|
||||
auto alternativeVisitor = [&callContext, variantClassElement](auto&& elementAlt)
|
||||
@@ -503,6 +505,9 @@ namespace AZ
|
||||
};
|
||||
|
||||
AZStd::visit(AZStd::move(alternativeVisitor), *reinterpret_cast<const VariantType*>(variantPtr));
|
||||
// To avoid including ObjectStream.h into this file, we static cast the value of 0
|
||||
// to an AZ::ObjectStreamWriteElemntResponse which corresponds to the CompletedWrite enum value
|
||||
return static_cast<AZ::ObjectStreamWriteOverrideResponse>(0);
|
||||
}
|
||||
|
||||
VariantSerializationInternal::AZStdVariantContainer<Types...> m_variantContainer;
|
||||
|
||||
Reference in New Issue
Block a user