|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: keir@google.com (Keir Mierle)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_INTERNAL_CASTS_H_
|
|
Packit |
ea1746 |
#define CERES_INTERNAL_CASTS_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <cassert>
|
|
Packit |
ea1746 |
#include <cstddef> // For NULL.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Identity metafunction.
|
|
Packit |
ea1746 |
template <class T>
|
|
Packit |
ea1746 |
struct identity_ {
|
|
Packit |
ea1746 |
typedef T type;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Use implicit_cast as a safe version of static_cast or const_cast
|
|
Packit |
ea1746 |
// for implicit conversions. For example:
|
|
Packit |
ea1746 |
// - Upcasting in a type hierarchy.
|
|
Packit |
ea1746 |
// - Performing arithmetic conversions (int32 to int64, int to double, etc.).
|
|
Packit |
ea1746 |
// - Adding const or volatile qualifiers.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// In general, implicit_cast can be used to convert this code
|
|
Packit |
ea1746 |
// To to = from;
|
|
Packit |
ea1746 |
// DoSomething(to);
|
|
Packit |
ea1746 |
// to this
|
|
Packit |
ea1746 |
// DoSomething(implicit_cast<To>(from));
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// base::identity_ is used to make a non-deduced context, which
|
|
Packit |
ea1746 |
// forces all callers to explicitly specify the template argument.
|
|
Packit |
ea1746 |
template<typename To>
|
|
Packit |
ea1746 |
inline To implicit_cast(typename identity_<To>::type to) {
|
|
Packit |
ea1746 |
return to;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This version of implicit_cast is used when two template arguments
|
|
Packit |
ea1746 |
// are specified. It's obsolete and should not be used.
|
|
Packit |
ea1746 |
template<typename To, typename From>
|
|
Packit |
ea1746 |
inline To implicit_cast(typename identity_<From>::type const &f) {
|
|
Packit |
ea1746 |
return f;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// When you upcast (that is, cast a pointer from type Foo to type
|
|
Packit |
ea1746 |
// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
|
|
Packit |
ea1746 |
// always succeed. When you downcast (that is, cast a pointer from
|
|
Packit |
ea1746 |
// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
|
|
Packit |
ea1746 |
// how do you know the pointer is really of type SubclassOfFoo? It
|
|
Packit |
ea1746 |
// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
|
|
Packit |
ea1746 |
// when you downcast, you should use this macro. In debug mode, we
|
|
Packit |
ea1746 |
// use dynamic_cast<> to double-check the downcast is legal (we die
|
|
Packit |
ea1746 |
// if it's not). In normal mode, we do the efficient static_cast<>
|
|
Packit |
ea1746 |
// instead. Thus, it's important to test in debug mode to make sure
|
|
Packit |
ea1746 |
// the cast is legal!
|
|
Packit |
ea1746 |
// This is the only place in the code we should use dynamic_cast<>.
|
|
Packit |
ea1746 |
// In particular, you SHOULDN'T be using dynamic_cast<> in order to
|
|
Packit |
ea1746 |
// do RTTI (eg code like this:
|
|
Packit |
ea1746 |
// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
|
|
Packit |
ea1746 |
// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
|
|
Packit |
ea1746 |
// You should design the code some other way not to need this.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
template<typename To, typename From> // use like this: down_cast<T*>(foo);
|
|
Packit |
ea1746 |
inline To down_cast(From* f) { // so we only accept pointers
|
|
Packit |
ea1746 |
// Ensures that To is a sub-type of From *. This test is here only
|
|
Packit |
ea1746 |
// for compile-time type checking, and has no overhead in an
|
|
Packit |
ea1746 |
// optimized build at run-time, as it will be optimized away
|
|
Packit |
ea1746 |
// completely.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// TODO(csilvers): This should use COMPILE_ASSERT.
|
|
Packit |
ea1746 |
if (false) {
|
|
Packit |
ea1746 |
implicit_cast<From*, To>(NULL);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// uses RTTI in dbg and fastbuild. asserts are disabled in opt builds.
|
|
Packit |
ea1746 |
assert(f == NULL || dynamic_cast<To>(f) != NULL); // NOLINT
|
|
Packit |
ea1746 |
return static_cast<To>(f);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_INTERNAL_CASTS_H_
|