return_setget.cpp 1.2 KB

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