]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
2 | // Copyright Oliver Kowalke 2014. | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt or copy at | |
5 | // http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #include <cstdlib> | |
8 | #include <exception> | |
9 | #include <iostream> | |
10 | #include <memory> | |
11 | #include <string> | |
12 | ||
13 | #include <boost/variant.hpp> | |
14 | #include <boost/context/all.hpp> | |
15 | #include <boost/lexical_cast.hpp> | |
16 | ||
17 | typedef boost::variant<int,std::string> variant_t; | |
18 | ||
19 | namespace ctx = boost::context; | |
20 | ||
21 | class X{ | |
22 | private: | |
23 | std::exception_ptr excptr_; | |
24 | ctx::execution_context<variant_t> ctx_; | |
25 | ||
26 | public: | |
27 | X(): | |
28 | excptr_(), | |
29 | ctx_( | |
30 | [this](ctx::execution_context<variant_t> ctx, variant_t data){ | |
31 | try { | |
32 | for (;;) { | |
33 | int i = boost::get<int>(data); | |
34 | data = boost::lexical_cast<std::string>(i); | |
35 | auto result = ctx( data); | |
36 | ctx = std::move( std::get<0>( result) ); | |
37 | data = std::get<1>( result); | |
38 | } | |
39 | } catch ( std::bad_cast const&) { | |
40 | excptr_=std::current_exception(); | |
41 | } | |
42 | return ctx; | |
43 | }) | |
44 | {} | |
45 | ||
46 | std::string operator()(int i){ | |
47 | variant_t data = i; | |
48 | auto result = ctx_( data); | |
49 | ctx_ = std::move( std::get<0>( result) ); | |
50 | data = std::get<1>( result); | |
51 | if(excptr_){ | |
52 | std::rethrow_exception(excptr_); | |
53 | } | |
54 | return boost::get<std::string>(data); | |
55 | } | |
56 | }; | |
57 | ||
58 | int main() { | |
59 | X x; | |
60 | std::cout<<x(7)<<std::endl; | |
61 | std::cout << "done" << std::endl; | |
62 | } |