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 <assert.h>
52 #include <type_traits>
53 
54 #ifndef Q_MOC_RUN
55 #include <boost/mpl/eval_if.hpp>
56 #include <boost/mpl/identity.hpp>
57 #endif
58 
59 #include <factory/Object.h>
60 
61 namespace mira { namespace serialization {
62 
64 //@cond INTERNAL
65 // for internal use by void casts.
66 
67 struct VoidUpcastNormal {
68  template <typename T>
69  static void* invoke(T* pointer) {
70  typedef typename std::remove_const<T>::type Tc;
71  Tc* p = const_cast<Tc*>(pointer);
72  return static_cast<void*>(p);
73  }
74 };
75 
76 struct VoidDowncastNormal {
77  template <typename T>
78  static T* invoke(void* pointer) {
79  return static_cast<T*>(pointer);
80  }
81 };
82 
83 // For polymorphic types we cast via the base mira::Object type. This ensures
84 // safety even for multiple inheritance, since the address of the mira::Object
85 // part is "fixed" for all classes along the type hierarchy.
86 
87 struct VoidUpcastPolymorphic {
88  template <typename T>
89  static void* invoke(T* pointer) {
90  typedef typename std::remove_const<T>::type Tc;
91  Tc* p = const_cast<Tc*>(pointer);
92  mira::Object* obj = p;
93  return static_cast<void*>(obj);
94  }
95 };
96 
97 struct VoidDowncastPolymorphic {
98  template <typename T>
99  static T* invoke(void* pointer) {
100  mira::Object* obj = static_cast<mira::Object*>(pointer);
101  T* p = dynamic_cast<T*>(obj);
102  assert(p!=NULL); // this should never happen, since all polymorphic types
103  // MUST be derived from mira::Object
104  return p;
105  }
106 };
108 
110 
116 template <typename T>
117 void* void_upcast(T* pointer)
118 {
119  typedef typename
120  boost::mpl::eval_if<std::is_base_of<mira::Object, T>,
121  boost::mpl::identity< VoidUpcastPolymorphic >,
122  //else
123  boost::mpl::identity< VoidUpcastNormal >
124  >::type type;
125 
126  return type::template invoke<T>(pointer);
127 }
128 
134 template <typename T>
135 T* void_downcast(void* pointer)
136 {
137  typedef typename
138  boost::mpl::eval_if<std::is_base_of<mira::Object, T>,
139  boost::mpl::identity< VoidDowncastPolymorphic >,
140  //else
141  boost::mpl::identity< VoidDowncastNormal >
142  >::type type;
143  return type::template invoke<T>(pointer);
144 }
145 
147 
148 } } // namespace
149 
150 #endif
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
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:135
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:117