casts.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. #ifndef CERES_INTERNAL_CASTS_H_
  31. #define CERES_INTERNAL_CASTS_H_
  32. #include <cassert>
  33. namespace ceres {
  34. // Identity metafunction.
  35. template <class T>
  36. struct identity_ {
  37. using type = T;
  38. };
  39. // Use implicit_cast as a safe version of static_cast or const_cast
  40. // for implicit conversions. For example:
  41. // - Upcasting in a type hierarchy.
  42. // - Performing arithmetic conversions (int32 to int64, int to double, etc.).
  43. // - Adding const or volatile qualifiers.
  44. //
  45. // In general, implicit_cast can be used to convert this code
  46. // To to = from;
  47. // DoSomething(to);
  48. // to this
  49. // DoSomething(implicit_cast<To>(from));
  50. //
  51. // base::identity_ is used to make a non-deduced context, which
  52. // forces all callers to explicitly specify the template argument.
  53. template <typename To>
  54. inline To implicit_cast(typename identity_<To>::type to) {
  55. return to;
  56. }
  57. // This version of implicit_cast is used when two template arguments
  58. // are specified. It's obsolete and should not be used.
  59. template <typename To, typename From>
  60. inline To implicit_cast(typename identity_<From>::type const& f) {
  61. return f;
  62. }
  63. // When you upcast (that is, cast a pointer from type Foo to type
  64. // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
  65. // always succeed. When you downcast (that is, cast a pointer from
  66. // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
  67. // how do you know the pointer is really of type SubclassOfFoo? It
  68. // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
  69. // when you downcast, you should use this macro. In debug mode, we
  70. // use dynamic_cast<> to double-check the downcast is legal (we die
  71. // if it's not). In normal mode, we do the efficient static_cast<>
  72. // instead. Thus, it's important to test in debug mode to make sure
  73. // the cast is legal!
  74. // This is the only place in the code we should use dynamic_cast<>.
  75. // In particular, you SHOULDN'T be using dynamic_cast<> in order to
  76. // do RTTI (eg code like this:
  77. // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
  78. // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
  79. // You should design the code some other way not to need this.
  80. // TODO(sameeragarwal): Modernize this.
  81. template <typename To, typename From> // use like this: down_cast<T*>(foo);
  82. inline To down_cast(From* f) { // so we only accept pointers
  83. // Ensures that To is a sub-type of From *. This test is here only
  84. // for compile-time type checking, and has no overhead in an
  85. // optimized build at run-time, as it will be optimized away
  86. // completely.
  87. // TODO(csilvers): This should use COMPILE_ASSERT.
  88. if (false) {
  89. implicit_cast<From*, To>(nullptr);
  90. }
  91. // uses RTTI in dbg and fastbuild. asserts are disabled in opt builds.
  92. assert(f == nullptr || dynamic_cast<To>(f) != nullptr); // NOLINT
  93. return static_cast<To>(f);
  94. }
  95. } // namespace ceres
  96. #endif // CERES_INTERNAL_CASTS_H_