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 @@
*
*/
template <typename T>
boost::optional<PropertyVector<T> &>
Properties::createNewPropertyVector(std::string const& name,
PropertyVector<T>* Properties::createNewPropertyVector(
std::string const& name,
MeshItemType mesh_item_type,
std::size_t n_components)
{
......@@ -23,7 +22,7 @@ Properties::createNewPropertyVector(std::string const& name,
if (it != _properties.end()) {
ERR("A property of the name \"%s\" is already assigned to the mesh.",
name.c_str());
return boost::optional<PropertyVector<T> &>();
return nullptr;
}
auto entry_info(
_properties.insert(
......@@ -32,15 +31,12 @@ Properties::createNewPropertyVector(std::string const& name,
)
)
);
return boost::optional<PropertyVector<T> &>(*(
static_cast<PropertyVector<T>*>((entry_info.first)->second)
)
);
return static_cast<PropertyVector<T>*>((entry_info.first)->second);
}
template <typename T>
boost::optional<PropertyVector<T> &>
Properties::createNewPropertyVector(std::string const& name,
PropertyVector<T>* Properties::createNewPropertyVector(
std::string const& name,
std::size_t n_prop_groups,
std::vector<std::size_t> const& item2group_mapping,
MeshItemType mesh_item_type,
......@@ -54,7 +50,7 @@ Properties::createNewPropertyVector(std::string const& name,
if (it != _properties.end()) {
ERR("A property of the name \"%s\" already assigned to the mesh.",
name.c_str());
return boost::optional<PropertyVector<T> &>();
return nullptr;
}
// check entries of item2group_mapping for consistence
......@@ -62,7 +58,7 @@ Properties::createNewPropertyVector(std::string const& name,
std::size_t const group_id (item2group_mapping[k]);
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);
return boost::optional<PropertyVector<T> &>();
return nullptr;
}
}
......@@ -75,47 +71,37 @@ Properties::createNewPropertyVector(std::string const& name,
)
)
);
return boost::optional<PropertyVector<T> &>
(*(static_cast<PropertyVector<T>*>((entry_info.first)->second)));
return static_cast<PropertyVector<T>*>((entry_info.first)->second);
}
template <typename T>
boost::optional<PropertyVector<T> const&>
Properties::getPropertyVector(std::string const& name) const
PropertyVector<T> const* Properties::getPropertyVector(
std::string const& name) const
{
std::map<std::string, PropertyVectorBase*>::const_iterator it(
_properties.find(name)
);
if (it == _properties.end()) {
_properties.find(name));
if (it == _properties.end())
{
ERR("A property with the specified name \"%s\" is not available.",
name.c_str());
return boost::optional<PropertyVector<T> const&>();
return nullptr;
}
PropertyVector<T> const* t=dynamic_cast<PropertyVector<T>const*>(it->second);
if (!t) {
return boost::optional<PropertyVector<T> const&>();
}
return *t;
return dynamic_cast<PropertyVector<T> const*>(it->second);
}
template <typename T>
boost::optional<PropertyVector<T>&>
Properties::getPropertyVector(std::string const& name)
PropertyVector<T>* Properties::getPropertyVector(std::string const& name)
{
std::map<std::string, PropertyVectorBase*>::iterator it(
_properties.find(name)
);
if (it == _properties.end()) {
_properties.find(name));
if (it == _properties.end())
{
ERR("A property with the specified name \"%s\" is not available.",
name.c_str());
return boost::optional<PropertyVector<T>&>();
return nullptr;
}
PropertyVector<T> *t=dynamic_cast<PropertyVector<T>*>(it->second);
if (!t) {
return boost::optional<PropertyVector<T> &>();
}
return *t;
return dynamic_cast<PropertyVector<T>*>(it->second);
}
......@@ -18,8 +18,6 @@
#include <string>
#include <map>
#include <boost/optional.hpp>
#include <logog/include/logog.hpp>
#include "Location.h"
......@@ -42,28 +40,25 @@ class Properties
public:
/// 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
/// already such a PropertyVector the returned boost::optional holds an
/// invalid/unusable PropertyVector.
/// There are two versions of this method. This method is used
/// when every mesh item at hand has its own property value, i.e. \f$n\f$
/// mesh item and \f$n\f$ different property values.
/// already such a PropertyVector a nullptr is returned.
/// There are two versions of this method. This method is used when every
/// mesh item at hand has its own property value, i.e. \f$n\f$ mesh item and
/// \f$n\f$ different property values.
/// The user has to ensure the correct usage of the vector later on.
/// @tparam T type of the property value
/// @param name the name of the property
/// @param mesh_item_type for instance node or element assigned properties
/// @param n_components number of components for each tuple
/// @return On success a reference to a PropertyVector packed into a
/// boost::optional else an empty boost::optional.
/// @return A pointer to a PropertyVector on success and a nullptr
/// otherwise.
template <typename T>
boost::optional<PropertyVector<T> &>
createNewPropertyVector(std::string const& name,
MeshItemType mesh_item_type,
std::size_t n_components = 1);
PropertyVector<T>* createNewPropertyVector(std::string const& name,
MeshItemType mesh_item_type,
std::size_t n_components = 1);
/// 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
/// already such a PropertyVector the returned boost::optional holds an
/// invalid/unusable PropertyVector.
/// already such a PropertyVector a nullptr is returned.
/// 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 this case a mapping between mesh items and properties (stored
......@@ -75,25 +70,25 @@ public:
/// group
/// @param mesh_item_type for instance node or element assigned properties
/// @param n_components number of components for each tuple
/// @return On success a reference to a PropertyVector packed into a
/// boost::optional else an empty boost::optional.
/// @return A pointer to a PropertyVector on success and a nullptr
/// otherwise.
template <typename T>
boost::optional<PropertyVector<T> &>
createNewPropertyVector(std::string const& name,
PropertyVector<T>* createNewPropertyVector(
std::string const& name,
std::size_t n_prop_groups,
std::vector<std::size_t> const& item2group_mapping,
MeshItemType mesh_item_type,
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>
boost::optional<PropertyVector<T> const&>
getPropertyVector(std::string const& name) const;
PropertyVector<T> 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>
boost::optional<PropertyVector<T>&>
getPropertyVector(std::string const& name);
PropertyVector<T>* getPropertyVector(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