You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Code/CryEngine/CryCommon/Serialization/SmartPtrImpl.h

74 lines
2.5 KiB
C++

/*
* 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 <Serialization/Serializer.h>
#include "ClassFactory.h"
// Exposes _smart_ptr<> as serializeable type for Serialization::IArchive
template<class T>
class SmartPtrSerializer
: public Serialization::IPointer
{
public:
SmartPtrSerializer(_smart_ptr<T>& ptr)
: m_ptr(ptr)
{}
const char* registeredTypeName() const override
{
if (m_ptr)
{
return Serialization::ClassFactory<T>::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<T>::the().create(registeredTypeName));
}
else
{
m_ptr.reset((T*)0);
}
}
Serialization::TypeID baseType() const{ return Serialization::TypeID::get<T>(); }
virtual Serialization::SStruct serializer() const{ return Serialization::SStruct(*m_ptr); }
void* get() const{ return reinterpret_cast<void*>(m_ptr.get()); }
const void* handle() const { return &m_ptr; }
Serialization::TypeID pointerType() const { return Serialization::TypeID::get<_smart_ptr<T> >(); }
Serialization::IClassFactory* factory() const{ return &Serialization::ClassFactory<T>::the(); }
protected:
_smart_ptr<T>& m_ptr;
};
template<class T>
bool Serialize(Serialization::IArchive& ar, _smart_ptr<T>& ptr, const char* name, const char* label)
{
SmartPtrSerializer<T> serializer(ptr);
return ar(static_cast<Serialization::IPointer&>(serializer), name, label);
}
#endif // CRYINCLUDE_CRYCOMMON_SERIALIZATION_SMARTPTRIMPL_H