[857] | 1 | /*=============================================================================
|
---|
| 2 | Copyright (c) 2003 Martin Wille
|
---|
| 3 | http://spirit.sourceforge.net/
|
---|
| 4 |
|
---|
| 5 | Use, modification and distribution is subject to the Boost Software
|
---|
| 6 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
| 7 | http://www.boost.org/LICENSE_1_0.txt)
|
---|
| 8 | =============================================================================*/
|
---|
| 9 | #ifndef BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP
|
---|
| 10 | #define BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP
|
---|
| 11 |
|
---|
| 12 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 13 | #if !defined(BOOST_SPIRIT_COMPOSITE_HPP)
|
---|
| 14 | #include <boost/spirit/core/composite.hpp>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 18 | namespace boost { namespace spirit {
|
---|
| 19 |
|
---|
| 20 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 21 | //
|
---|
| 22 | // scoped_lock_parser class
|
---|
| 23 | //
|
---|
| 24 | // implements locking of a mutex during execution of
|
---|
| 25 | // the parse method of an embedded parser
|
---|
| 26 | //
|
---|
| 27 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 28 | template <typename MutexT, typename ParserT>
|
---|
| 29 | struct scoped_lock_parser
|
---|
| 30 | : public unary< ParserT, parser< scoped_lock_parser<MutexT, ParserT> > >
|
---|
| 31 | {
|
---|
| 32 | typedef scoped_lock_parser<MutexT, ParserT> self_t;
|
---|
| 33 | typedef MutexT mutex_t;
|
---|
| 34 | typedef ParserT parser_t;
|
---|
| 35 |
|
---|
| 36 | template <typename ScannerT>
|
---|
| 37 | struct result
|
---|
| 38 | {
|
---|
| 39 | typedef typename parser_result<parser_t, ScannerT>::type type;
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | scoped_lock_parser(mutex_t &m, parser_t const &p)
|
---|
| 43 | : unary< ParserT, parser< scoped_lock_parser<MutexT, ParserT> > >(p)
|
---|
| 44 | , mutex(m)
|
---|
| 45 | {}
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | template <typename ScannerT>
|
---|
| 49 | typename parser_result<self_t, ScannerT>::type
|
---|
| 50 | parse(ScannerT const &scan) const
|
---|
| 51 | {
|
---|
| 52 | typedef typename mutex_t::scoped_lock scoped_lock_t;
|
---|
| 53 | scoped_lock_t lock(mutex);
|
---|
| 54 | return this->subject().parse(scan);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | mutex_t &mutex;
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 61 | //
|
---|
| 62 | // scoped_lock_parser_gen
|
---|
| 63 | //
|
---|
| 64 | // generator for scoped_lock_parser objects
|
---|
| 65 | // operator[] returns scoped_lock_parser according to its argument
|
---|
| 66 | //
|
---|
| 67 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 68 | template <typename MutexT>
|
---|
| 69 | struct scoped_lock_parser_gen
|
---|
| 70 | {
|
---|
| 71 | typedef MutexT mutex_t;
|
---|
| 72 | explicit scoped_lock_parser_gen(mutex_t &m) : mutex(m) {}
|
---|
| 73 |
|
---|
| 74 | template<typename ParserT>
|
---|
| 75 | scoped_lock_parser
|
---|
| 76 | <
|
---|
| 77 | MutexT,
|
---|
| 78 | typename as_parser<ParserT>::type
|
---|
| 79 | >
|
---|
| 80 | operator[](ParserT const &p) const
|
---|
| 81 | {
|
---|
| 82 | typedef ::boost::spirit::as_parser<ParserT> as_parser_t;
|
---|
| 83 | typedef typename as_parser_t::type parser_t;
|
---|
| 84 |
|
---|
| 85 | return scoped_lock_parser<mutex_t, parser_t>
|
---|
| 86 | (mutex, as_parser_t::convert(p));
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | mutex_t &mutex;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 94 | //
|
---|
| 95 | // scoped_lock_d parser directive
|
---|
| 96 | //
|
---|
| 97 | // constructs a scoped_lock_parser generator from its argument
|
---|
| 98 | //
|
---|
| 99 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 100 | template <typename MutexT>
|
---|
| 101 | scoped_lock_parser_gen<MutexT>
|
---|
| 102 | scoped_lock_d(MutexT &mutex)
|
---|
| 103 | {
|
---|
| 104 | return scoped_lock_parser_gen<MutexT>(mutex);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | }} // namespace boost::spirit
|
---|
| 108 | #endif // BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP
|
---|