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