MIRA
TypedVoidPtr.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 
47 #ifndef _MIRA_TYPEDVOIDPTR_H_
48 #define _MIRA_TYPEDVOIDPTR_H_
49 
50 #include <error/Exceptions.h>
51 #include <factory/TypeId.h>
52 #include <platform/Typename.h>
53 
54 namespace mira {
55 
57 
102 {
103 
104 public:
105 
107  TypedVoidPtr() : mType(-1), mPtr(NULL) {}
108 
110  template <typename T>
111  TypedVoidPtr(T* ptr) :
112  mType(typeId<T>()), mPtr(ptr) {}
113 
114 public:
115 
117  bool isNull() const { return mPtr==NULL; }
118 
119 public:
120 
125  template <typename T>
126  operator T*() { return cast<T>(); }
127 
132  template <typename T>
133  operator const T*() const { return cast<T>(); }
134 
139  template <typename T>
140  T* cast() {
141  if(mType!=typeId<T>())
142  MIRA_THROW(XBadCast, "Cannot cast to type '" << typeName<T>() << "'");
143  // cast is safe due to our above type id check
144  return static_cast<T*>(mPtr);
145  }
146 
151  template <typename T>
152  const T* cast() const {
153  TypedVoidPtr* This = const_cast<TypedVoidPtr*>(this);
154  return This->cast<T>();
155  }
156 
157 private:
158 
160  TypeId mType;
161 
163  void* mPtr;
164 };
165 
167 
172 {
173 
174 public:
175 
177  TypedVoidConstPtr() : mType(-1), mPtr(NULL) {}
178 
180  template <typename T>
181  TypedVoidConstPtr(const T* ptr) :
182  mType(typeId<T>()), mPtr(ptr) {}
183 
184 public:
185 
187  bool isNull() const { return mPtr==NULL; }
188 
189 public:
190 
195  template <typename T>
196  operator const T*() const { return cast<T>(); }
197 
202  template <typename T>
203  const T* cast() const {
204  if(mType!=typeId<T>())
205  MIRA_THROW(XBadCast, "Cannot cast to type '" << typeName<T>() << "'");
206  // cast is safe due to our above type name check
207  return static_cast<const T*>(mPtr);
208  }
209 
210 private:
211 
213  TypeId mType;
214 
216  const void* mPtr;
217 };
218 
220 
221 }
222 
223 #endif
TypeId typeId()
Generates unique IDs for different types.
Definition: TypeId.h:94
TypedVoidConstPtr()
Creates nullptr.
Definition: TypedVoidPtr.h:177
TypedVoidPtr()
Creates nullptr.
Definition: TypedVoidPtr.h:107
TypedVoidPtr(T *ptr)
Creates a typed void pointer from the given pointer.
Definition: TypedVoidPtr.h:111
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
bool isNull() const
Return if underlying pointer is NULL.
Definition: TypedVoidPtr.h:117
Same as TypedVoidPtr but const.
Definition: TypedVoidPtr.h:171
Get compiler and platform independent typenames.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Class that allows to maintain type-safety when passing void pointers.
Definition: TypedVoidPtr.h:101
Commonly used exception classes.
T * cast()
Safely casts the object pointer that is stored to T*.
Definition: TypedVoidPtr.h:140
int TypeId
The type of the integral TypeId, that can be retrieved by typeId<T>()
Definition: TypeId.h:64
const T * cast() const
Safely casts the object pointer that is stored to const T*.
Definition: TypedVoidPtr.h:203
bool isNull() const
Return if underlying pointer is NULL.
Definition: TypedVoidPtr.h:187
TypedVoidConstPtr(const T *ptr)
Creates a typed void pointer from the given pointer.
Definition: TypedVoidPtr.h:181
Provides method for generating a unique id for any type.
const T * cast() const
Safely casts the object pointer that is stored to const T*.
Definition: TypedVoidPtr.h:152