MIRA
VoidCast.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_VOIDCAST_H_
49 #define _MIRA_VOIDCAST_H_
50 
51 #include <type_traits>
52 
53 #include <factory/Object.h>
54 
55 namespace mira::serialization {
56 
58 //@cond INTERNAL
59 // for internal use by void casts.
60 
61 template<typename T>
62 void* voidUpcastNormal(T* pointer)
63 {
64  typedef typename std::remove_const<T>::type Tc;
65  Tc* p = const_cast<Tc*>(pointer);
66  return static_cast<void*>(p);
67 }
68 
69 template<typename T>
70 T* voidDowncastNormal(void* pointer)
71 {
72  return static_cast<T*>(pointer);
73 }
74 
75 // For polymorphic types we cast via the base mira::Object type. This ensures
76 // safety even for multiple inheritance, since the address of the mira::Object
77 // part is "fixed" for all classes along the type hierarchy.
78 
79 template<typename T>
80 void* voidUpcastPolymorphic(T* pointer)
81 {
82  typedef typename std::remove_const<T>::type Tc;
83  Tc* p = const_cast<Tc*>(pointer);
84  mira::Object* obj = p;
85  return static_cast<void*>(obj);
86 }
87 
88 template<typename T>
89 T* voidDowncastPolymorphic(void* pointer)
90 {
91  auto* obj = static_cast<mira::Object*>(pointer);
92  T* p = dynamic_cast<T*>(obj);
93  assert(p != NULL); // this should never happen, since all polymorphic types
94  // MUST be derived from mira::Object
95  return p;
96 }
97 
99 
101 
107 template<typename T>
108 void* void_upcast(T* pointer)
109 {
110  if constexpr (std::is_base_of_v<mira::Object, T>) {
111  return voidUpcastPolymorphic(pointer);
112  }
113  else {
114  return voidUpcastNormal(pointer);
115  }
116 }
117 
123 template<typename T>
124 T* void_downcast(void* pointer)
125 {
126  if constexpr (std::is_base_of_v<mira::Object, T>) {
127  return voidDowncastPolymorphic<T>(pointer);
128  }
129  else {
130  return voidDowncastNormal<T>(pointer);
131  }
132 }
133 
135 
136 } // namespace mira::serialization
137 
138 #endif
Definition: StlCollections.h:61
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
T * void_downcast(void *pointer)
Safe cast for casting from a void pointer to a derived pointer T* while taking care of polymorphism a...
Definition: VoidCast.h:124
The object class acts as a generic base class for classes which should be used with the classFactory...
Definition: Object.h:144
$Defines object class as base class for classFactory compatible classes$.
void * void_upcast(T *pointer)
Safe cast for casting from a pointer upwards to void* while taking care of polymorphism and multiple ...
Definition: VoidCast.h:108