/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #ifndef CRYINCLUDE_CRYCOMMON_SERIALIZATION_SMARTPTRIMPL_H #define CRYINCLUDE_CRYCOMMON_SERIALIZATION_SMARTPTRIMPL_H #pragma once #include "SmartPtr.h" #include #include "ClassFactory.h" // Exposes _smart_ptr<> as serializeable type for Serialization::IArchive template class SmartPtrSerializer : public Serialization::IPointer { public: SmartPtrSerializer(_smart_ptr& ptr) : m_ptr(ptr) {} const char* registeredTypeName() const override { if (m_ptr) { return Serialization::ClassFactory::the().getRegisteredTypeName(m_ptr.get()); } else { return ""; } } void create(const char* registeredTypeName) const override { CRY_ASSERT(!m_ptr || m_ptr->NumRefs() == 1); if (registeredTypeName && registeredTypeName[0] != '\0') { m_ptr.reset(Serialization::ClassFactory::the().create(registeredTypeName)); } else { m_ptr.reset((T*)0); } } Serialization::TypeID baseType() const{ return Serialization::TypeID::get(); } virtual Serialization::SStruct serializer() const{ return Serialization::SStruct(*m_ptr); } void* get() const{ return reinterpret_cast(m_ptr.get()); } const void* handle() const { return &m_ptr; } Serialization::TypeID pointerType() const { return Serialization::TypeID::get<_smart_ptr >(); } Serialization::IClassFactory* factory() const{ return &Serialization::ClassFactory::the(); } protected: _smart_ptr& m_ptr; }; template bool Serialize(Serialization::IArchive& ar, _smart_ptr& ptr, const char* name, const char* label) { SmartPtrSerializer serializer(ptr); return ar(static_cast(serializer), name, label); } #endif // CRYINCLUDE_CRYCOMMON_SERIALIZATION_SMARTPTRIMPL_H