Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / archive / iterators / mb_from_wchar.hpp
1 #ifndef BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP
2 #define BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP
3
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER)
6 # pragma once
7 #endif
8
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // mb_from_wchar.hpp
11
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
16
17 //  See http://www.boost.org for updates, documentation, and revision history.
18
19 #include <boost/assert.hpp>
20 #include <cstddef> // size_t
21 #include <cwchar> //  mbstate_t
22
23 #include <boost/config.hpp>
24 #if defined(BOOST_NO_STDC_NAMESPACE)
25 namespace std{ 
26     using ::mbstate_t;
27 } // namespace std
28 #endif
29
30 #include <boost/archive/detail/utf8_codecvt_facet.hpp>
31 #include <boost/iterator/iterator_adaptor.hpp>
32
33 namespace boost { 
34 namespace archive {
35 namespace iterators {
36
37 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
38 // class used by text archives to translate wide strings and to char
39 // strings of the currently selected locale
40 template<class Base>    // the input iterator
41 class mb_from_wchar
42     : public boost::iterator_adaptor<
43         mb_from_wchar<Base>, 
44         Base, 
45         wchar_t,
46         single_pass_traversal_tag,
47         char
48     >
49 {
50     friend class boost::iterator_core_access;
51
52     typedef typename boost::iterator_adaptor<
53         mb_from_wchar<Base>, 
54         Base, 
55         wchar_t,
56         single_pass_traversal_tag,
57         char
58     > super_t;
59
60     typedef mb_from_wchar<Base> this_t;
61
62     char dereference_impl() {
63         if(! m_full){
64             fill();
65             m_full = true;
66         }
67         return m_buffer[m_bnext];
68     }
69
70     char dereference() const {
71         return (const_cast<this_t *>(this))->dereference_impl();
72     }
73     // test for iterator equality
74     bool equal(const mb_from_wchar<Base> & rhs) const {
75         // once the value is filled, the base_reference has been incremented
76         // so don't permit comparison anymore.
77         return 
78             0 == m_bend
79             && 0 == m_bnext
80             && this->base_reference() == rhs.base_reference()
81         ;
82     }
83
84     void fill(){
85         wchar_t value = * this->base_reference();
86         const wchar_t *wend;
87         char *bend;
88         std::codecvt_base::result r = m_codecvt_facet.out(
89             m_mbs,
90             & value, & value + 1, wend,
91             m_buffer, m_buffer + sizeof(m_buffer), bend
92         );
93         BOOST_ASSERT(std::codecvt_base::ok == r);
94         m_bnext = 0;
95         m_bend = bend - m_buffer;
96     }
97
98     void increment(){
99         if(++m_bnext < m_bend)
100             return;
101         m_bend = 
102         m_bnext = 0;
103         ++(this->base_reference());
104         m_full = false;
105     }
106
107     boost::archive::detail::utf8_codecvt_facet m_codecvt_facet;
108     std::mbstate_t m_mbs;
109     // buffer to handle pending characters
110     char m_buffer[9 /* MB_CUR_MAX */];
111     std::size_t m_bend;
112     std::size_t m_bnext;
113     bool m_full;
114
115 public:
116     // make composible buy using templated constructor
117     template<class T>
118     mb_from_wchar(T start) :
119         super_t(Base(static_cast< T >(start))),
120         m_mbs(std::mbstate_t()),
121         m_bend(0),
122         m_bnext(0),
123         m_full(false)
124     {}
125     // intel 7.1 doesn't like default copy constructor
126     mb_from_wchar(const mb_from_wchar & rhs) : 
127         super_t(rhs.base_reference()),
128         m_bend(rhs.m_bend),
129         m_bnext(rhs.m_bnext),
130         m_full(rhs.m_full)
131     {}
132 };
133
134 } // namespace iterators
135 } // namespace archive
136 } // namespace boost
137
138 #endif // BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP