Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Stroika::Foundation::Execution::DeclareActivity< ACTIVITY > Class Template Reference

#include <Activity.h>

Public Member Functions

 DeclareActivity (const ACTIVITY *activity) noexcept
 

Detailed Description

template<typename ACTIVITY>
class Stroika::Foundation::Execution::DeclareActivity< ACTIVITY >

Push the argument Activity onto the current thread's Activity stack in the constructor, and pop it off in the destructor.

An exception captures the activities in force when it is THROWN with Execution::Throw (), not those in force when it was constructed. So an exception built in one context (a static const, say) and thrown in another reports the context of the throw. Throw () fills in only an exception that specified no activities of its own (any Stroika Exception<>), and a bare throw X{} gets none. For a third-party exception, wrap it in a NestedException and Throw () that.

Each activity's AsString () runs at that Throw (), while its DeclareActivity is still in scope - so a LazyEvalActivity's lambda may capture by reference.

Example Usage
static constexpr Activity kBuildingThingy_ {"building thingy"sv };
try {
DeclareActivity declareActivity { &kBuildingThingy_ };
doBuildThing (); // Execution::Throw () any Exception<>
}
catch (...) {
String exceptionMsg = Characters::ToString (current_exception ());
Assert (exceptionMsg.Contains (kBuildingThingy_.AsString ())); // exception e while building thingy...
}
String ToString(T &&t, ARGS... args)
Return a debug-friendly, display version of the argument: not guaranteed parsable or usable except fo...
Definition ToString.inl:424
Note
DeclareActivity objects must be destroyed in the reverse order of their construction, on the thread that constructed them (Asserted in the destructor) - so in practice automatic variables, and never one that lives across a co_await, which breaks that order even on one thread. The stack is per-thread: work handed to another thread (a thread pool task, say) does not see it.

Definition at line 153 of file Activity.h.

Constructor & Destructor Documentation

◆ DeclareActivity()

template<typename ACTIVITY >
Stroika::Foundation::Execution::DeclareActivity< ACTIVITY >::DeclareActivity ( const ACTIVITY *  activity)
noexcept

The caller must always declare an activity object (an Activity<> or LazyEvalActivity<>) whose address can be taken, and whose lifetime exceeds that of the DeclareActivity object.

Note
the activity argument MAY be nullptr, in which case this does nothing.
    This is allowed to facilitate examples like:
        \code
            DeclareActivity declareActivity { flag ? &kSomeActivity_ : nullptr }; // so we only conditionally declare the activity
        \endcode

    I considered using optional<ACTIVITY> - but tricky since we pass in pointer - and really not much point. This has practically zero
    overhead, and is easy enough to understand and use.

Definition at line 56 of file Activity.inl.


The documentation for this class was generated from the following files: