blob: f8846498df7e4c03f0197979c0ca6233eb4d8fb4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_CASTS_H_
18#define ART_RUNTIME_BASE_CASTS_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
Carl Shapiro6d860c12011-07-20 16:40:16 -070020#include <assert.h>
Andreas Gampee34a42c2015-04-25 14:44:29 -070021#include <limits>
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070022#include <string.h>
Andreas Gampeeafdb962014-10-23 11:24:08 -070023#include <type_traits>
24
Andreas Gampee34a42c2015-04-25 14:44:29 -070025#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070027
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070028namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070029
Carl Shapiro6d860c12011-07-20 16:40:16 -070030// Use implicit_cast as a safe version of static_cast or const_cast
31// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
32// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
33// a const pointer to Foo).
34// When you use implicit_cast, the compiler checks that the cast is safe.
35// Such explicit implicit_casts are necessary in surprisingly many
36// situations where C++ demands an exact type match instead of an
37// argument type convertable to a target type.
38//
39// The From type can be inferred, so the preferred syntax for using
40// implicit_cast is the same as for static_cast etc.:
41//
42// implicit_cast<ToType>(expr)
43//
44// implicit_cast would have been part of the C++ standard library,
45// but the proposal was submitted too late. It will probably make
46// its way into the language in the future.
47template<typename To, typename From>
48inline To implicit_cast(From const &f) {
49 return f;
50}
51
52// When you upcast (that is, cast a pointer from type Foo to type
53// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
54// always succeed. When you downcast (that is, cast a pointer from
55// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
56// how do you know the pointer is really of type SubclassOfFoo? It
57// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
58// when you downcast, you should use this macro. In debug mode, we
59// use dynamic_cast<> to double-check the downcast is legal (we die
60// if it's not). In normal mode, we do the efficient static_cast<>
61// instead. Thus, it's important to test in debug mode to make sure
62// the cast is legal!
63// This is the only place in the code we should use dynamic_cast<>.
64// In particular, you SHOULDN'T be using dynamic_cast<> in order to
65// do RTTI (eg code like this:
66// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
67// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
68// You should design the code some other way not to need this.
69
70template<typename To, typename From> // use like this: down_cast<T*>(foo);
71inline To down_cast(From* f) { // so we only accept pointers
Andreas Gampeeafdb962014-10-23 11:24:08 -070072 static_assert(std::is_base_of<From, typename std::remove_pointer<To>::type>::value,
73 "down_cast unsafe as To is not a subtype of From");
Carl Shapiro6d860c12011-07-20 16:40:16 -070074
Carl Shapiro6d860c12011-07-20 16:40:16 -070075 return static_cast<To>(f);
76}
77
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070078template <class Dest, class Source>
79inline Dest bit_cast(const Source& source) {
80 // Compile time assertion: sizeof(Dest) == sizeof(Source)
81 // A compile error here means your Dest and Source have different sizes.
Andreas Gampe575e78c2014-11-03 23:41:03 -080082 static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal");
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070083 Dest dest;
84 memcpy(&dest, &source, sizeof(dest));
85 return dest;
86}
87
Andreas Gampee34a42c2015-04-25 14:44:29 -070088// A version of static_cast that DCHECKs that the value can be precisely represented
89// when converting to Dest.
90template <typename Dest, typename Source>
91inline Dest dchecked_integral_cast(const Source source) {
92 DCHECK(
93 // Check that the value is within the lower limit of Dest.
94 (static_cast<intmax_t>(std::numeric_limits<Dest>::min()) <=
95 static_cast<intmax_t>(std::numeric_limits<Source>::min()) ||
96 source >= static_cast<Source>(std::numeric_limits<Dest>::min())) &&
97 // Check that the value is within the upper limit of Dest.
98 (static_cast<uintmax_t>(std::numeric_limits<Dest>::max()) >=
99 static_cast<uintmax_t>(std::numeric_limits<Source>::max()) ||
100 source <= static_cast<Source>(std::numeric_limits<Dest>::max())));
101
102 return static_cast<Dest>(source);
103}
104
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700105} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700106
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700107#endif // ART_RUNTIME_BASE_CASTS_H_