blob: c7e39a29fbcf9446c96371b40d6504517a40ebf8 [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>
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070021#include <string.h>
Andreas Gampeeafdb962014-10-23 11:24:08 -070022#include <type_traits>
23
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070025
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070026namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070027
Carl Shapiro6d860c12011-07-20 16:40:16 -070028// Use implicit_cast as a safe version of static_cast or const_cast
29// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
30// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
31// a const pointer to Foo).
32// When you use implicit_cast, the compiler checks that the cast is safe.
33// Such explicit implicit_casts are necessary in surprisingly many
34// situations where C++ demands an exact type match instead of an
35// argument type convertable to a target type.
36//
37// The From type can be inferred, so the preferred syntax for using
38// implicit_cast is the same as for static_cast etc.:
39//
40// implicit_cast<ToType>(expr)
41//
42// implicit_cast would have been part of the C++ standard library,
43// but the proposal was submitted too late. It will probably make
44// its way into the language in the future.
45template<typename To, typename From>
46inline To implicit_cast(From const &f) {
47 return f;
48}
49
50// When you upcast (that is, cast a pointer from type Foo to type
51// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
52// always succeed. When you downcast (that is, cast a pointer from
53// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
54// how do you know the pointer is really of type SubclassOfFoo? It
55// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
56// when you downcast, you should use this macro. In debug mode, we
57// use dynamic_cast<> to double-check the downcast is legal (we die
58// if it's not). In normal mode, we do the efficient static_cast<>
59// instead. Thus, it's important to test in debug mode to make sure
60// the cast is legal!
61// This is the only place in the code we should use dynamic_cast<>.
62// In particular, you SHOULDN'T be using dynamic_cast<> in order to
63// do RTTI (eg code like this:
64// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
65// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
66// You should design the code some other way not to need this.
67
68template<typename To, typename From> // use like this: down_cast<T*>(foo);
69inline To down_cast(From* f) { // so we only accept pointers
Andreas Gampeeafdb962014-10-23 11:24:08 -070070 static_assert(std::is_base_of<From, typename std::remove_pointer<To>::type>::value,
71 "down_cast unsafe as To is not a subtype of From");
Carl Shapiro6d860c12011-07-20 16:40:16 -070072
Carl Shapiro6d860c12011-07-20 16:40:16 -070073 return static_cast<To>(f);
74}
75
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070076template <class Dest, class Source>
77inline Dest bit_cast(const Source& source) {
78 // Compile time assertion: sizeof(Dest) == sizeof(Source)
79 // A compile error here means your Dest and Source have different sizes.
Andreas Gampe575e78c2014-11-03 23:41:03 -080080 static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal");
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070081 Dest dest;
82 memcpy(&dest, &source, sizeof(dest));
83 return dest;
84}
85
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070086} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070087
Brian Carlstromfc0e3212013-07-17 14:40:12 -070088#endif // ART_RUNTIME_BASE_CASTS_H_