diff options
author | David Benjamin <davidben@chromium.org> | 2015-03-22 23:31:27 +0300 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2015-04-01 02:03:06 +0300 |
commit | 45fb1be33e6194aa8c7c414416b6cc657df660f4 (patch) | |
tree | 123fb9e3b596007259be2eef0febc18b9182c2ec /crypto/test | |
parent | d7c5dfb233bfc0e5666efd343329fa94a3668818 (diff) |
Remove std::unique_ptr dependency on bssl_shim's scoped types.
This is in preparation for using RAII in the unit tests. Those tests are built
in Chromium as well, but Chromium does not have C++11 library support across
all its toolchains. Compiler support is available, so add a partial
reimplementation of std::unique_ptr and std::move under crypto/test/. The
scopers for the crypto/ library are also moved there while the ones for ssl/
stay in ssl/test/.
Change-Id: I38f769acbc16a870db34649928575c7314b6e9f6
Reviewed-on: https://boringssl-review.googlesource.com/4120
Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/scoped_types.h | 40 | ||||
-rw-r--r-- | crypto/test/stl_compat.h | 131 |
2 files changed, 171 insertions, 0 deletions
diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h new file mode 100644 index 00000000..c9894b6b --- /dev/null +++ b/crypto/test/scoped_types.h @@ -0,0 +1,40 @@ +/* Copyright (c) 2015, Google Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#ifndef OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H +#define OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H + +#include <openssl/bio.h> +#include <openssl/dh.h> +#include <openssl/evp.h> + +#include "stl_compat.h" + + +template<typename T, void (*func)(T*)> +struct OpenSSLDeleter { + void operator()(T *obj) { + func(obj); + } +}; + +template<typename T, void (*func)(T*)> +using ScopedOpenSSLType = bssl::unique_ptr<T, OpenSSLDeleter<T, func>>; + +using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>; +using ScopedDH = ScopedOpenSSLType<DH, DH_free>; +using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>; + + +#endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H diff --git a/crypto/test/stl_compat.h b/crypto/test/stl_compat.h new file mode 100644 index 00000000..39ad86fe --- /dev/null +++ b/crypto/test/stl_compat.h @@ -0,0 +1,131 @@ +/* Copyright (c) 2015, Google Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#ifndef OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H +#define OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H + +#include <assert.h> + + +// This header contains re-implementations of library functions from C++11. They +// will be replaced with their standard counterparts once Chromium has C++11 +// library support in its toolchain. + +namespace bssl { + +// remove_reference is a reimplementation of |std::remove_reference| from C++11. +template <class T> +struct remove_reference { + using type = T; +}; + +template <class T> +struct remove_reference<T&> { + using type = T; +}; + +template <class T> +struct remove_reference<T&&> { + using type = T; +}; + +// move is a reimplementation of |std::move| from C++11. +template <class T> +typename remove_reference<T>::type &&move(T &&t) { + return static_cast<typename remove_reference<T>::type&&>(t); +} + +// default_delete is a partial reimplementation of |std::default_delete| from +// C++11. +template <class T> +struct default_delete { + void operator()(T *t) const { + enum { type_must_be_complete = sizeof(T) }; + delete t; + } +}; + +// nullptr_t is |std::nullptr_t| from C++11. +using nullptr_t = decltype(nullptr); + +// unique_ptr is a partial reimplementation of |std::unique_ptr| from C++11. It +// intentionally does not support stateful deleters to avoid having to bother +// with the empty member optimization. +template <class T, class Deleter = default_delete<T>> +class unique_ptr { + public: + unique_ptr() : ptr_(nullptr) {} + unique_ptr(nullptr_t) : ptr_(nullptr) {} + unique_ptr(T *ptr) : ptr_(ptr) {} + unique_ptr(const unique_ptr &u) = delete; + + unique_ptr(unique_ptr &&u) : ptr_(nullptr) { + reset(u.release()); + } + + ~unique_ptr() { + reset(); + } + + unique_ptr &operator=(nullptr_t) { + reset(); + return *this; + } + + unique_ptr &operator=(unique_ptr &&u) { + reset(u.release()); + return *this; + } + + unique_ptr& operator=(const unique_ptr &u) = delete; + + explicit operator bool() const { + return ptr_ != nullptr; + } + + T &operator*() const { + assert(ptr_ != nullptr); + return *ptr_; + } + + T *operator->() const { + assert(ptr_ != nullptr); + return ptr_; + } + + T *get() const { + return ptr_; + } + + T *release() { + T *ptr = ptr_; + ptr_ = nullptr; + return ptr; + } + + void reset(T *ptr = nullptr) { + if (ptr_ != nullptr) { + Deleter()(ptr_); + } + ptr_ = ptr; + } + + private: + T *ptr_; +}; + +} // namespace bssl + + +#endif // OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H |