NameKeyGenerator.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: NameKeyGenerator.h ///////////////////////////////////////////////////////////////////////
  24. // Created: Michael Booth, May 2001
  25. // Colin Day, May 2001
  26. // Desc: Name key system to translate between names and unique key ids
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. #pragma once
  29. #ifndef __NAMEKEYGENERATOR_H_
  30. #define __NAMEKEYGENERATOR_H_
  31. #include "Lib/BaseType.h"
  32. #include "Common/SubsystemInterface.h"
  33. #include "Common/GameMemory.h"
  34. #include "Common/AsciiString.h"
  35. //-------------------------------------------------------------------------------------------------
  36. /**
  37. Note that NameKeyType isn't a "real" enum, but an enum type used to enforce the
  38. fact that NameKeys are really magic cookies, and aren't really interchangeable
  39. with ints. NAMEKEY_INVALID is always a legal value, but all other values are dynamically
  40. determined at runtime. (The generated code is basically identical, of course.)
  41. */
  42. //-------------------------------------------------------------------------------------------------
  43. enum NameKeyType
  44. {
  45. NAMEKEY_INVALID = 0,
  46. NAMEKEY_MAX = 1<<23, // max ordinal value of a NameKey (some code relies on these fitting into 24 bits safely)
  47. FORCE_NAMEKEYTYPE_LONG = 0x7fffffff // a trick to ensure the NameKeyType is a 32-bit int
  48. };
  49. //-------------------------------------------------------------------------------------------------
  50. /** A bucket entry for the name key generator */
  51. //-------------------------------------------------------------------------------------------------
  52. class Bucket : public MemoryPoolObject
  53. {
  54. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( Bucket, "NameKeyBucketPool" );
  55. public:
  56. Bucket();
  57. //~Bucket();
  58. Bucket *m_nextInSocket;
  59. NameKeyType m_key;
  60. AsciiString m_nameString;
  61. };
  62. inline Bucket::Bucket() : m_nextInSocket(NULL), m_key(NAMEKEY_INVALID) { }
  63. inline Bucket::~Bucket() { }
  64. //-------------------------------------------------------------------------------------------------
  65. /** This class implements the conversion of an arbitrary string into a unique
  66. * integer "key". Calling the nameToKey() method with the same string is
  67. * guaranteed to return the same key. Also, all keys generated by an
  68. * instance of this class are guaranteed to be unique with respect to that
  69. * instance's catalog of names. Multiple instances of this class can be
  70. * created to service multiple namespaces. */
  71. //-------------------------------------------------------------------------------------------------
  72. class NameKeyGenerator : public SubsystemInterface
  73. {
  74. public:
  75. NameKeyGenerator();
  76. virtual ~NameKeyGenerator();
  77. virtual void init();
  78. virtual void reset();
  79. virtual void update() { }
  80. /// Given a string, convert into a unique integer key.
  81. NameKeyType nameToKey(const AsciiString& name) { return nameToKey(name.str()); }
  82. NameKeyType nameToLowercaseKey(const AsciiString& name) { return nameToLowercaseKey(name.str()); }
  83. /// Given a string, convert into a unique integer key.
  84. NameKeyType nameToKey(const char* name);
  85. NameKeyType nameToLowercaseKey(const char *name);
  86. /**
  87. given a key, return the name. this is almost never needed,
  88. except for a few rare cases like object serialization. also
  89. note that it's not particularly fast; it does a dumb linear
  90. search for the key.
  91. */
  92. AsciiString keyToName(NameKeyType key);
  93. // Get a string out of the INI. Store it into a NameKeyType
  94. static void parseStringAsNameKeyType( INI *ini, void *instance, void *store, const void* userData );
  95. private:
  96. enum
  97. {
  98. // socketcount should be prime, and not "close" to a power of 2, for best results.
  99. SOCKET_COUNT = 45007
  100. };
  101. void freeSockets();
  102. Bucket* m_sockets[SOCKET_COUNT]; ///< Catalog of all Buckets already generated
  103. UnsignedInt m_nextID; ///< Next available ID
  104. }; // end class NameKeyGenerator
  105. //-------------------------------------------------------------------------------------------------
  106. // Externals
  107. //-------------------------------------------------------------------------------------------------
  108. extern NameKeyGenerator *TheNameKeyGenerator; ///< just one namespace for now
  109. // typing "TheNameKeyGenerator->nameToKey()" is awfully wordy. Here are shorter synonyms:
  110. inline NameKeyType NAMEKEY(const AsciiString& name) { return TheNameKeyGenerator->nameToKey(name); }
  111. inline NameKeyType NAMEKEY(const char* name) { return TheNameKeyGenerator->nameToKey(name); }
  112. inline AsciiString KEYNAME(NameKeyType nk) { return TheNameKeyGenerator->keyToName(nk); }
  113. //-------------------------------------------------------------------------------------------------
  114. class StaticNameKey
  115. {
  116. private:
  117. mutable NameKeyType m_key;
  118. const char* m_name;
  119. public:
  120. StaticNameKey(const char* p) : m_key(NAMEKEY_INVALID), m_name(p) {}
  121. NameKeyType key() const;
  122. // ugh, this is a little hokey, but lets us pretend that a StaticNameKey == NameKeyType
  123. inline operator NameKeyType() const { return key(); }
  124. };
  125. #endif // __NAMEKEYGENERATOR_H_