| 1 | /* |
| 2 | * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. |
| 3 | * |
| 4 | * NVIDIA CORPORATION and its licensors retain all intellectual property |
| 5 | * and proprietary rights in and to this software, related documentation |
| 6 | * and any modifications thereto. Any use, reproduction, disclosure or |
| 7 | * distribution of this software and related documentation without an express |
| 8 | * license agreement from NVIDIA CORPORATION is strictly prohibited. |
| 9 | */ |
| 10 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 11 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 12 | #ifndef PX_REPX_SERIALIZER_H |
| 13 | #define PX_REPX_SERIALIZER_H |
| 14 | /** \addtogroup Serializers |
| 15 | @{ |
| 16 | */ |
| 17 | |
| 18 | #include "common/PxBase.h" |
| 19 | #include "extensions/PxRepXSimpleType.h" |
| 20 | |
| 21 | #ifndef PX_DOXYGEN |
| 22 | namespace physx |
| 23 | { |
| 24 | #endif |
| 25 | |
| 26 | class XmlMemoryAllocator; |
| 27 | class XmlWriter; |
| 28 | class XmlReader; |
| 29 | class MemoryBuffer; |
| 30 | |
| 31 | /** |
| 32 | \brief Serializer interface for RepX (Xml) serialization. |
| 33 | |
| 34 | In order to serialize a class to RepX both a PxSerializer and |
| 35 | a PxRepXSerializer implementation are needed. |
| 36 | |
| 37 | A repx Serializer provides the ability to capture a live |
| 38 | object to a descriptor or static state and the ability to |
| 39 | write that state out to a file. Objects allocated |
| 40 | by the Serializer using the allocator are freed when the |
| 41 | collection itself is freed. |
| 42 | SnRepXCoreSerializers.cpp implements a set of Serializers |
| 43 | for the core PhysX types. |
| 44 | |
| 45 | \note Implementing a PxRepXSerializer is currently not practical without including the internal PhysXExtension header "SnRepXSerializerImpl.h". |
| 46 | |
| 47 | @see PxSerializer, PX_NEW_REPX_SERIALIZER, PxSerializationRegistry::registerRepXSerializer |
| 48 | */ |
| 49 | class PxRepXSerializer |
| 50 | { |
| 51 | protected: |
| 52 | virtual ~PxRepXSerializer(){} |
| 53 | public: |
| 54 | |
| 55 | /** |
| 56 | \brief The type this Serializer is meant to operate on. |
| 57 | @see PxRepXObject::typeName |
| 58 | */ |
| 59 | virtual const char* getTypeName() = 0; |
| 60 | |
| 61 | /** |
| 62 | \brief Convert from a RepX object to a key-value pair hierarchy |
| 63 | |
| 64 | \param[in] inLiveObject The object to convert to the passed in descriptor. |
| 65 | \param[in] inCollection The collection to use to find ids of references of this object. |
| 66 | \param[in] inWriter Interface to write data to. |
| 67 | \param[in] inTempBuffer used to for temporary allocations. |
| 68 | \param[in] inArgs The arguments used in create resources and objects. |
| 69 | */ |
| 70 | virtual void objectToFile( const PxRepXObject& inLiveObject, PxCollection* inCollection, XmlWriter& inWriter, MemoryBuffer& inTempBuffer, PxRepXInstantiationArgs& inArgs ) = 0; |
| 71 | |
| 72 | /** |
| 73 | \brief Convert from a descriptor to a live object. Must be an object of this Serializer type. |
| 74 | |
| 75 | \param[in] inReader The inverse of the writer, a key-value pair database. |
| 76 | \param[in] inAllocator An allocator to use for temporary allocations. These will be freed after instantiation completes. |
| 77 | \param[in] inArgs The arguments used in create resources and objects. |
| 78 | \param[in] inCollection The collection used to find references. |
| 79 | |
| 80 | \return The new live object. It can be an invalid object if the instantiation cannot take place. |
| 81 | */ |
| 82 | virtual PxRepXObject fileToObject( XmlReader& inReader, XmlMemoryAllocator& inAllocator, PxRepXInstantiationArgs& inArgs, PxCollection* inCollection ) = 0; |
| 83 | |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | \brief Inline helper template function to create PxRepXObject from TDataType type supporting PxTypeInfo<TDataType>::name. |
| 88 | */ |
| 89 | template<typename TDataType> |
| 90 | PX_INLINE PxRepXObject createRepXObject( const TDataType* inType, const PxSerialObjectId inId ) |
| 91 | { |
| 92 | return PxRepXObject( PxTypeInfo<TDataType>::name(), inType, inId ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | \brief Inline helper function to create PxRepXObject from a PxBase instance. |
| 97 | */ |
| 98 | PX_INLINE PxRepXObject createRepXObject( const PxBase* inType, const PxSerialObjectId inId ) |
| 99 | { |
| 100 | PX_ASSERT( inType ); |
| 101 | return PxRepXObject( inType->getConcreteTypeName(), inType, inId ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | \brief Inline helper template function to create PxRepXObject form TDataType type using inType pointer as a PxSerialObjectId id. |
| 106 | */ |
| 107 | template<typename TDataType> |
| 108 | PX_INLINE PxRepXObject createRepXObject( const TDataType* inType ) |
| 109 | { |
| 110 | return createRepXObject( inType, static_cast<PxSerialObjectId>( reinterpret_cast<size_t>( inType) ) ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | \brief Preprocessor macro for RepX serializer creation. |
| 115 | */ |
| 116 | #define PX_NEW_REPX_SERIALIZER(T) \ |
| 117 | *PX_PLACEMENT_NEW(PxGetFoundation().getAllocatorCallback().allocate(sizeof(T), "PxRepXSerializer", __FILE__, __LINE__ ), T)(PxGetFoundation().getAllocatorCallback()) |
| 118 | |
| 119 | /** |
| 120 | \brief Preprocessor Macro to simplify RepX serializer delete. |
| 121 | */ |
| 122 | #define PX_DELETE_REPX_SERIALIZER(x) \ |
| 123 | { PxRepXSerializer* s = x; if (s) { PxGetFoundation().getAllocatorCallback().deallocate(s); } } |
| 124 | |
| 125 | #ifndef PX_DOXYGEN |
| 126 | } // namespace physx |
| 127 | #endif |
| 128 | |
| 129 | /** @} */ |
| 130 | #endif |
| 131 | |