StringTable.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief A simple class to encode and decode known strings based on a lookup table. Similar to the StringCompressor class.
  12. ///
  13. #ifndef __STRING_TABLE_H
  14. #define __STRING_TABLE_H
  15. #include "DS_OrderedList.h"
  16. #include "Export.h"
  17. #include "RakMemoryOverride.h"
  18. /// Forward declaration
  19. namespace RakNet
  20. {
  21. class BitStream;
  22. };
  23. /// StringTableType should be the smallest type possible, or else it defeats the purpose of the StringTable class, which is to save bandwidth.
  24. typedef unsigned char StringTableType;
  25. /// The string plus a bool telling us if this string was copied or not.
  26. struct StrAndBool
  27. {
  28. char *str;
  29. bool b;
  30. };
  31. namespace RakNet
  32. {
  33. int RAK_DLL_EXPORT StrAndBoolComp( char *const &key, const StrAndBool &data );
  34. /// \details This is an even more efficient alternative to StringCompressor in that it writes a single byte from a lookup table and only does compression.<BR>
  35. /// if the string does not already exist in the table.<BR>
  36. /// All string tables must match on all systems - hence you must add all the strings in the same order on all systems.<BR>
  37. /// Furthermore, this must be done before sending packets that use this class, since the strings are ordered for fast lookup. Adding after that time would mess up all the indices so don't do it.<BR>
  38. /// Don't use this class to write strings which were not previously registered with AddString, since you just waste bandwidth then. Use StringCompressor instead.
  39. /// \brief Writes a string index, instead of the whole string
  40. class RAK_DLL_EXPORT StringTable
  41. {
  42. public:
  43. // Destructor
  44. ~StringTable();
  45. /// static function because only static functions can access static members
  46. /// The RakPeer constructor adds a reference to this class, so don't call this until an instance of RakPeer exists, or unless you call AddReference yourself.
  47. /// \return the unique instance of the StringTable
  48. static StringTable* Instance(void);
  49. /// Add a string to the string table.
  50. /// \param[in] str The string to add to the string table
  51. /// \param[in] copyString true to make a copy of the passed string (takes more memory), false to not do so (if your string is in static memory).
  52. void AddString(const char *str, bool copyString);
  53. /// Writes input to output, compressed. Takes care of the null terminator for you.
  54. /// Relies on the StringCompressor class, which is automatically reference counted in the constructor and destructor in RakPeer. You can call the reference counting functions yourself if you wish too.
  55. /// \param[in] input Pointer to an ASCII string
  56. /// \param[in] maxCharsToWrite The size of \a input
  57. /// \param[out] output The bitstream to write the compressed string to
  58. void EncodeString( const char *input, int maxCharsToWrite, RakNet::BitStream *output );
  59. /// Writes input to output, uncompressed. Takes care of the null terminator for you.
  60. /// Relies on the StringCompressor class, which is automatically reference counted in the constructor and destructor in RakPeer. You can call the reference counting functions yourself if you wish too.
  61. /// \param[out] output A block of bytes to receive the output
  62. /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A NULL terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated.
  63. /// \param[in] input The bitstream containing the compressed string
  64. bool DecodeString( char *output, int maxCharsToWrite, RakNet::BitStream *input );
  65. /// Used so I can allocate and deallocate this singleton at runtime
  66. static void AddReference(void);
  67. /// Used so I can allocate and deallocate this singleton at runtime
  68. static void RemoveReference(void);
  69. /// Private Constructor
  70. StringTable();
  71. protected:
  72. /// Called when you mess up and send a string using this class that was not registered with AddString
  73. /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A NULL terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated.
  74. void LogStringNotFound(const char *strName);
  75. /// Singleton instance
  76. static StringTable *instance;
  77. static int referenceCount;
  78. DataStructures::OrderedList<char *, StrAndBool, StrAndBoolComp> orderedStringList;
  79. };
  80. }
  81. #endif
粤ICP备19079148号