From 5718545d25fea43ee81e4942bb3b74cf9c0296c7 Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Mon, 12 Nov 2018 10:26:16 +0100
Subject: [PATCH] [MeL] New existsPropertyVector() with more checks.

The additional version tests despite the data type
and the name also the item_type and the number of
components. The user has less to test which makes
OGS more secure.
---
 MeshLib/Properties-impl.h | 27 +++++++++++++++++++++++++++
 MeshLib/Properties.h      |  8 ++++++++
 2 files changed, 35 insertions(+)

diff --git a/MeshLib/Properties-impl.h b/MeshLib/Properties-impl.h
index ff61aab5242..8b7669dd7c2 100644
--- a/MeshLib/Properties-impl.h
+++ b/MeshLib/Properties-impl.h
@@ -87,6 +87,33 @@ bool Properties::existsPropertyVector(std::string const& name) const
     return dynamic_cast<PropertyVector<T> const*>(it->second) != nullptr;
 }
 
+template <typename T>
+bool Properties::existsPropertyVector(std::string const& name,
+                                      MeshItemType const item_type,
+                                      int const number_of_components) const
+{
+    auto const it = _properties.find(name);
+    if (it == _properties.end())
+    {
+        return false;
+    }
+
+    auto property = dynamic_cast<PropertyVector<T>*>(it->second);
+    if (property == nullptr)
+    {
+        return false;
+    }
+    if (property->getMeshItemType() != item_type)
+    {
+        return false;
+    }
+    if (property->getNumberOfComponents() != number_of_components)
+    {
+        return false;
+    }
+    return true;
+}
+
 template <typename T>
 PropertyVector<T> const* Properties::getPropertyVector(
     std::string const& name) const
diff --git a/MeshLib/Properties.h b/MeshLib/Properties.h
index f31ea3c70af..1dcf650bbda 100644
--- a/MeshLib/Properties.h
+++ b/MeshLib/Properties.h
@@ -85,6 +85,14 @@ public:
     template <typename T>
     bool existsPropertyVector(std::string const& name) const;
 
+    /// Checks if a property vector with given type \c T, \c name, \c item_type
+    /// and \c number_of_components exists.
+    /// @param name name of the requested property vector
+    template <typename T>
+    bool existsPropertyVector(std::string const& name,
+                              MeshItemType const item_type,
+                              int const number_of_components) const;
+
     /// Returns a property vector with given \c name or aborts calling OGS_FATAL
     /// if no such property vector exists.
     template <typename T>
-- 
GitLab