BVB Source Codes

CRYENGINE Show XMLCPB_NodeLiveReader.h Source code

Return Download CRYENGINE: download XMLCPB_NodeLiveReader.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4. *************************************************************************/
  5.  
  6. #pragma once
  7. #ifndef XMLCPB_NODELIVEREADER_H
  8.         #define XMLCPB_NODELIVEREADER_H
  9.  
  10.         #include "XMLCPB_AttrReader.h"
  11.         #include "../XMLCPB_Common.h"
  12.  
  13. namespace XMLCPB {
  14.  
  15. class CNodeLiveReader;
  16. class CReader;
  17.  
  18. // from outside, CNodeLiveReaderRef is the class that is always used to access a node. CNodeLiveReader is never used directly from outside.
  19. // The main reason for this is because the live nodes are reusable: It could be possible to keep a "valid" pointer to a livenode even after the original node has been discarded and another one has taken its place.
  20. // That situation is avoided with the use of this wrapper reference and a refcounter. LiveNodes are reused only when refcounter is 0.
  21. // This is slightly different than CNodeLiveWriterRef, read there for more info.
  22.  
  23. class CNodeLiveReaderRef
  24. {
  25. public:
  26.  
  27.         CNodeLiveReaderRef(CReader& Reader)
  28.                 : m_Reader(Reader)
  29.                 , m_nodeId(XMLCPB_INVALID_ID)
  30.                 , m_pNode_Debug(NULL)
  31.         {}
  32.  
  33.         explicit CNodeLiveReaderRef(CReader& Reader, NodeLiveID nodeId);
  34.  
  35.         ~CNodeLiveReaderRef()
  36.         {
  37.                 FreeRef();
  38.         }
  39.  
  40.         CNodeLiveReaderRef(const CNodeLiveReaderRef& other)
  41.                 : m_Reader(other.m_Reader)
  42.                 , m_nodeId(XMLCPB_INVALID_ID)
  43.         {
  44.                 CopyFrom(other);
  45.         }
  46.  
  47.         CNodeLiveReaderRef& operator=(const CNodeLiveReaderRef& other)
  48.         {
  49.                 FreeRef();
  50.                 CopyFrom(other);
  51.                 return *this;
  52.         }
  53.  
  54.         CNodeLiveReader* operator->() const { return GetNode(); }
  55.  
  56.         bool             IsValid() const    { return m_nodeId != XMLCPB_INVALID_ID; }
  57.  
  58. private:
  59.  
  60.         void             CopyFrom(const CNodeLiveReaderRef& other);
  61.         void             FreeRef();
  62.         CNodeLiveReader* GetNode() const;
  63.  
  64. private:
  65.         CReader&               m_Reader;
  66.         NodeLiveID             m_nodeId;
  67.         const CNodeLiveReader* m_pNode_Debug;   // is only to help debugging. Dont use it for anything.
  68. };
  69.  
  70. // When a node is needed, an available CNodeLiveReader object is constructed (reused actually) from the raw binary data that is in the main buffer.
  71. // the live node keeps the data of that raw node until all references are gone. Then it become available again to be used to read other nodes in the raw data.
  72.  
  73. class CNodeLiveReader
  74. {
  75.         friend class CReader;
  76.         friend class CNodeLiveReaderRef;
  77.  
  78. public:
  79.  
  80.         CNodeLiveReader(CReader& Reader) : m_Reader(Reader) { Reset(); }
  81.         CNodeLiveReader(const CNodeLiveReader& other) : m_Reader(other.m_Reader) { *this = other; }
  82.         ~CNodeLiveReader();
  83.  
  84.         CNodeLiveReader& operator=(const CNodeLiveReader& other)
  85.         {
  86.                 if (this != &other)
  87.                 {
  88.                         m_liveId = other.m_liveId;
  89.                         m_globalId = other.m_globalId;
  90.                         m_attrsSetId = other.m_attrsSetId;
  91.                         m_refCounter = other.m_refCounter;
  92.                         m_indexOfNextChild = other.m_indexOfNextChild;
  93.                         m_numChildren = other.m_numChildren;
  94.                         m_numAttrs = other.m_numAttrs;
  95.                         m_addrFirstAttr = other.m_addrFirstAttr;
  96.                         m_pTag = other.m_pTag;
  97.                         m_valid = other.m_valid;
  98.                         m_childBlocks.resize(other.m_childBlocks.size());
  99.                         m_childBlocks = other.m_childBlocks;
  100.                 }
  101.                 return *this;
  102.         }
  103.  
  104.         CNodeLiveReaderRef GetChildNode(const char* pChildName);
  105.         CNodeLiveReaderRef GetChildNode(uint32 index);
  106.         uint32             GetNumChildren() const { return m_numChildren; }
  107.         uint32             GetNumAttrs() const    { return m_numAttrs; }
  108.         const char*        GetTag() const         { return m_pTag; }
  109.         uint32             GetSizeWithoutChilds() const;
  110.         template<class T>
  111.         bool               ReadAttr(const char* pAttrName, T& data) const;
  112.         bool               ReadAttr(const char* pAttrName, uint8*& rdata, uint32& outSize) const;
  113.         template<class T>
  114.         void               ReadAttr(uint index, T& data) const;
  115.         void               ReadAttr(uint index, uint8*& rdata, uint32& outSize) const;
  116.         bool               ReadAttrAsString(const char* pAttrName, string& str) const;
  117.  
  118.         bool               HaveAttr(const char* pAttrName) const;
  119.         FlatAddr           GetAddrNextNode() const;
  120.  
  121.         // use ReadAttr functions whenever possible, instead of Obtain() ones.
  122.         CAttrReader ObtainAttr(const char* pAttrName) const;
  123.         CAttrReader ObtainAttr(uint32 indAttr) const;
  124.  
  125. private:
  126.  
  127.         bool         IsValid() const { return m_valid; }
  128.         void         Reset();
  129.         void         ActivateFromCompact(NodeLiveID liveId, NodeGlobalID globalId);
  130.         NodeLiveID   GetLiveId() const { return m_liveId; }
  131.         bool         FindAttr(const char* pAttrName, CAttrReader& attr) const;
  132.         void         GetAttr(uint32 indAttr, CAttrReader& attr) const;
  133.  
  134.         const char*  GetTagNode(NodeGlobalID nodeId) const;
  135.         NodeGlobalID GetChildGlobalId(uint32 indChild) const;
  136.         uint16       GetHeaderAttr(uint32 attrInd) const;
  137.  
  138.         #ifndef _RELEASE
  139.         void Log() const;
  140.         #endif
  141.  
  142. private:
  143.  
  144.         CReader&                  m_Reader;
  145.         NodeLiveID                m_liveId;   // is unique for every live node, but it does not change, which means that the same live node will have always the same ID even when it stores the info of diferent raw nodes.
  146.         NodeGlobalID              m_globalId; // unique for every node in the file.
  147.         AttrSetID                 m_attrsSetId;
  148.         uint32                    m_refCounter;       // to know when it can be discarded and reused.
  149.         uint32                    m_indexOfNextChild; // to speed up the sequential reading of childs.
  150.         uint32                    m_numChildren;
  151.         uint32                    m_numAttrs;
  152.         FlatAddr                  m_addrFirstAttr;  // all attrs are sequentially stored
  153.         std::vector<NodeGlobalID> m_childBlocks;    // stores the globalId of the first child of each block
  154.  
  155.         const char*               m_pTag;
  156.         bool                      m_valid;  // live nodes are reused. this is false when the node is invalid, which means is ready to be reused.
  157. };
  158.  
  159. template<class T>
  160. bool CNodeLiveReader::ReadAttr(const char* pAttrName, T& data) const
  161. {
  162.         CAttrReader attr(m_Reader);
  163.         bool found = FindAttr(pAttrName, attr);
  164.  
  165.         if (found)
  166.                 attr.Get(data);
  167.  
  168.         return found;
  169. }
  170.  
  171. template<class T>
  172. void CNodeLiveReader::ReadAttr(uint32 index, T& data) const
  173. {
  174.         assert(index < m_numAttrs);
  175.         CAttrReader attr(m_Reader);
  176.         FlatAddr nextAddr = m_addrFirstAttr;
  177.  
  178.         for (uint32 a = 0; a < index; a++)
  179.         {
  180.                 attr.InitFromCompact(nextAddr, GetHeaderAttr(a));
  181.                 nextAddr = attr.GetAddrNextAttr();
  182.         }
  183.         attr.Get(data);
  184. }
  185.  
  186. } //end namespace
  187.  
  188. #endif
  189.  
downloadXMLCPB_NodeLiveReader.h 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