Add CryptoManager class to provide a guard-style class to manage the current Crypto instance

pull/625/head
Michael 5 years ago
parent c595e175fd
commit aea0e32efc
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -1 +1,6 @@
#include <crypto/crypto.hpp>
namespace llarp
{
Crypto* CryptoManager::m_crypto = nullptr;
}

@ -6,6 +6,7 @@
#include <util/buffer.hpp>
#include <absl/base/optimization.h>
#include <functional>
#include <stdbool.h>
#include <stdint.h>
@ -120,6 +121,36 @@ namespace llarp
const byte_t *
pq_keypair_to_secret(const PQKeyPair &keypair);
struct CryptoManager
{
private:
static Crypto *m_crypto;
Crypto *m_prevCrypto;
public:
CryptoManager(Crypto *crypto) : m_prevCrypto(m_crypto)
{
m_crypto = crypto;
}
~CryptoManager()
{
m_crypto = m_prevCrypto;
}
static Crypto *
instance() ABSL_ATTRIBUTE_RETURNS_NONNULL
{
if(ABSL_PREDICT_TRUE(m_crypto))
{
return m_crypto;
}
throw std::logic_error("Cryptomanager::instance() was undefined");
}
};
} // namespace llarp
#endif

Loading…
Cancel
Save