gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
36 namespace 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 
47 template <class T, class U>
48 inline T
49 safe_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 
72 template <class T, class U>
73 inline T
74 safe_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__
gem5::safe_cast
T safe_cast(U &&ref_or_ptr)
Definition: cast.hh:74
logging.hh
gem5_assert
#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
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37

Generated on Sun Jul 30 2023 01:56:50 for gem5 by doxygen 1.8.17