Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

145 строки
3.5 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. template <class T>
  28. static const T *vector_data(const std::vector<T> *out) {
  29. return out->empty() ? nullptr : &(*out)[0];
  30. }
  31. // remove_reference is a reimplementation of |std::remove_reference| from C++11.
  32. template <class T>
  33. struct remove_reference {
  34. using type = T;
  35. };
  36. template <class T>
  37. struct remove_reference<T&> {
  38. using type = T;
  39. };
  40. template <class T>
  41. struct remove_reference<T&&> {
  42. using type = T;
  43. };
  44. // move is a reimplementation of |std::move| from C++11.
  45. template <class T>
  46. typename remove_reference<T>::type &&move(T &&t) {
  47. return static_cast<typename remove_reference<T>::type&&>(t);
  48. }
  49. // default_delete is a partial reimplementation of |std::default_delete| from
  50. // C++11.
  51. template <class T>
  52. struct default_delete {
  53. void operator()(T *t) const {
  54. enum { type_must_be_complete = sizeof(T) };
  55. delete t;
  56. }
  57. };
  58. // nullptr_t is |std::nullptr_t| from C++11.
  59. using nullptr_t = decltype(nullptr);
  60. // unique_ptr is a partial reimplementation of |std::unique_ptr| from C++11. It
  61. // intentionally does not support stateful deleters to avoid having to bother
  62. // with the empty member optimization.
  63. template <class T, class Deleter = default_delete<T>>
  64. class unique_ptr {
  65. public:
  66. unique_ptr() : ptr_(nullptr) {}
  67. unique_ptr(nullptr_t) : ptr_(nullptr) {}
  68. unique_ptr(T *ptr) : ptr_(ptr) {}
  69. unique_ptr(const unique_ptr &u) = delete;
  70. unique_ptr(unique_ptr &&u) : ptr_(nullptr) {
  71. reset(u.release());
  72. }
  73. ~unique_ptr() {
  74. reset();
  75. }
  76. unique_ptr &operator=(nullptr_t) {
  77. reset();
  78. return *this;
  79. }
  80. unique_ptr &operator=(unique_ptr &&u) {
  81. reset(u.release());
  82. return *this;
  83. }
  84. unique_ptr& operator=(const unique_ptr &u) = delete;
  85. explicit operator bool() const {
  86. return ptr_ != nullptr;
  87. }
  88. T &operator*() const {
  89. assert(ptr_ != nullptr);
  90. return *ptr_;
  91. }
  92. T *operator->() const {
  93. assert(ptr_ != nullptr);
  94. return ptr_;
  95. }
  96. T *get() const {
  97. return ptr_;
  98. }
  99. T *release() {
  100. T *ptr = ptr_;
  101. ptr_ = nullptr;
  102. return ptr;
  103. }
  104. void reset(T *ptr = nullptr) {
  105. if (ptr_ != nullptr) {
  106. Deleter()(ptr_);
  107. }
  108. ptr_ = ptr;
  109. }
  110. private:
  111. T *ptr_;
  112. };
  113. } // namespace bssl
  114. #endif // OPENSSL_HEADER_CRYPTO_TEST_STL_COMPAT_H