11 #ifndef BOOST_IOSTREAMS_FINITE_STATE_FILTER_HPP_INCLUDED 12 #define BOOST_IOSTREAMS_FINITE_STATE_FILTER_HPP_INCLUDED 19 #include <boost/config.hpp> 20 #include <boost/detail/workaround.hpp> 21 #include <boost/iostreams/categories.hpp> 22 #include <boost/iostreams/char_traits.hpp> 23 #include <boost/iostreams/checked_operations.hpp> 24 #include <boost/iostreams/concepts.hpp> 25 #include <boost/iostreams/detail/ios.hpp> 26 #include <boost/iostreams/filter/stdio.hpp> 27 #include <boost/iostreams/operations.hpp> 28 #include <boost/mpl/begin_end.hpp> 29 #include <boost/mpl/deref.hpp> 30 #include <boost/preprocessor/control/expr_if.hpp> 31 #include <boost/static_assert.hpp> 32 #include <boost/type_traits/is_base_and_derived.hpp> 34 namespace boost {
namespace iostreams {
46 static bool test(Ch,
const std::locale&) {
return true; }
51 #define BOOST_IOSTREAMS_CHARACTER_CLASS(class) \ 52 struct BOOST_JOIN(is_, class) { \ 53 template<typename Ch> \ 54 static bool test(Ch event, const std::locale& loc) \ 55 { return std::BOOST_JOIN(is, class)(event, loc); } \ 71 #undef BOOST_IOSTREAMS_CHARACTER_CLASS 78 static bool test(Ch event,
const std::locale&)
89 template<
typename FiniteStateMachine>
94 template<
typename Derived,
typename Ch =
char>
99 void imbue(
const std::locale& loc) { loc_ = loc; }
100 const std::locale&
getloc()
const {
return loc_; }
107 typename CharacterClass,
109 void (Derived::*Action)(char_type) >
112 static const int state = State;
113 static const int next_state = NextState;
114 static void execute(Derived& d, char_type event)
124 return off_ == buf_.size();
126 void push(
char c) { buf_ += c; }
129 char_type result = buf_[off_++];
130 if (off_ == buf_.size())
134 char_type&
top() {
return buf_[off_]; }
146 #if BOOST_WORKAROUND(__MWERKS__, <= 0x3206) 147 template<
typename Ch>
148 void _push_impl(Ch c) { push(c); }
151 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS 152 template<
typename FiniteStateFilter>
166 #if !BOOST_WORKAROUND(__MWERKS__, <= 0x3206) 167 # define BOOST_IOSTREAMS_FSM(fsm) \ 168 template<typename Ch> \ 170 { ::boost::iostreams::finite_state_machine<fsm, Ch>::push(c); } \ 171 template<typename Ch> \ 172 void skip(Ch c) { (void) c; } \ 174 #else // #ifndef __MWERKS__ 175 # define BOOST_IOSTREAMS_FSM(fsm) \ 176 void push(char c) { this->_push_impl(c); } \ 177 void push(wchar_t c) { this->_push_impl(c); } \ 178 void skip(char c) { (void) c; } \ 179 void skip(wchar_t c) { (void) c; } \ 187 template<
typename FiniteStateMachine>
188 class finite_state_filter_impl :
public FiniteStateMachine
191 template<
typename First,
typename Last>
194 typedef typename char_type_of<FiniteStateMachine>::type
char_type;
198 template<
typename T0>
200 : FiniteStateMachine(t0), state_(FiniteStateMachine::initial_state)
203 template<
typename T0,
typename T1>
205 : FiniteStateMachine(t0, t1), state_(FiniteStateMachine::initial_state)
208 template<
typename T0,
typename T1,
typename T2>
210 : FiniteStateMachine(t0, t1, t2),
211 state_(FiniteStateMachine::initial_state)
216 typedef typename FiniteStateMachine::transition_table transitions;
217 typedef typename mpl::begin<transitions>::type first;
218 typedef typename mpl::end<transitions>::type last;
224 state_ = FiniteStateMachine::initial_state;
228 template<
typename First,
typename Last>
230 static int execute(FiniteStateMachine& fsm,
int state, char_type event)
232 typedef typename mpl::deref<First>::type rule;
233 typedef typename mpl::next<First>::type next;
234 typedef typename rule::character_class character_class;
236 if ( state == rule::state &&
237 character_class::test(event, fsm.getloc()) )
240 rule::execute(fsm, event);
241 return rule::next_state;
249 template<
typename Last>
251 static int execute(FiniteStateMachine& fsm,
int state, char_type c)
257 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) \ 258 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) 261 template<
typename FSM>
262 static void on_any(FSM& fsm, char_type c) { fsm.on_any(c); }
271 template<
typename FiniteStateMachine>
282 : dual_use, filter_tag, closable_tag, localizable_tag
287 template<
typename T0>
289 : base_type(t0), flags_(0)
292 template<
typename T0,
typename T1>
294 : base_type(t0, t1), flags_(0)
297 template<
typename T0,
typename T1,
typename T2>
299 : base_type(t0, t1, t2), flags_(0)
302 template<
typename Source>
303 int_type
get(Source& src)
305 assert((flags_ & f_write) == 0);
309 if ((flags_ & f_eof) == 0) {
316 }
else if (!traits_type::would_block(c)) {
317 this->process_event(c);
324 else if ((flags_ & f_eof) != 0)
325 return traits_type::eof();
329 template<
typename Sink>
330 bool put(Sink& dest, char_type c)
332 assert((flags_ & f_read) == 0);
335 this->process_event(c);
336 while (!this->empty() && iostreams::put(dest, this->top()))
342 template<
typename Device>
343 void close(Device& dev, BOOST_IOS::openmode which)
345 if (which == BOOST_IOS::out) {
346 if (flags_ & f_write)
347 while (!this->empty())
348 iostreams::put_if(dev, this->pop());
356 f_write = f_read << 1,
365 #endif // #ifndef BOOST_IOSTREAMS_FINITE_STATE_FILTER_HPP_INCLUDED static bool test(Ch, const std::locale &)
static bool test(Ch event, const std::locale &)
void close(Device &dev, BOOST_IOS::openmode which)
finite_state_filter_impl(const T0 &t0)
static void on_any(FSM &fsm, char_type c)
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
finite_state_filter_impl()
CharacterClass character_class
finite_state_filter(const T0 &t0, const T1 &t1, const T2 &t2)
finite_state_filter_impl(const T0 &t0, const T1 &t1)
char_traits< char_type > traits_type
detail::finite_state_filter_impl< FiniteStateMachine > base_type
static const int initial_state
void process_event(char_type c)
base_type::char_type char_type
const std::locale & getloc() const
finite_state_filter_impl(const T0 &t0, const T1 &t1, const T2 &t2)
char_type_of< FiniteStateMachine >::type char_type
base_type::int_type int_type
string_type::size_type size_type
static int execute(FiniteStateMachine &fsm, int state, char_type c)
bool put(Sink &dest, char_type c)
std::basic_string< char_type > string_type
#define BOOST_IOSTREAMS_CHARACTER_CLASS(class)
finite_state_filter(const T0 &t0, const T1 &t1)
static void execute(Derived &d, char_type event)
void imbue(const std::locale &loc)
finite_state_filter(const T0 &t0)
static int execute(FiniteStateMachine &fsm, int state, char_type event)
char_traits< Ch >::int_type int_type