BVB Source Codes

CRYENGINE Show XMLCPB_NodeLiveWriter.h Source code

Return Download CRYENGINE: download XMLCPB_NodeLiveWriter.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_NODELIVEWRITER_H
  8.         #define XMLCPB_NODELIVEWRITER_H
  9.  
  10.         #include "XMLCPB_AttrWriter.h"
  11.         #include "../XMLCPB_Common.h"
  12.         #include "XMLCPB_BufferWriter.h"
  13.  
  14. namespace XMLCPB {
  15.  
  16. class CWriter;
  17. class CNodeLiveWriter;
  18.  
  19. //////////////////////////////////////////////////////////////////////////
  20. // from outside, CNodeLiveWriterRef is the class that is always used to access a node. CNodeLiveWriter is never used directly from outside.
  21. // 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.
  22. // That situation is avoided with the use of this wrapper reference and the safecheckID. Every time a livenode is reused, its safecheckID changes. If the safecheckID of
  23. //   the CNodeLiveWriterRef does not match the one in the LiveNode, then it can not be used.
  24.  
  25. // Note: this object is slightly different than the CNodeLiveReaderRef used in the reader. the reader one does not need the safecheckID because the life of a LiveNodeReader
  26. //       is directly determined by how many references are active. When no references are active, the LiveNodeReader is discarded. In the case of the writer we can not do
  27. //       that because livenodes can ( and have to ) be moved into the main data buffer only at the right time, independently of what is the external use of them.
  28.  
  29. class CNodeLiveWriterRef
  30. {
  31. public:
  32.  
  33.         CNodeLiveWriterRef(CWriter& Writer)
  34.                 : m_Writer(Writer)
  35.                 , m_nodeId(XMLCPB_INVALID_ID)
  36.                 , m_pNode_Debug(NULL)
  37.                 , m_safecheckID(XMLCPB_INVALID_SAFECHECK_ID)
  38.         {}
  39.  
  40.         explicit CNodeLiveWriterRef(CWriter& Writer, NodeLiveID nodeId);
  41.  
  42.         CNodeLiveWriterRef(const CNodeLiveWriterRef& other) : m_Writer(other.m_Writer)
  43.         {
  44.                 CopyFrom(other);
  45.         }
  46.  
  47.         CNodeLiveWriterRef& operator=(const CNodeLiveWriterRef& other)
  48.         {
  49.                 CopyFrom(other);
  50.                 return *this;
  51.         }
  52.  
  53.         CNodeLiveWriter* operator->() const { return GetNode(); }
  54.  
  55.         bool             IsValid() const;
  56.  
  57. private:
  58.  
  59.         void             CopyFrom(const CNodeLiveWriterRef& other);
  60.         CNodeLiveWriter* GetNode() const;
  61.  
  62. private:
  63.         CWriter&               m_Writer;
  64.         NodeLiveID             m_nodeId;
  65.         uint32                 m_safecheckID;
  66.         const CNodeLiveWriter* m_pNode_Debug;   // only to help debugging. Dont use it.
  67. };
  68.  
  69. // Nodes in the writting process follow this state path:
  70. // - created as "LiveNode".
  71. // - add children, attributes
  72. // - flaged as "done" -> cant modify or add anything to it. this can be explicitely called, or automatically when another livenode of the same parent is created.
  73. // - compacted -> its data is converted into binary format, and stored into the main buffer. All node children of a parent are compacted at the same time (actually, for nodes with big amount of children, they are compacted in blocks, not all at once )
  74. // - novalid -> right after being compacted, the object is flaged as "novalid" so it can be reused for the next "LiveNode".
  75.  
  76. class CNodeLiveWriter : public _reference_target_t
  77. {
  78.         friend class CWriter;
  79.         friend class CNodeLiveWriterRef;
  80.  
  81. public:
  82.  
  83.         CNodeLiveWriter(CWriter& Writer, NodeLiveID ID, StringID IDTag, uint32 safecheckID);
  84.         CNodeLiveWriter(const CNodeLiveWriter& other) : m_Writer(other.m_Writer) { *this = other; }
  85.  
  86.         CNodeLiveWriter& operator=(const CNodeLiveWriter& other)
  87.         {
  88.                 if (this != &other)
  89.                 {
  90.                         m_ID = other.m_ID;
  91.                         m_IDParent = other.m_IDParent;
  92.                         m_IDTag = other.m_IDTag;
  93.                         m_children = other.m_children;
  94.                         m_done = other.m_done;
  95.                         m_valid = other.m_valid;
  96.                         m_childrenAreCompacted = other.m_childrenAreCompacted;
  97.                         m_totalAmountChildren = other.m_totalAmountChildren;
  98.                         m_safecheckID = other.m_safecheckID;
  99.                         m_attrs = other.m_attrs;
  100.                         m_globalIdLastChildInBlock = other.m_globalIdLastChildInBlock;
  101.                 }
  102.                 return *this;
  103.         }
  104.  
  105.         CNodeLiveWriterRef AddChildNode(const char* pChildName);
  106.         void               Done();
  107.         template<class T>
  108.         void               AddAttr(const char* pAttrName, T& data);
  109.         void               AddAttr(const char* pAttrName, const uint8* data, uint32 len, bool needInmediateCopy = false);
  110.         const char*        GetTag() const;
  111.  
  112.         // TODO: they do linear strcmp searchs, check if they could be avoided or if they need optimization
  113.         bool        HaveAttr(const char* pAttrName);
  114.         const char* ReadAttrStr(const char* pAttrName);
  115.  
  116. private:
  117.  
  118.         void       ChildIsDone();
  119.         bool       IsValid() const { return m_valid; }
  120.         bool       IsDone() const  { return m_done; }
  121.         void       SetParent(NodeLiveID ID);
  122.         NodeLiveID GetID() const   { return m_ID; }
  123.         void       Reuse(StringID IDTag, uint32 safecheckID);
  124.         void       CompactPendingChildren();
  125.         void       Compact();
  126.         uint32     GetSafeCheckID() const { return m_safecheckID; }
  127.  
  128.         #ifdef XMLCPB_CHECK_HARDCODED_LIMITS
  129.         void CheckHardcodedLimits();
  130.         void ShowExtendedErrorInfo() const;
  131.         #endif
  132.  
  133.         CWriter&                         m_Writer;
  134.         NodeLiveID                       m_ID; // is unique on all the live nodes, but already compacted nodes could have used the same one, because the live nodes are reused and their ID do not change
  135.         NodeLiveID                       m_IDParent;
  136.         StringID                         m_IDTag;
  137.         std::vector<NodeLiveID>          m_children;
  138.         bool                             m_done;                 // true when is closed: cant add more children, more attrs, or anything else. a "done" node is just waiting to be compacted
  139.         bool                             m_valid;                // live nodes are reused. this is false when the node is invalid, which meants is ready to be reused.
  140.         bool                             m_childrenAreCompacted; // true: all children are already in the main buffer, they dont exist anymore as livenodes.
  141.         uint                             m_totalAmountChildren;
  142.         uint32                           m_safecheckID; // to prevent wrong use.
  143.         std::vector<XMLCPB::CAttrWriter> m_attrs;
  144.         std::vector<NodeGlobalID>        m_globalIdLastChildInBlock; // every entry is the globalId of the LAST child on each block of children
  145.  
  146.         // children blocks:
  147.         // the children in a block are compacted at the same time, so they are contiguous in memory.
  148.         // it would be possible to have all the children compacted at the same time, without blocks, but the separation in blocks avoid the need for an excesive amount of active live nodes at once for the very few nodes that have a big amount of children.
  149.  
  150.         static const int INITIAL_SIZE_CHILDREN_VECTOR;
  151.         static const int INITIAL_SIZE_ATTRS_VECTOR;
  152.  
  153.         #ifdef XMLCPB_COLLECT_STATS
  154.         // statistics
  155.         struct TStats
  156.         {
  157.                 TStats()
  158.                 {
  159.                         Reset();
  160.                 }
  161.                 void Reset()
  162.                 {
  163.                         m_maxNumChildren = 0;
  164.                         m_maxNumAttrs = 0;
  165.                         m_totalNodesCreated = 0;
  166.                         m_totalAttrs = 0;
  167.                         m_totalChildren = 0;
  168.                         m_totalSizeNodeData = 0;
  169.                         m_totalSizeAttrData = 0;
  170.                 }
  171.  
  172.                 uint32 m_maxNumChildren;
  173.                 uint32 m_maxNumAttrs;
  174.                 uint32 m_totalNodesCreated;
  175.                 uint32 m_totalAttrs;
  176.                 uint32 m_totalChildren;
  177.                 uint32 m_totalSizeNodeData;            // not including attrs
  178.                 uint32 m_totalSizeAttrData;
  179.         };
  180.  
  181.         static TStats m_stats;
  182.         #endif
  183.  
  184. };
  185.  
  186. //////////////////////////////////////////////////////////////////////////
  187. template<class T>
  188. inline void CNodeLiveWriter::AddAttr(const char* pAttrName, T& data)
  189. {
  190.         assert(!m_done && m_valid);
  191.  
  192.         CAttrWriter attr(m_Writer);
  193.         attr.Set(pAttrName, data);
  194.  
  195.         m_attrs.push_back(attr);
  196. }
  197.  
  198. } //end namespace
  199.  
  200. #endif
  201.  
downloadXMLCPB_NodeLiveWriter.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