OMNeT++ Simulation Library  6.0.3
any_ptr.h
1 //==========================================================================
2 // ANY_PTR.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_ANY_PTR_H
17 #define __OMNETPP_ANY_PTR_H
18 
19 #include <typeinfo>
20 #include <cassert>
21 #include "simkerneldefs.h"
22 #include "cexception.h"
23 #include "simutil.h"
24 
25 namespace omnetpp {
26 
61 class SIM_API any_ptr
62 {
63  private:
64  void *ptr;
65  const std::type_info *type; // ptrType
66  private:
67  void checkType(const std::type_info& asType) const;
68  public:
69  any_ptr() : any_ptr(nullptr) {}
70 
71  explicit any_ptr(std::nullptr_t) : ptr(nullptr), type(&typeid(std::nullptr_t)) {}
72 
73  template<typename T>
74  explicit any_ptr(T *ptr) : ptr(ptr), type(&typeid(T*)) {}
75 
76  template<typename T>
77  explicit any_ptr(const T *ptr) : any_ptr(const_cast<T*>(ptr)) {}
78 
79  any_ptr(void *ptr, const std::type_info& type) : ptr(ptr), type(&type) {}
80 
81  any_ptr(const any_ptr &other) : ptr(other.ptr), type(other.type) {}
82 
83  const any_ptr& operator=(const any_ptr& other) {ptr=other.ptr; type=other.type; return *this;}
84 
85  bool operator==(const any_ptr& other) const {return ptr==other.ptr && (ptr==nullptr || type==other.type);}
86  bool operator!=(const any_ptr& other) const {return !operator==(other);}
87 
88  bool operator==(std::nullptr_t) const {return ptr==nullptr;}
89  bool operator!=(std::nullptr_t) const {return ptr!=nullptr;}
90 
91  template<typename T>
92  bool contains() const {return typeid(T*) == *type;}
93 
94  void *raw() const {return ptr;}
95  const std::type_info& pointerType() const {return *type;}
96  const char *pointerTypeName() const {return opp_typename(*type);}
97  const char *typeName() const;
98 
99  template<typename T>
100  T *get() { checkType(typeid(T*)); return reinterpret_cast<T*>(ptr); }
101 
102  template<typename T>
103  const T *get() const { checkType(typeid(T*)); return reinterpret_cast<const T*>(ptr);}
104 
105  std::string str() const;
106 };
107 
108 class cObject;
109 
110 template<typename T>
111 T *fromAnyPtr(any_ptr object) = delete; // =delete prevents undeclared specializations
112 
113 template<>
114 inline omnetpp::cObject *fromAnyPtr(any_ptr ptr) { return ptr.get<omnetpp::cObject>(); }
115 
116 inline any_ptr toAnyPtr(const cObject *obj) { return any_ptr(const_cast<cObject*>(obj)); }
117 
118 } // namespace omnetpp
119 
120 #endif
121 
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::any_ptr
A type-safe equivalent of the void* pointer.
Definition: any_ptr.h:61
omnetpp::opp_typename
const SIM_API char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.