dynalink.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // dynalink.h
  21. //
  22. // History:
  23. // 01/10/96 JMI Started.
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26. //
  27. // This object implements linking at run time. We do this by keeping an array
  28. // of function pointers in a class whose constructor fills an array element
  29. // with a function pointer. We are guaranteed to be able to access the
  30. // function safely (i.e., we know it's already allocated) b/c it is a static
  31. // member of the constructing class.
  32. //
  33. // Use the macros below to instantiate your class and to provide easy user
  34. // implementation.
  35. //
  36. //////////////////////////////////////////////////////////////////////////////
  37. #ifndef DYNALINK_H
  38. #define DYNALINK_H
  39. //////////////////////////////////////////////////////////////////////////////
  40. // Includes.
  41. //////////////////////////////////////////////////////////////////////////////
  42. #include "common/bdebug.h"
  43. //////////////////////////////////////////////////////////////////////////////
  44. // Macros.
  45. //////////////////////////////////////////////////////////////////////////////
  46. // Use this macro to instantiate your class and its array of function ptrs.
  47. // FunctionTypedef is a typedef of a pointer to your function type.
  48. // FriendClass is a class to befriend this one (allowing it, and only it,
  49. // to access the function array.
  50. // MaxElements is the maximum number of functions in your array list. It must
  51. // be 1 or greater.
  52. #define LINKINSTANTIATE(FunctionTypedef, FriendClass, MaxElements) \
  53. FunctionTypedef \
  54. CDynaLink<FunctionTypedef, FriendClass, MaxElements>::ms_afp[MaxElements] \
  55. = { NULL, }
  56. // Use this macro to create a macro for your users to use to link.
  57. // You can use this function so you don't have to remember any template
  58. // declaration crap. Create a macro in your header for your users that calls
  59. // LINKLATE similar to the following:
  60. // #define LINKME(pUserFunc, lUserFuncIndex) \
  61. // LINKLATE([Your Function Typedef], [Your Friend Class], [Max functions], \
  62. // pUserFunc, lUserFuncIndex)
  63. // FunctionTypedef is a typedef of a pointer to your function type.
  64. // FriendClass is a class to befriend this one (allowing it, and only it,
  65. // to access the function array.
  66. // MaxElements is the maximum number of functions in your array list. It must
  67. // be 1 or greater.
  68. // pUserFunc is a pointer to the user's function.
  69. // lUserFuncIndex is the index in the function array to be filled with
  70. // pUserFunc.
  71. // Note that dl##FunctionTypedef##lUserFuncIndex creates a name for the
  72. // CDynaLink that is specific for each element of the array which means
  73. // one module can link more than one function and allows easy identification
  74. // of the CDynaLink during compile, link, and debug steps. This means,
  75. // however, you cannot put anything for lUserFuncIndex that would make an
  76. // invalid variable name (i.e., a + 1, MACRO(ARG), etc. are sux).
  77. #define LINKLATE(FunctionTypedef, FriendClass, MaxElements, pUserFunc, lUserFuncIndex) \
  78. static CDynaLink<FunctionTypedef, FriendClass, MaxElements> \
  79. dl##FunctionTypedef##lUserFuncIndex(pUserFunc, lUserFuncIndex)
  80. // To access a function from your array.
  81. // FunctionTypedef is a typedef of a pointer to your function type.
  82. // FriendClass is a class to befriend this one (allowing it, and only it,
  83. // to access the function array.
  84. // MaxElements is the maximum number of functions in your array list. It must
  85. // be 1 or greater.
  86. // lUserFuncIndex is the index in the function array to be filled with
  87. // pUserFunc.
  88. // Returns a pointer to the user function.
  89. #define GETLINKFUNC(FunctionTypedef, FriendClass, MaxElements, lUserFuncIndex) \
  90. (CDynaLink<FunctionTypedef, FriendClass, MaxElements>::ms_afp[lUserFuncIndex])
  91. //////////////////////////////////////////////////////////////////////////////
  92. // Typedefs.
  93. //////////////////////////////////////////////////////////////////////////////
  94. template <class FUNCTYPEPTR, class FRIEND, long lMax> class CDynaLink
  95. {
  96. public:
  97. // Constructor Especial.
  98. CDynaLink(FUNCTYPEPTR pfn, long lIndex)
  99. {
  100. ASSERT(lIndex < lMax);
  101. ASSERT(ms_afp[lIndex] == NULL);
  102. ms_afp[lIndex] = pfn;
  103. }
  104. friend FRIEND;
  105. protected:
  106. static FUNCTYPEPTR ms_afp[lMax];
  107. };
  108. #endif // DYNALINK_H