cpp: Add RAII class for saving/restoring the key list mode

* lang/cpp/src/context.h, lang/cpp/src/context.cpp (class Context):
Add nested class KeyListModeSaver.
--

This RAII-style class can be used to save the currently used key list
mode in case it needs to be changed temporarily. On destruction, it
will restore the key list mode that was active at construction time.

GnuPG-bug-id: 5951
This commit is contained in:
Ingo Klöcker 2022-05-02 15:50:18 +02:00
parent 99fd565889
commit 34786132fe
2 changed files with 24 additions and 0 deletions

View File

@ -195,6 +195,19 @@ std::ostream &operator<<(std::ostream &os, const Error &err)
return os << "GpgME::Error(" << err.encodedError() << " (" << err.asString() << "))"; return os << "GpgME::Error(" << err.encodedError() << " (" << err.asString() << "))";
} }
Context::KeyListModeSaver::KeyListModeSaver(Context *ctx)
: mCtx{ctx}
, mKeyListMode{ctx ? ctx->keyListMode() : 0}
{
}
Context::KeyListModeSaver::~KeyListModeSaver()
{
if (mCtx) {
mCtx->setKeyListMode(mKeyListMode);
}
}
Context::Context(gpgme_ctx_t ctx) : d(new Private(ctx)) Context::Context(gpgme_ctx_t ctx) : d(new Private(ctx))
{ {
} }

View File

@ -64,6 +64,17 @@ class GPGMEPP_EXPORT Context
public: public:
//using GpgME::Protocol; //using GpgME::Protocol;
/// RAII-style class for saving/restoring the key list mode.
class KeyListModeSaver
{
public:
explicit KeyListModeSaver(Context *ctx);
~KeyListModeSaver();
private:
Context *mCtx;
unsigned int mKeyListMode;
};
// //
// Creation and destruction: // Creation and destruction:
// //