From 7b461d62b8669b4f564afeac4f68b7c8f7780220 Mon Sep 17 00:00:00 2001 From: Nicolas Gallego-Ortiz Date: Tue, 13 Aug 2019 17:35:06 +0200 Subject: [PATCH 1/6] data member added to handle type name as a string --- src/OpenMesh/Core/Utils/BaseProperty.hh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/OpenMesh/Core/Utils/BaseProperty.hh b/src/OpenMesh/Core/Utils/BaseProperty.hh index 9991b3ba..1e63d6d3 100644 --- a/src/OpenMesh/Core/Utils/BaseProperty.hh +++ b/src/OpenMesh/Core/Utils/BaseProperty.hh @@ -80,13 +80,13 @@ public: /// /// \param _name Optional textual name for the property. /// - BaseProperty(const std::string& _name = "") - : name_(_name), persistent_(false) + BaseProperty(const std::string& _name = "", const std::string& _internal_type_name = "" ) + : name_(_name), internal_type_name_(_internal_type_name), persistent_(false) {} /// \brief Copy constructor BaseProperty(const BaseProperty & _rhs) - : name_( _rhs.name_ ), persistent_( _rhs.persistent_ ) {} + : name_( _rhs.name_ ), internal_type_name_(_rhs.internal_type_name_), persistent_( _rhs.persistent_ ) {} /// Destructor. virtual ~BaseProperty() {} @@ -119,6 +119,9 @@ public: // named property interface /// Return the name of the property const std::string& name() const { return name_; } + /// Return internal type name of the property for type safe casting alternative to runtime information + const std::string& internal_type_name() const { return internal_type_name_; } + virtual void stats(std::ostream& _ostr) const; public: // I/O support @@ -173,6 +176,7 @@ protected: private: std::string name_; + std::string internal_type_name_; bool persistent_; }; From 1f0f036e779d997b3c62b7d40ef3b02256af00b5 Mon Sep 17 00:00:00 2001 From: Nicolas Gallego-Ortiz Date: Tue, 13 Aug 2019 18:24:26 +0200 Subject: [PATCH 2/6] accesors to internal typename from derived class --- src/OpenMesh/Core/Utils/Property.hh | 50 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/OpenMesh/Core/Utils/Property.hh b/src/OpenMesh/Core/Utils/Property.hh index 629225c6..c6507b09 100644 --- a/src/OpenMesh/Core/Utils/Property.hh +++ b/src/OpenMesh/Core/Utils/Property.hh @@ -99,8 +99,10 @@ public: public: /// Default constructor - explicit PropertyT(const std::string& _name = "") - : BaseProperty(_name) + explicit PropertyT( + const std::string& _name = "", + const std::string& _internal_type_name = "") + : BaseProperty(_name, _internal_type_name) {} /// Copy constructor @@ -173,17 +175,17 @@ public: // data access interface return &data_[0]; } - - /// Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) - vector_type& data_vector() { - return data_; - } - - /// Const access to property vector - const vector_type& data_vector() const { - return data_; - } - + + /// Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) + vector_type& data_vector() { + return data_; + } + + /// Const access to property vector + const vector_type& data_vector() const { + return data_; + } + /// Access the i'th element. No range check is performed! reference operator[](int _idx) { @@ -337,17 +339,17 @@ public: public: - - /// Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) - vector_type& data_vector() { - return data_; - } - - /// Const access to property vector - const vector_type& data_vector() const { - return data_; - } - + + /// Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) + vector_type& data_vector() { + return data_; + } + + /// Const access to property vector + const vector_type& data_vector() const { + return data_; + } + /// Access the i'th element. No range check is performed! reference operator[](int _idx) { From 3e83f7311b9c20d3e6385c3d277ff7c256ba617e Mon Sep 17 00:00:00 2001 From: Nicolas Gallego-Ortiz Date: Wed, 14 Aug 2019 11:25:36 +0200 Subject: [PATCH 3/6] internal name mechanism added at property creation --- .gitignore | 2 ++ src/OpenMesh/Core/Utils/Property.hh | 4 +-- src/OpenMesh/Core/Utils/PropertyContainer.hh | 3 +- src/OpenMesh/Core/Utils/typename.hh | 29 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/OpenMesh/Core/Utils/typename.hh diff --git a/.gitignore b/.gitignore index 2ae4ed78..de469da2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ CMakeLists.txt.user build* *.swp .settings +# ignore mac temporal files +.DS_Store diff --git a/src/OpenMesh/Core/Utils/Property.hh b/src/OpenMesh/Core/Utils/Property.hh index c6507b09..d896e1b8 100644 --- a/src/OpenMesh/Core/Utils/Property.hh +++ b/src/OpenMesh/Core/Utils/Property.hh @@ -232,8 +232,8 @@ public: public: - explicit PropertyT(const std::string& _name = "") - : BaseProperty(_name) + explicit PropertyT(const std::string& _name = "", const std::string& _internal_type_name="" ) + : BaseProperty(_name, _internal_type_name) { } public: // inherited from BaseProperty diff --git a/src/OpenMesh/Core/Utils/PropertyContainer.hh b/src/OpenMesh/Core/Utils/PropertyContainer.hh index dcc9a3ac..6f1835e4 100644 --- a/src/OpenMesh/Core/Utils/PropertyContainer.hh +++ b/src/OpenMesh/Core/Utils/PropertyContainer.hh @@ -50,6 +50,7 @@ #endif #include +#include //----------------------------------------------------------------------------- namespace OpenMesh @@ -104,7 +105,7 @@ public: int idx=0; for ( ; p_it!=p_end && *p_it!=nullptr; ++p_it, ++idx ) {}; if (p_it==p_end) properties_.push_back(nullptr); - properties_[idx] = new PropertyT(_name); + properties_[idx] = new PropertyT(_name, get_type_name() ); // create a new property with requested name and given (system dependent) internal typename return BasePropHandleT(idx); } diff --git a/src/OpenMesh/Core/Utils/typename.hh b/src/OpenMesh/Core/Utils/typename.hh new file mode 100644 index 00000000..3dd4ba30 --- /dev/null +++ b/src/OpenMesh/Core/Utils/typename.hh @@ -0,0 +1,29 @@ +#ifndef TYPENAME_HH +#define TYPENAME_HH + +/// Get an internal name for a type +/// Important, this is depends on compilers and versions, do NOT se in file formats! +/// This provides property type safty when only limited RTTI is available +/// Solution adapted from OpenVolumeMesh + +#include +#include + +namespace OpenMesh { + +template +std::string get_type_name() +{ +#ifdef _MSC_VER + // MSVC'S type_name returns only a friendly name with name() method, + // to get a unique name use raw_name() method instead + return typeid(T).raw_name(); +#else + // GCC and clang curently return mangled name as name(), there is no raw_name() method + return typeid(T).name(); +#endif +} + +} + +#endif // TYPENAME_HH From 6bb9f668b8328ed58277c70a5a47113322bfed15 Mon Sep 17 00:00:00 2001 From: Nicolas Gallego-Ortiz Date: Wed, 14 Aug 2019 15:20:50 +0200 Subject: [PATCH 4/6] name mechanism used for property type checking at retreival, OM_FORCE_STATIC_CAST removed --- src/OpenMesh/Core/Utils/PropertyContainer.hh | 24 ++++---------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/OpenMesh/Core/Utils/PropertyContainer.hh b/src/OpenMesh/Core/Utils/PropertyContainer.hh index 6f1835e4..6d9506c0 100644 --- a/src/OpenMesh/Core/Utils/PropertyContainer.hh +++ b/src/OpenMesh/Core/Utils/PropertyContainer.hh @@ -44,11 +44,6 @@ #ifndef OPENMESH_PROPERTYCONTAINER #define OPENMESH_PROPERTYCONTAINER -// Use static casts when not debugging -#ifdef NDEBUG -#define OM_FORCE_STATIC_CAST -#endif - #include #include @@ -118,10 +113,7 @@ public: { if (*p_it != nullptr && (*p_it)->name() == _name //skip deleted properties -// Skip type check -#ifndef OM_FORCE_STATIC_CAST - && dynamic_cast*>(properties_[idx]) != nullptr //check type -#endif + && (*p_it)->internal_type_name() == get_type_name() // new check type ) { return BasePropHandleT(idx); @@ -147,13 +139,10 @@ public: { assert(_h.idx() >= 0 && _h.idx() < (int)properties_.size()); assert(properties_[_h.idx()] != nullptr); -#ifdef OM_FORCE_STATIC_CAST - return *static_cast *> (properties_[_h.idx()]); -#else - PropertyT* p = dynamic_cast*>(properties_[_h.idx()]); + assert( properties_[_h.idx()]->internal_type_name() == get_type_name() ); + PropertyT *p = static_cast< PropertyT* > (properties_[_h.idx()]); assert(p != nullptr); return *p; -#endif } @@ -161,13 +150,10 @@ public: { assert(_h.idx() >= 0 && _h.idx() < (int)properties_.size()); assert(properties_[_h.idx()] != nullptr); -#ifdef OM_FORCE_STATIC_CAST - return *static_cast*>(properties_[_h.idx()]); -#else - PropertyT* p = dynamic_cast*>(properties_[_h.idx()]); + assert( properties_[_h.idx()]->internal_type_name() == get_type_name() ); + PropertyT *p = static_cast< PropertyT* > (properties_[_h.idx()]); assert(p != nullptr); return *p; -#endif } From b85b66d679172a2edcfcf5af383350dc7ce01ad6 Mon Sep 17 00:00:00 2001 From: Nicolas Gallego-Ortiz Date: Thu, 15 Aug 2019 10:47:37 +0200 Subject: [PATCH 5/6] lowercase filename, pragma once and property specializations corrected --- src/OpenMesh/Core/Utils/Property.hh | 4 ++-- src/OpenMesh/Core/Utils/PropertyContainer.hh | 2 +- src/OpenMesh/Core/Utils/typename.hh | 5 +---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/OpenMesh/Core/Utils/Property.hh b/src/OpenMesh/Core/Utils/Property.hh index d896e1b8..7f913ec1 100644 --- a/src/OpenMesh/Core/Utils/Property.hh +++ b/src/OpenMesh/Core/Utils/Property.hh @@ -396,8 +396,8 @@ public: public: - explicit PropertyT(const std::string& _name = "") - : BaseProperty(_name) + explicit PropertyT(const std::string& _name = "", const std::string& _internal_type_name="" ) + : BaseProperty(_name, _internal_type_name) { } public: // inherited from BaseProperty diff --git a/src/OpenMesh/Core/Utils/PropertyContainer.hh b/src/OpenMesh/Core/Utils/PropertyContainer.hh index 6d9506c0..297d16b9 100644 --- a/src/OpenMesh/Core/Utils/PropertyContainer.hh +++ b/src/OpenMesh/Core/Utils/PropertyContainer.hh @@ -45,7 +45,7 @@ #define OPENMESH_PROPERTYCONTAINER #include -#include +#include //----------------------------------------------------------------------------- namespace OpenMesh diff --git a/src/OpenMesh/Core/Utils/typename.hh b/src/OpenMesh/Core/Utils/typename.hh index 3dd4ba30..8c6ba8bc 100644 --- a/src/OpenMesh/Core/Utils/typename.hh +++ b/src/OpenMesh/Core/Utils/typename.hh @@ -1,5 +1,4 @@ -#ifndef TYPENAME_HH -#define TYPENAME_HH +#pragma once /// Get an internal name for a type /// Important, this is depends on compilers and versions, do NOT se in file formats! @@ -25,5 +24,3 @@ std::string get_type_name() } } - -#endif // TYPENAME_HH From 83c7408ff21c757d8b9a6a0ff24e3e4b06f2fe2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=B6bius?= Date: Mon, 26 Aug 2019 16:27:13 +0200 Subject: [PATCH 6/6] Update typename.hh --- src/OpenMesh/Core/Utils/typename.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenMesh/Core/Utils/typename.hh b/src/OpenMesh/Core/Utils/typename.hh index 8c6ba8bc..f67ae18b 100644 --- a/src/OpenMesh/Core/Utils/typename.hh +++ b/src/OpenMesh/Core/Utils/typename.hh @@ -1,8 +1,8 @@ #pragma once /// Get an internal name for a type -/// Important, this is depends on compilers and versions, do NOT se in file formats! -/// This provides property type safty when only limited RTTI is available +/// Important, this is depends on compilers and versions, do NOT use in file formats! +/// This provides property type safety when only limited RTTI is available /// Solution adapted from OpenVolumeMesh #include