Skip to content

Commit

Permalink
crtp improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro-elsweijer committed Sep 25, 2024
1 parent b3c97fb commit b51469b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions example/multilevel/t8_multilevel_concept_base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <array>
#include <variant>
#include <vector>
#include <example/multilevel/t8_multilevel_concept_crtp.hxx>

typedef enum { triangle_eclass, quad_eclass, eclass_count } eclass;

Expand All @@ -11,7 +12,7 @@ typedef struct element element_t;

// Using CRTP to avoid virtual function calls
template <class Derived_scheme_t>
class Scheme_base {
class Scheme_base: public crtp<Derived_scheme_t> {
public:
~Scheme_base ()
{
Expand All @@ -21,21 +22,21 @@ class Scheme_base {
get_level (element_t *elem)
{
// cast derived class into base class to avoid virtual functions
return static_cast<Derived_scheme_t const &> (*this).get_level (elem);
return this->underlying ().get_level (elem);
};

inline int
get_num_children (element_t *elem)
{
// cast derived class into base class to avoid virtual functions
return static_cast<Derived_scheme_t const &> (*this).get_num_children (elem);
return this->underlying ().get_num_children (elem);
};

inline int
get_num_vertices ()
{
// cast derived class into base class to avoid virtual functions
return static_cast<Derived_scheme_t const &> (*this).get_num_vertices ();
return this->underlying ().get_num_vertices ();
};

private:
Expand Down
15 changes: 15 additions & 0 deletions example/multilevel/t8_multilevel_concept_crtp.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

template <class Derived_t>
class crtp {
Derived_t&
underlying ()
{
return static_cast<Derived_t&> (*this);
}
Derived_t const&
underlying () const
{
return static_cast<Derived_t const&> (*this);
}
};

0 comments on commit b51469b

Please sign in to comment.