blob: af94452061896b9a954a3f1b4e03732eaf9161e3 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
31#ifndef CERES_INTERNAL_CASTS_H_
32#define CERES_INTERNAL_CASTS_H_
33
34#include <cassert>
Austin Schuh70cc9552019-01-21 19:46:48 -080035
36namespace ceres {
37
38// Identity metafunction.
39template <class T>
40struct identity_ {
Austin Schuh3de38b02024-06-25 18:25:10 -070041 using type = T;
Austin Schuh70cc9552019-01-21 19:46:48 -080042};
43
44// Use implicit_cast as a safe version of static_cast or const_cast
45// for implicit conversions. For example:
46// - Upcasting in a type hierarchy.
47// - Performing arithmetic conversions (int32 to int64, int to double, etc.).
48// - Adding const or volatile qualifiers.
49//
50// In general, implicit_cast can be used to convert this code
51// To to = from;
52// DoSomething(to);
53// to this
54// DoSomething(implicit_cast<To>(from));
55//
56// base::identity_ is used to make a non-deduced context, which
57// forces all callers to explicitly specify the template argument.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080058template <typename To>
Austin Schuh70cc9552019-01-21 19:46:48 -080059inline To implicit_cast(typename identity_<To>::type to) {
60 return to;
61}
62
63// This version of implicit_cast is used when two template arguments
64// are specified. It's obsolete and should not be used.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080065template <typename To, typename From>
66inline To implicit_cast(typename identity_<From>::type const& f) {
Austin Schuh70cc9552019-01-21 19:46:48 -080067 return f;
68}
69
70// When you upcast (that is, cast a pointer from type Foo to type
71// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
72// always succeed. When you downcast (that is, cast a pointer from
73// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
74// how do you know the pointer is really of type SubclassOfFoo? It
75// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
76// when you downcast, you should use this macro. In debug mode, we
77// use dynamic_cast<> to double-check the downcast is legal (we die
78// if it's not). In normal mode, we do the efficient static_cast<>
79// instead. Thus, it's important to test in debug mode to make sure
80// the cast is legal!
81// This is the only place in the code we should use dynamic_cast<>.
82// In particular, you SHOULDN'T be using dynamic_cast<> in order to
83// do RTTI (eg code like this:
84// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
85// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
86// You should design the code some other way not to need this.
87
Austin Schuh3de38b02024-06-25 18:25:10 -070088// TODO(sameeragarwal): Modernize this.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080089template <typename To, typename From> // use like this: down_cast<T*>(foo);
90inline To down_cast(From* f) { // so we only accept pointers
Austin Schuh70cc9552019-01-21 19:46:48 -080091 // Ensures that To is a sub-type of From *. This test is here only
92 // for compile-time type checking, and has no overhead in an
93 // optimized build at run-time, as it will be optimized away
94 // completely.
95
96 // TODO(csilvers): This should use COMPILE_ASSERT.
97 if (false) {
Austin Schuh3de38b02024-06-25 18:25:10 -070098 implicit_cast<From*, To>(nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -080099 }
100
101 // uses RTTI in dbg and fastbuild. asserts are disabled in opt builds.
Austin Schuh3de38b02024-06-25 18:25:10 -0700102 assert(f == nullptr || dynamic_cast<To>(f) != nullptr); // NOLINT
Austin Schuh70cc9552019-01-21 19:46:48 -0800103 return static_cast<To>(f);
104}
105
106} // namespace ceres
107
108#endif // CERES_INTERNAL_CASTS_H_