Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:02:08

0001 /*=============================================================================
0002     Copyright (c) 2003 Martin Wille
0003     http://spirit.sourceforge.net/
0004 
0005   Distributed under the Boost Software License, Version 1.0. (See accompanying
0006   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007  =============================================================================*/
0008 #ifndef BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP
0009 #define BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP
0010 
0011 ///////////////////////////////////////////////////////////////////////////////
0012 #include <boost/spirit/home/classic/namespace.hpp>
0013 #include <boost/spirit/home/classic/core/composite/composite.hpp>
0014 #include <boost/thread/lock_types.hpp>
0015 
0016 ///////////////////////////////////////////////////////////////////////////////
0017 namespace boost { namespace spirit {
0018 
0019 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
0020 
0021     ///////////////////////////////////////////////////////////////////////////
0022     //
0023     // scoped_lock_parser class
0024     //
0025     //      implements locking of a mutex during execution of
0026     //      the parse method of an embedded parser
0027     //
0028     ///////////////////////////////////////////////////////////////////////////
0029     template <typename MutexT, typename ParserT>
0030     struct scoped_lock_parser
0031         : public unary< ParserT, parser< scoped_lock_parser<MutexT, ParserT> > >
0032     {
0033         typedef scoped_lock_parser<MutexT, ParserT> self_t;
0034         typedef MutexT      mutex_t;
0035         typedef ParserT     parser_t;
0036 
0037         template <typename ScannerT>
0038         struct result
0039         {
0040             typedef typename parser_result<parser_t, ScannerT>::type type;
0041         };
0042 
0043         scoped_lock_parser(mutex_t &m, parser_t const &p)
0044             : unary< ParserT, parser< scoped_lock_parser<MutexT, ParserT> > >(p)
0045             , mutex(m)
0046         {}
0047 
0048 
0049         template <typename ScannerT>
0050         typename parser_result<self_t, ScannerT>::type
0051         parse(ScannerT const &scan) const
0052         {
0053             typedef boost::unique_lock<mutex_t> scoped_lock_t;
0054             scoped_lock_t lock(mutex);
0055             return this->subject().parse(scan);
0056         }
0057 
0058         mutex_t &mutex;
0059     };
0060 
0061     ///////////////////////////////////////////////////////////////////////////
0062     //
0063     // scoped_lock_parser_gen
0064     //
0065     //      generator for scoped_lock_parser objects
0066     //      operator[] returns scoped_lock_parser according to its argument
0067     //
0068     ///////////////////////////////////////////////////////////////////////////
0069     template <typename MutexT>
0070     struct scoped_lock_parser_gen
0071     {
0072         typedef MutexT mutex_t;
0073         explicit scoped_lock_parser_gen(mutex_t &m) : mutex(m) {}
0074 
0075         template<typename ParserT>
0076         scoped_lock_parser
0077         <
0078             MutexT,
0079             typename as_parser<ParserT>::type
0080         >
0081         operator[](ParserT const &p) const
0082         {
0083             typedef ::BOOST_SPIRIT_CLASSIC_NS::as_parser<ParserT> as_parser_t;
0084             typedef typename as_parser_t::type parser_t;
0085 
0086             return scoped_lock_parser<mutex_t, parser_t>
0087                 (mutex, as_parser_t::convert(p));
0088         }
0089 
0090         mutex_t &mutex;
0091     };
0092 
0093 
0094     ///////////////////////////////////////////////////////////////////////////
0095     //
0096     // scoped_lock_d parser directive
0097     //
0098     //      constructs a scoped_lock_parser generator from its argument
0099     //
0100     ///////////////////////////////////////////////////////////////////////////
0101     template <typename MutexT>
0102     scoped_lock_parser_gen<MutexT>
0103     scoped_lock_d(MutexT &mutex)
0104     {
0105         return scoped_lock_parser_gen<MutexT>(mutex);
0106     }
0107 
0108 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
0109 
0110 }} // namespace BOOST_SPIRIT_CLASSIC_NS
0111 #endif // BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP