|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +#ifndef HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_ |
| 3 | +#define HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_ |
| 4 | + |
| 5 | +#include <functional> |
| 6 | +#include <memory> |
| 7 | + |
| 8 | +namespace Hiero::internal::OpenSSLUtils |
| 9 | +{ |
| 10 | +/** |
| 11 | + * Templated base wrapper class to be used for crypto library objects that require custom deleter and copier functions. |
| 12 | + * |
| 13 | + * @tparam ObjectType The type of crypto object this class should wrap. |
| 14 | + * @tparam CopierFunc The copier type (function signature) for the crypto object. |
| 15 | + */ |
| 16 | +template<typename ObjectType, typename CopierFunc = std::function<ObjectType*(const ObjectType*)>> |
| 17 | +class CryptoObjectWrapper |
| 18 | +{ |
| 19 | +public: |
| 20 | + virtual ~CryptoObjectWrapper() = default; |
| 21 | + |
| 22 | + /** |
| 23 | + * Copy constructor. |
| 24 | + * |
| 25 | + * @param other The CryptoObjectWrapper object to copy. |
| 26 | + */ |
| 27 | + CryptoObjectWrapper(const CryptoObjectWrapper& other) |
| 28 | + : mObject({ other.mCopier(other.mObject.get()), other.mObject.get_deleter() }) |
| 29 | + , mCopier(other.mCopier) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Copy assignment operator. |
| 35 | + * |
| 36 | + * @param other The CryptoObjectWrapper object to copy. |
| 37 | + */ |
| 38 | + CryptoObjectWrapper& operator=(const CryptoObjectWrapper& other) |
| 39 | + { |
| 40 | + if (this != &other) |
| 41 | + { |
| 42 | + mObject = { other.mCopier(other.mObject.get()), other.mObject.get_deleter() }; |
| 43 | + mCopier = other.mCopier; |
| 44 | + } |
| 45 | + |
| 46 | + return *this; |
| 47 | + } |
| 48 | + |
| 49 | + CryptoObjectWrapper(CryptoObjectWrapper&&) noexcept = default; |
| 50 | + CryptoObjectWrapper& operator=(CryptoObjectWrapper&&) noexcept = default; |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the wrapped crypto object. |
| 54 | + * |
| 55 | + * @return A pointer to the wrapped crypto object. nullptr if no object exists. |
| 56 | + */ |
| 57 | + [[nodiscard]] ObjectType* get() { return mObject.get(); } |
| 58 | + [[nodiscard]] const ObjectType* get() const { return mObject.get(); } |
| 59 | + |
| 60 | + /** |
| 61 | + * Release ownership of the wrapped crypto object. This will cause memory leaks if ownership has not already been |
| 62 | + * taken by another object. |
| 63 | + */ |
| 64 | + void release() { mObject.release(); } |
| 65 | + |
| 66 | + /** |
| 67 | + * Determine if this CryptoObjectWrapper has a valid crypto object. |
| 68 | + * |
| 69 | + * @return \c TRUE if there exists a crypto object, otherwise \c FALSE. |
| 70 | + */ |
| 71 | + [[nodiscard]] explicit operator bool() const { return mObject != nullptr; } |
| 72 | + |
| 73 | +protected: |
| 74 | + CryptoObjectWrapper() = default; |
| 75 | + |
| 76 | + /** |
| 77 | + * Construct with values for the object, its custom deleter, and optionally a custom copier. |
| 78 | + * |
| 79 | + * @param object The crypto object to wrap. |
| 80 | + * @param deleter The deleter function for the crypto object. |
| 81 | + * @param copier The copier function for the crypto object. |
| 82 | + */ |
| 83 | + CryptoObjectWrapper(ObjectType* object, |
| 84 | + const std::function<void(ObjectType*)>& deleter, |
| 85 | + const CopierFunc& copier = CopierFunc()) |
| 86 | + : mObject({ object, deleter }) |
| 87 | + , mCopier(copier) |
| 88 | + { |
| 89 | + } |
| 90 | + |
| 91 | +private: |
| 92 | + /** |
| 93 | + * Pointer to the crypto object with its associated deleter. |
| 94 | + */ |
| 95 | + std::unique_ptr<ObjectType, std::function<void(ObjectType*)>> mObject = { nullptr, |
| 96 | + std::function<void(ObjectType*)>() }; |
| 97 | + |
| 98 | + /** |
| 99 | + * The copier function to use to copy the wrapped crypto object. |
| 100 | + */ |
| 101 | + CopierFunc mCopier; |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace Hiero::internal::OpenSSLUtils |
| 105 | + |
| 106 | +#endif // HIERO_SDK_CPP_IMPL_CRYPTO_OBJECT_WRAPPER_H_ |
0 commit comments