BVB Source Codes

CRYENGINE Show BinarySerializeHelper.cpp Source code

Return Download CRYENGINE: download BinarySerializeHelper.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.    Description: Manages one-off reader/writer usages for binary serialization
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 02:06:2010: Created by Kevin Kirst
  12.  
  13. *************************************************************************/
  14.  
  15. #include "StdAfx.h"
  16. #include "BinarySerializeHelper.h"
  17.  
  18. //////////////////////////////////////////////////////////////////////////
  19. CBinarySerializedObject::CBinarySerializedObject(const char* szSection)
  20.         : m_sSection(szSection)
  21.         , m_nRefCount(0)
  22.         , m_uSerializedDataSize(0)
  23.         , m_pSerializedData(0)
  24. {
  25.         assert(szSection && szSection[0]);
  26. }
  27.  
  28. //////////////////////////////////////////////////////////////////////////
  29. CBinarySerializedObject::~CBinarySerializedObject()
  30. {
  31.         FreeData();
  32. }
  33.  
  34. //////////////////////////////////////////////////////////////////////////
  35. void CBinarySerializedObject::FreeData()
  36. {
  37.         //SAFE_DELETE_ARRAY(m_pSerializedData);
  38.         if (m_pSerializedData)
  39.         {
  40.                 free(m_pSerializedData);
  41.                 m_pSerializedData = NULL;
  42.         }
  43.  
  44.         m_uSerializedDataSize = 0;
  45. }
  46.  
  47. //////////////////////////////////////////////////////////////////////////
  48. void CBinarySerializedObject::GetMemoryUsage(ICrySizer* pSizer) const
  49. {
  50.         pSizer->Add(*this);
  51.         pSizer->Add(m_pSerializedData, m_uSerializedDataSize);
  52. }
  53.  
  54. //////////////////////////////////////////////////////////////////////////
  55. bool CBinarySerializedObject::IsEmpty() const
  56. {
  57.         return (!m_pSerializedData || m_uSerializedDataSize == 0);
  58. }
  59.  
  60. //////////////////////////////////////////////////////////////////////////
  61. void CBinarySerializedObject::Reset()
  62. {
  63.         FreeData();
  64. }
  65.  
  66. //////////////////////////////////////////////////////////////////////////
  67. void CBinarySerializedObject::Serialize(TSerialize& serialize)
  68. {
  69.         ISerialize* pISerialize = GetImpl(serialize);
  70.  
  71.         // FIXME: ISerialize helper does not support POD arrays...
  72.         if (serialize.IsReading())
  73.         {
  74.                 CSimpleSerializeWithDefaults<CSerializeReaderXMLCPBin>* pReaderSerialize = static_cast<CSimpleSerializeWithDefaults<CSerializeReaderXMLCPBin>*>(pISerialize);
  75.                 pReaderSerialize->GetInnerImpl()->ValueByteArray("m_pSerializedData", m_pSerializedData, m_uSerializedDataSize);
  76.         }
  77.         else
  78.         {
  79.                 CSimpleSerializeWithDefaults<CSerializeWriterXMLCPBin>* pWriterSerialize = static_cast<CSimpleSerializeWithDefaults<CSerializeWriterXMLCPBin>*>(pISerialize);
  80.                 pWriterSerialize->GetInnerImpl()->ValueByteArray("m_pSerializedData", m_pSerializedData, m_uSerializedDataSize);
  81.         }
  82. }
  83.  
  84. //////////////////////////////////////////////////////////////////////////
  85. bool CBinarySerializedObject::FinishWriting(XMLCPB::CWriterInterface& Writer)
  86. {
  87.         return Writer.WriteAllIntoMemory(m_pSerializedData, m_uSerializedDataSize);
  88. }
  89.  
  90. //////////////////////////////////////////////////////////////////////////
  91. bool CBinarySerializedObject::PrepareReading(XMLCPB::CReaderInterface& Reader)
  92. {
  93.         bool bResult = false;
  94.         if (!IsEmpty())
  95.         {
  96.                 bResult = Reader.ReadBinaryMemory(m_pSerializedData, m_uSerializedDataSize);
  97.         }
  98.         return bResult;
  99. }
  100.  
  101. //////////////////////////////////////////////////////////////////////////
  102. //////////////////////////////////////////////////////////////////////////
  103.  
  104. //////////////////////////////////////////////////////////////////////////
  105. CBinarySerializeHelper::CBinarySerializeHelper()
  106.         : m_nRefCount(1)
  107. {
  108.  
  109. }
  110.  
  111. //////////////////////////////////////////////////////////////////////////
  112. CBinarySerializeHelper::~CBinarySerializeHelper()
  113. {
  114.  
  115. }
  116.  
  117. //////////////////////////////////////////////////////////////////////////
  118. void CBinarySerializeHelper::GetMemoryUsage(ICrySizer* pSizer) const
  119. {
  120.         pSizer->Add(*this);
  121. }
  122.  
  123. //////////////////////////////////////////////////////////////////////////
  124. _smart_ptr<ISerializedObject> CBinarySerializeHelper::CreateSerializedObject(const char* szSection)
  125. {
  126.         assert(szSection && szSection[0]);
  127.         return _smart_ptr<ISerializedObject>(new CBinarySerializedObject(szSection));
  128. }
  129.  
  130. //////////////////////////////////////////////////////////////////////////
  131. CBinarySerializedObject* CBinarySerializeHelper::GetBinarySerializedObject(ISerializedObject* pObject)
  132. {
  133.         assert(pObject);
  134.  
  135.         CBinarySerializedObject* pBinaryObject = NULL;
  136.         if (pObject && CBinarySerializedObject::GUID == pObject->GetGUID())
  137.         {
  138.                 pBinaryObject = static_cast<CBinarySerializedObject*>(pObject);
  139.                 assert(pBinaryObject);
  140.         }
  141.  
  142.         return pBinaryObject;
  143. }
  144.  
  145. //////////////////////////////////////////////////////////////////////////
  146. bool CBinarySerializeHelper::Write(ISerializedObject* pObject, TSerializeFunc serializeFunc, void* pArgument /*= NULL*/)
  147. {
  148.         assert(pObject);
  149.  
  150.         bool bResult = false;
  151.  
  152.         if (CBinarySerializedObject* pBinaryObject = GetBinarySerializedObject(pObject))
  153.         {
  154.                 XMLCPB::CWriterInterface Writer;
  155.                 Writer.Init("BSHelperWriter", NULL);
  156.  
  157.                 XMLCPB::CNodeLiveWriterRef writeNode = Writer.GetRoot()->AddChildNode(pBinaryObject->GetSectionName());
  158.  
  159.                 std::unique_ptr<CSerializeWriterXMLCPBin> pWriterXMLCPBin = std::unique_ptr<CSerializeWriterXMLCPBin>(new CSerializeWriterXMLCPBin(writeNode, Writer));
  160.                 std::unique_ptr<ISerialize> pWriter = std::unique_ptr<ISerialize>(new CSimpleSerializeWithDefaults<CSerializeWriterXMLCPBin>(*pWriterXMLCPBin));
  161.  
  162.                 TSerialize stateWriter(pWriter.get());
  163.                 if (serializeFunc(stateWriter, pArgument))
  164.                 {
  165.                         bResult = pBinaryObject->FinishWriting(Writer);
  166.                 }
  167.         }
  168.  
  169.         return bResult;
  170. }
  171.  
  172. //////////////////////////////////////////////////////////////////////////
  173. bool CBinarySerializeHelper::Read(ISerializedObject* pObject, TSerializeFunc serializeFunc, void* pArgument /*= NULL*/)
  174. {
  175.         assert(pObject);
  176.  
  177.         bool bResult = false;
  178.  
  179.         if (CBinarySerializedObject* pBinaryObject = GetBinarySerializedObject(pObject))
  180.         {
  181.                 _smart_ptr<IGeneralMemoryHeap> pHeap = XMLCPB::CReader::CreateHeap();
  182.                 XMLCPB::CReaderInterface Reader(pHeap);
  183.                 if (pBinaryObject->PrepareReading(Reader))
  184.                 {
  185.                         XMLCPB::CNodeLiveReaderRef readNode = Reader.GetRoot()->GetChildNode(pBinaryObject->GetSectionName());
  186.                         assert(readNode.IsValid());
  187.  
  188.                         std::unique_ptr<CSerializeReaderXMLCPBin> pReaderXMLCPBin = std::unique_ptr<CSerializeReaderXMLCPBin>(new CSerializeReaderXMLCPBin(readNode, Reader));
  189.                         std::unique_ptr<ISerialize> pReader = std::unique_ptr<ISerialize>(new CSimpleSerializeWithDefaults<CSerializeReaderXMLCPBin>(*pReaderXMLCPBin));
  190.  
  191.                         TSerialize stateReader(pReader.get());
  192.                         if (serializeFunc(stateReader, pArgument))
  193.                         {
  194.                                 bResult = (!pBinaryObject->IsEmpty());
  195.                         }
  196.                 }
  197.         }
  198.  
  199.         return bResult;
  200. }
  201.  
downloadBinarySerializeHelper.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top