### Fiber Generator Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff Demonstrates using Boost.Context 'fiber' to implement a generator, calculating Fibonacci numbers. It shows how to suspend and resume fibers, transfer data via captures, and manage the fiber state. ```cpp namespace ctx=boost::context; int a; ctx::fiber source{[&a](ctx::fiber&& sink){ a=0; int b=1; for(;;){ sink=std::move(sink).resume(); int next=a+b; a=b; b=next; } return std::move(sink); }}; for (int j=0;j<10;++j) { source=std::move(source).resume(); std::cout << a << " "; } output: 0 1 1 2 3 5 8 13 21 34 ``` -------------------------------- ### Windows Fiber API context switching example Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/rationale/other_apis_ Illustrates the typical pattern for using Windows Fiber API for context switching. It includes checks to convert the current thread to a fiber if it isn't already, switches to the target fiber, and then converts back if necessary. It also mentions the `IsThreadAFiber()` function and its compatibility issues. ```cpp if ( ! is_a_fiber() ) { ConvertThreadToFiber( 0); SwitchToFiber( ctx); ConvertFiberToThread(); } ``` -------------------------------- ### Allocate control structures on top of fiber stack Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff Shows how to manually allocate control structures on top of a fiber's stack. This involves reserving space on the stack, using placement new to construct the control structure, and manually destructing it. The `my_control_structure` example demonstrates capturing a fiber and initializing it with preallocated stack space. ```cpp namespace ctx=boost::context; // stack-allocator used for (de-)allocating stack fixedsize_stack salloc(4048); // allocate stack space stack_context sctx(salloc.allocate()); // reserve space for control structure on top of the stack void * sp=static_cast(sctx.sp)-sizeof(my_control_structure); std::size_t size=sctx.size-sizeof(my_control_structure); // placement new creates control structure on reserved space my_control_structure * cs=new(sp)my_control_structure(sp,size,sctx,salloc); ... // destructing the control structure cs->~my_control_structure(); ... struct my_control_structure { // captured fiber ctx::fiber f; template< typename StackAllocator > my_control_structure(void * sp,std::size_t size,stack_context sctx,StackAllocator salloc) : // create captured fiber f{ std::allocator_arg,preallocated(sp,size,sctx),salloc,entry_func} { } ... }; ``` -------------------------------- ### callcc() Generator Pattern - Fibonacci Sequence with Continuations Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc Demonstrates basic usage of callcc() and continuation::resume() to implement a generator pattern that yields Fibonacci numbers. The example shows how to capture the current continuation, execute a lambda within a new continuation context, and transfer state between continuations using variables and context switches. Local variables maintain their values across context switches due to separate stack allocation. ```cpp namespace ctx=boost::context; int a; ctx::continuation source=ctx::callcc( [&a](ctx::continuation && sink){ a=0; int b=1; for(;;){ sink=sink.resume(); int next=a+b; a=b; b=next; } return std::move(sink); }); for (int j=0;j<10;++j) { std::cout << a << " "; source=source.resume(); } output: 0 1 1 2 3 5 8 13 21 34 ``` -------------------------------- ### Parameter Passing Between Fibers (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff Illustrates how to pass data between two Boost.Context 'fiber' instances using lambda captures. This example shows a simple increment operation on a captured variable during fiber resumption. ```cpp namespace ctx=boost::context; int i=1; ctx::fiber f1{[&i](ctx::fiber&& f2){ std::printf("inside f1,i==%d\n",i); i+=1; return std::move(f2).resume(); }}; f1=std::move(f1).resume(); std::printf("i==%d\n",i); output: inside c1,i==1 i==2 ``` -------------------------------- ### Pass Parameters Between Continuations with C++ Lambda Captures Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc Demonstrates passing data between continuations using lambda captures in C++. The example shows how a variable captured by reference in a lambda can be modified when the continuation is resumed. ```C++ namespace ctx=boost::context; int i=1; ctx::continuation c1=callcc([&i](ctx::continuation && c2){ std::printf("inside c1,i==%d\n",i); i+=1; return c2.resume(); }); std::printf("i==%d\n",i); output: inside c1,i==1 i==2 ``` -------------------------------- ### Boost C++ continuation Resume Operations Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Provides 'resume' and 'resume_with' member functions. 'resume' captures the current continuation and resumes it. 'resume_with' executes a given function 'fn' within the resumed continuation's context. 'fn' must return a continuation. ```cpp continuation resume(); template continuation resume_with(Fn && fn); ``` -------------------------------- ### Recursive Descent Parser with Callback - C++ Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff A Parser class implementing a recursive descent parser for mathematical expressions following a defined grammar (P → E '\0', E → T {('+' | '-') T}, T → S {('*' | '/') S}, S → digit | '(' E ')'). The parser accepts an input stream and a callback function that is invoked for each parsed symbol. The run() method initiates parsing by scanning and evaluating expressions recursively through E(), T(), and S() methods. ```cpp namespace ctx=boost::context; /* * grammar: * P ---> E '\0' * E ---> T {('+' | '-') T} * T ---> S {('*' | '/') S} * S ---> digit | '(' E ')' */ class Parser{ char next; std::istream& is; std::function cb; char pull(){ return std::char_traits::to_char_type(is.get()); } void scan(){ do{ next=pull(); } while(isspace(next)); } public: Parser(std::istream& is_,std::function cb_) : next(), is(is_), cb(cb_) {} void run() { scan(); E(); } private: void E(){ T(); while (next=='+'||next=='-'){ cb(next); scan(); T(); } } void T(){ S(); while (next=='*'||next=='/'){ cb(next); scan(); S(); } } void S(){ if (isdigit(next)){ cb(next); scan(); } else if(next=='('){ cb(next); scan(); E(); if (next==')'){ cb(next); scan(); }else{ throw std::runtime_error("parsing failed"); } } else{ throw std::runtime_error("parsing failed"); } } }; ``` -------------------------------- ### Allocate Control Structures on Stack with Boost.Context Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc Demonstrates allocating control structures on the stack using Boost.Context. It involves creating a stack context, reserving space, and using placement new for the control structure. The user is responsible for destructing the control structure. Dependencies include boost::context and a fixed-size stack allocator. ```cpp namespace ctx=boost::context; // stack-allocator used for (de-)allocating stack fixedsize_stack salloc(4048); // allocate stack space stack_context sctx(salloc.allocate()); // reserve space for control structure on top of the stack void * sp=static_cast(sctx.sp)-sizeof(my_control_structure); std::size_t size=sctx.size-sizeof(my_control_structure); // placement new creates control structure on reserved space my_control_structure * cs=new(sp)my_control_structure(sp,size,sctx,salloc); ... // destructing the control structure cs->~my_control_structure(); ... struct my_control_structure { // captured continuation ctx::continuation c; template< typename StackAllocator > my_control_structure(void * sp,std::size_t size,stack_context sctx,StackAllocator salloc) : // create captured continuation c{} { c=ctx::callcc(std::allocator_arg,preallocated(sp,size,sctx),salloc,entry_func); } ... }; ``` -------------------------------- ### Execute function on fiber with resume_with Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff Demonstrates how to execute a new function on top of a resumed fiber using `continuation::resume_with()`. The passed function must accept an rvalue reference to a fiber and return a fiber. This allows for additional stack frames to be allocated on top of the existing fiber's stack. ```cpp namespace ctx=boost::context; int data=0; ctx::fiber f1{[&data](ctx::fiber&& f2) { std::cout << "f1: entered first time: " << data << std::endl; data+=1; f2=std::move(f2).resume(); std::cout << "f1: entered second time: " << data << std::endl; data+=1; f2=std::move(f2).resume(); std::cout << "f1: entered third time: " << data << std::endl; return std::move(f2); }}; f1=std::move(f1).resume(); std::cout << "f1: returned first time: " << data << std::endl; data+=1; f1=std::move(f1).resume(); std::cout << "f1: returned second time: " << data << std::endl; data+=1; f1=std::move(f1).resume_with([&data](ctx::fiber&& f2){ std::cout << "f2: entered: " << data << std::endl; data=-1; return std::move(f2); }); std::cout << "f1: returned third time" << std::endl; output: f1: entered first time: 0 f1: returned first time: 1 f1: entered second time: 2 f1: returned second time: 3 f2: entered: 4 f1: entered third time: -1 f1: returned third time ``` -------------------------------- ### C++: Callcc Function Overloads for Continuation Creation Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Demonstrates the C++ `callcc` function overloads available in `boost/context/continuation.hpp`. These functions capture the current continuation and prepare a new one to execute a given function `fn`. They support default stack allocation, custom stack allocators, and preallocated stack buffers. The returned continuation can be invoked to resume execution. ```cpp #include template continuation callcc(Fn && fn); template continuation callcc(std::allocator_arg_t,StackAlloc salloc,Fn && fn); template continuation callcc(std::allocator_arg_t,preallocated palloc,StackAlloc salloc,Fn && fn); ``` -------------------------------- ### makecontext() function signature for ucontext_t Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/rationale/other_apis_ Demonstrates the function signature of `makecontext()` used with `ucontext_t`. It takes a pointer to a `ucontext_t` structure, a function pointer, and the number of integer arguments, with a note on potential issues with argument casting and passing pointers. ```c void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...); ``` -------------------------------- ### Throw exception on top of fiber with resume_with Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff Illustrates how to throw an exception from a function executed on top of a fiber using `resume_with`. The exception is then caught within the fiber's execution context, demonstrating exception propagation and handling across fiber boundaries. The custom `my_exception` class carries the fiber that was executing when the exception occurred. ```cpp namespace ctx=boost::context; struct my_exception : public std::runtime_error { ctx::fiber f; my_exception(ctx::fiber&& f_,std::string const& what) : std::runtime_error{ what }, f{ std::move(f_) } { } }; ctx::fiber f{[](ctx::fiber && f) ->ctx::fiber { std::cout << "entered" << std::endl; try { f=std::move(f).resume(); } catch (my_exception & ex) { std::cerr << "my_exception: " << ex.what() << std::endl; return std::move(ex.f); } return {}; }); f=std::move(f).resume(); f=std::move(f).resume_with([](ctx::fiber && f) ->ctx::fiber { throw my_exception(std::move(f),"abc"); return {}; }); output: entered my_exception: abc ``` -------------------------------- ### Invert Control Flow with Boost.Context and Recursive Descent Parser Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc Illustrates inverting control flow using Boost.Context and a recursive descent parser. A parser is created with a callback that resumes the main continuation. This allows the user code to pull parsed data instead of being pushed via callbacks, demonstrating coroutine-like behavior for parsing. Dependencies include boost::context, iostream, and functional. ```cpp namespace ctx=boost::context; /* * grammar: * P ---> E '\0' * E ---> T {('+'|'-') T} * T ---> S {('*'|'/') S} * S ---> digit | '(' E ')' */ class Parser{ char next; std::istream& is; std::function cb; char pull(){ return std::char_traits::to_char_type(is.get()); } void scan(){ do{ next=pull(); } while(isspace(next)); } public: Parser(std::istream& is_,std::function cb_) : next(), is(is_), cb(cb_) {} void run() { scan(); E(); } private: void E(){ T(); while (next=='+'||next=='-'){ cb(next); scan(); T(); } } void T(){ S(); while (next=='*'||next=='/'){ cb(next); scan(); S(); } } void S(){ if (isdigit(next)){ cb(next); scan(); } else if(next=='('){ cb(next); scan(); E(); if (next==')'){ cb(next); scan(); }else{ throw std::runtime_error("parsing failed"); } } else{ throw std::runtime_error("parsing failed"); } } }; std::istringstream is("1+1"); // execute parser in new continuation ctx::continuation source; // user-code pulls parsed data from parser // invert control flow char c; bool done=false; source=ctx::callcc( [&is,&c,&done](ctx::continuation && sink){ // create parser with callback function Parser p(is, [&sink,&c](char c_){ // resume main continuation c=c_; sink=sink.resume(); }); // start recursive parsing p.run(); // signal termination done=true; // resume main continuation return std::move(sink); }); while(!done){ printf("Parsed: %c\n",c); source=source.resume(); } ``` -------------------------------- ### Boost C++ continuation Constructor and Destructor Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Details the default constructor for creating an invalid continuation and the destructor for cleaning up associated resources. Both operations do not throw exceptions. ```cpp continuation() noexcept; ~continuation(); ``` -------------------------------- ### Boost.Context Fiber Move Operations (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Illustrates the move constructor and move assignment operator for the Boost.Context 'fiber' class. These operations allow efficient transfer of fiber ownership and state without deep copying. ```cpp fiber(fiber && other) noexcept; fiber & operator=(fiber && other) noexcept; ``` -------------------------------- ### Boost.Context Fiber Resume Operations (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Explains the `resume()` and `resume_with()` member functions of the Boost.Context 'fiber' class. These functions capture the current execution context and transfer control to the target fiber, optionally executing a new function within the resumed fiber's context. They are rvalue-qualified because the current fiber becomes invalidated. ```cpp fiber resume() &&; template fiber resume_with(Fn && fn) &&; ``` -------------------------------- ### Boost C++ Class continuation Definition Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Defines the structure and interface for the continuation class in Boost C++, including constructors, destructor, move operations, resume functions, and comparison operators. It is designed for managing execution contexts. ```cpp #include class continuation { public: continuation() noexcept = default; ~continuation(); continuation(continuation && other) noexcept; continuation & operator=(continuation && other) noexcept; continuation(continuation const& other) noexcept = delete; continuation & operator=(continuation const& other) noexcept = delete; continuation resume(); template continuation resume_with(Fn && fn); explicit operator bool() const noexcept; bool operator!() const noexcept; bool operator==(continuation const& other) const noexcept; bool operator!=(continuation const& other) const noexcept; bool operator<(continuation const& other) const noexcept; bool operator>(continuation const& other) const noexcept; bool operator<=(continuation const& other) const noexcept; bool operator>=(continuation const& other) const noexcept; template friend std::basic_ostream & operator<<(std::basic_ostream & os,continuation const& other) { void swap(continuation & other) noexcept; }; ``` -------------------------------- ### Invert Parser Control Flow Using Boost Context Fiber - C++ Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff Demonstrates inverting control flow using Boost.Context fibers to transform a push-based parser callback model into a pull-based model. A fiber executes the parser with a custom callback that suspends and resumes the main fiber, transferring each parsed character. The main code loop pulls symbols from the parser fiber by repeatedly resuming it until parsing completes. ```cpp std::istringstream is("1+1"); // user-code pulls parsed data from parser // invert control flow char c; bool done=false; // execute parser in new fiber ctx::fiber source{[&is,&c,&done](ctx::fiber&& sink){ // create parser with callback function Parser p(is, [&sink,&c](char c_){ // resume main fiber c=c_; sink=std::move(sink).resume(); }); // start recursive parsing p.run(); // signal termination done=true; // resume main fiber return std::move(sink); }}; source=std::move(source).resume(); while(!done){ printf("Parsed: %c\n",c); source=std::move(source).resume(); } output: Parsed: 1 Parsed: + Parsed: 1 ``` -------------------------------- ### Boost.Context Fiber Constructors (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Details the constructors for the Boost.Context 'fiber' class. The default constructor creates an invalid fiber. Other constructors initialize a new fiber to execute a given function, with options for custom stack allocation. ```cpp fiber() noexcept; template fiber(Fn && fn); template fiber(std::allocator_arg_t, StackAlloc && salloc, Fn && fn); ``` -------------------------------- ### Boost C++ continuation Move Semantics Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Implements move constructor and move assignment operator for the continuation class, enabling efficient transfer of continuation state. These operations are noexcept. ```cpp continuation(continuation && other) noexcept; continuation & operator=(continuation && other) noexcept; ``` -------------------------------- ### Include and Define pooled_fixedsize_stack in Boost.Context Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/pooled_fixedsize Demonstrates how to include the pooled_fixedsize_stack header and define the template class with traits. The class manages fixed-size stack memory pools internally using boost::pool without appending guard pages. ```cpp #include template< typename traitsT > struct basic_pooled_fixedsize_stack { typedef traitT traits_type; basic_pooled_fixedsize_stack(std::size_t stack_size = traits_type::default_size(), std::size_t next_size = 32, std::size_t max_size = 0); stack_context allocate(); void deallocate( stack_context &); } typedef basic_pooled_fixedsize_stack< stack_traits > pooled_fixedsize_stack; ``` -------------------------------- ### Construct Boost preallocated Object Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/struct__preallocated_ The constructor for the `preallocated` struct. It takes a void pointer to the stack space, the size of the allocation, and a `stack_allocator` object to initialize a pre-allocated memory context. ```cpp preallocated( void * sp, std:size_t size, stack_allocator sctx) noexcept; ``` -------------------------------- ### Boost C++ continuation Comparison Operators Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Defines equality ('==', '!='), and ordering ('<', '>', '<=', '>=') operators for comparing continuation objects. These operators are noexcept and facilitate the ordering of continuation states. ```cpp bool operator==(continuation const& other) const noexcept; bool operator!=(continuation const& other) const noexcept; bool operator<(continuation const& other) const noexcept; bool operator>(continuation const& other) const noexcept; bool operator<=(continuation const& other) const noexcept; bool operator>=(continuation const& other) const noexcept; ``` -------------------------------- ### Execute New Function on Resumed Continuation with C++ resume_with Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc Illustrates executing a new function on top of a resumed continuation using `continuation::resume_with()` in C++. The provided lambda receives a continuation and returns a continuation, allowing for interleaved execution. ```C++ namespace ctx=boost::context; int data=0; ctx::continuation c=ctx::callcc([&data](ctx::continuation && c) { std::cout << "f1: entered first time: " << data << std::endl; data+=1; c=c.resume(); std::cout << "f1: entered second time: " << data << std::endl; data+=1; c=c.resume(); std::cout << "f1: entered third time: " << data << std::endl; return std::move(c); }); std::cout << "f1: returned first time: " << data << std::endl; data+=1; c=c.resume(); std::cout << "f1: returned second time: " << data << std::endl; data+=1; c=c.resume_with([&data](ctx::continuation && c){ std::cout << "f2: entered: " << data << std::endl; data=-1; return std::move( c); }); std::cout << "f1: returned third time" output: f1: entered first time: 0 f1: returned first time: 1 f1: entered second time: 2 f1: returned second time: 3 f2: entered: 4 f1: entered third time: -1 f1: returned third time ``` -------------------------------- ### Define stack_context Structure in C++ Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_context This C++ code defines the `stack_context` structure, which holds a pointer to the stack's beginning (`sp`) and its size (`size`). It is designed to accommodate potential additional control structures for segmented stacks. ```cpp struct stack_context { void * sp; std::size_t size; // might contain additional control structures // for segmented stacks } ``` -------------------------------- ### Boost.Context segmented_stack Class Definition Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/segmented Defines the basic_segmented_stack template, modeling the stack-allocator concept for growable stacks. It includes constructors for stack size, and methods for allocating and deallocating stack contexts. This is the core structure for on-demand stack growth. ```cpp #include template< typename traitsT > struct basic_segmented_stack { typedef traitT traits_type; basic_segmented_stack(std::size_t size = traits_type::default_size()); stack_context allocate(); void deallocate( stack_context &); } typedef basic_segmented_stack< stack_traits > segmented_stack; ``` -------------------------------- ### Boost C++ continuation Boolean Operators Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Includes explicit 'operator bool()' to check if a continuation is valid and 'operator!' to check for invalidity. These are noexcept operations. ```cpp explicit operator bool() const noexcept; bool operator!() const noexcept; ``` -------------------------------- ### Boost.Context Fiber Class Definition (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Defines the 'fiber' class from the Boost.Context library. This class manages coroutine execution contexts. It includes constructors for creating and initializing fibers, methods for resuming execution, and overloaded operators for comparison and state checking. It does not support copying. ```cpp #include class fiber { public: fiber() noexcept; template fiber(Fn && fn); template fiber(std::allocator_arg_t, StackAlloc && salloc, Fn && fn); ~fiber(); fiber(fiber && other) noexcept; fiber & operator=(fiber && other) noexcept; fiber(fiber const& other) noexcept = delete; fiber & operator=(fiber const& other) noexcept = delete; fiber resume() &&; template fiber resume_with(Fn && fn) &&; explicit operator bool() const noexcept; bool operator!() const noexcept; bool operator==(fiber const& other) const noexcept; bool operator!=(fiber const& other) const noexcept; bool operator<(fiber const& other) const noexcept; bool operator>(fiber const& other) const noexcept; bool operator<=(fiber const& other) const noexcept; bool operator>=(fiber const& other) const noexcept; template friend std::basic_ostream & operator<<(std::basic_ostream & os,fiber const& other) { void swap(fiber & other) noexcept; }; ``` -------------------------------- ### Transfer Exceptions Between Continuations Using C++ std::exception_ptr Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc Shows how to transfer exceptions between different continuations in C++ using `std::exception_ptr`. This allows for robust error handling across asynchronous operations managed by continuations. ```C++ namespace ctx=boost::context; struct my_exception : public std::runtime_error { ctx::continuation c; my_exception(ctx::continuation && c_,std::string const& what) : std::runtime_error{ what }, c{ std::move( c_) } { } }; ctx::continuation c=ctx::callcc([](ctx::continuation && c) { for (;;) { try { std::cout << "entered" << std::endl; c=c.resume(); } catch (my_exception & ex) { std::cerr << "my_exception: " << ex.what() << std::endl; return std::move(ex.c); } } return std::move(c); }); c = c.resume_with( [](ctx::continuation && c){ throw my_exception(std::move(c),"abc"); return std::move( c); }); output: entered my_exception: abc ``` -------------------------------- ### Boost stack_traits minimum_size() Method Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_traits The `minimum_size` static method of the Boost stack_traits class returns the minimum stack size in bytes, as defined by the environment (e.g., 4kB on Win32, 8kB on Win64, or defined by `rlimit` on POSIX systems). It does not throw any exceptions. ```cpp static std::size_t minimum_size() noexcept; ``` -------------------------------- ### Boost C++ continuation Swap Function Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Declares the 'swap' non-member function to exchange the states of two continuation objects. This operation is noexcept. ```cpp void swap(continuation & other) noexcept; ``` -------------------------------- ### Boost Context fixedsize_stack Class Definition and Usage (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/fixedsize Defines the `basic_fixedsize_stack` template and its typedef `fixedsize_stack`. This class manages stack memory using `std::malloc` and `std::free`, suitable for stack allocation without guard pages. It includes methods for allocating and deallocating stack contexts. ```cpp #include template< typename traitsT > struct basic_fixedsize_stack { typedef traitT traits_type; basic_fixesize_stack(std::size_t size = traits_type::default_size()); stack_context allocate(); void deallocate( stack_context &); } typedef basic_fixedsize_stack< stack_traits > fixedsize_stack; ``` -------------------------------- ### Boost.Context Fiber Boolean and Comparison Operators (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Documents the boolean conversion operator and inequality operators for the Boost.Context 'fiber' class. The boolean operator checks if a fiber points to a valid captured context. Comparison operators define an implementation-defined total order between fiber objects. ```cpp explicit operator bool() const noexcept; bool operator!() const noexcept; bool operator==(fiber const& other) const noexcept; bool operator!=(fiber const& other) const noexcept; bool operator<(fiber const& other) const noexcept; bool operator>(fiber const& other) const noexcept; bool operator<=(fiber const& other) const noexcept; bool operator>=(fiber const& other) const noexcept; ``` -------------------------------- ### Define Boost preallocated Struct Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/struct__preallocated_ Defines the `preallocated` struct used for managing pre-allocated stack memory. It includes members for the stack pointer, size, and stack context. The constructor initializes these members. ```cpp struct preallocated { void * sp; std::size_t size; stack_context sctx; preallocated( void * sp, std:size_t size, stack_allocator sctx) noexcept; }; ``` -------------------------------- ### Output stream operator for Boost Context fiber (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Overloads the output stream operator `<<` for the `fiber` class in Boost Context. This function takes an output stream and a `fiber` object, writing the fiber's representation to the stream. It returns the modified output stream. Dependencies include the `fiber` class definition. ```cpp template std::basic_ostream & operator<<(std::basic_ostream & os,fiber const& other); ``` -------------------------------- ### Boost stack_traits Class Definition Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_traits Defines the stack_traits class used by Boost stack allocators to query environment-specific stack properties. It includes static methods to determine if a stack has an unbounded size, retrieve page size, default size, minimum size, and maximum size. ```cpp #include struct stack_traits { static bool is_unbounded() noexcept; static std::size_t page_size() noexcept; static std::size_t default_size() noexcept; static std::size_t minimum_size() noexcept; static std::size_t maximum_size() noexcept; } ``` -------------------------------- ### Basic Protected Fixedsize Stack Allocation in C++ Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/protected_fixedsize Defines the basic_protected_fixedsize struct for stack allocation with guard pages. It includes methods for allocating and deallocating stack memory, ensuring protection against exceeding stack boundaries. The `allocate` method reserves memory and stores stack details, while `deallocate` releases it. ```C++ #include template< typename traitsT > struct basic_protected_fixedsize { typedef traitT traits_type; basic_protected_fixesize(std::size_t size = traits_type::default_size()); stack_context allocate(); void deallocate( stack_context &); } typedef basic_protected_fixedsize< stack_traits > protected_fixedsize ``` -------------------------------- ### Boost C++ continuation Stream Output Operator Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/cc/class__continuation_ Overloads the 'operator<<' for std::basic_ostream to allow writing the representation of a continuation object to an output stream. This is a non-member friend function. ```cpp template friend std::basic_ostream & operator<<(std::basic_ostream & os,continuation const& other); ``` -------------------------------- ### Boost stack_traits page_size() Method Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_traits The `page_size` static method of the Boost stack_traits class returns the memory page size in bytes. This information is useful for memory management and alignment. It does not throw any exceptions. ```cpp static std::size_t page_size() noexcept; ``` -------------------------------- ### Allocate Stack Memory with pooled_fixedsize_stack Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/pooled_fixedsize Calls the allocate() method to request at least stack_size bytes of memory. The method stores a pointer and actual size in a stack_context object, with the stored address being the highest or lowest address depending on architecture stack growth direction. ```cpp stack_context allocate(); ``` -------------------------------- ### Boost.Context Fiber Destructor (C++) Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/ff/class__fiber_ Describes the destructor for the Boost.Context 'fiber' class. It ensures that the associated stack is properly deallocated if the fiber is valid. ```cpp ~fiber(); ``` -------------------------------- ### Boost stack_traits maximum_size() Method Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_traits The `maximum_size` static method of the Boost stack_traits class returns the maximum stack size in bytes, provided that `is_unbounded()` returns false. It requires that the environment defines a limit for the stack size. It does not throw any exceptions. ```cpp static std::size_t maximum_size() noexcept; ``` -------------------------------- ### Boost stack_traits default_size() Method Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_traits The `default_size` static method of the Boost stack_traits class returns a platform-specific default stack size. If the stack is unbounded, it returns the maximum of 128 kB and the minimum stack size. It does not throw any exceptions. ```cpp static std::size_t default_size() noexcept; ``` -------------------------------- ### Deallocate Stack Memory with pooled_fixedsize_stack Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/pooled_fixedsize Calls the deallocate() method to release previously allocated stack space. Requires a valid stack_context object whose sp pointer and size must satisfy trait constraints. ```cpp void deallocate( stack_context & sctx); ``` -------------------------------- ### Boost stack_traits is_unbounded() Method Source: https://www.boost.org/doc/libs/latest/libs/context/doc/html/context/stack/stack_traits The `is_unbounded` static method of the Boost stack_traits class returns a boolean indicating whether the environment imposes a limit on stack size. It does not throw any exceptions. ```cpp static bool is_unbounded() noexcept; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.