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