| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | |
| 31 | #ifndef PX_SERIALIZER_H |
| 32 | #define PX_SERIALIZER_H |
| 33 | /** \addtogroup extensions |
| 34 | @{ |
| 35 | */ |
| 36 | |
| 37 | #include "foundation/PxAssert.h" |
| 38 | #include "foundation/PxAllocatorCallback.h" |
| 39 | #include "common/PxSerialFramework.h" |
| 40 | #include "common/PxCollection.h" |
| 41 | #include "PxFoundation.h" |
| 42 | |
| 43 | |
| 44 | #if !PX_DOXYGEN |
| 45 | namespace physx |
| 46 | { |
| 47 | #endif |
| 48 | |
| 49 | /** |
| 50 | \brief Serialization interface class. |
| 51 | |
| 52 | PxSerializer is used to extend serializable PxBase classes with serialization functionality. The |
| 53 | interface is structured such that per-class adapter instances can be used as opposed to per-object |
| 54 | adapter instances, avoiding per object allocations. Hence the methods take a reference to PxBase as a parameter. |
| 55 | |
| 56 | The PxSerializer interface needs to be implemented for binary or RepX serialization to work on custom |
| 57 | types. If only RepX serialization is needed, some methods can be left empty, as they are only needed |
| 58 | for binary serialization. |
| 59 | |
| 60 | A default implementation is available as a template adapter (PxSerializerDefaultAdapter). |
| 61 | |
| 62 | @see PxSerializerDefaultAdapter, PX_NEW_SERIALIZER_ADAPTER, PxSerializationRegistry::registerSerializer |
| 63 | */ |
| 64 | class PxSerializer |
| 65 | { |
| 66 | public: |
| 67 | |
| 68 | /**********************************************************************************************************************/ |
| 69 | |
| 70 | /** @name Basics needed for Binary- and RepX-Serialization |
| 71 | */ |
| 72 | //@{ |
| 73 | |
| 74 | /** |
| 75 | \brief Returns string name of dynamic type. |
| 76 | |
| 77 | \return Class name of most derived type of this object. |
| 78 | */ |
| 79 | virtual const char* getConcreteTypeName() const = 0; |
| 80 | |
| 81 | /** |
| 82 | \brief Adds required objects to the collection. |
| 83 | |
| 84 | This method does not add the required objects recursively, e.g. objects required by required objects. |
| 85 | |
| 86 | @see PxCollection, PxSerialization::complete |
| 87 | */ |
| 88 | virtual void requiresObjects(PxBase&, PxProcessPxBaseCallback&) const = 0; |
| 89 | |
| 90 | /** |
| 91 | \brief Whether the object is subordinate. |
| 92 | |
| 93 | A class is subordinate, if it can only be instantiated in the context of another class. |
| 94 | |
| 95 | \return Whether the class is subordinate |
| 96 | |
| 97 | @see PxSerialization::isSerializable |
| 98 | */ |
| 99 | virtual bool isSubordinate() const = 0; |
| 100 | |
| 101 | //@} |
| 102 | /**********************************************************************************************************************/ |
| 103 | |
| 104 | /**********************************************************************************************************************/ |
| 105 | |
| 106 | /** @name Functionality needed for Binary Serialization only |
| 107 | */ |
| 108 | //@{ |
| 109 | |
| 110 | /** |
| 111 | \brief Exports object's extra data to stream. |
| 112 | */ |
| 113 | virtual void (PxBase&, PxSerializationContext&) const = 0; |
| 114 | |
| 115 | /** |
| 116 | \brief Exports object's data to stream. |
| 117 | */ |
| 118 | virtual void exportData(PxBase&, PxSerializationContext&) const = 0; |
| 119 | |
| 120 | /** |
| 121 | \brief Register references that the object maintains to other objects. |
| 122 | */ |
| 123 | virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const = 0; |
| 124 | |
| 125 | /** |
| 126 | \brief Returns size needed to create the class instance. |
| 127 | |
| 128 | \return sizeof class instance. |
| 129 | */ |
| 130 | virtual size_t getClassSize() const = 0; |
| 131 | |
| 132 | /** |
| 133 | \brief Create object at a given address, resolve references and import extra data. |
| 134 | |
| 135 | \param address Location at which object is created. Address is increased by the size of the created object. |
| 136 | \param context Context for reading external data and resolving references. |
| 137 | \return Created PxBase pointer (needs to be identical to address before increment). |
| 138 | */ |
| 139 | virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const = 0; |
| 140 | |
| 141 | //@} |
| 142 | /**********************************************************************************************************************/ |
| 143 | virtual ~PxSerializer() {} |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | \brief Default PxSerializer implementation. |
| 149 | */ |
| 150 | template<class T> |
| 151 | class PxSerializerDefaultAdapter : public PxSerializer |
| 152 | { |
| 153 | public: |
| 154 | |
| 155 | /************************************************************************************************/ |
| 156 | |
| 157 | /** @name Basics needed for Binary- and RepX-Serialization |
| 158 | */ |
| 159 | //@{ |
| 160 | |
| 161 | PxSerializerDefaultAdapter(const char* name) : mTypeName(name){} |
| 162 | |
| 163 | virtual const char* getConcreteTypeName() const |
| 164 | { |
| 165 | return mTypeName; |
| 166 | } |
| 167 | |
| 168 | virtual void requiresObjects(PxBase& obj, PxProcessPxBaseCallback& c) const |
| 169 | { |
| 170 | T& t = static_cast<T&>(obj); |
| 171 | t.requiresObjects(c); |
| 172 | } |
| 173 | |
| 174 | virtual bool isSubordinate() const |
| 175 | { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | //@} |
| 180 | /************************************************************************************************/ |
| 181 | |
| 182 | /** @name Functionality needed for Binary Serialization only |
| 183 | */ |
| 184 | //@{ |
| 185 | |
| 186 | // object methods |
| 187 | |
| 188 | virtual void (PxBase& obj, PxSerializationContext& s) const |
| 189 | { |
| 190 | T& t = static_cast<T&>(obj); |
| 191 | t.exportExtraData(s); |
| 192 | } |
| 193 | |
| 194 | virtual void exportData(PxBase& obj, PxSerializationContext& s) const |
| 195 | { |
| 196 | PxAllocatorCallback& allocator = PxGetFoundation().getAllocatorCallback(); |
| 197 | T* copy = reinterpret_cast<T*>(allocator.allocate(size: sizeof(T), typeName: "TmpAllocExportData" , __FILE__, __LINE__)); |
| 198 | PxMemCopy(copy, &obj, sizeof(T)); |
| 199 | copy->preExportDataReset(); |
| 200 | s.writeData(data: copy, size: sizeof(T)); |
| 201 | allocator.deallocate(ptr: copy); |
| 202 | } |
| 203 | |
| 204 | virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const |
| 205 | { |
| 206 | T& t = static_cast<T&>(obj); |
| 207 | |
| 208 | s.registerReference(base&: obj, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&obj)); |
| 209 | |
| 210 | struct RequiresCallback : public PxProcessPxBaseCallback |
| 211 | { |
| 212 | RequiresCallback(PxSerializationContext& c) : context(c) {} |
| 213 | RequiresCallback& operator=(RequiresCallback&) { PX_ASSERT(0); return *this; } |
| 214 | void process(physx::PxBase& base) |
| 215 | { |
| 216 | context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&base)); |
| 217 | } |
| 218 | PxSerializationContext& context; |
| 219 | }; |
| 220 | |
| 221 | RequiresCallback callback(s); |
| 222 | t.requiresObjects(callback); |
| 223 | } |
| 224 | |
| 225 | // class methods |
| 226 | |
| 227 | virtual size_t getClassSize() const |
| 228 | { |
| 229 | return sizeof(T); |
| 230 | } |
| 231 | |
| 232 | virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const |
| 233 | { |
| 234 | return T::createObject(address, context); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | //@} |
| 239 | /************************************************************************************************/ |
| 240 | |
| 241 | private: |
| 242 | const char* mTypeName; |
| 243 | }; |
| 244 | |
| 245 | /** |
| 246 | \brief Preprocessor Macro to simplify adapter creation. |
| 247 | |
| 248 | Note: that the allocator used for creation needs to match with the one used in PX_DELETE_SERIALIZER_ADAPTER. |
| 249 | */ |
| 250 | #define PX_NEW_SERIALIZER_ADAPTER(x) \ |
| 251 | *new( PxGetFoundation().getAllocatorCallback().allocate(sizeof(PxSerializerDefaultAdapter<x>), \ |
| 252 | "PxSerializerDefaultAdapter", __FILE__, __LINE__ )) PxSerializerDefaultAdapter<x>(#x) |
| 253 | |
| 254 | /** |
| 255 | \brief Preprocessor Macro to simplify adapter deletion. |
| 256 | */ |
| 257 | #define PX_DELETE_SERIALIZER_ADAPTER(x) \ |
| 258 | { PxSerializer* s = x; if (s) { s->~PxSerializer(); PxGetFoundation().getAllocatorCallback().deallocate(s); } } |
| 259 | |
| 260 | #if !PX_DOXYGEN |
| 261 | } // namespace physx |
| 262 | #endif |
| 263 | |
| 264 | /** @} */ |
| 265 | #endif |
| 266 | |