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.

502 lines
16 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/obsolete/functionals.hpp (originally MLCpp.h, ::SynesisStd)
  3. *
  4. * Purpose: Basic functionals.
  5. *
  6. * Created: 19th January 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /// \file stlsoft/obsolete/functionals.hpp
  40. ///
  41. /// Basic functionals.
  42. #ifndef STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_FUNCTIONALS
  43. #define STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_FUNCTIONALS
  44. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  45. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_FUNCTIONALS_MAJOR 3
  46. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_FUNCTIONALS_MINOR 0
  47. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_FUNCTIONALS_REVISION 3
  48. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_FUNCTIONALS_EDIT 45
  49. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  50. /* /////////////////////////////////////////////////////////////////////////
  51. * Compatibility
  52. */
  53. /*
  54. [Incompatibilies-start]
  55. STLSOFT_COMPILER_IS_WATCOM:
  56. [Incompatibilies-end]
  57. */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Includes
  60. */
  61. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  62. # include <stlsoft/stlsoft.h>
  63. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  64. #ifndef STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_NOOP
  65. # include <stlsoft/functional/noop.hpp>
  66. #endif /* !STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_NOOP */
  67. #ifndef STLSOFT_INCL_FUNCTIONAL
  68. # define STLSOFT_INCL_FUNCTIONAL
  69. # include <functional> // for std::unary_function
  70. #endif /* !STLSOFT_INCL_FUNCTIONAL */
  71. /* /////////////////////////////////////////////////////////////////////////
  72. * Namespace
  73. */
  74. #ifndef _STLSOFT_NO_NAMESPACE
  75. namespace stlsoft
  76. {
  77. #endif /* _STLSOFT_NO_NAMESPACE */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Classes
  80. */
  81. // struct delete_instance
  82. //
  83. /** \brief This functional deletes an object instance, via scalar delete
  84. *
  85. * \ingroup group__library__functional
  86. *
  87. * \deprecated
  88. */
  89. template <ss_typename_param_k T>
  90. // [[synesis:class:unary-functor: delete_instance]]
  91. struct delete_instance
  92. : public stlsoft_ns_qual_std(unary_function)<T *, void>
  93. {
  94. public:
  95. /// The function call operator, which deletes the given instance
  96. ///
  97. /// \param pt A pointer to an instance of type T to be deleted
  98. void operator ()(T *pt) stlsoft_throw_0()
  99. {
  100. delete pt;
  101. }
  102. };
  103. // struct delete_array
  104. //
  105. /** \brief This functional deletes an array of objects, via vector delete
  106. *
  107. * \ingroup group__library__functional
  108. *
  109. * \deprecated
  110. */
  111. template <ss_typename_param_k T>
  112. // [[synesis:class:unary-functor: delete_array]]
  113. struct delete_array
  114. : public stlsoft_ns_qual_std(unary_function)<T [], void>
  115. {
  116. public:
  117. /// The function call operator, which deletes the given array
  118. ///
  119. /// \param t A pointer to an array of type T to be deleted
  120. void operator ()(T t[]) stlsoft_throw_0()
  121. {
  122. delete [] t;
  123. }
  124. };
  125. #if 0
  126. // struct selector1st
  127. /** \brief Selects the <b><code>first</code></b> member of an instance and applies the
  128. * parameterising functional to it. This functional selects the \c first member
  129. * of an instance (obviously this is usually the \c std::pair type), and
  130. * applies the parameterising functional to it.
  131. *
  132. * \ingroup group__library__functional
  133. *
  134. * \param F The parameterising functional
  135. *
  136. * For example, if you have a std::map and wish to write out the keys
  137. * with a dump_key functional, you could achieve this with the following:
  138. *
  139. * &nbsp;&nbsp;<code>std::for_each(m.begin(), m.end(), stlsoft::selector1st<dump_key>());</code>
  140. *
  141. * \deprecated
  142. */
  143. template< ss_typename_param_k F
  144. #ifndef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  145. , ss_typename_param_k T
  146. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  147. >
  148. // [[synesis:class:unary-functor: selector1st]]
  149. struct selector1st
  150. : public stlsoft_ns_qual_std(unary_function)< ss_typename_type_k F::argument_type
  151. , ss_typename_type_k F::result_type
  152. >
  153. {
  154. public:
  155. typedef ss_typename_type_k F::argument_type argument_type;
  156. typedef ss_typename_type_k F::result_type result_type;
  157. public:
  158. /// Default constructor
  159. selector1st()
  160. : m_f()
  161. {}
  162. /// Constructs from the given function class, which it will then apply
  163. /// via operator ()()
  164. ss_explicit_k selector1st(F f)
  165. : m_f(f)
  166. {}
  167. /// Function call operator, which applies the parameterising function class
  168. /// to the \c first part of the pair \c t
  169. ///
  170. /// \param t An instance of a \c pair like type, to whose \c first member will be applied the function F
  171. // Regrettably, the current implementation only provides an instantiation
  172. // returning void
  173. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  174. template <ss_typename_param_k T>
  175. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  176. result_type operator ()(T const& t) const
  177. {
  178. return m_f(t.first);
  179. }
  180. // Members
  181. private:
  182. F m_f;
  183. };
  184. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  185. template< ss_typename_param_k F
  186. >
  187. inline selector1st<F> select1st(F f)
  188. {
  189. return selector1st<F>(f);
  190. }
  191. #else /* ? STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
  192. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  193. // struct selector2nd
  194. //
  195. /** \brief Selects the <b><code>second</code></b> member of an instance and
  196. * applies the parameterising functional to it.
  197. *
  198. * \ingroup group__library__functional
  199. *
  200. * \param F The parameterising functional
  201. *
  202. * This functional selects the "second" member of an instance (obviously
  203. * this is usually the std::pair type), and applies the parameterising
  204. * functional to it.
  205. *
  206. * For example, if you have a std::map and wish to write out the values
  207. * with a dump_value functional, you could achieve this with the following:
  208. *
  209. * &nbsp;&nbsp;<code>std::for_each(m.begin(), m.end(), stlsoft::selector2nd<dump_value>());</code>
  210. *
  211. * \deprecated
  212. */
  213. template< ss_typename_param_k F
  214. #ifndef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  215. , ss_typename_param_k T
  216. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  217. >
  218. // [[synesis:class:unary-functor: selector2nd]]
  219. struct selector2nd
  220. : public stlsoft_ns_qual_std(unary_function)< ss_typename_type_k F::argument_type
  221. , ss_typename_type_k F::result_type
  222. >
  223. {
  224. public:
  225. typedef ss_typename_type_k F::argument_type argument_type;
  226. typedef ss_typename_type_k F::result_type result_type;
  227. public:
  228. /// Default constructor
  229. selector2nd()
  230. : m_f()
  231. {}
  232. /// Constructs from the given function class, which it will then apply
  233. /// via operator ()()
  234. ss_explicit_k selector2nd(F f)
  235. : m_f(f)
  236. {}
  237. /// Function call operator, which applies the parameterising function class
  238. /// to the \c second part of the pair \c t
  239. ///
  240. /// \param t An instance of a \c pair like type, to whose \c second member will be applied the function F
  241. // Regrettably, the current implementation only provides an instantiation
  242. // returning void
  243. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  244. template <ss_typename_param_k T>
  245. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  246. result_type operator ()(T &t) const
  247. {
  248. return m_f(t.second);
  249. }
  250. // Members
  251. private:
  252. F m_f;
  253. };
  254. #endif /* 0 */
  255. // struct select_1st
  256. /** \brief Selects the <b><code>first</code></b> member of an instance and applies the
  257. * parameterising functional to it. This functional selects the \c first member
  258. * of an instance (obviously this is usually the \c std::pair type), and
  259. * applies the parameterising functional to it.
  260. *
  261. * \ingroup group__library__functional
  262. *
  263. * \param F The parameterising functional
  264. *
  265. * For example, if you have a std::map and wish to write out the keys
  266. * with a dump_key functional, you could achieve this with the following:
  267. *
  268. * &nbsp;&nbsp;<code>std::for_each(m.begin(), m.end(), stlsoft::select_1st<dump_key>());</code>
  269. *
  270. * \deprecated
  271. */
  272. template< ss_typename_param_k F
  273. #ifndef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  274. , ss_typename_param_k T
  275. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  276. >
  277. // [[synesis:class:unary-functor: select_1st]]
  278. struct select_1st
  279. : public stlsoft_ns_qual_std(unary_function)< ss_typename_type_k F::argument_type
  280. , ss_typename_type_k F::result_type>
  281. {
  282. public:
  283. /// Default constructor
  284. select_1st()
  285. : m_f()
  286. {}
  287. /// Constructs from the given function class, which it will then apply
  288. /// via operator ()()
  289. ss_explicit_k select_1st(F f)
  290. : m_f(f)
  291. {}
  292. /// Function call operator, which applies the parameterising function class
  293. /// to the \c first part of the pair \c t
  294. ///
  295. /// \param t An instance of a \c pair like type, to whose \c first member will be applied the function F
  296. ///
  297. /// \note Regrettably, the current implementation only provides an instantiation
  298. /// returning void
  299. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  300. template <ss_typename_param_k T>
  301. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  302. void operator ()(T &t)
  303. {
  304. m_f(t.first);
  305. }
  306. // Members
  307. private:
  308. F m_f;
  309. };
  310. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  311. template< ss_typename_param_k F
  312. >
  313. inline select_1st<F> make_1st_selector(F f)
  314. {
  315. return select_1st<F>(f);
  316. }
  317. #else /* ? STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
  318. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  319. // struct select_2nd
  320. //
  321. /** \brief Selects the <b><code>second</code></b> member of an instance and
  322. * applies the parameterising functional to it.
  323. *
  324. * \ingroup group__library__functional
  325. *
  326. * \param F The parameterising functional
  327. *
  328. * This functional selects the "second" member of an instance (obviously
  329. * this is usually the std::pair type), and applies the parameterising
  330. * functional to it.
  331. *
  332. * For example, if you have a std::map and wish to write out the values
  333. * with a dump_value functional, you could achieve this with the following:
  334. *
  335. * &nbsp;&nbsp;<code>std::for_each(m.begin(), m.end(), stlsoft::select_2nd<dump_value>());</code>
  336. *
  337. * \deprecated
  338. */
  339. template< ss_typename_param_k F
  340. #ifndef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  341. , ss_typename_param_k T
  342. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  343. >
  344. // [[synesis:class:unary-functor: select_2nd]]
  345. struct select_2nd
  346. : public stlsoft_ns_qual_std(unary_function)<ss_typename_type_k F::argument_type, ss_typename_type_k F::result_type>
  347. {
  348. public:
  349. /// Default constructor
  350. select_2nd()
  351. : m_f()
  352. {}
  353. /// Constructs from the given function class, which it will then apply
  354. /// via operator ()()
  355. ss_explicit_k select_2nd(F f)
  356. : m_f(f)
  357. {}
  358. /// Function call operator, which applies the parameterising function class
  359. /// to the \c second part of the pair \c t
  360. ///
  361. /// \param t An instance of a \c pair like type, to whose \c second member will be applied the function F
  362. // Regrettably, the current implementation only provides an instantiation
  363. // returning void
  364. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  365. template <ss_typename_param_k T>
  366. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  367. void operator ()(T &t)
  368. {
  369. m_f(t.second);
  370. }
  371. // Members
  372. private:
  373. F m_f;
  374. };
  375. // struct select_both
  376. //
  377. /** \brief This functional selects both the \c first and \c second members of an instance
  378. * (obviously this is usually the std::pair type), and applies the respective
  379. * parameterising functionals to them.
  380. *
  381. * \ingroup group__library__functional
  382. *
  383. * \param F1 The functional to apply to the <b><code>first</code></b> part of the elements
  384. * \param F2 The functional to apply to the <b><code>second</code></b> part of the elements
  385. *
  386. * For example, if you have a std::map and wish to write out the keys with the
  387. * dump_key functional and the values with the dump_value functional, you could
  388. * achieve this with the following:
  389. *
  390. * &nbsp;&nbsp;<code>std::for_each(m.begin(), m.end(), stlsoft::select_both<dump_key, dump_value>());</code>
  391. *
  392. * \deprecated
  393. */
  394. template< ss_typename_param_k F1
  395. , ss_typename_param_k F2
  396. #ifndef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  397. , ss_typename_param_k T
  398. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  399. >
  400. // [[synesis:class:unary-functor: select_both]]
  401. struct select_both
  402. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  403. : public stlsoft_ns_qual_std(unary_function)<void, void>
  404. #else
  405. : public stlsoft_ns_qual_std(unary_function)<T &, void>
  406. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  407. {
  408. public:
  409. /// Default constructor
  410. select_both()
  411. : m_f1()
  412. , m_f2()
  413. {}
  414. /// Constructs from the given function classes, which it will then apply
  415. /// via operator ()()
  416. ss_explicit_k select_both(F1 f1, F2 f2)
  417. : m_f1(f1)
  418. , m_f2(f2)
  419. {}
  420. /// Function call operator, which applies the parameterising function classes
  421. /// to the \c first and \c second parts of the pair \c t
  422. ///
  423. /// \param t An instance of a \c pair like type, to whose \c first and \c second members will be applied the functions F1 and F2
  424. // Regrettably, the current implementation only provides an instantiation
  425. // returning void
  426. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  427. template <ss_typename_param_k T>
  428. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  429. void operator ()(T &t)
  430. {
  431. m_f1(t.first);
  432. m_f2(t.second);
  433. }
  434. // Members
  435. private:
  436. F1 m_f1;
  437. F2 m_f2;
  438. };
  439. /* ////////////////////////////////////////////////////////////////////// */
  440. #ifndef _STLSOFT_NO_NAMESPACE
  441. } /* namespace stlsoft */
  442. #endif /* _STLSOFT_NO_NAMESPACE */
  443. /* ////////////////////////////////////////////////////////////////////// */
  444. #endif /* !STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_FUNCTIONALS */
  445. /* ///////////////////////////// end of file //////////////////////////// */