FunctionLexicon.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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: FunctionLexicon.h ////////////////////////////////////////////////////////////////////////
  24. // Created: Colin Day, September 2001
  25. // Desc: Collection of function pointers to help us in managing
  26. // and assign callbacks
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. #pragma once
  29. #ifndef __FUNCTIONLEXICON_H_
  30. #define __FUNCTIONLEXICON_H_
  31. #include "Common/SubsystemInterface.h"
  32. #include "Common/NameKeyGenerator.h"
  33. #include "Common/GameMemory.h"
  34. #include "GameClient/GameWindow.h"
  35. #include "GameClient/WindowLayout.h"
  36. //-------------------------------------------------------------------------------------------------
  37. /** Collection of function pointers to help us in managing callbacks */
  38. //-------------------------------------------------------------------------------------------------
  39. class FunctionLexicon : public SubsystemInterface
  40. {
  41. public:
  42. struct TableEntry
  43. {
  44. NameKeyType key;
  45. const char *name;
  46. void *func;
  47. };
  48. enum TableIndex
  49. {
  50. TABLE_ANY = -1, ///< use this when searching to search any table
  51. TABLE_GAME_WIN_SYSTEM = 0,
  52. TABLE_GAME_WIN_INPUT,
  53. TABLE_GAME_WIN_TOOLTIP,
  54. TABLE_GAME_WIN_DEVICEDRAW,
  55. TABLE_GAME_WIN_DRAW,
  56. TABLE_WIN_LAYOUT_INIT,
  57. TABLE_WIN_LAYOUT_DEVICEINIT,
  58. TABLE_WIN_LAYOUT_UPDATE,
  59. TABLE_WIN_LAYOUT_SHUTDOWN,
  60. MAX_FUNCTION_TABLES // keep this last
  61. };
  62. public:
  63. FunctionLexicon( void );
  64. virtual ~FunctionLexicon( void );
  65. virtual void init( void );
  66. virtual void reset( void );
  67. virtual void update( void );
  68. /// validate the tables and make sure all entries are unique
  69. Bool validate( void );
  70. /// get internal function table
  71. TableEntry *getTable( TableIndex index );
  72. //
  73. // !NOTE! We do NOT have a functionToName() method becuase we assume
  74. // that functions in the tables are unique and that there is a 1 to 1
  75. // mapping of a symbol to a function address. However, when compiling
  76. // in release, functions that have the same arguments and the same
  77. // body (mainly empty stub functions) get optimized to the
  78. // SAME ADDRESS. That destroyes our 1 to 1 mapping so it is something
  79. // that we must avoid
  80. //
  81. // translate a function pointer to its symbolic name
  82. // char *functionToName( void *func );
  83. // Game window functions ------------------------------------------------------------------------
  84. GameWinSystemFunc gameWinSystemFunc( NameKeyType key, TableIndex = TABLE_GAME_WIN_SYSTEM );
  85. GameWinInputFunc gameWinInputFunc( NameKeyType key, TableIndex = TABLE_GAME_WIN_INPUT );
  86. GameWinTooltipFunc gameWinTooltipFunc( NameKeyType key, TableIndex = TABLE_GAME_WIN_TOOLTIP );
  87. GameWinDrawFunc gameWinDrawFunc( NameKeyType key, TableIndex = TABLE_ANY );
  88. // Window layout functions ----------------------------------------------------------------------
  89. WindowLayoutInitFunc winLayoutInitFunc( NameKeyType key, TableIndex = TABLE_ANY );
  90. WindowLayoutUpdateFunc winLayoutUpdateFunc( NameKeyType key, TableIndex = TABLE_WIN_LAYOUT_UPDATE );
  91. WindowLayoutShutdownFunc winLayoutShutdownFunc( NameKeyType key, TableIndex = TABLE_WIN_LAYOUT_SHUTDOWN );
  92. protected:
  93. /// load a lookup table with run time values needed and save in table list
  94. void loadTable( TableEntry *table, TableIndex tableIndex );
  95. /** given a key find the function, the index parameter can limit the search
  96. to a single table or to ANY of the tables */
  97. void *findFunction( NameKeyType key, TableIndex index );
  98. #ifdef NOT_IN_USE
  99. const char *funcToName( void *func, TableEntry *table ); ///< internal searching
  100. #endif
  101. void *keyToFunc( NameKeyType key, TableEntry *table ); ///< internal searching
  102. TableEntry *m_tables[ MAX_FUNCTION_TABLES ]; ///< the lookup tables
  103. }; // end class FunctionLexicon
  104. ///////////////////////////////////////////////////////////////////////////////////////////////////
  105. // INLINING
  106. ///////////////////////////////////////////////////////////////////////////////////////////////////
  107. inline FunctionLexicon::TableEntry *FunctionLexicon::getTable( TableIndex index )
  108. { return m_tables[ index ]; }
  109. inline GameWinSystemFunc FunctionLexicon::gameWinSystemFunc( NameKeyType key, TableIndex index )
  110. { return (GameWinSystemFunc)findFunction( key, index ); }
  111. inline GameWinInputFunc FunctionLexicon::gameWinInputFunc( NameKeyType key, TableIndex index )
  112. { return (GameWinInputFunc)findFunction( key, index ); }
  113. inline GameWinTooltipFunc FunctionLexicon::gameWinTooltipFunc( NameKeyType key, TableIndex index )
  114. { return (GameWinTooltipFunc)findFunction( key, index ); }
  115. inline WindowLayoutUpdateFunc FunctionLexicon::winLayoutUpdateFunc( NameKeyType key, TableIndex index )
  116. { return (WindowLayoutUpdateFunc)findFunction( key, index ); }
  117. inline WindowLayoutShutdownFunc FunctionLexicon::winLayoutShutdownFunc( NameKeyType key, TableIndex index )
  118. { return (WindowLayoutShutdownFunc)findFunction( key, index ); }
  119. ///////////////////////////////////////////////////////////////////////////////////////////////////
  120. // EXTERNALS
  121. ///////////////////////////////////////////////////////////////////////////////////////////////////
  122. extern FunctionLexicon *TheFunctionLexicon; ///< function dictionary external
  123. #endif // end __FUNCTIONLEXICON_H_