1 // Copyright (C) 2013 Vicente J. Botet Escriba
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // <boost/thread/synchronized_value.hpp>
7 
8 // class synchronized_value<T,M>
9 
10 //    template <typename F>
11 //    inline typename boost::result_of<F(value_type&)>::type
12 //    operator()(BOOST_THREAD_RV_REF(F) fct);
13 //    template <typename F>
14 //    inline typename boost::result_of<F(value_type const&)>::type
15 //    operator()(BOOST_THREAD_RV_REF(F) fct) const;
16 
17 #include <boost/config.hpp>
18 #if ! defined  BOOST_NO_CXX11_DECLTYPE
19 #define BOOST_RESULT_OF_USE_DECLTYPE
20 #endif
21 
22 #define BOOST_THREAD_VERSION 4
23 
24 #include <boost/thread/synchronized_value.hpp>
25 
26 #include <boost/detail/lightweight_test.hpp>
27 
28 struct S {
fS29   int f() const {return 1;}
gS30   int g() {return 1;}
31 };
32 
c(S const & s)33 void c(S const& s)
34 {
35   BOOST_TEST(s.f()==1);
36 }
37 
nc(S & s)38 void nc(S & s)
39 {
40   BOOST_TEST(s.f()==1);
41   BOOST_TEST(s.g()==1);
42 }
43 
44 struct cfctr {
45   typedef void result_type;
operator ()cfctr46   void operator()(S const& s) const {
47     BOOST_TEST(s.f()==1);
48   }
49 };
50 struct ncfctr {
51   typedef void result_type;
operator ()ncfctr52   void operator()(S& s) const {
53     BOOST_TEST(s.f()==1);
54     BOOST_TEST(s.g()==1);
55   }
56 };
57 
58 struct cfctr3 {
59   typedef void result_type;
60   BOOST_THREAD_MOVABLE_ONLY(cfctr3)
cfctr3cfctr361   cfctr3()
62   {}
cfctr3cfctr363   cfctr3(BOOST_THREAD_RV_REF(cfctr3))
64   {}
operator ()cfctr365   void operator()(S const& s) const {
66     BOOST_TEST(s.f()==1);
67   }
68 };
69 struct ncfctr3 {
70   typedef void result_type;
71   BOOST_THREAD_MOVABLE_ONLY(ncfctr3)
ncfctr3ncfctr372   ncfctr3()
73   {}
ncfctr3ncfctr374   ncfctr3(BOOST_THREAD_RV_REF(ncfctr3))
75   {}
operator ()ncfctr376   void operator()(S& s) const {
77     BOOST_TEST(s.f()==1);
78     BOOST_TEST(s.g()==1);
79   }
80 };
81 
make_cfctr3()82 cfctr3 make_cfctr3() {
83   return BOOST_THREAD_MAKE_RV_REF(cfctr3());
84 }
85 
make_ncfctr3()86 ncfctr3 make_ncfctr3() {
87   return BOOST_THREAD_MAKE_RV_REF(ncfctr3());
88 }
89 
main()90 int main()
91 {
92   {
93       boost::synchronized_value<S> v;
94       v(&nc);
95       v(&c);
96   }
97   {
98       const boost::synchronized_value<S> v;
99       v(&c);
100   }
101   {
102       boost::synchronized_value<S> v;
103       v(ncfctr());
104   }
105   {
106       const boost::synchronized_value<S> v;
107       v(cfctr());
108   }
109   {
110       boost::synchronized_value<S> v;
111       ncfctr fct;
112       v(fct);
113   }
114   {
115       const boost::synchronized_value<S> v;
116       cfctr fct;
117       v(fct);
118   }
119   {
120       boost::synchronized_value<S> v;
121       v(make_ncfctr3());
122   }
123   {
124       const boost::synchronized_value<S> v;
125       v(make_cfctr3());
126   }
127 #if ! defined BOOST_NO_CXX11_LAMBDAS
128   {
129       boost::synchronized_value<S> v;
130       v([](S& s) {
131         BOOST_TEST(s.f()==1);
132         BOOST_TEST(s.g()==1);
133       });
134   }
135   {
136       const boost::synchronized_value<S> v;
137       v([](S const& s) {
138         BOOST_TEST(s.f()==1);
139       });
140   }
141 #endif
142   return boost::report_errors();
143 }
144 
145