Commit 435cf124 authored by Martin Heistermann's avatar Martin Heistermann

Properties: Store and compare type names instead of relying on dynamic_cast

parent 4368575b
Pipeline #10496 passed with stage
in 8 minutes and 10 seconds
...@@ -32,14 +32,6 @@ ...@@ -32,14 +32,6 @@
* * * *
\*===========================================================================*/ \*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $Date$ *
* $LastChangedBy$ *
* *
\*===========================================================================*/
#ifndef BASEPROPERTY_HH_ #ifndef BASEPROPERTY_HH_
#define BASEPROPERTY_HH_ #define BASEPROPERTY_HH_
...@@ -93,6 +85,8 @@ public: ...@@ -93,6 +85,8 @@ public:
protected: protected:
virtual const std::string &internal_type_name() const = 0;
/// Copy data from other property. `other` MUST point to an object with the same type as `this`! /// Copy data from other property. `other` MUST point to an object with the same type as `this`!
/// Currently no type check is performed. /// Currently no type check is performed.
virtual void assign_values_from(const BaseProperty *other) = 0; virtual void assign_values_from(const BaseProperty *other) = 0;
......
...@@ -32,15 +32,6 @@ ...@@ -32,15 +32,6 @@
* * * *
\*===========================================================================*/ \*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $Date$ *
* $LastChangedBy$ *
* *
\*===========================================================================*/
#ifndef OPENVOLUMEMESHBASEPROPERTY_HH #ifndef OPENVOLUMEMESHBASEPROPERTY_HH
#define OPENVOLUMEMESHBASEPROPERTY_HH #define OPENVOLUMEMESHBASEPROPERTY_HH
...@@ -71,9 +62,14 @@ public: ...@@ -71,9 +62,14 @@ public:
public: public:
explicit OpenVolumeMeshBaseProperty(const std::string& _name = "<unknown>") : explicit OpenVolumeMeshBaseProperty(
name_(_name), persistent_(false), handle_(-1) { const std::string& _name = "<unknown>",
} const std::string& _internal_type_name = "<unknown>")
: name_(_name),
internal_type_name_(_internal_type_name),
persistent_(false),
handle_(-1)
{}
OpenVolumeMeshBaseProperty(const OpenVolumeMeshBaseProperty& _rhs) = default; OpenVolumeMeshBaseProperty(const OpenVolumeMeshBaseProperty& _rhs) = default;
...@@ -107,6 +103,10 @@ public: ...@@ -107,6 +103,10 @@ public:
return name_; return name_;
} }
const std::string& internal_type_name() const {
return internal_type_name_;
}
// Function to serialize a property // Function to serialize a property
virtual void serialize(std::ostream& /*_ostr*/) const {} virtual void serialize(std::ostream& /*_ostr*/) const {}
...@@ -148,6 +148,7 @@ protected: ...@@ -148,6 +148,7 @@ protected:
private: private:
std::string name_; std::string name_;
std::string internal_type_name_;
bool persistent_; bool persistent_;
......
...@@ -62,7 +62,7 @@ template<class T> ...@@ -62,7 +62,7 @@ template<class T>
class OpenVolumeMeshPropertyT: public OpenVolumeMeshBaseProperty { class OpenVolumeMeshPropertyT: public OpenVolumeMeshBaseProperty {
public: public:
template <class PropT, class HandleT> friend class PropertyPtr; template <class PropT, class Entity> friend class PropertyPtr;
typedef T Value; typedef T Value;
typedef std::vector<T> vector_type; typedef std::vector<T> vector_type;
...@@ -72,10 +72,13 @@ public: ...@@ -72,10 +72,13 @@ public:
public: public:
OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const T &_def = T()) : explicit OpenVolumeMeshPropertyT(
OpenVolumeMeshBaseProperty(_name), const std::string& _name = "<unknown>",
def_(_def) { const std::string& _internal_type_name = "<unknown>",
} const T &_def = T())
: OpenVolumeMeshBaseProperty(_name, _internal_type_name),
def_(_def)
{}
OpenVolumeMeshPropertyT(const OpenVolumeMeshPropertyT& _rhs) = default; OpenVolumeMeshPropertyT(const OpenVolumeMeshPropertyT& _rhs) = default;
......
...@@ -89,7 +89,7 @@ public: ...@@ -89,7 +89,7 @@ public:
: PropertyTT(std::move(mesh->template request_property<T, Entity>(_name, _def))) : PropertyTT(std::move(mesh->template request_property<T, Entity>(_name, _def)))
{} {}
using PropertyHandleT = OpenVolumeMesh::PropHandleT<Entity>; using PropertyHandleT = OpenVolumeMesh::PropHandleT<Entity>;
PropertyTT(const std::string& _name, ResourceManager& _resMan, PropertyHandleT _handle, const T _def = T()); PropertyTT(const std::string& _name, const std::string& _internal_type_name, ResourceManager& _resMan, PropertyHandleT _handle, const T _def = T());
virtual ~PropertyTT() = default; virtual ~PropertyTT() = default;
virtual BaseProperty* clone(ResourceManager &_resMan, OpenVolumeMeshHandle _handle) const; virtual BaseProperty* clone(ResourceManager &_resMan, OpenVolumeMeshHandle _handle) const;
virtual const std::string entityType() const { return entityTypeName<Entity>(); } virtual const std::string entityType() const { return entityTypeName<Entity>(); }
......
...@@ -42,8 +42,8 @@ ...@@ -42,8 +42,8 @@
namespace OpenVolumeMesh { namespace OpenVolumeMesh {
template<typename T, typename Entity> template<typename T, typename Entity>
PropertyTT<T,Entity>::PropertyTT(const std::string& _name, ResourceManager& _resMan, PropertyHandleT _handle, const T _def) : PropertyTT<T,Entity>::PropertyTT(const std::string& _name, const std::string& _internal_type_name, ResourceManager& _resMan, PropertyHandleT _handle, const T _def) :
PropertyPtr<OpenVolumeMeshPropertyT<T>, Entity>(new OpenVolumeMeshPropertyT<T>(_name, _def), _resMan, _handle) { PropertyPtr<OpenVolumeMeshPropertyT<T>, Entity>(new OpenVolumeMeshPropertyT<T>(_name, _internal_type_name, _def), _resMan, _handle) {
} }
......
...@@ -114,6 +114,7 @@ public: ...@@ -114,6 +114,7 @@ public:
virtual bool anonymous() const { return ptr::shared_ptr<PropT>::get()->name().empty(); } virtual bool anonymous() const { return ptr::shared_ptr<PropT>::get()->name().empty(); }
protected: protected:
virtual const std::string &internal_type_name() const { return ptr::shared_ptr<PropT>::get()->internal_type_name(); }
void assign_values_from(const BaseProperty *other) override; void assign_values_from(const BaseProperty *other) override;
void move_values_from(BaseProperty *other) override; void move_values_from(BaseProperty *other) override;
......
...@@ -44,9 +44,11 @@ ...@@ -44,9 +44,11 @@
#include "ResourceManager.hh" #include "ResourceManager.hh"
#include "PropertyDefines.hh" #include "PropertyDefines.hh"
#include "TypeName.hh"
namespace OpenVolumeMesh { namespace OpenVolumeMesh {
template<class T> template<class T>
VertexPropertyT<T> ResourceManager::request_vertex_property(const std::string& _name, const T _def) { VertexPropertyT<T> ResourceManager::request_vertex_property(const std::string& _name, const T _def) {
...@@ -90,25 +92,24 @@ MeshPropertyT<T> ResourceManager::request_mesh_property(const std::string& _name ...@@ -90,25 +92,24 @@ MeshPropertyT<T> ResourceManager::request_mesh_property(const std::string& _name
} }
template<class StdVecT, class PropT, class HandleT, class T> template<class StdVecT, class PropT, class HandleT, class T>
PropT ResourceManager::internal_request_property(StdVecT& _vec, const std::string& _name, size_t _size, const T _def) { PropT ResourceManager::internal_request_property(StdVecT& _vec, const std::string& _name, size_t _size, const T _def)
{
auto type_name = get_type_name<T>();
if(!_name.empty()) { if(!_name.empty()) {
for(typename StdVecT::iterator it = _vec.begin(); for(typename StdVecT::iterator it = _vec.begin();
it != _vec.end(); ++it) { it != _vec.end(); ++it) {
if((*it)->name() == _name) { if((*it)->name() == _name
#if OVM_FORCE_STATIC_CAST && (*it)->internal_type_name() == type_name)
{
return *static_cast<PropT*>(*it); return *static_cast<PropT*>(*it);
#else
PropT* prop = dynamic_cast<PropT*>(*it);
if(prop != NULL) return *prop;
#endif
} }
} }
} }
HandleT handle((int)_vec.size()); HandleT handle((int)_vec.size());
PropT* prop = new PropT(_name, *this, handle, _def); PropT* prop = new PropT(_name, type_name, *this, handle, _def);
prop->resize(_size); prop->resize(_size);
// Store property pointer // Store property pointer
......
#include <string>
#include <typeinfo>
/// Get an internal name for a type. Important: this differs between
/// compilers and versions, do NOT use in file formats!
/// We need this in order to provide property type safety when
/// only limited RTTI support is available.
template<typename T>
std::string get_type_name()
{
#ifdef _MSC_VER
// MSVC's type_name only returns a friendly name with .name(),
// get the more unique mangled name using .raw_name():
return typeid(T).raw_name();
#else
// GCC and clang currently return the mangled name as .name(),
// there is no .raw_name()
return typeid(T).name();
#endif
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment