Skip to content

Instantly share code, notes, and snippets.

@kirkshoop
Created September 6, 2013 06:10
Show Gist options
  • Save kirkshoop/6460133 to your computer and use it in GitHub Desktop.
Save kirkshoop/6460133 to your computer and use it in GitHub Desktop.
implementation of operation pattern
template <class O>
struct OperationPattern
{
typedef decltype(((O)nullptr)->GetDeferral()) D;
OperationPattern(O operation) :
operation(operation),
deferral(operation->GetDeferral())
{
}
O Operation() const {
return operation;
};
D Deferral() const {
return deferral;
};
void Dispose()
{
deferral->Complete();
}
operator Disposable() const
{
// make sure to capture state and not 'this'.
// usage means that 'this' will usualy be destructed
// immediately
auto local = deferral;
return Disposable([local]{
local->Complete();
});
}
private:
O operation;
D deferral;
};
template <class O>
OperationPattern<O> make_operation_pattern(O o)
{
return OperationPattern<O>(std::move(o));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment