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.

500 lines
15 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/obsolete/proxy_iterator.hpp
  3. *
  4. * Purpose: proxy_iterator template class.
  5. *
  6. * Created: 28th June 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-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/proxy_iterator.hpp
  40. ///
  41. /// proxy_iterator template class.
  42. #ifndef STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR
  43. #define STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR
  44. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  45. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR_MAJOR 3
  46. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR_MINOR 0
  47. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR_REVISION 3
  48. # define STLSOFT_VER_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR_EDIT 53
  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_UTIL_STD_HPP_ITERATOR_HELPER
  65. # include <stlsoft/util/std/iterator_helper.hpp>
  66. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  67. /* /////////////////////////////////////////////////////////////////////////
  68. * Namespace
  69. */
  70. #ifndef _STLSOFT_NO_NAMESPACE
  71. namespace stlsoft
  72. {
  73. #endif /* _STLSOFT_NO_NAMESPACE */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Warnings
  76. */
  77. #if defined(STLSOFT_COMPILER_IS_MSVC)
  78. # if _MSC_VER >= 1200
  79. # pragma warning(push)
  80. # endif /* compiler */
  81. # pragma warning(disable : 4355)
  82. #endif /* compiler */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Classes
  85. */
  86. /** \brief Provides translation between the elements in a range and a different value type
  87. *
  88. * \ingroup group__library__iterators
  89. *
  90. * \param R The element type. This type is the element in the underlying sequence
  91. * \param V The value type. This is the type to which the element type is translated
  92. * \param T The traits_type. This type provides a static method \c make_value(), which
  93. * converts the element type to the value type
  94. * \param C The iterator category
  95. * \param R The reference type
  96. * \param P The pointer type
  97. *
  98. * \remarks Iterator for proxy_sequence
  99. *
  100. * \deprecated This is maintained for backwards compatibility with the
  101. * recls/COM library. New users should instead use
  102. * stlsoft::transform_iterator.
  103. */
  104. template< ss_typename_param_k E
  105. , ss_typename_param_k V
  106. , ss_typename_param_k T
  107. , ss_typename_param_k C
  108. , ss_typename_param_k R = T&
  109. , ss_typename_param_k P = T*
  110. >
  111. class proxy_iterator
  112. : public iterator_base<C, V, ss_ptrdiff_t, P, R>
  113. {
  114. /// \name Types
  115. /// @{
  116. private:
  117. typedef iterator_base<C, V, ss_ptrdiff_t, P, R> parent_class_type;
  118. typedef ss_typename_type_k parent_class_type::value_type raw_value_type;
  119. public:
  120. typedef E element_type;
  121. typedef raw_value_type value_type;
  122. typedef ss_typename_type_k parent_class_type::reference reference;
  123. typedef value_type const& const_reference;
  124. typedef T traits_type;
  125. typedef ss_typename_type_k parent_class_type::iterator_category iterator_category;
  126. typedef ss_typename_type_k parent_class_type::pointer pointer;
  127. typedef proxy_iterator<E, V, T, C, R, P> class_type;
  128. typedef ss_size_t size_type;
  129. typedef ss_typename_type_k parent_class_type::difference_type difference_type;
  130. /// @}
  131. /// \name Construction
  132. /// @{
  133. public:
  134. /// Default constructor
  135. proxy_iterator()
  136. : m_begin(NULL)
  137. , m_end(NULL)
  138. , m_value(value_type())
  139. , m_modified(true)
  140. {}
  141. #if !defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
  142. defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED)
  143. proxy_iterator(element_type *from, element_type *to)
  144. : m_begin(from)
  145. , m_end(to)
  146. , m_value(value_type())
  147. , m_modified(true)
  148. {}
  149. proxy_iterator(element_type *p, size_type n)
  150. : m_begin(p)
  151. , m_end(p + n)
  152. , m_value(value_type())
  153. , m_modified(true)
  154. {}
  155. #endif /* !STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED */
  156. #if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT)
  157. template <ss_typename_param_k P2>
  158. proxy_iterator(P2 *from, P2 *to)
  159. : m_begin(from)
  160. , m_end(to)
  161. , m_value(value_type())
  162. , m_modified(true)
  163. {}
  164. template <ss_typename_param_k P2>
  165. proxy_iterator(P2 *p, size_type n)
  166. : m_begin(p)
  167. , m_end(p + n)
  168. , m_value(value_type())
  169. , m_modified(true)
  170. {}
  171. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
  172. proxy_iterator(class_type const& rhs)
  173. : m_begin(rhs.begin())
  174. , m_end(rhs.end())
  175. , m_value(value_type())
  176. , m_modified(true)
  177. {}
  178. #if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) && \
  179. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  180. _MSC_VER >= 1310)
  181. template <ss_typename_param_k I>
  182. /* ss_explicit_k */ proxy_iterator(I &i)
  183. : m_begin(i.begin())
  184. , m_end(i.end())
  185. , m_value(value_type())
  186. , m_modified(true)
  187. {}
  188. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT && compiler */
  189. class_type& operator =(class_type const& rhs)
  190. {
  191. m_begin = rhs.begin();
  192. m_end = rhs.end();
  193. m_modified = true;
  194. return *this;
  195. }
  196. #if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT)
  197. template <ss_typename_param_k I>
  198. class_type& operator =(I& rhs)
  199. {
  200. m_begin = rhs.begin();
  201. m_end = rhs.end();
  202. m_modified = true;
  203. return *this;
  204. }
  205. #endif /* STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
  206. /// @}
  207. /// \name Helper functions
  208. /// @{
  209. public:
  210. /// The proxy iterator begin-point
  211. ///
  212. /// \return A pointer to the current position of the proxy iterator
  213. element_type *begin()
  214. {
  215. return m_begin;
  216. }
  217. /// The proxy iterator endpoint
  218. ///
  219. /// \return A pointer to the end point of the proxy iterator
  220. element_type *end()
  221. {
  222. return m_end;
  223. }
  224. /// The proxy iterator begin-point
  225. ///
  226. /// \return A pointer to the current position of the proxy iterator
  227. element_type const *begin() const
  228. {
  229. return m_begin;
  230. }
  231. /// The proxy iterator endpoint
  232. ///
  233. /// \return A pointer to the end point of the proxy iterator
  234. element_type const *end() const
  235. {
  236. return m_end;
  237. }
  238. /// @}
  239. /// \name Iterator methods
  240. /// @{
  241. public:
  242. /// Pre-increment operator
  243. class_type& operator ++()
  244. {
  245. STLSOFT_MESSAGE_ASSERT("Incrementing invalid iterator", m_begin != m_end);
  246. ++m_begin;
  247. m_modified = true;
  248. return *this;
  249. }
  250. /// Post-increment operator
  251. class_type operator ++(int)
  252. {
  253. class_type r(*this);
  254. operator ++();
  255. return r;
  256. }
  257. /// Pre-decrement operator
  258. class_type& operator --()
  259. {
  260. --m_begin;
  261. m_modified = true;
  262. return *this;
  263. }
  264. /// Post-decrement operator
  265. class_type operator --(int)
  266. {
  267. class_type r(*this);
  268. operator --();
  269. return r;
  270. }
  271. /// Dereference to return a value at the current position of type V
  272. const_reference operator *() const
  273. {
  274. STLSOFT_MESSAGE_ASSERT("Attempting to dereference an invalid iterator", m_begin != m_end);
  275. if(m_modified)
  276. {
  277. // Can't make m_value mutable, as it confuses too many compilers
  278. remove_const(m_value) = value_type(traits_type::make_value(*m_begin));
  279. m_modified = false;
  280. }
  281. return m_value;
  282. }
  283. /// Evaluates whether \c this and \c rhs are equivalent
  284. ss_bool_t equal(class_type const& rhs) const
  285. {
  286. ss_bool_t bEqual;
  287. if(m_end == rhs.m_end)
  288. {
  289. // It's a copy of the same iterator, so it's only equal if the m_begin's are the tame
  290. bEqual = m_begin == rhs.m_begin;
  291. }
  292. else
  293. {
  294. // It's sourced from a different iterator, so they're only the same if they're both closed
  295. bEqual = (m_begin == m_end) == (rhs.m_begin == rhs.m_end);
  296. }
  297. return bEqual;
  298. }
  299. ss_ptrdiff_t compare(class_type const& rhs) const
  300. {
  301. return m_begin - rhs.m_begin;
  302. }
  303. /// @}
  304. private:
  305. element_type *m_begin;
  306. element_type *m_end;
  307. value_type m_value; // Can't make this mutable, as it confuses too many compilers
  308. ss_mutable_k ss_bool_t m_modified;
  309. };
  310. /* /////////////////////////////////////////////////////////////////////////
  311. * Operators
  312. */
  313. template< ss_typename_param_k E
  314. , ss_typename_param_k V
  315. , ss_typename_param_k T
  316. , ss_typename_param_k C
  317. , ss_typename_param_k R
  318. , ss_typename_param_k P
  319. >
  320. inline ss_bool_t operator ==(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  321. {
  322. return lhs.equal(rhs);
  323. }
  324. template< ss_typename_param_k E
  325. , ss_typename_param_k V
  326. , ss_typename_param_k T
  327. , ss_typename_param_k C
  328. , ss_typename_param_k R
  329. , ss_typename_param_k P
  330. >
  331. inline ss_bool_t operator !=(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  332. {
  333. return !lhs.equal(rhs);
  334. }
  335. template< ss_typename_param_k E
  336. , ss_typename_param_k V
  337. , ss_typename_param_k T
  338. , ss_typename_param_k C
  339. , ss_typename_param_k R
  340. , ss_typename_param_k P
  341. >
  342. inline proxy_iterator<E, V, T, C, R, P> operator +(proxy_iterator<E, V, T, C, R, P> const& lhs, ss_ptrdiff_t d)
  343. {
  344. return proxy_iterator<E, V, T, C, R, P>(lhs.begin() + d, lhs.end());
  345. }
  346. template< ss_typename_param_k E
  347. , ss_typename_param_k V
  348. , ss_typename_param_k T
  349. , ss_typename_param_k C
  350. , ss_typename_param_k R
  351. , ss_typename_param_k P
  352. >
  353. inline proxy_iterator<E, V, T, C, R, P> operator +(ss_ptrdiff_t d, proxy_iterator<E, V, T, C, R, P> const& rhs)
  354. {
  355. return proxy_iterator<E, V, T, C, R, P>(rhs.begin() + d, rhs.end());
  356. }
  357. template< ss_typename_param_k E
  358. , ss_typename_param_k V
  359. , ss_typename_param_k T
  360. , ss_typename_param_k C
  361. , ss_typename_param_k R
  362. , ss_typename_param_k P
  363. >
  364. inline proxy_iterator<E, V, T, C, R, P> operator -(proxy_iterator<E, V, T, C, R, P> const& lhs, ss_ptrdiff_t d)
  365. {
  366. return proxy_iterator<E, V, T, C, R, P>(lhs.begin() - d, lhs.end());
  367. }
  368. template< ss_typename_param_k E
  369. , ss_typename_param_k V
  370. , ss_typename_param_k T
  371. , ss_typename_param_k C
  372. , ss_typename_param_k R
  373. , ss_typename_param_k P
  374. >
  375. inline ss_ptrdiff_t operator -(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  376. {
  377. return rhs.begin() - lhs.begin();
  378. }
  379. template< ss_typename_param_k E
  380. , ss_typename_param_k V
  381. , ss_typename_param_k T
  382. , ss_typename_param_k C
  383. , ss_typename_param_k R
  384. , ss_typename_param_k P
  385. >
  386. inline ss_ptrdiff_t operator <(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  387. {
  388. return lhs.compare(rhs) < 0;
  389. }
  390. template< ss_typename_param_k E
  391. , ss_typename_param_k V
  392. , ss_typename_param_k T
  393. , ss_typename_param_k C
  394. , ss_typename_param_k R
  395. , ss_typename_param_k P
  396. >
  397. inline ss_ptrdiff_t operator <=(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  398. {
  399. return lhs.compare(rhs) <= 0;
  400. }
  401. template< ss_typename_param_k E
  402. , ss_typename_param_k V
  403. , ss_typename_param_k T
  404. , ss_typename_param_k C
  405. , ss_typename_param_k R
  406. , ss_typename_param_k P
  407. >
  408. inline ss_ptrdiff_t operator >(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  409. {
  410. return lhs.compare(rhs) > 0;
  411. }
  412. template< ss_typename_param_k E
  413. , ss_typename_param_k V
  414. , ss_typename_param_k T
  415. , ss_typename_param_k C
  416. , ss_typename_param_k R
  417. , ss_typename_param_k P
  418. >
  419. inline ss_ptrdiff_t operator >=(proxy_iterator<E, V, T, C, R, P> const& lhs, proxy_iterator<E, V, T, C, R, P> const& rhs)
  420. {
  421. return lhs.compare(rhs) >= 0;
  422. }
  423. /* /////////////////////////////////////////////////////////////////////////
  424. * Warnings
  425. */
  426. #if defined(STLSOFT_COMPILER_IS_MSVC)
  427. # if _MSC_VER >= 1200
  428. # pragma warning(pop)
  429. # else /* ? compiler */
  430. # pragma warning(default: 4355)
  431. # endif /* _MSC_VER */
  432. #endif /* compiler */
  433. /* ////////////////////////////////////////////////////////////////////// */
  434. #ifndef _STLSOFT_NO_NAMESPACE
  435. } /* namespace stlsoft */
  436. #endif /* _STLSOFT_NO_NAMESPACE */
  437. /* ////////////////////////////////////////////////////////////////////// */
  438. #endif /* !STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_PROXY_ITERATOR */
  439. /* ///////////////////////////// end of file //////////////////////////// */