BVB Source Codes

CRYENGINE Show XMLCPB_StringTableWriter.h Source code

Return Download CRYENGINE: download XMLCPB_StringTableWriter.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_STRING_TABLE_WRITER_H
  8.         #define XMLCPB_STRING_TABLE_WRITER_H
  9.  
  10.         #include "../XMLCPB_Common.h"
  11.         #include "XMLCPB_BufferWriter.h"
  12.         #include <CryCore/StlUtils.h>
  13.  
  14. namespace XMLCPB {
  15.  
  16. class CWriter;
  17.  
  18. class CStringTableWriter
  19. {
  20. public:
  21.  
  22.         CStringTableWriter(int maxNumStrings, int bufferSize);
  23.         ~CStringTableWriter();
  24.         void        Init(CWriter* pWriter);
  25.  
  26.         StringID    GetStringID(const char* pString, bool addIfCantFind = true);
  27.         void        LogStrings();
  28.         int         GetNumStrings() const { return (int)m_stringAddrs.size(); }
  29.         void        FillFileHeaderInfo(SFileHeader::SStringTable& info);
  30.         void        WriteToFile();
  31.         void        WriteToMemory(uint8*& rpData, uint32& outWriteLoc);
  32.         uint32      GetDataSize();
  33.         const char* GetString(const StringID stringId) const { const SAddr* addr; return GetStringRealPointer(stringId, addr); }
  34.         void        CreateStringsFromConstants();
  35.  
  36.         #ifdef XMLCPB_CHECK_HARDCODED_LIMITS
  37.         const char* GetStringSafe(StringID stringId) const;
  38.         #endif
  39.  
  40. private:
  41.  
  42.         struct SAddr : CBufferWriter::SAddr
  43.         {
  44.                 size_t hash;
  45.         };
  46.  
  47.         CStringTableWriter&       operator=(const CStringTableWriter& other); // just to prevent assignments and copy constructors
  48.         CStringTableWriter(const CStringTableWriter&);                        //
  49.         const CStringTableWriter& GetThis() const { return *this; }
  50.  
  51.         static const StringID SEARCHING_ID;
  52.  
  53.         struct StringHasher
  54.         {
  55.                 const CStringTableWriter& m_owner;
  56.  
  57.                 StringHasher(const CStringTableWriter& owner) : m_owner(owner) {}
  58.  
  59.                 ILINE static size_t HashString(const char* __restrict str)
  60.                 {
  61.                         size_t hash = 5381;
  62.                         size_t c;
  63.                         while (c = *str++)
  64.                                 hash = ((hash << 5) + hash) + c;   /* hash * 33 + c */
  65.                         return hash;
  66.                 }
  67.  
  68.                 size_t operator()(const StringID& _Keyval) const
  69.                 {
  70.                         return _Keyval == CStringTableWriter::SEARCHING_ID ? m_owner.m_checkAddr.hash : m_owner.m_stringAddrs[_Keyval].hash;
  71.                 }
  72.  
  73.                 bool operator()(const StringID& _Keyval1, const StringID& _Keyval2) const
  74.                 {
  75.                         const SAddr* addr0, * addr1;
  76.                         const char* __restrict str0 = m_owner.GetStringRealPointer(_Keyval1, addr0);
  77.                         const char* __restrict str1 = m_owner.GetStringRealPointer(_Keyval2, addr1);
  78.                         if (addr0->hash != addr1->hash)
  79.                                 return false;
  80.                         for (; * str0 && * str0 == * str1; ++str0, ++str1) {}
  81.                         return *str0 == *str1;
  82.                 }
  83.         };
  84.  
  85. private:
  86.  
  87.         ILINE const char* GetStringRealPointer(StringID ID, const SAddr*& addr) const
  88.         {
  89.                 assert(ID != XMLCPB_INVALID_ID);
  90.  
  91.                 if (ID == SEARCHING_ID)
  92.                 {
  93.                         addr = &m_checkAddr;
  94.                         return m_pCheckString;
  95.                 }
  96.  
  97.                 addr = &m_stringAddrs[ID];
  98.                 return (const char*)m_buffer.GetPointerFromAddr(*addr);
  99.         }
  100.  
  101.         bool HasStringID(const char* pString) const
  102.         {
  103.                 //Prefer HasStringID() for use in asserts to avoid cppcheck believeing GetStringID() is causing unwanted side effects
  104.                 CStringTableWriter* self = const_cast<CStringTableWriter*> (this);
  105.                 return self->GetStringID(pString, false) != XMLCPB_INVALID_ID;
  106.         }
  107.  
  108.         StringID AddString(const char* pString, size_t hash);
  109.  
  110.         typedef std::vector<FlatAddr> FlatAddrVec;
  111.         void CalculateFlatAddrs(FlatAddrVec& outFlatAddrs);
  112.  
  113. private:
  114.  
  115.         CBufferWriter      m_buffer;                  // holds all strings, including the end '0's.
  116.         std::vector<SAddr> m_stringAddrs;             // pointers into m_buffer
  117.         typedef std::unordered_set<StringID, StringHasher, StringHasher, AttrStringAllocator<StringID>> StringHasherType;
  118.         StringHasherType   m_sortedIndexes;
  119.         CWriter*           m_pWriter;
  120.         int                m_maxNumStrings;                  // used for checks. we need to control it because in the final data we use a limited amount of bits to store the ID. This amount of bits can be diferent for diferent types of string (tags, attrnames, strdata)
  121.         const char*        m_pCheckString;                   // only used when comparing strings inside the GetStringID function. it should be NULL otherwise
  122.         SAddr              m_checkAddr;                      // ditto, used for the hash
  123. };
  124.  
  125. }  // end namespace
  126.  
  127. #endif
  128.  
downloadXMLCPB_StringTableWriter.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