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.

166 lines
6.2 KiB

  1. // slbRCComp.h -- Comparator helpers for reference counting smart pointer.
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #if !defined(SLB_RCCOMP_H)
  8. #define SLB_RCCOMP_H
  9. #include <functional> // for binary_function
  10. namespace slbRefCnt {
  11. // slbRCComp.h declares several helpers to deal with smart pointer
  12. // testing and comparisons as if they were real pointers.
  13. //
  14. // Testing and comparing pointer smart pointers to one another is
  15. // problematic. A smart pointer (reference couting pointer)
  16. // represents a handle to the actual (dumb) pointer of interest.
  17. // There is no straight-forward way to compare the dumb pointers
  18. // without allowing the clients direct access to the dump pointer and
  19. // bypassing all the features the smart pointer is trying keep intact.
  20. // There are solutions but they usually require constructs that aren't
  21. // natural for pointers.
  22. //
  23. // The facilities defined in this header provide the primitives for
  24. // the smart pointers to be compared in a syntactically natural way
  25. // without allowing heterogeneous comparisons and that won't violate
  26. // the protections the smart pointers provide.
  27. //
  28. // Meyers describes some of these peculiar pointer comparisons in
  29. // Item #28 found in the book "More Effective C++," Scott Meyers,
  30. // Addison-Wesley, 1996.
  31. // Problem: Comparing pointer values of the smart pointers to one
  32. // another is problematic. A smart pointer (reference couting
  33. // pointer) represents a handle to the actual (dumb) pointer of
  34. // interest. There is no straight-forward way to compare the dumb
  35. // pointers without allowing the clients direct access to the dump
  36. // pointer and bypassing all the features the smart pointer is trying
  37. // keep intact.
  38. // Solution: Provide a set of comparators, or comparison functors
  39. // (function objects), that perform the appropriate comparisons.
  40. // These comparators are referenced by the RCPtr and GRCPtr classes to
  41. // carryout the pointer comparisons.
  42. //
  43. // An abstract Predicate struct is defined to establish the functor
  44. // interface. All predicates used by Comparators must be derived from
  45. // this class. These predicate functors are passed const versions of
  46. // the dumb pointers the smart pointer represents. The functor
  47. // performs the comparison returning the bool result. Since const
  48. // versions of the dumb pointers are used, then exposure of the dumb
  49. // pointer is limited.
  50. //
  51. // Two sets of comparators are defined which should handle most of the
  52. // cases. The first is a shallow comparator which compares the two
  53. // dumb pointer values using ==. The second is deep comparator which
  54. // compares the objects the dumb pointers reference, testing for
  55. // equivalence.
  56. //
  57. // WARNING: Using the DeepComparator, any complex object being being
  58. // compared to another will have to define either an operator==,
  59. // operator< or both to carry out the comparison.
  60. // template struct Predicate -- abstract functor definition for
  61. // elements of Comparator.
  62. template<class T>
  63. struct Predicate : public std::binary_function<T const *, T const *, bool>
  64. {
  65. public:
  66. result_type operator()(first_argument_type lhs,
  67. second_argument_type rhs) const;
  68. };
  69. template<class T>
  70. struct ShallowEquatesTester : public Predicate<T>
  71. {
  72. public:
  73. result_type operator()(first_argument_type lhs, second_argument_type rhs)
  74. const { return lhs == rhs; };
  75. };
  76. template<class T>
  77. struct DeepEquatesTester : public Predicate<T>
  78. {
  79. public:
  80. result_type operator()(first_argument_type lhs, second_argument_type rhs)
  81. const { return *lhs == *rhs; };
  82. };
  83. template<class T>
  84. struct ShallowLessTester : public Predicate<T>
  85. {
  86. public:
  87. result_type operator()(first_argument_type lhs, second_argument_type rhs)
  88. const { return lhs < rhs; };
  89. };
  90. template<class T>
  91. struct DeepLessTester : public Predicate<T>
  92. {
  93. public:
  94. result_type operator()(first_argument_type lhs, second_argument_type rhs)
  95. const { return *lhs < *rhs; };
  96. };
  97. // template struct Comparator -- Aggregation of comparison predicate
  98. // functors
  99. //
  100. // Comparator is a template defining the aggregation of the comparison
  101. // functors (function objects) used by the pointer comparison
  102. // operators ==, !=, <, >, <= and >= in the RCPtr and GRCPtr classes
  103. // (see slbRCPtr.h and slbGRCPtr.h). The RCPtr and GRCPtr reference
  104. // the specified comparator to access the appropriate predicate
  105. // functor to compare the pointer values these reference counting
  106. // (smart) pointers represent.
  107. //
  108. // Two comparators are predefined. First is ShallowComparator for
  109. // testing relative equality. Second is DeepComparator for testing
  110. // relative equivalency of the pointers by calling operator== of the
  111. // object being reference counted.
  112. //
  113. // The DeepComparator is provided since smart pointers can be used as
  114. // "handles" to other "body" objects. As such, one needs to be able
  115. // to compare their bodies as if there was a direct reference to the
  116. // body while maintaining syntactic integrity without exposing the
  117. // body to the client code.
  118. //
  119. // CONSTRAINTS: When using DeepComparator, the body class (the
  120. // derivation of RCObject) must have the corresponding comparison operator
  121. // defined for that class, operator== and/or operator<.
  122. //
  123. // Clients may define their own comparator by deriving from Comparator
  124. // and specifying derived class when instantiating an RCPtr or GRCPtr
  125. // template.
  126. template<class EquatesTester, class LessTester>
  127. struct Comparator
  128. {
  129. public:
  130. // Predicates
  131. EquatesTester Equates;
  132. LessTester IsLess;
  133. };
  134. // template struct ShallowComparator -- minimal set of comparison
  135. // functors for testing equality.
  136. template<class T>
  137. struct ShallowComparator : public Comparator<ShallowEquatesTester<T>,
  138. ShallowLessTester<T> >
  139. {
  140. };
  141. // template struct DeepComparator -- minimal set of comparison
  142. // functors for testing equivalency.
  143. template<class T>
  144. struct DeepComparator : public Comparator<DeepEquatesTester<T>,
  145. DeepLessTester<T> >
  146. {
  147. };
  148. } // namespace
  149. #endif // SLB_RCCOMP_H