Skip to content

Commit

Permalink
beautified usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro-elsweijer committed Sep 13, 2024
1 parent 62faded commit b3c97fb
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 190 deletions.
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ add_t8_example( NAME t8_example_empty_trees SOURCES remove/t8_exampl

add_t8_example( NAME t8_version SOURCES version/t8_version.cxx )

add_t8_example( NAME t8_example_multilevel SOURCES multilevel/t8_example_multilevel.cxx multilevel/t8_multilevel_concept.hxx)
add_t8_example( NAME t8_example_multilevel SOURCES multilevel/t8_example_multilevel.cxx multilevel/t8_multilevel_concept_base.hxx multilevel/t8_multilevel_concept_default.hxx multilevel/t8_multilevel_concept_multilevel.hxx)
55 changes: 22 additions & 33 deletions example/multilevel/t8_example_multilevel.cxx
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
#include <example/multilevel/t8_multilevel_concept.hxx>
#include <iostream>
#include <variant>
#include <vector>

template <typename... derived_scheme_t>
using scheme_v = std::variant<derived_scheme_t...>;
#include <example/multilevel/t8_multilevel_concept_base.hxx>
#include <example/multilevel/t8_multilevel_concept_default.hxx>
#include <example/multilevel/t8_multilevel_concept_multilevel.hxx>
#include <example/multilevel/t8_multilevel_concept_scheme.hxx>

using scheme_container = std::vector<
scheme_v<triangle_scheme, quad_scheme, multilevel_scheme<triangle_scheme>, multilevel_scheme<quad_scheme> > >;
#include <iostream>

int
main ()
{
typedef enum { triangle_eclass, quad_eclass, triangle_m_eclass, quad_m_eclass } eclass;

scheme_container schemes { triangle_scheme (), quad_scheme (), multilevel_scheme<triangle_scheme> (),
multilevel_scheme<quad_scheme> () };
Scheme default_scheme = new_default_scheme ();
Scheme multilevel_scheme = new_default_scheme (true);

triangle tri;
tri.level = 1;
tri.orientation = 0;
element_t *tri_elem = (element_t *) (&tri);

std::visit ([tri_elem] (auto &&scheme) {
std::cout << "Triangle level: " << scheme.get_level (tri_elem) << std::endl;
std::cout << "Triangle num children: " << scheme.get_num_children (tri_elem) << std::endl;
std::cout << "Triangle num vertices: " << scheme.get_num_vertices () << std::endl;
}, schemes[triangle_eclass]);
std::cout << "Triangle level: " << default_scheme.get_level (triangle_eclass, tri_elem) << std::endl;
std::cout << "Triangle num children: " << default_scheme.get_num_children (triangle_eclass, tri_elem) << std::endl;
std::cout << "Triangle num vertices: " << default_scheme.get_num_vertices (triangle_eclass) << std::endl;

quad q;
q.level = 1;
element_t *q_elem = (element_t *) (&q);

std::visit ([q_elem] (auto &&scheme) {
std::cout << "Quad level: " << scheme.get_level (q_elem) << std::endl;
std::cout << "Quad num children: " << scheme.get_num_children (q_elem) << std::endl;
std::cout << "Quad num vertices: " << scheme.get_num_vertices () << std::endl;
}, schemes[quad_eclass]);
std::cout << "Quad level: " << default_scheme.get_level (quad_eclass, q_elem) << std::endl;
std::cout << "Quad num children: " << default_scheme.get_num_children (quad_eclass, q_elem) << std::endl;
std::cout << "Quad num vertices: " << default_scheme.get_num_vertices (quad_eclass) << std::endl;

multilevel_element<triangle> tri_m;
tri_m.elem = tri;
Expand All @@ -48,17 +38,16 @@ main ()
q_m.multilevel_level = 2;
element_t *q_m_elem = (element_t *) (&q_m);

std::visit ([tri_m_elem] (auto &&scheme) {
std::cout << "Multilevel triangle level: " << scheme.get_level (tri_m_elem) << std::endl;
std::cout << "Multilevel triangle num children: " << scheme.get_num_children (tri_m_elem) << std::endl;
std::cout << "Multilevel triangle num vertices: " << scheme.get_num_vertices () << std::endl;
}, schemes[triangle_m_eclass]);
std::cout << "Multilevel triangle level: " << multilevel_scheme.get_level (triangle_eclass, tri_m_elem) << std::endl;
std::cout << "Multilevel triangle num children: " << multilevel_scheme.get_num_children (triangle_eclass, tri_m_elem)
<< std::endl;
std::cout << "Multilevel triangle num vertices: " << multilevel_scheme.get_num_vertices (triangle_eclass)
<< std::endl;

std::visit ([q_m_elem] (auto &&scheme) {
std::cout << "Multilevel quad level: " << scheme.get_level (q_m_elem) << std::endl;
std::cout << "Multilevel quad num children: " << scheme.get_num_children (q_m_elem) << std::endl;
std::cout << "Multilevel quad num vertices: " << scheme.get_num_vertices () << std::endl;
}, schemes[quad_m_eclass]);
std::cout << "Multilevel quad level: " << multilevel_scheme.get_level (quad_eclass, q_m_elem) << std::endl;
std::cout << "Multilevel quad num children: " << multilevel_scheme.get_num_children (quad_eclass, q_m_elem)
<< std::endl;
std::cout << "Multilevel quad num vertices: " << multilevel_scheme.get_num_vertices (quad_eclass) << std::endl;

return 0;
}
156 changes: 0 additions & 156 deletions example/multilevel/t8_multilevel_concept.hxx

This file was deleted.

46 changes: 46 additions & 0 deletions example/multilevel/t8_multilevel_concept_base.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <array>
#include <variant>
#include <vector>

typedef enum { triangle_eclass, quad_eclass, eclass_count } eclass;

// Opaque pointer to cast the element type into
typedef struct element element_t;

// Using CRTP to avoid virtual function calls
template <class Derived_scheme_t>
class Scheme_base {
public:
~Scheme_base ()
{
}

inline int
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);
};

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);
};

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 ();
};

private:
// This way the derived class and only the derived class can use this constructor.
// This way you get an error when doing: `class triangle_scheme: public scheme_base <quad_scheme>`
Scheme_base () {};
friend Derived_scheme_t;
};
79 changes: 79 additions & 0 deletions example/multilevel/t8_multilevel_concept_default.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#include <array>
#include <variant>
#include <vector>
#include <example/multilevel/t8_multilevel_concept_base.hxx>

struct triangle
{
int level;
int orientation;
};

struct quad
{
int level;
};

// inherits from base which is a template for this class
class Triangle_scheme: public Scheme_base<Triangle_scheme> {
public:
Triangle_scheme () {};

inline int
get_level (element_t *elem) const
{
elem_type *tri = (elem_type *) elem;
return tri->level;
};

inline int
get_num_children (element_t *elem) const
{
return 4;
};

inline int
get_num_vertices () const
{
return 3;
};

protected:
// When the multilevel class inherits from this it needs to know the element type of this scheme
using elem_type = triangle;

private:
};

// inherits from base which is a template for this class
class Quad_scheme: public Scheme_base<Quad_scheme> {
public:
Quad_scheme () {};

inline int
get_level (element_t *elem) const
{
elem_type *q = (elem_type *) elem;
return q->level;
};

int
get_num_children (element_t *elem) const
{
return 4;
};

int
get_num_vertices () const
{
return 4;
};

protected:
// When the multilevel class inherits from this it needs to know the element type of this scheme
using elem_type = quad;

private:
};
Loading

0 comments on commit b3c97fb

Please sign in to comment.