ATLAS Offline Software
Loading...
Searching...
No Matches
DVLCast.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 Copyright (C) 2002-, 20262026 CERN for the benefit of the ATLAS collaboration
4*/
11
12
13#ifndef ATHCONTAINERS_TOOLS_DVLCAST_H
14#define ATHCONTAINERS_TOOLS_DVLCAST_H
15
16
17#include <limits>
18#include <limits.h>
19#include <typeinfo>
20#include <cstdlib>
21#include <cstddef>
22#include <print>
23
24
25namespace DataModel_detail {
26
27
47template <class DVL, bool has_virtual=DVL::has_virtual> struct DVLCast {};
48
49
53template <class DVL>
54struct DVLCast<DVL, false>
55{
56 typedef typename DVL::base_value_type T;
57
58
63 template <class U>
64 static T* cast (U* b)
65 {
66 return static_cast<T*> (b);
67 }
68};
69
70
74template <class DVL>
75struct DVLCast<DVL, true>
76{
77 typedef typename DVL::base_value_type T;
78
79
83 template <class U>
84 static int find_offset (U* b)
85 {
86 T* ret = dynamic_cast<T*> (b);
87 int offs = reinterpret_cast<char*>(ret) - reinterpret_cast<char*>(b);
88 return offs;
89 }
90
91
96 template <class U>
97 static T* cast (U* b)
98 {
99 // The common case will be for the dynamic type of b to be T.
100 // So test to avoid a dynamic_cast in that case.
101 // The extra test shouldn't add significant overhead
102 // to the case where we really need to run dynamic_cast.
103 if (!b)
104 return 0;
105 if (typeid(*b) == typeid(T)) {
106 static const ptrdiff_t offs = find_offset (b);
107 return reinterpret_cast<T*> (reinterpret_cast<char*>(b) + offs);
108 }
109 else
110 return dynamic_cast<T*> (b);
111 }
112};
113
114
115} // namespace DataModel_detail
116
117
118#endif // not ATHCONTAINERS_TOOLS_DVLCAST_H
static T * cast(U *b)
Cast b to a T*.
Definition DVLCast.h:64
static T * cast(U *b)
Cast b to a T*.
Definition DVLCast.h:97
static int find_offset(U *b)
Find the offset of T within U.
Definition DVLCast.h:84
casting operations for DataVector/DataList.
Definition DVLCast.h:47