Path: blob/master/thirdparty/jolt_physics/Jolt/ObjectStream/SerializableAttribute.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#ifdef JPH_OBJECT_STREAM78JPH_NAMESPACE_BEGIN910class RTTI;11class IObjectStreamIn;12class IObjectStreamOut;1314/// Data type15enum class EOSDataType16{17/// Control codes18Declare, ///< Used to declare the attributes of a new object type19Object, ///< Start of a new object20Instance, ///< Used in attribute declaration, indicates that an object is an instanced attribute (no pointer)21Pointer, ///< Used in attribute declaration, indicates that an object is a pointer attribute22Array, ///< Used in attribute declaration, indicates that this is an array of objects2324// Basic types (primitives)25#define JPH_DECLARE_PRIMITIVE(name) T_##name,2627// This file uses the JPH_DECLARE_PRIMITIVE macro to define all types28#include <Jolt/ObjectStream/ObjectStreamTypes.h>2930// Error values for read functions31Invalid, ///< Next token on the stream was not a valid data type32};3334/// Attributes are members of classes that need to be serialized.35class SerializableAttribute36{37public:38///@ Serialization functions39using pGetMemberPrimitiveType = const RTTI * (*)();40using pIsType = bool (*)(int inArrayDepth, EOSDataType inDataType, const char *inClassName);41using pReadData = bool (*)(IObjectStreamIn &ioStream, void *inObject);42using pWriteData = void (*)(IObjectStreamOut &ioStream, const void *inObject);43using pWriteDataType = void (*)(IObjectStreamOut &ioStream);4445/// Constructor46SerializableAttribute(const char *inName, uint inMemberOffset, pGetMemberPrimitiveType inGetMemberPrimitiveType, pIsType inIsType, pReadData inReadData, pWriteData inWriteData, pWriteDataType inWriteDataType) : mName(inName), mMemberOffset(inMemberOffset), mGetMemberPrimitiveType(inGetMemberPrimitiveType), mIsType(inIsType), mReadData(inReadData), mWriteData(inWriteData), mWriteDataType(inWriteDataType) { }4748/// Construct from other attribute with base class offset49SerializableAttribute(const SerializableAttribute &inOther, int inBaseOffset) : mName(inOther.mName), mMemberOffset(inOther.mMemberOffset + inBaseOffset), mGetMemberPrimitiveType(inOther.mGetMemberPrimitiveType), mIsType(inOther.mIsType), mReadData(inOther.mReadData), mWriteData(inOther.mWriteData), mWriteDataType(inOther.mWriteDataType) { }5051/// Name of the attribute52void SetName(const char *inName) { mName = inName; }53const char * GetName() const { return mName; }5455/// Access to the memory location that contains the member56template <class T>57inline T * GetMemberPointer(void *inObject) const { return reinterpret_cast<T *>(reinterpret_cast<uint8 *>(inObject) + mMemberOffset); }58template <class T>59inline const T * GetMemberPointer(const void *inObject) const { return reinterpret_cast<const T *>(reinterpret_cast<const uint8 *>(inObject) + mMemberOffset); }6061/// In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)62const RTTI * GetMemberPrimitiveType() const63{64return mGetMemberPrimitiveType();65}6667/// Check if this attribute is of a specific type68bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const69{70return mIsType(inArrayDepth, inDataType, inClassName);71}7273/// Read the data for this attribute into attribute containing class inObject74bool ReadData(IObjectStreamIn &ioStream, void *inObject) const75{76return mReadData(ioStream, GetMemberPointer<void>(inObject));77}7879/// Write the data for this attribute from attribute containing class inObject80void WriteData(IObjectStreamOut &ioStream, const void *inObject) const81{82mWriteData(ioStream, GetMemberPointer<void>(inObject));83}8485/// Write the data type of this attribute to a stream86void WriteDataType(IObjectStreamOut &ioStream) const87{88mWriteDataType(ioStream);89}9091private:92// Name of the attribute93const char * mName;9495// Offset of the member relative to the class96uint mMemberOffset;9798// In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)99pGetMemberPrimitiveType mGetMemberPrimitiveType;100101// Serialization operations102pIsType mIsType;103pReadData mReadData;104pWriteData mWriteData;105pWriteDataType mWriteDataType;106};107108JPH_NAMESPACE_END109110#endif // JPH_OBJECT_STREAM111112113