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.

132 lines
3.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: varntcmp.hxx
  7. //
  8. // Contents: Variant Comparator
  9. //
  10. // Classes: CCompareVariants
  11. //
  12. // Functions:
  13. //
  14. // History: 5-01-95 srikants Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #pragma once
  18. #include <compare.hxx>
  19. class CPathStore;
  20. //+---------------------------------------------------------------------------
  21. //
  22. // Class: CCompareVariants
  23. //
  24. // Purpose: A comparator for a pair of variants of the same type.
  25. //
  26. // History: 5-23-95 srikants Created
  27. //
  28. // Notes:
  29. //
  30. //----------------------------------------------------------------------------
  31. class CCompareVariants
  32. {
  33. enum { eSortDescend = 0x1, eSortNullFirst = 0x2 };
  34. public:
  35. CCompareVariants() : _dir(0), _vt(VT_EMPTY), _iDirMult(1), _CompareFn(0) {}
  36. // CCompareVariants( ULONG dir, VARENUM vt )
  37. CCompareVariants( ULONG dir, VARTYPE vt )
  38. {
  39. Init( dir, vt );
  40. }
  41. // void Init( ULONG dir, VARENUM vt = VT_EMPTY )
  42. void Init( ULONG dir, VARTYPE vt = VT_EMPTY )
  43. {
  44. _dir = dir;
  45. _vt = vt;
  46. _iDirMult = ( ( dir & eSortDescend ) != 0 ) ? -1 : 1;
  47. _CompareFn = CComparators::GetComparator( (VARENUM) _vt );
  48. }
  49. int Compare( const PROPVARIANT * pVarnt1, const PROPVARIANT * pVarnt2 );
  50. int GetDirMult() const { return _iDirMult; }
  51. private:
  52. void _UpdateCompare( VARTYPE vt );
  53. ULONG _dir; // direction of comparison (ascent/descend)
  54. int _iDirMult; // -1 for descending; +1 for ascending
  55. FCmp _CompareFn; // Function used to compare variants of
  56. // type '_vt'
  57. // ULONG _vt; // Type of the variant currently initialized.
  58. VARTYPE _vt; // Type of the variant currently initialized.
  59. };
  60. inline
  61. int CCompareVariants::Compare( const PROPVARIANT * pVarnt1, const PROPVARIANT * pVarnt2 )
  62. {
  63. int iComp = 0;
  64. Win4Assert( 0 != pVarnt1 && 0 != pVarnt2 );
  65. VARTYPE vt1 = pVarnt1->vt;
  66. VARTYPE vt2 = pVarnt2->vt;
  67. if ( vt1 == VT_EMPTY )
  68. {
  69. if ( vt2 == VT_EMPTY )
  70. iComp = 0;
  71. else
  72. iComp = (_dir & eSortNullFirst) != 0 ? -1 : 1;
  73. }
  74. else if ( vt2 == VT_EMPTY )
  75. {
  76. iComp = (_dir & eSortNullFirst) != 0 ? 1 : -1;
  77. }
  78. else
  79. {
  80. //
  81. // Both are non empty.
  82. //
  83. if ( vt1 != vt2 )
  84. {
  85. iComp = vt2 - vt1;
  86. }
  87. else
  88. {
  89. if ( _vt != vt1 )
  90. {
  91. _UpdateCompare( (VARTYPE) vt1 );
  92. }
  93. if ( 0 != _CompareFn )
  94. {
  95. iComp = _CompareFn( *pVarnt1, *pVarnt2 );
  96. }
  97. }
  98. iComp *= _iDirMult;
  99. }
  100. return iComp;
  101. }
  102. inline
  103. void CCompareVariants::_UpdateCompare( VARTYPE vt )
  104. {
  105. _vt = vt;
  106. _CompareFn = CComparators::GetComparator( (VARENUM) vt );
  107. }