Skip to content
Snippets Groups Projects
Commit 065d256b authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

[MeL] Return PropertyVector* instead of opt refs.

Since boost-1.61 (and also not in the std::optional)
optionals holding references do have some restrictions
and are difficult to use. In this special case
optional<PropertyVector&> is equivalent semantically
to a PropertyVector*. The pointer indicates that it is
not owning (contrary to usage of unique_ptr).

Thanks to Christoph pointing this issue out.
parent 4e030e95
No related branches found
No related tags found
No related merge requests found
...@@ -10,10 +10,9 @@ ...@@ -10,10 +10,9 @@
* *
*/ */
template <typename T> template <typename T>
boost::optional<PropertyVector<T> &> PropertyVector<T>* Properties::createNewPropertyVector(
Properties::createNewPropertyVector(std::string const& name, std::string const& name,
MeshItemType mesh_item_type, MeshItemType mesh_item_type,
std::size_t n_components) std::size_t n_components)
{ {
...@@ -23,7 +22,7 @@ Properties::createNewPropertyVector(std::string const& name, ...@@ -23,7 +22,7 @@ Properties::createNewPropertyVector(std::string const& name,
if (it != _properties.end()) { if (it != _properties.end()) {
ERR("A property of the name \"%s\" is already assigned to the mesh.", ERR("A property of the name \"%s\" is already assigned to the mesh.",
name.c_str()); name.c_str());
return boost::optional<PropertyVector<T> &>(); return nullptr;
} }
auto entry_info( auto entry_info(
_properties.insert( _properties.insert(
...@@ -32,15 +31,12 @@ Properties::createNewPropertyVector(std::string const& name, ...@@ -32,15 +31,12 @@ Properties::createNewPropertyVector(std::string const& name,
) )
) )
); );
return boost::optional<PropertyVector<T> &>(*( return static_cast<PropertyVector<T>*>((entry_info.first)->second);
static_cast<PropertyVector<T>*>((entry_info.first)->second)
)
);
} }
template <typename T> template <typename T>
boost::optional<PropertyVector<T> &> PropertyVector<T>* Properties::createNewPropertyVector(
Properties::createNewPropertyVector(std::string const& name, std::string const& name,
std::size_t n_prop_groups, std::size_t n_prop_groups,
std::vector<std::size_t> const& item2group_mapping, std::vector<std::size_t> const& item2group_mapping,
MeshItemType mesh_item_type, MeshItemType mesh_item_type,
...@@ -54,7 +50,7 @@ Properties::createNewPropertyVector(std::string const& name, ...@@ -54,7 +50,7 @@ Properties::createNewPropertyVector(std::string const& name,
if (it != _properties.end()) { if (it != _properties.end()) {
ERR("A property of the name \"%s\" already assigned to the mesh.", ERR("A property of the name \"%s\" already assigned to the mesh.",
name.c_str()); name.c_str());
return boost::optional<PropertyVector<T> &>(); return nullptr;
} }
// check entries of item2group_mapping for consistence // check entries of item2group_mapping for consistence
...@@ -62,7 +58,7 @@ Properties::createNewPropertyVector(std::string const& name, ...@@ -62,7 +58,7 @@ Properties::createNewPropertyVector(std::string const& name,
std::size_t const group_id (item2group_mapping[k]); std::size_t const group_id (item2group_mapping[k]);
if (group_id >= n_prop_groups) { if (group_id >= n_prop_groups) {
ERR("The mapping to property %d for item %d is not in the correct range [0,%d).", group_id, k, n_prop_groups); ERR("The mapping to property %d for item %d is not in the correct range [0,%d).", group_id, k, n_prop_groups);
return boost::optional<PropertyVector<T> &>(); return nullptr;
} }
} }
...@@ -75,47 +71,37 @@ Properties::createNewPropertyVector(std::string const& name, ...@@ -75,47 +71,37 @@ Properties::createNewPropertyVector(std::string const& name,
) )
) )
); );
return boost::optional<PropertyVector<T> &> return static_cast<PropertyVector<T>*>((entry_info.first)->second);
(*(static_cast<PropertyVector<T>*>((entry_info.first)->second)));
} }
template <typename T> template <typename T>
boost::optional<PropertyVector<T> const&> PropertyVector<T> const* Properties::getPropertyVector(
Properties::getPropertyVector(std::string const& name) const std::string const& name) const
{ {
std::map<std::string, PropertyVectorBase*>::const_iterator it( std::map<std::string, PropertyVectorBase*>::const_iterator it(
_properties.find(name) _properties.find(name));
); if (it == _properties.end())
if (it == _properties.end()) { {
ERR("A property with the specified name \"%s\" is not available.", ERR("A property with the specified name \"%s\" is not available.",
name.c_str()); name.c_str());
return boost::optional<PropertyVector<T> const&>(); return nullptr;
} }
PropertyVector<T> const* t=dynamic_cast<PropertyVector<T>const*>(it->second); return dynamic_cast<PropertyVector<T> const*>(it->second);
if (!t) {
return boost::optional<PropertyVector<T> const&>();
}
return *t;
} }
template <typename T> template <typename T>
boost::optional<PropertyVector<T>&> PropertyVector<T>* Properties::getPropertyVector(std::string const& name)
Properties::getPropertyVector(std::string const& name)
{ {
std::map<std::string, PropertyVectorBase*>::iterator it( std::map<std::string, PropertyVectorBase*>::iterator it(
_properties.find(name) _properties.find(name));
); if (it == _properties.end())
if (it == _properties.end()) { {
ERR("A property with the specified name \"%s\" is not available.", ERR("A property with the specified name \"%s\" is not available.",
name.c_str()); name.c_str());
return boost::optional<PropertyVector<T>&>(); return nullptr;
} }
PropertyVector<T> *t=dynamic_cast<PropertyVector<T>*>(it->second); return dynamic_cast<PropertyVector<T>*>(it->second);
if (!t) {
return boost::optional<PropertyVector<T> &>();
}
return *t;
} }
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
#include <string> #include <string>
#include <map> #include <map>
#include <boost/optional.hpp>
#include <logog/include/logog.hpp> #include <logog/include/logog.hpp>
#include "Location.h" #include "Location.h"
...@@ -42,28 +40,25 @@ class Properties ...@@ -42,28 +40,25 @@ class Properties
public: public:
/// Method creates a PropertyVector if a PropertyVector with the same name /// Method creates a PropertyVector if a PropertyVector with the same name
/// and the same type T was not already created before. In case there exists /// and the same type T was not already created before. In case there exists
/// already such a PropertyVector the returned boost::optional holds an /// already such a PropertyVector a nullptr is returned.
/// invalid/unusable PropertyVector. /// There are two versions of this method. This method is used when every
/// There are two versions of this method. This method is used /// mesh item at hand has its own property value, i.e. \f$n\f$ mesh item and
/// when every mesh item at hand has its own property value, i.e. \f$n\f$ /// \f$n\f$ different property values.
/// mesh item and \f$n\f$ different property values.
/// The user has to ensure the correct usage of the vector later on. /// The user has to ensure the correct usage of the vector later on.
/// @tparam T type of the property value /// @tparam T type of the property value
/// @param name the name of the property /// @param name the name of the property
/// @param mesh_item_type for instance node or element assigned properties /// @param mesh_item_type for instance node or element assigned properties
/// @param n_components number of components for each tuple /// @param n_components number of components for each tuple
/// @return On success a reference to a PropertyVector packed into a /// @return A pointer to a PropertyVector on success and a nullptr
/// boost::optional else an empty boost::optional. /// otherwise.
template <typename T> template <typename T>
boost::optional<PropertyVector<T> &> PropertyVector<T>* createNewPropertyVector(std::string const& name,
createNewPropertyVector(std::string const& name, MeshItemType mesh_item_type,
MeshItemType mesh_item_type, std::size_t n_components = 1);
std::size_t n_components = 1);
/// Method creates a PropertyVector if a PropertyVector with the same name /// Method creates a PropertyVector if a PropertyVector with the same name
/// and the same type T was not already created before. In case there exists /// and the same type T was not already created before. In case there exists
/// already such a PropertyVector the returned boost::optional holds an /// already such a PropertyVector a nullptr is returned.
/// invalid/unusable PropertyVector.
/// This method is used if only a small number of distinct property values /// This method is used if only a small number of distinct property values
/// in a property exist (e.g. mapping property groups to elements). /// in a property exist (e.g. mapping property groups to elements).
/// In this case a mapping between mesh items and properties (stored /// In this case a mapping between mesh items and properties (stored
...@@ -75,25 +70,25 @@ public: ...@@ -75,25 +70,25 @@ public:
/// group /// group
/// @param mesh_item_type for instance node or element assigned properties /// @param mesh_item_type for instance node or element assigned properties
/// @param n_components number of components for each tuple /// @param n_components number of components for each tuple
/// @return On success a reference to a PropertyVector packed into a /// @return A pointer to a PropertyVector on success and a nullptr
/// boost::optional else an empty boost::optional. /// otherwise.
template <typename T> template <typename T>
boost::optional<PropertyVector<T> &> PropertyVector<T>* createNewPropertyVector(
createNewPropertyVector(std::string const& name, std::string const& name,
std::size_t n_prop_groups, std::size_t n_prop_groups,
std::vector<std::size_t> const& item2group_mapping, std::vector<std::size_t> const& item2group_mapping,
MeshItemType mesh_item_type, MeshItemType mesh_item_type,
std::size_t n_components = 1); std::size_t n_components = 1);
/// Method to get a vector of property values. /// Returns a property vector with given \c name or nullptr if no such
/// property vector exists.
template <typename T> template <typename T>
boost::optional<PropertyVector<T> const&> PropertyVector<T> const* getPropertyVector(std::string const& name) const;
getPropertyVector(std::string const& name) const;
/// Method to get a vector of property values. /// Returns a property vector with given \c name or nullptr if no such
/// property vector exists.
template <typename T> template <typename T>
boost::optional<PropertyVector<T>&> PropertyVector<T>* getPropertyVector(std::string const& name);
getPropertyVector(std::string const& name);
void removePropertyVector(std::string const& name); void removePropertyVector(std::string const& name);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment