Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
513 changes: 430 additions & 83 deletions crates/codegen/src/unrealcpp.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,62 @@ namespace UE::SpacetimeDB {
/** @} */ // end of CustomOptionalStructSerialization group


// =============================================================================
// Custom Result struct Serialization
// =============================================================================
/**
* @brief Helper macro to generate serialization for Blueprint compatible result structs
* @{
*
* This macro creates serialize/deserialize specializations for a struct that
* mimics the behaviour of Result types by exposing an "is ok" boolean flag,
* an "ok" value field, and an "error" value field.
*
* @param StructType Name of the result wrapper struct
* @param IsOkField Name of the boolean member that signals if the result is ok
* @param OkValueField Name of the value member for the ok case
* @param ErrValueField Name of the value member for the error case
*
* Example usage:
* @code
* USTRUCT(BlueprintType)
* struct FResultStringBool {
* int32 OkValue;
* FString ErrorValue;
* bool bIsOk;
* };
*
* namespace UE::SpacetimeDB {
* UE_SPACETIMEDB_RESULT(FResultStringBool, bIsOk, OkValue, ErrorValue);
* }
* @endcode
*/
#define UE_SPACETIMEDB_RESULT(StructType, IsOkField, OkValueField, ErrValueField) \
template<> inline void serialize<StructType>(UEWriter& w, const StructType& value) { \
if (value.IsOkField) { \
w.write_u8(0); /* ok tag */ \
serialize(w, value.OkValueField); \
} else { \
w.write_u8(1); /* err tag */ \
serialize(w, value.ErrValueField); \
} \
} \
template<> inline StructType deserialize<StructType>(UEReader& r) { \
StructType result; \
uint8_t tag = r.read_u8(); \
if (tag == 0) { \
result.IsOkField = true; \
result.OkValueField = deserialize<decltype(result.OkValueField)>(r); \
} else if (tag == 1) { \
result.IsOkField = false; \
result.ErrValueField = deserialize<decltype(result.ErrValueField)>(r); \
} else { \
ensureMsgf(false, TEXT("Invalid result tag: %d"), tag); \
return StructType(); \
} \
return result; \
}

// =============================================================================
// UE Utility Type Serialization
// =============================================================================
Expand Down
Loading
Loading