summaryrefslogtreecommitdiff
path: root/libs/context/example/execution_context/parameter.cpp
blob: c33de7382438cf6b645c5faf6aef5aae14b1b993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

//          Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <cstdlib>
#include <exception>
#include <iostream>
#include <string>

#include <boost/context/all.hpp>
#include <boost/lexical_cast.hpp>

class X{
private:
    int * inp_;
    std::string outp_;
    std::exception_ptr excptr_;
    boost::context::execution_context caller_;
    boost::context::execution_context callee_;

public:
    X():
        inp_( nullptr),
        outp_(),
        excptr_(),
        caller_(boost::context::execution_context::current()),
        callee_(boost::context::fixedsize_stack(),
             [=](){
                try {
                    int i = * inp_;
                    outp_ = boost::lexical_cast<std::string>(i);
                    caller_.resume();
                } catch (...) {
                    excptr_=std::current_exception();
                }
             })
    {}

    std::string operator()(int i){
        inp_ = & i;
        callee_.resume();
        if(excptr_){
            std::rethrow_exception(excptr_);
        }
        return outp_;
    }
};

int main() {
    X x;
    std::cout<<x(7)<<std::endl;
    std::cout << "done" << std::endl;
}