Files
salmanoff/include/asynchronousContinuation.h
T
2025-09-10 15:10:10 -04:00

51 lines
1.5 KiB
C++

#ifndef ASYNCHRONOUS_CONTINUATION_H
#define ASYNCHRONOUS_CONTINUATION_H
#include <functional>
#include <memory>
/**
* AsynchronousContinuation - Template base class for async sequence management
*
* This template provides a common pattern for managing asynchronous operations
* that need to maintain object lifetime through a sequence of callbacks.
*
* The template parameter OriginalCbFnT represents the signature of the original
* callback that will be invoked when the async sequence completes.
*
* Usage:
* class MyAsyncReq
* : public AsynchronousContinuation<std::function<void(bool)>>
* {
* public:
* MyAsyncReq(std::function<void(bool)> originalCbFn)
* : AsynchronousContinuation(originalCbFn)
* {}
*
* // Segment methods take only the shared_ptr for lifetime management
* void myAsyncReq1(std::shared_ptr<MyAsyncReq> context);
* void myAsyncReq2(std::shared_ptr<MyAsyncReq> context);
* };
*/
template <class OriginalCbFnT>
class AsynchronousContinuation
{
public:
explicit AsynchronousContinuation(OriginalCbFnT originalCbFn)
: originalCbFn(std::move(originalCbFn))
{}
/** EXPLANATION:
* Each numbered segmented sequence persists the lifetime of the
* continuation object by taking a copy of its shared_ptr.
*/
typedef void (SegmentFn)(
std::shared_ptr<AsynchronousContinuation<OriginalCbFnT>>
lifetimePreservingConveyance);
protected:
OriginalCbFnT originalCbFn;
};
#endif // ASYNCHRONOUS_CONTINUATION_H