blob: 99cf2186cc75284aa5ca4717bc96d7ff4f729613 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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>
35#include <cstddef> // For NULL.
36
37namespace ceres {
38
39// Identity metafunction.
40template <class T>
41struct identity_ {
42 typedef T type;
43};
44
45// Use implicit_cast as a safe version of static_cast or const_cast
46// for implicit conversions. For example:
47// - Upcasting in a type hierarchy.
48// - Performing arithmetic conversions (int32 to int64, int to double, etc.).
49// - Adding const or volatile qualifiers.
50//
51// In general, implicit_cast can be used to convert this code
52// To to = from;
53// DoSomething(to);
54// to this
55// DoSomething(implicit_cast<To>(from));
56//
57// base::identity_ is used to make a non-deduced context, which
58// forces all callers to explicitly specify the template argument.
59template<typename To>
60inline To implicit_cast(typename identity_<To>::type to) {
61 return to;
62}
63
64// This version of implicit_cast is used when two template arguments
65// are specified. It's obsolete and should not be used.
66template<typename To, typename From>
67inline To implicit_cast(typename identity_<From>::type const &f) {
68 return f;
69}
70
71// When you upcast (that is, cast a pointer from type Foo to type
72// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
73// always succeed. When you downcast (that is, cast a pointer from
74// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
75// how do you know the pointer is really of type SubclassOfFoo? It
76// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
77// when you downcast, you should use this macro. In debug mode, we
78// use dynamic_cast<> to double-check the downcast is legal (we die
79// if it's not). In normal mode, we do the efficient static_cast<>
80// instead. Thus, it's important to test in debug mode to make sure
81// the cast is legal!
82// This is the only place in the code we should use dynamic_cast<>.
83// In particular, you SHOULDN'T be using dynamic_cast<> in order to
84// do RTTI (eg code like this:
85// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
86// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
87// You should design the code some other way not to need this.
88
89template<typename To, typename From> // use like this: down_cast<T*>(foo);
90inline To down_cast(From* f) { // so we only accept pointers
91 // 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) {
98 implicit_cast<From*, To>(NULL);
99 }
100
101 // uses RTTI in dbg and fastbuild. asserts are disabled in opt builds.
102 assert(f == NULL || dynamic_cast<To>(f) != NULL); // NOLINT
103 return static_cast<To>(f);
104}
105
106} // namespace ceres
107
108#endif // CERES_INTERNAL_CASTS_H_