You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

140 lines
3.3 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H
  15. #define OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H
  16. #include <assert.h>
  17. #include <vector>
  18. // This header contains re-implementations of library functions from C++11. They
  19. // will be replaced with their standard counterparts once Chromium has C++11
  20. // library support in its toolchain.
  21. namespace bssl {
  22. // vector_data is a reimplementation of |std::vector::data| from C++11.
  23. template <class T>
  24. static T *vector_data(std::vector<T> *out) {
  25. return out->empty() ? nullptr : &(*out)[0];
  26. }
  27. // remove_reference is a reimplementation of |std::remove_reference| from C++11.
  28. template <class T>
  29. struct remove_reference {
  30. using type = T;
  31. };
  32. template <class T>
  33. struct remove_reference<T&> {
  34. using type = T;
  35. };
  36. template <class T>
  37. struct remove_reference<T&&> {
  38. using type = T;
  39. };
  40. // move is a reimplementation of |std::move| from C++11.
  41. template <class T>
  42. typename remove_reference<T>::type &&move(T &&t) {
  43. return static_cast<typename remove_reference<T>::type&&>(t);
  44. }
  45. // default_delete is a partial reimplementation of |std::default_delete| from
  46. // C++11.
  47. template <class T>
  48. struct default_delete {
  49. void operator()(T *t) const {
  50. enum { type_must_be_complete = sizeof(T) };
  51. delete t;
  52. }
  53. };
  54. // nullptr_t is |std::nullptr_t| from C++11.
  55. using nullptr_t = decltype(nullptr);
  56. // unique_ptr is a partial reimplementation of |std::unique_ptr| from C++11. It
  57. // intentionally does not support stateful deleters to avoid having to bother
  58. // with the empty member optimization.
  59. template <class T, class Deleter = default_delete<T>>
  60. class unique_ptr {
  61. public:
  62. unique_ptr() : ptr_(nullptr) {}
  63. unique_ptr(nullptr_t) : ptr_(nullptr) {}
  64. unique_ptr(T *ptr) : ptr_(ptr) {}
  65. unique_ptr(const unique_ptr &u) = delete;
  66. unique_ptr(unique_ptr &&u) : ptr_(nullptr) {
  67. reset(u.release());
  68. }
  69. ~unique_ptr() {
  70. reset();
  71. }
  72. unique_ptr &operator=(nullptr_t) {
  73. reset();
  74. return *this;
  75. }
  76. unique_ptr &operator=(unique_ptr &&u) {
  77. reset(u.release());
  78. return *this;
  79. }
  80. unique_ptr& operator=(const unique_ptr &u) = delete;
  81. explicit operator bool() const {
  82. return ptr_ != nullptr;
  83. }
  84. T &operator*() const {
  85. assert(ptr_ != nullptr);
  86. return *ptr_;
  87. }
  88. T *operator->() const {
  89. assert(ptr_ != nullptr);
  90. return ptr_;
  91. }
  92. T *get() const {
  93. return ptr_;
  94. }
  95. T *release() {
  96. T *ptr = ptr_;
  97. ptr_ = nullptr;
  98. return ptr;
  99. }
  100. void reset(T *ptr = nullptr) {
  101. if (ptr_ != nullptr) {
  102. Deleter()(ptr_);
  103. }
  104. ptr_ = ptr;
  105. }
  106. private:
  107. T *ptr_;
  108. };
  109. } // namespace bssl
  110. #endif // OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H