escape.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef BOOST_ARCHIVE_ITERATORS_ESCAPE_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_ESCAPE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // escape.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <boost/assert.hpp>
  15. #include <cstddef> // NULL
  16. #include <boost/iterator/iterator_adaptor.hpp>
  17. #include <boost/iterator/iterator_traits.hpp>
  18. namespace boost {
  19. namespace archive {
  20. namespace iterators {
  21. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  22. // insert escapes into text
  23. template<class Derived, class Base>
  24. class escape :
  25. public boost::iterator_adaptor<
  26. Derived,
  27. Base,
  28. typename boost::iterator_value<Base>::type,
  29. single_pass_traversal_tag,
  30. typename boost::iterator_value<Base>::type
  31. >
  32. {
  33. typedef typename boost::iterator_value<Base>::type base_value_type;
  34. typedef typename boost::iterator_reference<Base>::type reference_type;
  35. friend class boost::iterator_core_access;
  36. typedef typename boost::iterator_adaptor<
  37. Derived,
  38. Base,
  39. base_value_type,
  40. single_pass_traversal_tag,
  41. base_value_type
  42. > super_t;
  43. typedef escape<Derived, Base> this_t;
  44. void dereference_impl() {
  45. m_current_value = static_cast<Derived *>(this)->fill(m_bnext, m_bend);
  46. m_full = true;
  47. }
  48. //Access the value referred to
  49. reference_type dereference() const {
  50. if(!m_full)
  51. const_cast<this_t *>(this)->dereference_impl();
  52. return m_current_value;
  53. }
  54. bool equal(const this_t & rhs) const {
  55. if(m_full){
  56. if(! rhs.m_full)
  57. const_cast<this_t *>(& rhs)->dereference_impl();
  58. }
  59. else{
  60. if(rhs.m_full)
  61. const_cast<this_t *>(this)->dereference_impl();
  62. }
  63. if(m_bnext != rhs.m_bnext)
  64. return false;
  65. if(this->base_reference() != rhs.base_reference())
  66. return false;
  67. return true;
  68. }
  69. void increment(){
  70. if(++m_bnext < m_bend){
  71. m_current_value = *m_bnext;
  72. return;
  73. }
  74. ++(this->base_reference());
  75. m_bnext = NULL;
  76. m_bend = NULL;
  77. m_full = false;
  78. }
  79. // buffer to handle pending characters
  80. const base_value_type *m_bnext;
  81. const base_value_type *m_bend;
  82. bool m_full;
  83. base_value_type m_current_value;
  84. public:
  85. escape(Base base) :
  86. super_t(base),
  87. m_bnext(NULL),
  88. m_bend(NULL),
  89. m_full(false),
  90. m_current_value(0)
  91. {
  92. }
  93. };
  94. } // namespace iterators
  95. } // namespace archive
  96. } // namespace boost
  97. #endif // BOOST_ARCHIVE_ITERATORS_ESCAPE_HPP