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.

220 lines
6.8 KiB

  1. // Procedural.h -- Procedural binder and adapter template classes
  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(SLBCSP_PROCEDURAL_H)
  8. #define SLBCSP_PROCEDURAL_H
  9. #include <functional>
  10. // Template classes that support composition of procedure objects
  11. // (proctors). Proctors are like functors (function objects) but
  12. // don't return a value (void). The template classes defined here
  13. // follow C++ member function binders and adapters.
  14. /////////////////////////// BINDERS /////////////////////////////////
  15. template<class Arg>
  16. struct UnaryProcedure
  17. : public std::unary_function<Arg, void>
  18. {};
  19. template<class Arg1, class Arg2>
  20. struct BinaryProcedure
  21. : public std::binary_function<Arg1, Arg2, void>
  22. {};
  23. template<class BinaryProc>
  24. class ProcedureBinder2nd
  25. : public UnaryProcedure<BinaryProc::first_argument_type>
  26. {
  27. public:
  28. ProcedureBinder2nd(BinaryProc const &rproc,
  29. BinaryProc::second_argument_type const &rarg)
  30. : m_proc(rproc),
  31. m_arg2(rarg)
  32. {}
  33. void
  34. operator()(argument_type const &arg1) const
  35. {
  36. m_proc(arg1, m_arg2);
  37. }
  38. protected:
  39. BinaryProc m_proc;
  40. BinaryProc::second_argument_type m_arg2;
  41. };
  42. template<class BinaryProc, class T>
  43. ProcedureBinder2nd<BinaryProc>
  44. ProcedureBind2nd(BinaryProc const &rProc, T const &rv)
  45. {
  46. return ProcedureBinder2nd<BinaryProc>(rProc, BinaryProc::second_argument_type(rv));
  47. };
  48. //////////////////// MEMBER PROCEDURE ADAPTERS //////////////////////////
  49. template<class T>
  50. class MemberProcedureType
  51. : public UnaryProcedure<T *>
  52. {
  53. public:
  54. // Types
  55. // C'tors/D'tors
  56. explicit
  57. MemberProcedureType(void (T::* p)())
  58. : m_pmp(p)
  59. {}
  60. // Operators
  61. void
  62. operator()(T *p) const
  63. {
  64. (p->*m_pmp)();
  65. }
  66. // Operations
  67. // Access
  68. // Predicates
  69. protected:
  70. // Types
  71. // C'tors/D'tors
  72. // Operators
  73. // Operations
  74. // Access
  75. // Predicates
  76. // Variables
  77. private:
  78. // Types
  79. // C'tors/D'tors
  80. // Operators
  81. // Operations
  82. // Access
  83. // Predicates
  84. // Variables
  85. void (T::* m_pmp)();
  86. };
  87. template<class T>
  88. MemberProcedureType<T>
  89. MemberProcedure(void (T::* p)())
  90. {
  91. return MemberProcedureType<T>(p);
  92. };
  93. //////////////////// POINTER TO PROCEDURE ADAPTERS ///////////////////////
  94. template<class T1, class T2>
  95. class PointerToBinaryProcedure
  96. : public BinaryProcedure<T1, T2>
  97. {
  98. public:
  99. // Types
  100. // C'tors/D'tors
  101. explicit
  102. PointerToBinaryProcedure(void (*p)(T1, T2))
  103. : m_p(p)
  104. {}
  105. // Operators
  106. void
  107. operator()(T1 arg1,
  108. T2 arg2) const
  109. {
  110. m_p(arg1, arg2);
  111. }
  112. // Operations
  113. // Access
  114. // Predicates
  115. protected:
  116. // Types
  117. // C'tors/D'tors
  118. // Operators
  119. // Operations
  120. // Access
  121. // Predicates
  122. // Variables
  123. void (*m_p)(T1, T2);
  124. private:
  125. // Types
  126. // C'tors/D'tors
  127. // Operators
  128. // Operations
  129. // Access
  130. // Predicates
  131. // Variables
  132. };
  133. template<class T1, class T2>
  134. PointerToBinaryProcedure<T1, T2>
  135. PointerProcedure(void (*p)(T1, T2))
  136. {
  137. return PointerToBinaryProcedure<T1, T2>(p);
  138. };
  139. template<class T>
  140. class PointerToUnaryProcedure
  141. : public UnaryProcedure<T>
  142. {
  143. public:
  144. // Types
  145. // C'tors/D'tors
  146. explicit
  147. PointerToUnaryProcedure(void (*p)(T))
  148. : m_p(p)
  149. {}
  150. // Operators
  151. void
  152. operator()(T arg) const
  153. {
  154. m_p(arg);
  155. }
  156. // Operations
  157. // Access
  158. // Predicates
  159. protected:
  160. // Types
  161. // C'tors/D'tors
  162. // Operators
  163. // Operations
  164. // Access
  165. // Predicates
  166. // Variables
  167. void (*m_p)(T);
  168. private:
  169. // Types
  170. // C'tors/D'tors
  171. // Operators
  172. // Operations
  173. // Access
  174. // Predicates
  175. // Variables
  176. };
  177. template<class T>
  178. PointerToUnaryProcedure<T>
  179. PointerProcedure(void (*p)(T))
  180. {
  181. return PointerToUnaryProcedure<T>(p);
  182. }
  183. #endif // SLBCSP_PROCEDURAL_H