诸暨麻将添加redis
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

135 рядки
5.7 KiB

  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 <type_traits>
  33. #include <google/protobuf/stubs/common.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace internal {
  37. // Use implicit_cast as a safe version of static_cast or const_cast
  38. // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
  39. // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
  40. // a const pointer to Foo).
  41. // When you use implicit_cast, the compiler checks that the cast is safe.
  42. // Such explicit implicit_casts are necessary in surprisingly many
  43. // situations where C++ demands an exact type match instead of an
  44. // argument type convertable to a target type.
  45. //
  46. // The From type can be inferred, so the preferred syntax for using
  47. // implicit_cast is the same as for static_cast etc.:
  48. //
  49. // implicit_cast<ToType>(expr)
  50. //
  51. // implicit_cast would have been part of the C++ standard library,
  52. // but the proposal was submitted too late. It will probably make
  53. // its way into the language in the future.
  54. template<typename To, typename From>
  55. inline To implicit_cast(From const &f) {
  56. return f;
  57. }
  58. // When you upcast (that is, cast a pointer from type Foo to type
  59. // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
  60. // always succeed. When you downcast (that is, cast a pointer from
  61. // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
  62. // how do you know the pointer is really of type SubclassOfFoo? It
  63. // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
  64. // when you downcast, you should use this macro. In debug mode, we
  65. // use dynamic_cast<> to double-check the downcast is legal (we die
  66. // if it's not). In normal mode, we do the efficient static_cast<>
  67. // instead. Thus, it's important to test in debug mode to make sure
  68. // the cast is legal!
  69. // This is the only place in the code we should use dynamic_cast<>.
  70. // In particular, you SHOULDN'T be using dynamic_cast<> in order to
  71. // do RTTI (eg code like this:
  72. // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
  73. // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
  74. // You should design the code some other way not to need this.
  75. template<typename To, typename From> // use like this: down_cast<T*>(foo);
  76. inline To down_cast(From* f) { // so we only accept pointers
  77. // Ensures that To is a sub-type of From *. This test is here only
  78. // for compile-time type checking, and has no overhead in an
  79. // optimized build at run-time, as it will be optimized away
  80. // completely.
  81. if (false) {
  82. implicit_cast<From*, To>(0);
  83. }
  84. #if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
  85. assert(f == nullptr || dynamic_cast<To>(f) != nullptr); // RTTI: debug mode only!
  86. #endif
  87. return static_cast<To>(f);
  88. }
  89. template<typename To, typename From> // use like this: down_cast<T&>(foo);
  90. inline To down_cast(From& f) {
  91. typedef typename std::remove_reference<To>::type* ToAsPointer;
  92. // Ensures that To is a sub-type of From *. This test is here only
  93. // for compile-time type checking, and has no overhead in an
  94. // optimized build at run-time, as it will be optimized away
  95. // completely.
  96. if (false) {
  97. implicit_cast<From*, ToAsPointer>(0);
  98. }
  99. #if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
  100. // RTTI: debug mode only!
  101. assert(dynamic_cast<ToAsPointer>(&f) != nullptr);
  102. #endif
  103. return *static_cast<ToAsPointer>(&f);
  104. }
  105. template<typename To, typename From>
  106. inline To bit_cast(const From& from) {
  107. GOOGLE_COMPILE_ASSERT(sizeof(From) == sizeof(To),
  108. 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. #endif // GOOGLE_PROTOBUF_CASTS_H__