1 | |
---|---|
2 | // Copyright (C) 2009-2012 Lorenzo Caminiti |
3 | // Distributed under the Boost Software License, Version 1.0 |
4 | // (see accompanying file LICENSE_1_0.txt or a copy at |
5 | // http://www.boost.org/LICENSE_1_0.txt) |
6 | // Home at http://www.boost.org/libs/local_function |
7 | |
8 | #include <boost/config.hpp> |
9 | #ifdef BOOST_NO_CXX11_VARIADIC_MACROS |
10 | # error "variadic macros required" |
11 | #else |
12 | |
13 | #include <boost/local_function.hpp> |
14 | #include <boost/function.hpp> |
15 | #include <boost/typeof/std/string.hpp> |
16 | #include <boost/detail/lightweight_test.hpp> |
17 | #include <string> |
18 | |
19 | boost::function<void (const std::string&)> set; |
20 | boost::function<const std::string& (void)> get; |
21 | |
22 | void action(void) { |
23 | // State `message` hidden behind access functions from here. |
24 | BOOST_TEST(get() == "abc"); |
25 | set("xyz"); |
26 | BOOST_TEST(get() == "xyz"); |
27 | } |
28 | |
29 | int main(void) { |
30 | std::string message = "abc"; // Reference valid where closure used. |
31 | |
32 | void BOOST_LOCAL_FUNCTION(bind& message, const std::string& text) { |
33 | message = text; |
34 | } BOOST_LOCAL_FUNCTION_NAME(s) |
35 | set = s; |
36 | |
37 | const std::string& BOOST_LOCAL_FUNCTION(const bind& message) { |
38 | return message; |
39 | } BOOST_LOCAL_FUNCTION_NAME(g) |
40 | get = g; |
41 | |
42 | action(); |
43 | return boost::report_errors(); |
44 | } |
45 | |
46 | #endif // VARIADIC_MACROS |
47 | |
48 |