PageRenderTime 38ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/src/contrib/boost/spirit/home/classic/utility/scoped_lock.hpp

http://pythonocc.googlecode.com/
C++ Header | 112 lines | 63 code | 17 blank | 32 comment | 0 complexity | c7dc377ac1998a2d9bd3a1db543742c4 MD5 | raw file
  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#if !defined(BOOST_SPIRIT_COMPOSITE_HPP)
 14#include <boost/spirit/home/classic/core/composite.hpp>
 15#endif
 16
 17///////////////////////////////////////////////////////////////////////////////
 18namespace boost { namespace spirit {
 19
 20BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
 21
 22    ///////////////////////////////////////////////////////////////////////////
 23    //
 24    // scoped_lock_parser class
 25    //
 26    //      implements locking of a mutex during execution of
 27    //      the parse method of an embedded parser
 28    //
 29    ///////////////////////////////////////////////////////////////////////////
 30    template <typename MutexT, typename ParserT>
 31    struct scoped_lock_parser
 32        : public unary< ParserT, parser< scoped_lock_parser<MutexT, ParserT> > >
 33    {
 34        typedef scoped_lock_parser<MutexT, ParserT> self_t;
 35        typedef MutexT      mutex_t;
 36        typedef ParserT     parser_t;
 37
 38        template <typename ScannerT>
 39        struct result
 40        {
 41            typedef typename parser_result<parser_t, ScannerT>::type type;
 42        };
 43
 44        scoped_lock_parser(mutex_t &m, parser_t const &p)
 45            : unary< ParserT, parser< scoped_lock_parser<MutexT, ParserT> > >(p)
 46            , mutex(m)
 47        {}
 48
 49
 50        template <typename ScannerT>
 51        typename parser_result<self_t, ScannerT>::type
 52        parse(ScannerT const &scan) const
 53        {
 54            typedef typename  mutex_t::scoped_lock scoped_lock_t;
 55            scoped_lock_t lock(mutex);
 56            return this->subject().parse(scan);
 57        }
 58
 59        mutex_t &mutex;
 60    };
 61
 62    ///////////////////////////////////////////////////////////////////////////
 63    //
 64    // scoped_lock_parser_gen
 65    //
 66    //      generator for scoped_lock_parser objects
 67    //      operator[] returns scoped_lock_parser according to its argument
 68    //
 69    ///////////////////////////////////////////////////////////////////////////
 70    template <typename MutexT>
 71    struct scoped_lock_parser_gen
 72    {
 73        typedef MutexT mutex_t;
 74        explicit scoped_lock_parser_gen(mutex_t &m) : mutex(m) {}
 75
 76        template<typename ParserT>
 77        scoped_lock_parser
 78        <
 79            MutexT,
 80            typename as_parser<ParserT>::type
 81        >
 82        operator[](ParserT const &p) const
 83        {
 84            typedef ::BOOST_SPIRIT_CLASSIC_NS::as_parser<ParserT> as_parser_t;
 85            typedef typename as_parser_t::type parser_t;
 86
 87            return scoped_lock_parser<mutex_t, parser_t>
 88                (mutex, as_parser_t::convert(p));
 89        }
 90
 91        mutex_t &mutex;
 92    };
 93
 94
 95    ///////////////////////////////////////////////////////////////////////////
 96    //
 97    // scoped_lock_d parser directive
 98    //
 99    //      constructs a scoped_lock_parser generator from its argument
100    //
101    ///////////////////////////////////////////////////////////////////////////
102    template <typename MutexT>
103    scoped_lock_parser_gen<MutexT>
104    scoped_lock_d(MutexT &mutex)
105    {
106        return scoped_lock_parser_gen<MutexT>(mutex);
107    }
108
109BOOST_SPIRIT_CLASSIC_NAMESPACE_END
110
111}} // namespace BOOST_SPIRIT_CLASSIC_NS
112#endif // BOOST_SPIRIT_UTILITY_SCOPED_LOCK_HPP