code cleanup; fix move ctors and assignment

This commit is contained in:
Günter Obiltschnig 2020-01-21 17:52:43 +01:00
parent de52e23d12
commit 18adb1e43b
20 changed files with 327 additions and 188 deletions

View File

@ -90,9 +90,21 @@ public:
/// Creates a new CipherKeyImpl object. Autoinitializes key and
/// initialization vector.
CipherKey(const CipherKey& other);
/// Copy constructor.
CipherKey(CipherKey&& other) noexcept;
/// Copy constructor.
~CipherKey();
/// Destroys the CipherKeyImpl.
CipherKey& operator = (const CipherKey& other);
/// Assignment.
CipherKey& operator = (CipherKey&& other) noexcept;
/// Move assignment.
const std::string& name() const;
/// Returns the name of the Cipher.

View File

@ -73,9 +73,21 @@ public:
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
ECKey(const ECKey& key);
/// Creates the ECKey by copying another one.
ECKey(ECKey&& key) noexcept;
/// Creates the ECKey by moving another one.
~ECKey();
/// Destroys the ECKey.
ECKey& operator = (const ECKey& other);
/// Assignment.
ECKey& operator = (ECKey&& other) noexcept;
/// Move assignment.
ECKeyImpl::Ptr impl() const;
/// Returns the impl object.
@ -97,9 +109,6 @@ public:
static bool hasCurve(const std::string& name);
/// Returns true if the named curve is found,
/// false otherwise.
private:
ECKeyImpl::Ptr _pImpl;
};
@ -108,7 +117,7 @@ private:
//
inline ECKeyImpl::Ptr ECKey::impl() const
{
return _pImpl;
return KeyPair::impl().cast<ECKeyImpl>();
}

View File

@ -1,7 +1,6 @@
//
// KeyPair.h
//
//
// Library: Crypto
// Package: CryptoCore
// Module: KeyPair
@ -48,6 +47,18 @@ public:
explicit KeyPair(KeyPairImpl::Ptr pKeyPairImpl = 0);
/// Extracts the RSA public key from the given certificate.
KeyPair(const KeyPair& other);
/// Copy constructor.
KeyPair(KeyPair&& other) noexcept;
/// Move constructor.
KeyPair& operator = (const KeyPair& other);
/// Assignment.
KeyPair& operator = (KeyPair&& other) noexcept;
/// Move assignment.
virtual ~KeyPair();
/// Destroys the KeyPair.
@ -87,7 +98,6 @@ private:
//
// inlines
//
inline int KeyPair::size() const
{
return _pImpl->size();
@ -115,6 +125,7 @@ inline const std::string& KeyPair::name() const
return _pImpl->name();
}
inline KeyPairImpl::Ptr KeyPair::impl() const
{
return _pImpl;

View File

@ -90,9 +90,21 @@ public:
/// If a private key is specified, you don't need to specify a public key file.
/// OpenSSL will auto-create the public key from the private key.
RSAKey(const RSAKey& other);
/// Copy constructor.
RSAKey(RSAKey&& other) noexcept;
/// Move constructor.
~RSAKey();
/// Destroys the RSAKey.
RSAKey& operator = (const RSAKey& other);
/// Assignment.
RSAKey& operator = (RSAKey&& other) noexcept;
/// Move assignment.
RSAKeyImpl::ByteVec modulus() const;
/// Returns the RSA modulus.
@ -104,9 +116,6 @@ public:
RSAKeyImpl::Ptr impl() const;
/// Returns the impl object.
private:
RSAKeyImpl::Ptr _pImpl;
};
@ -115,7 +124,7 @@ private:
//
inline RSAKeyImpl::Ptr RSAKey::impl() const
{
return _pImpl;
return KeyPair::impl().cast<RSAKeyImpl>();
}

View File

@ -41,9 +41,38 @@ CipherKey::CipherKey(const std::string& name):
}
CipherKey::CipherKey(const CipherKey& other):
_pImpl(other._pImpl)
{
}
CipherKey::CipherKey(CipherKey&& other) noexcept:
_pImpl(std::move(other._pImpl))
{
}
CipherKey::~CipherKey()
{
}
CipherKey& CipherKey::operator = (const CipherKey& other)
{
if (&other != this)
{
_pImpl = other._pImpl;
}
return *this;
}
CipherKey& CipherKey::operator = (CipherKey&& other) noexcept
{
_pImpl = std::move(other._pImpl);
return *this;
}
} } // namespace Poco::Crypto

View File

@ -22,47 +22,49 @@ namespace Crypto {
ECKey::ECKey(const EVPPKey& key):
KeyPair(new ECKeyImpl(key)),
_pImpl(KeyPair::impl().cast<ECKeyImpl>())
KeyPair(new ECKeyImpl(key))
{
}
ECKey::ECKey(const X509Certificate& cert):
KeyPair(new ECKeyImpl(cert)),
_pImpl(KeyPair::impl().cast<ECKeyImpl>())
KeyPair(new ECKeyImpl(cert))
{
}
ECKey::ECKey(const PKCS12Container& cont):
KeyPair(new ECKeyImpl(cont)),
_pImpl(KeyPair::impl().cast<ECKeyImpl>())
KeyPair(new ECKeyImpl(cont))
{
}
ECKey::ECKey(const std::string& eccGroup):
KeyPair(new ECKeyImpl(OBJ_txt2nid(eccGroup.c_str()))),
_pImpl(KeyPair::impl().cast<ECKeyImpl>())
KeyPair(new ECKeyImpl(OBJ_txt2nid(eccGroup.c_str())))
{
}
ECKey::ECKey(const std::string& publicKeyFile,
const std::string& privateKeyFile,
const std::string& privateKeyPassphrase):
KeyPair(new ECKeyImpl(publicKeyFile, privateKeyFile, privateKeyPassphrase)),
_pImpl(KeyPair::impl().cast<ECKeyImpl>())
ECKey::ECKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase):
KeyPair(new ECKeyImpl(publicKeyFile, privateKeyFile, privateKeyPassphrase))
{
}
ECKey::ECKey(std::istream* pPublicKeyStream,
std::istream* pPrivateKeyStream,
const std::string& privateKeyPassphrase):
KeyPair(new ECKeyImpl(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase)),
_pImpl(KeyPair::impl().cast<ECKeyImpl>())
ECKey::ECKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase):
KeyPair(new ECKeyImpl(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase))
{
}
ECKey::ECKey(const ECKey& other):
KeyPair(other)
{
}
ECKey::ECKey(ECKey&& other) noexcept:
KeyPair(std::move(other))
{
}
@ -72,4 +74,18 @@ ECKey::~ECKey()
}
ECKey& ECKey::operator = (const ECKey& other)
{
KeyPair::operator = (other);
return *this;
}
ECKey& ECKey::operator = (ECKey&& other) noexcept
{
KeyPair::operator = (std::move(other));
return *this;
}
} } // namespace Poco::Crypto

View File

@ -93,7 +93,6 @@ EVPPKey::EVPPKey(EVPPKey&& other) noexcept:
_pEVPPKey(other._pEVPPKey)
{
other._pEVPPKey = nullptr;
poco_check_ptr(_pEVPPKey);
}
@ -109,7 +108,6 @@ EVPPKey& EVPPKey::operator = (EVPPKey&& other) noexcept
{
_pEVPPKey = other._pEVPPKey;
other._pEVPPKey = nullptr;
poco_check_ptr(_pEVPPKey);
return *this;
}

View File

@ -1,7 +1,6 @@
//
// KeyPair.cpp
//
//
// Library: Crypto
// Package: CryptoCore
// Module: KeyPair
@ -21,7 +20,20 @@ namespace Poco {
namespace Crypto {
KeyPair::KeyPair(KeyPairImpl::Ptr pKeyPairImpl): _pImpl(pKeyPairImpl)
KeyPair::KeyPair(KeyPairImpl::Ptr pKeyPairImpl):
_pImpl(pKeyPairImpl)
{
}
KeyPair::KeyPair(const KeyPair& other):
_pImpl(other._pImpl)
{
}
KeyPair::KeyPair(KeyPair&& other) noexcept:
_pImpl(std::move(other._pImpl))
{
}
@ -31,4 +43,21 @@ KeyPair::~KeyPair()
}
KeyPair& KeyPair::operator = (const KeyPair& other)
{
if (&other != this)
{
_pImpl = other._pImpl;
}
return *this;
}
KeyPair& KeyPair::operator = (KeyPair&& other) noexcept
{
_pImpl = std::move(other._pImpl);
return *this;
}
} } // namespace Poco::Crypto

View File

@ -73,6 +73,17 @@ PKCS12Container::PKCS12Container(const PKCS12Container& other):
}
PKCS12Container::PKCS12Container(PKCS12Container&& other) noexcept:
_pKey(other._pKey),
_pX509Cert(std::move(other._pX509Cert)),
_caCertList(std::move(other._caCertList)),
_caCertNames(std::move(other._caCertNames)),
_pkcsFriendlyName(std::move(other._pkcsFriendlyName))
{
other._pKey = nullptr;
}
PKCS12Container& PKCS12Container::operator = (const PKCS12Container& other)
{
if (&other != this)
@ -88,17 +99,6 @@ PKCS12Container& PKCS12Container::operator = (const PKCS12Container& other)
}
PKCS12Container::PKCS12Container(PKCS12Container&& other) noexcept:
_pKey(other._pKey),
_pX509Cert(std::move(other._pX509Cert)),
_caCertList(std::move(other._caCertList)),
_caCertNames(std::move(other._caCertNames)),
_pkcsFriendlyName(std::move(other._pkcsFriendlyName))
{
other._pKey = nullptr;
}
PKCS12Container& PKCS12Container::operator = (PKCS12Container&& other) noexcept
{
if (_pKey) EVP_PKEY_free(_pKey);

View File

@ -21,43 +21,49 @@ namespace Crypto {
RSAKey::RSAKey(const EVPPKey& key):
KeyPair(new RSAKeyImpl(key)),
_pImpl(KeyPair::impl().cast<RSAKeyImpl>())
KeyPair(new RSAKeyImpl(key))
{
}
RSAKey::RSAKey(const X509Certificate& cert):
KeyPair(new RSAKeyImpl(cert)),
_pImpl(KeyPair::impl().cast<RSAKeyImpl>())
KeyPair(new RSAKeyImpl(cert))
{
}
RSAKey::RSAKey(const PKCS12Container& cont):
KeyPair(new RSAKeyImpl(cont)),
_pImpl(KeyPair::impl().cast<RSAKeyImpl>())
KeyPair(new RSAKeyImpl(cont))
{
}
RSAKey::RSAKey(KeyLength keyLength, Exponent exp):
KeyPair(new RSAKeyImpl(keyLength, (exp == EXP_LARGE) ? RSA_F4 : RSA_3)),
_pImpl(KeyPair::impl().cast<RSAKeyImpl>())
KeyPair(new RSAKeyImpl(keyLength, (exp == EXP_LARGE) ? RSA_F4 : RSA_3))
{
}
RSAKey::RSAKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase):
KeyPair(new RSAKeyImpl(publicKeyFile, privateKeyFile, privateKeyPassphrase)),
_pImpl(KeyPair::impl().cast<RSAKeyImpl>())
KeyPair(new RSAKeyImpl(publicKeyFile, privateKeyFile, privateKeyPassphrase))
{
}
RSAKey::RSAKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase):
KeyPair(new RSAKeyImpl(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase)),
_pImpl(KeyPair::impl().cast<RSAKeyImpl>())
KeyPair(new RSAKeyImpl(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase))
{
}
RSAKey::RSAKey(const RSAKey& other):
KeyPair(other)
{
}
RSAKey::RSAKey(RSAKey&& other) noexcept:
KeyPair(std::move(other))
{
}
@ -66,21 +72,36 @@ RSAKey::~RSAKey()
{
}
RSAKey& RSAKey::operator = (const RSAKey& other)
{
KeyPair::operator = (other);
return *this;
}
RSAKey& RSAKey::operator = (RSAKey&& other) noexcept
{
KeyPair::operator = (std::move(other));
return *this;
}
RSAKeyImpl::ByteVec RSAKey::modulus() const
{
return _pImpl->modulus();
return impl()->modulus();
}
RSAKeyImpl::ByteVec RSAKey::encryptionExponent() const
{
return _pImpl->encryptionExponent();
return impl()->encryptionExponent();
}
RSAKeyImpl::ByteVec RSAKey::decryptionExponent() const
{
return _pImpl->decryptionExponent();
return impl()->decryptionExponent();
}

View File

@ -29,12 +29,14 @@
#include <openssl/evp.h>
#include <openssl/bn.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define ASN1_STRING_get0_data ASN1_STRING_data
#define X509_get0_notBefore X509_get_notBefore
#define X509_get0_notAfter X509_get_notAfter
#endif
namespace Poco {
namespace Crypto {
@ -113,6 +115,7 @@ X509Certificate& X509Certificate::operator = (X509Certificate&& cert) noexcept
_issuerName = std::move(cert._issuerName);
_subjectName = std::move(cert._subjectName);
_serialNumber = std::move(cert._serialNumber);
if (_pCert) X509_free(_pCert);
_pCert = cert._pCert; cert._pCert = nullptr;
return *this;
}

View File

@ -91,6 +91,7 @@ MetaColumn& MetaColumn::operator = (MetaColumn&& other) noexcept
return *this;
}
void MetaColumn::swap(MetaColumn& other)
{
std::swap(_name, other._name);

View File

@ -50,14 +50,14 @@ Session::Session(const std::string& connection,
Session::Session(const Session& other):
_pImpl(other._pImpl),
_statementCreator(other._pImpl)
_statementCreator(other._statementCreator)
{
}
Session::Session(Session&& other) noexcept:
_pImpl(std::move(other._pImpl)),
_statementCreator(std::move(other._pImpl))
_statementCreator(std::move(other._statementCreator))
{
}

View File

@ -78,7 +78,7 @@ public:
Message(const Message& msg);
/// Creates a Message by copying another one.
Message(Message&& msg);
Message(Message&& msg) noexcept;
/// Creates a Message by copying another one.
Message(const Message& msg, const std::string& text);
@ -90,7 +90,7 @@ public:
Message& operator = (const Message& msg);
/// Assignment operator.
Message& operator = (Message&& msg);
Message& operator = (Message&& msg) noexcept;
/// Assignment operator.
void swap(Message& msg);

View File

@ -82,7 +82,7 @@ Message::Message(const Message& msg):
}
Message::Message(Message&& msg) :
Message::Message(Message&& msg) noexcept:
_source(std::move(msg._source)),
_text(std::move(msg._text)),
_prio(std::move(msg._prio)),
@ -147,9 +147,7 @@ Message& Message::operator = (const Message& msg)
}
Message& Message::operator = (Message&& msg)
{
if (&msg != this)
Message& Message::operator = (Message&& msg) noexcept
{
_source = std::move(msg._source);
_text = std::move(msg._text);
@ -163,7 +161,6 @@ Message& Message::operator = (Message&& msg)
delete _pMap;
_pMap = msg._pMap;
msg._pMap = nullptr;
}
return *this;
}

View File

@ -73,16 +73,16 @@ public:
Array(const Array& copy);
/// Creates an Array by copying another one.
Array(Array&& other);
Array(Array&& other) noexcept;
/// Move constructor
Array& operator=(Array&& other);
/// Move assignment operator.
Array& operator = (const Array& other);
/// Assignment operator.
virtual ~Array();
Array& operator = (Array&& other) noexcept;
/// Move assignment operator.
~Array();
/// Destroys the Array.
void setEscapeUnicode(bool escape = true);

View File

@ -84,18 +84,18 @@ public:
/// Struct is not copied to keep the operation as
/// efficient as possible (when needed, it will be generated upon request).
Object(Object&& other);
Object(Object&& other) noexcept;
/// Move constructor
Object &operator =(Object &&other);
// Move asignment operator
virtual ~Object();
~Object();
/// Destroys the Object.
Object &operator = (const Object &other);
// Assignment operator
Object &operator = (Object &&other) noexcept;
// Move asignment operator
void setEscapeUnicode(bool escape = true);
/// Sets the flag for escaping unicode.

View File

@ -25,15 +25,27 @@ namespace Poco {
namespace JSON {
Array::Array(int options): _modified(false),
Array::Array(int options):
_modified(false),
_escapeUnicode((options & Poco::JSON_ESCAPE_UNICODE) != 0)
{
}
Array::Array(const Array& other) : _values(other._values),
Array::Array(const Array& other) :
_values(other._values),
_pArray(other._pArray),
_modified(other._modified)
_modified(other._modified),
_escapeUnicode(other._escapeUnicode)
{
}
Array::Array(Array&& other) noexcept:
_values(std::move(other._values)),
_pArray(std::move(other._pArray)),
_modified(other._modified),
_escapeUnicode(other._escapeUnicode)
{
}
@ -45,26 +57,18 @@ Array &Array::operator=(const Array& other)
_values = other._values;
_pArray = other._pArray;
_modified = other._modified;
_escapeUnicode = other._escapeUnicode;
}
return *this;
}
Array::Array(Array&& other):
_values(std::move(other._values)),
_pArray(!other._modified ? other._pArray : 0),
_modified(other._modified)
{
_pArray = 0;
}
Array &Array::operator = (Array&& other)
Array& Array::operator = (Array&& other) noexcept
{
_values = std::move(other._values);
_pArray = other._pArray;
other._pArray = 0;
_pArray = std::move(other._pArray);
_modified = other._modified;
_escapeUnicode = other._escapeUnicode;
return *this;
}

View File

@ -42,29 +42,15 @@ Object::Object(const Object& other) : _values(other._values),
}
Object::Object(Object&& other):
Object::Object(Object&& other) noexcept:
_values(std::move(other._values)),
_keys(std::move(other._keys)),
_preserveInsOrder(other._preserveInsOrder),
_escapeUnicode(other._escapeUnicode),
_pStruct(!other._modified ? other._pStruct : 0),
_pStruct(std::move(other._pStruct)),
_pOrdStruct(std::move(other._pOrdStruct)),
_modified(other._modified)
{
other.clear();
}
Object &Object::operator = (Object&& other)
{
_values = other._values;
_preserveInsOrder = other._preserveInsOrder;
syncKeys(other._keys);
_escapeUnicode = other._escapeUnicode;
_pStruct = !other._modified ? other._pStruct : 0;
_modified = other._modified;
other.clear();
return *this;
}
@ -88,6 +74,20 @@ Object &Object::operator= (const Object &other)
}
Object& Object::operator = (Object&& other) noexcept
{
_values = std::move(other._values);
_keys = std::move(other._keys);
_preserveInsOrder = other._preserveInsOrder;
_escapeUnicode = other._escapeUnicode;
_pStruct = std::move(other._pStruct);
_pOrdStruct = std::move(other._pOrdStruct);
_modified = other._modified;
return *this;
}
void Object::syncKeys(const KeyList& keys)
{
if(_preserveInsOrder)

View File

@ -91,7 +91,7 @@ X509Certificate& X509Certificate::operator = (const X509Certificate& cert)
X509Certificate& X509Certificate::operator = (X509Certificate&& cert) noexcept
{
Poco::Crypto::X509Certificate::operator = (cert);
Poco::Crypto::X509Certificate::operator = (std::move(cert));
return *this;
}