ValueCategory.hpp 1.85 KB
Newer Older
1
2
#pragma once

3
#include <functional>
4
5
#include <type_traits>

6
7
8
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>

9
#include <amdis/common/Tags.hpp>
10

11
namespace AMDiS
12
{
13
14
  /// Category of type T, e.g. scalar, vector matrix, specified by a tag
  template <class T, class = void>
15
16
17
18
  struct ValueCategory
  {
    using type = tag::unknown;
  };
19
20

  template <class T>
21
  using ValueCategory_t = typename ValueCategory<remove_cvref_t<T>>::type;
22

23
24
25
26
27
  template <class T>
  struct ValueCategory<T, std::enable_if_t< std::is_arithmetic<T>::value >>
  {
    using type = tag::scalar;
  };
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  template <class K, int SIZE>
  struct ValueCategory< Dune::FieldVector<K, SIZE> >
  {
    using type = tag::vector;
  };

  template <class K>
  struct ValueCategory< Dune::FieldVector<K, 1> >
  {
    using type = tag::scalar;
  };

  template <class K, int ROWS, int COLS>
  struct ValueCategory< Dune::FieldMatrix<K, ROWS, COLS> >
  {
    using type = tag::matrix;
  };

  template <class K>
  struct ValueCategory< Dune::FieldMatrix<K, 1, 1> >
  {
    using type = tag::scalar;
  };
52

53
  template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
54
  struct ValueCategory< std::reference_wrapper<T> >
55
56
57
58
  {
    using type = typename ValueCategory<T>::type;
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
59

60
61
62
63
64
65
66
67
68
69
70
71
72
  namespace Category
  {
    template <class T>
    constexpr bool Scalar = std::is_same<ValueCategory_t<T>, tag::scalar>::value;

    template <class T>
    constexpr bool Vector = std::is_same<ValueCategory_t<T>, tag::vector>::value;

    template <class T>
    constexpr bool Matrix = std::is_same<ValueCategory_t<T>, tag::matrix>::value;

  } // end namespace Category

73
74
75
76
77
78
79
80
81
82
83
84
85
  template <class V>
  constexpr bool isVector(V const&)
  {
    static_assert(Category::Vector<V>,"");
    return Category::Vector<V>;
  }

  template <class V>
  constexpr bool isNotVector(V const&)
  {
    static_assert(!Category::Vector<V>,"");
    return !Category::Vector<V>;
  }
86
} // end namespace AMDiS