Skip to content
Snippets Groups Projects
Forked from ogs / ogs
18384 commits behind the upstream repository.
  • Dmitry Yu. Naumov's avatar
    065d256b
    [MeL] Return PropertyVector* instead of opt refs. · 065d256b
    Dmitry Yu. Naumov authored
    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.
    065d256b
    History
    [MeL] Return PropertyVector* instead of opt refs.
    Dmitry Yu. Naumov authored
    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.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Properties-impl.h 3.24 KiB
/**
 * \file
 * \brief  Implemenatiom of the template part of the class Properties.
 *
 * \copyright
 * Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org)
 *            Distributed under a Modified BSD License.
 *              See accompanying file LICENSE.txt or
 *              http://www.opengeosys.org/project/license
 *
 */

template <typename T>
PropertyVector<T>* Properties::createNewPropertyVector(
    std::string const& name,
    MeshItemType mesh_item_type,
    std::size_t n_components)
{
    std::map<std::string, PropertyVectorBase*>::const_iterator it(
        _properties.find(name)
    );
    if (it != _properties.end()) {
        ERR("A property of the name \"%s\" is already assigned to the mesh.",
            name.c_str());
        return nullptr;
    }
    auto entry_info(
        _properties.insert(
            std::make_pair(
                name, new PropertyVector<T>(name, mesh_item_type, n_components)
            )
        )
    );
    return static_cast<PropertyVector<T>*>((entry_info.first)->second);
}

template <typename T>
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,
    std::size_t n_components)
{
    // check if there is already a PropertyVector with the same name and
    // mesh_item_type
    std::map<std::string, PropertyVectorBase*>::const_iterator it(
        _properties.find(name)
    );
    if (it != _properties.end()) {
        ERR("A property of the name \"%s\" already assigned to the mesh.",
            name.c_str());
        return nullptr;
    }

    // check entries of item2group_mapping for consistence
    for (std::size_t k(0); k<item2group_mapping.size(); k++) {
        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 nullptr;
        }
    }

    auto entry_info(
        _properties.insert(
            std::pair<std::string, PropertyVectorBase*>(
                name,
                new PropertyVector<T>(n_prop_groups,
                    item2group_mapping, name, mesh_item_type, n_components)
            )
        )
    );
    return static_cast<PropertyVector<T>*>((entry_info.first)->second);
}

template <typename T>
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())
    {
        ERR("A property with the specified name \"%s\" is not available.",
            name.c_str());
        return nullptr;
    }

    return dynamic_cast<PropertyVector<T> const*>(it->second);
}

template <typename T>
PropertyVector<T>* Properties::getPropertyVector(std::string const& name)
{
    std::map<std::string, PropertyVectorBase*>::iterator it(
        _properties.find(name));
    if (it == _properties.end())
    {
        ERR("A property with the specified name \"%s\" is not available.",
            name.c_str());
        return nullptr;
    }

    return dynamic_cast<PropertyVector<T>*>(it->second);
}