Skip to content
Snippets Groups Projects
Commit 19f97cb1 authored by Robert K's avatar Robert K
Browse files

[feature][Grid] Add possibility to store user data pointers on the grid

object.
parent fd746950
No related branches found
No related tags found
1 merge request!771Draft: [feature][Grid] Add possibility to store user data pointers on the grid object.
Pipeline #77889 failed
......@@ -27,7 +27,8 @@ set(HEADERS
partitionset.hh
rangegenerators.hh
sizecache.hh
scsgmapper.hh)
scsgmapper.hh
userdata.hh)
add_subdirectory(test)
......
......@@ -10,8 +10,10 @@
*/
// system includes
#include <iostream>
#include <typeinfo>
#include <string>
#include <vector>
#include <map>
// dune-common includes
#include <dune/common/fvector.hh>
......@@ -29,6 +31,7 @@
#include <dune/grid/common/gridview.hh>
#include <dune/grid/common/defaultgridview.hh>
#include <dune/grid/common/entityseed.hh>
#include <dune/grid/common/userdata.hh>
// include this file after all other, because other files might undef the
// macros that are defined in that file
......@@ -971,8 +974,42 @@ namespace Dune {
return false;
}
/*! \brief return shared_ptr to user data object of given type
*
* \return shared_ptr< UserData > which either contains a pointer to a
* previously registered object or is empty.
*/
template <class UserData>
std::shared_ptr< UserData > getUserData() const
{
const std::string name( typeid( UserData ).name() );
auto it = userData_.find( name );
if( it != userData_.end() )
return it->second->template get< UserData >();
else
return std::shared_ptr< UserData >();
}
/*! \brief register shared_ptr to user data object provided as shared
*
* \note: Only one object of each type can be registered.
*/
template <class UserData>
void registerUserData( std::shared_ptr< UserData > userData ) const
{
const std::string name( typeid( UserData ).name() );
auto it = userData_.find( name );
if( it != userData_.end() )
DUNE_THROW(InvalidStateException,"addUserData: can only add a type once!");
typedef GridUserDataObject< UserData > ObjectType;
std::shared_ptr< GridUserDataIF > ptr( new ObjectType( userData ) );
userData_.insert( std::make_pair( name, ptr ) );
}
protected:
using Grid< dim, dimworld, ct, GridFamily >::asImp;
mutable std::map< const std::string, std::shared_ptr< GridUserDataIF > > userData_;
};
/** @} */
......
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_GRID_COMMON_USERDATA_HH
#define DUNE_GRID_COMMON_USERDATA_HH
#include <typeinfo>
#include <memory>
namespace Dune
{
template <class Implementation>
class GridUserDataObject;
/** \brief interface class for user data storage
*/
class GridUserDataIF
{
protected:
GridUserDataIF () {}
public:
virtual ~GridUserDataIF() {}
virtual std::string name() const { return std::string("interface"); }
template <class Implementation>
bool compare() const
{
return typeid(Implementation).name() == this->name();
}
template <class Implementation>
std::shared_ptr< Implementation >& get()
{
typedef GridUserDataObject< Implementation > GridUserDataObjectType;
//assert( dynamic_cast< GridUserDataObjectType& > (*this) );
return dynamic_cast< GridUserDataObjectType& > (*this).object();
}
};
template <class Implementation>
class GridUserDataObject : public GridUserDataIF
{
typedef std::shared_ptr< Implementation > ObjectPointerType;
ObjectPointerType obj_;
public:
GridUserDataObject( const ObjectPointerType& obj )
: obj_( obj )
{}
GridUserDataObject( ObjectPointerType&& obj )
: obj_( std::forward(obj) )
{}
std::string name() const override { return typeid(Implementation).name(); }
std::shared_ptr< Implementation >& object() { return obj_; }
};
} // end namespace Dune
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment