casts.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_CASTS_H__
  31. #define GOOGLE_PROTOBUF_CASTS_H__
  32. #include <google/protobuf/stubs/common.h>
  33. #include <google/protobuf/port_def.inc>
  34. #include <type_traits>
  35. namespace google {
  36. namespace protobuf {
  37. namespace internal {
  38. // Use implicit_cast as a safe version of static_cast or const_cast
  39. // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
  40. // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
  41. // a const pointer to Foo).
  42. // When you use implicit_cast, the compiler checks that the cast is safe.
  43. // Such explicit implicit_casts are necessary in surprisingly many
  44. // situations where C++ demands an exact type match instead of an
  45. // argument type convertible to a target type.
  46. //
  47. // The From type can be inferred, so the preferred syntax for using
  48. // implicit_cast is the same as for static_cast etc.:
  49. //
  50. // implicit_cast<ToType>(expr)
  51. //
  52. // implicit_cast would have been part of the C++ standard library,
  53. // but the proposal was submitted too late. It will probably make
  54. // its way into the language in the future.
  55. template<typename To, typename From>
  56. inline To implicit_cast(From const &f) {
  57. return f;
  58. }
  59. // When you upcast (that is, cast a pointer from type Foo to type
  60. // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
  61. // always succeed. When you downcast (that is, cast a pointer from
  62. // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
  63. // how do you know the pointer is really of type SubclassOfFoo? It
  64. // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
  65. // when you downcast, you should use this macro. In debug mode, we
  66. // use dynamic_cast<> to double-check the downcast is legal (we die
  67. // if it's not). In normal mode, we do the efficient static_cast<>
  68. // instead. Thus, it's important to test in debug mode to make sure
  69. // the cast is legal!
  70. // This is the only place in the code we should use dynamic_cast<>.
  71. // In particular, you SHOULDN'T be using dynamic_cast<> in order to
  72. // do RTTI (eg code like this:
  73. // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
  74. // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
  75. // You should design the code some other way not to need this.
  76. template<typename To, typename From> // use like this: down_cast<T*>(foo);
  77. inline To down_cast(From* f) { // so we only accept pointers
  78. // Ensures that To is a sub-type of From *. This test is here only
  79. // for compile-time type checking, and has no overhead in an
  80. // optimized build at run-time, as it will be optimized away
  81. // completely.
  82. if (false) {
  83. implicit_cast<From*, To>(0);
  84. }
  85. #if !defined(NDEBUG) && PROTOBUF_RTTI
  86. assert(f == nullptr || dynamic_cast<To>(f) != nullptr); // RTTI: debug mode only!
  87. #endif
  88. return static_cast<To>(f);
  89. }
  90. template<typename To, typename From> // use like this: down_cast<T&>(foo);
  91. inline To down_cast(From& f) {
  92. typedef typename std::remove_reference<To>::type* ToAsPointer;
  93. // Ensures that To is a sub-type of From *. This test is here only
  94. // for compile-time type checking, and has no overhead in an
  95. // optimized build at run-time, as it will be optimized away
  96. // completely.
  97. if (false) {
  98. implicit_cast<From*, ToAsPointer>(0);
  99. }
  100. #if !defined(NDEBUG) && PROTOBUF_RTTI
  101. // RTTI: debug mode only!
  102. assert(dynamic_cast<ToAsPointer>(&f) != nullptr);
  103. #endif
  104. return *static_cast<ToAsPointer>(&f);
  105. }
  106. template<typename To, typename From>
  107. inline To bit_cast(const From& from) {
  108. static_assert(sizeof(From) == sizeof(To), "bit_cast_with_different_sizes");
  109. To dest;
  110. memcpy(&dest, &from, sizeof(dest));
  111. return dest;
  112. }
  113. } // namespace internal
  114. // We made these internal so that they would show up as such in the docs,
  115. // but we don't want to stick "internal::" in front of them everywhere.
  116. using internal::implicit_cast;
  117. using internal::down_cast;
  118. using internal::bit_cast;
  119. } // namespace protobuf
  120. } // namespace google
  121. #include <google/protobuf/port_undef.inc>
  122. #endif // GOOGLE_PROTOBUF_CASTS_H__