Skip to content
Snippets Groups Projects
Commit 05dff1cf authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[common.crtp] added from dune-pymor

* this defines CHECK_CRTP and CHECK_AND_CALL_CRTP
  (has still to be made threadsafe perhaps?)
* defines CRTPInterface
parent f970ef13
No related branches found
No related tags found
No related merge requests found
// This file is part of the dune-stuff project:
// https://users.dune-project.org/projects/dune-stuff/
// Copyright Holders: Felix Albrecht, Rene Milk
// License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
#ifndef DUNE_STUFF_COMMON_CRTP_HH
#define DUNE_STUFF_COMMON_CRTP_HH
#include "exceptions.hh"
#include <dune/common/bartonnackmanifcheck.hh>
#ifdef CHECK_CRTP
#undef CHECK_CRTP
#endif
#ifdef CHECK_AND_CALL_CRTP
#undef CHECK_AND_CALL_CRTP
#endif
#ifdef NDEBUG
#define CHECK_CRTP(dummy)
#else
/**
* This macro is essentially a slightly modified copy of the CHECK_INTERFACE_IMPLEMENTATION macro.
*/
#define CHECK_CRTP(__interface_method_to_call__) \
{ \
static bool call = false; \
if (call) \
DUNE_THROW_COLORFULLY(Dune::Stuff::Exception::CRTP_check_failed, \
"The derived class does not implement a required method!"); \
call = true; \
try { \
(__interface_method_to_call__); \
call = false; \
} catch (...) { \
call = false; \
throw; \
} \
}
// CHECK_CRTP
#endif
#ifdef NDEBUG
#define CHECK_AND_CALL_CRTP(__interface_method_to_call__) (__interface_method_to_call__)
#else
/**
* This macro is essentially a slightly modified copy of the CHECK_AND_CALL_INTERFACE_IMPLEMENTATION macro.
*/
#define CHECK_AND_CALL_CRTP(__interface_method_to_call__) CHECK_CRTP(__interface_method_to_call__)
#endif
namespace Dune {
namespace Stuff {
template <class InterfaceType, class Traits>
class CRTPInterface
{
protected:
typedef typename Traits::derived_type derived_type;
inline static derived_type& as_imp(InterfaceType& that)
{
return static_cast<derived_type&>(that);
}
inline static const derived_type& as_imp(const InterfaceType& that) const
{
return static_cast<const derived_type&>(that);
}
}; // CRTPInterface
} // namespace Stuff
} // namespace Dune
#endif // DUNE_STUFF_COMMON_CRTP_HH
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