gem5 v24.0.0.0
Loading...
Searching...
No Matches
cast.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __BASE_CAST_HH__
30#define __BASE_CAST_HH__
31
32#include <cassert>
33#include <type_traits>
34#include "base/logging.hh"
35
36namespace gem5
37{
38
39// This is designed for situations where we have a pointer to a base
40// type, but in all cases when we cast it to a derived type, we know
41// by construction that it should work correctly.
42
43#if defined(GEM5_DEBUG)
44
45// In debug builds, do the dynamic cast and assert the result is good
46
47template <class T, class U>
48inline T
49safe_cast(U&& ref_or_ptr)
50{
51 /*
52 * srd::forward used in conjunction with forwarding references (template T
53 * + T&&) ensures that dynamic_cast will see the exact same type that was
54 * passed to safe_cast (a.k.a., perfect forwarding).
55 *
56 * Not using std::forward would make safe_cast compile with references to
57 * temporary objects and thus return a dangling reference.
58 */
59 T ret = dynamic_cast<T>(std::forward<U>(ref_or_ptr));
60 if constexpr (std::is_pointer_v<T>) {
61 gem5_assert(ret);
62 }
63 return ret;
64}
65
66#else
67
68// In non debug builds statically cast the result to the pointer we
69// want to use. This is technically unsafe, but this is only for
70// cases where we know that this should work by construction.
71
72template <class T, class U>
73inline T
74safe_cast(U&& ref_or_ptr)
75{
76 /*
77 * safe_cast should be reserved to polymorphic types while static_cast is
78 * also allowed for non-polymorphic types. It could make safe_cast able to
79 * compile in a non-debug build and fail in a debug build.
80 */
81 static_assert(std::is_polymorphic_v<
82 std::remove_pointer_t<
83 std::remove_reference_t<
84 U>>
85 >);
86 return static_cast<T>(std::forward<U>(ref_or_ptr));
87}
88
89#endif
90
91} // namespace gem5
92
93#endif // __BASE_CAST_HH__
#define gem5_assert(cond,...)
The assert macro will function like a normal assert, but will use panic instead of straight abort().
Definition logging.hh:317
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
T safe_cast(U &&ref_or_ptr)
Definition cast.hh:74

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0