OMNeT++ API 6.2.0
Discrete Event Simulation Library
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 
62 class SIM_API any_ptr
63 {
64  private:
65  void *ptr;
66  const std::type_info *type; // ptrType
67  private:
68  void checkType(const std::type_info& asType) const;
69  public:
70  any_ptr() : any_ptr(nullptr) {}
71 
72  explicit any_ptr(std::nullptr_t) : ptr(nullptr), type(&typeid(std::nullptr_t)) {}
73 
74  template<typename T>
75  explicit any_ptr(T *ptr) : ptr(ptr), type(&typeid(T*)) {}
76 
77  template<typename T>
78  explicit any_ptr(const T *ptr) : any_ptr(const_cast<T*>(ptr)) {}
79 
80  any_ptr(void *ptr, const std::type_info& type) : ptr(ptr), type(&type) {}
81 
82  any_ptr(const any_ptr &other) : ptr(other.ptr), type(other.type) {}
83 
84  const any_ptr& operator=(const any_ptr& other) {ptr=other.ptr; type=other.type; return *this;}
85 
86  bool operator==(const any_ptr& other) const {return ptr==other.ptr && (ptr==nullptr || type==other.type);}
87  bool operator!=(const any_ptr& other) const {return !operator==(other);}
88 
89  bool operator==(std::nullptr_t) const {return ptr==nullptr;}
90  bool operator!=(std::nullptr_t) const {return ptr!=nullptr;}
91 
92  template<typename T>
93  bool contains() const {return typeid(T*) == *type;}
94 
95  void *raw() const {return ptr;}
96  const std::type_info& pointerType() const {return *type;}
97  const char *pointerTypeName() const {return opp_typename(*type);}
98  const char *typeName() const;
99 
100  template<typename T>
101  T *get() { checkType(typeid(T*)); return reinterpret_cast<T*>(ptr); }
102 
103  template<typename T>
104  const T *get() const { checkType(typeid(T*)); return reinterpret_cast<const T*>(ptr);}
105 
106  std::string str() const;
107 };
108 
109 class cObject;
110 
111 template<typename T>
112 T *fromAnyPtr(any_ptr object) = delete; // =delete prevents undeclared specializations
113 
114 template<>
115 inline omnetpp::cObject *fromAnyPtr(any_ptr ptr) { return ptr.get<omnetpp::cObject>(); }
116 
117 inline any_ptr toAnyPtr(const cObject *obj) { return any_ptr(const_cast<cObject*>(obj)); }
118 
119 } // namespace omnetpp
120 
121 #endif
122 
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:62
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.