Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.9 KiB

  1. //--------------------------------------------------------------------
  2. // An example of how to create a wrapper for CLKRHashTable
  3. //--------------------------------------------------------------------
  4. #include <lkrhash.h>
  5. #ifndef __LKRHASH_NO_NAMESPACE__
  6. #define LKRHASH_NS LKRhash
  7. // using namespace LKRhash;
  8. #else // __LKRHASH_NO_NAMESPACE__
  9. #define LKRHASH_NS
  10. #endif // __LKRHASH_NO_NAMESPACE__
  11. #ifndef __HASHFN_NO_NAMESPACE__
  12. #define HASHFN_NS HashFn
  13. // using namespace HashFn;
  14. #else // __HASHFN_NO_NAMESPACE__
  15. #define HASHFN_NS
  16. #endif // __HASHFN_NO_NAMESPACE__
  17. // some random class
  18. class CTest
  19. {
  20. public:
  21. enum {BUFFSIZE=20};
  22. int m_n; // This will also be a key
  23. char m_sz[BUFFSIZE]; // This will be the primary key
  24. bool m_fWhatever;
  25. mutable LONG m_cRefs; // Reference count for lifetime management.
  26. // Must be mutable to use 'const CTest*' in
  27. // hashtables
  28. CTest(int n, const char* psz, bool f)
  29. : m_n(n), m_fWhatever(f), m_cRefs(0)
  30. {
  31. strncpy(m_sz, psz, BUFFSIZE-1);
  32. m_sz[BUFFSIZE-1] = '\0';
  33. }
  34. ~CTest()
  35. {
  36. IRTLASSERT(m_cRefs == 0);
  37. }
  38. };
  39. // A typed hash table of CTests, keyed on the string field. Case-insensitive.
  40. class CStringTestHashTable
  41. : public LKRHASH_NS::CTypedHashTable<CStringTestHashTable,
  42. const CTest, const char*>
  43. {
  44. public:
  45. CStringTestHashTable()
  46. : LKRHASH_NS::CTypedHashTable<CStringTestHashTable, const CTest,
  47. const char*>("string",
  48. LK_DFLT_MAXLOAD,
  49. LK_SMALL_TABLESIZE,
  50. LK_DFLT_NUM_SUBTBLS)
  51. {}
  52. static const char*
  53. ExtractKey(const CTest* pTest)
  54. {
  55. return pTest->m_sz;
  56. }
  57. static DWORD
  58. CalcKeyHash(const char* pszKey)
  59. {
  60. return HASHFN_NS::HashStringNoCase(pszKey);
  61. }
  62. static bool
  63. EqualKeys(const char* pszKey1, const char* pszKey2)
  64. {
  65. return _stricmp(pszKey1, pszKey2) == 0;
  66. }
  67. static void
  68. AddRefRecord(const CTest* pTest, LK_ADDREF_REASON lkar)
  69. {
  70. if (lkar > 0)
  71. {
  72. // or, perhaps, pIFoo->AddRef() (watch out for marshalling)
  73. // or ++pTest->m_cRefs (single-threaded only)
  74. InterlockedIncrement(&pTest->m_cRefs);
  75. }
  76. else if (lkar < 0)
  77. {
  78. // or, perhaps, pIFoo->Release() or --pTest->m_cRefs;
  79. LONG l = InterlockedDecrement(&pTest->m_cRefs);
  80. // For some hashtables, it may also make sense to add the following
  81. // if (l == 0) delete pTest;
  82. // but that would typically only apply when InsertRecord was
  83. // used thus
  84. // lkrc = ht.InsertRecord(new CTest(foo, bar));
  85. }
  86. else
  87. IRTLASSERT(0);
  88. IRTLTRACE("AddRef(%p, %s) %d, cRefs == %d\n",
  89. pTest, pTest->m_sz, lkar, pTest->m_cRefs);
  90. }
  91. };
  92. // Another typed hash table of CTests. This one is keyed on the numeric field.
  93. class CNumberTestHashTable
  94. : public LKRHASH_NS::CTypedHashTable<CNumberTestHashTable,
  95. const CTest, int>
  96. {
  97. public:
  98. CNumberTestHashTable()
  99. : LKRHASH_NS::CTypedHashTable<CNumberTestHashTable, const CTest, int>(
  100. "number") {}
  101. static int ExtractKey(const CTest* pTest) {return pTest->m_n;}
  102. static DWORD CalcKeyHash(int nKey) {return HASHFN_NS::Hash(nKey);}
  103. static bool EqualKeys(int nKey1, int nKey2) {return nKey1 == nKey2;}
  104. static void AddRefRecord(const CTest* pTest, LK_ADDREF_REASON lkar)
  105. {
  106. int nIncr = (lkar > 0) ? +1 : -1;
  107. InterlockedExchangeAdd(&pTest->m_cRefs, nIncr);
  108. IRTLTRACE("AddRef(%p, %d) %d (%d), cRefs == %d\n",
  109. pTest, pTest->m_n, nIncr, (int) lkar, pTest->m_cRefs);
  110. }
  111. };