ValueCategory.hpp 2.03 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
namespace AMDiS
10
{
11
  namespace tag
12
  {
13
    // some tags representing traits classes or categories
14
15
16
17
    struct none {};
    struct scalar {};
    struct vector {};
    struct matrix {};
18
    struct unknown {};
19
20
21

  } // end namespace tag

Praetorius, Simon's avatar
Praetorius, Simon committed
22

23
24
  /// Category of type T, e.g. scalar, vector matrix, specified by a tag
  template <class T, class = void>
25
26
27
28
  struct ValueCategory
  {
    using type = tag::unknown;
  };
29
30

  template <class T>
31
  using ValueCategory_t = typename ValueCategory<std::decay_t<T>>::type;
32

33
34
35
36
37
  template <class T>
  struct ValueCategory<T, std::enable_if_t< std::is_arithmetic<T>::value >>
  {
    using type = tag::scalar;
  };
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  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;
  };
62

63
  template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
64
  struct ValueCategory< std::reference_wrapper<T> >
65
66
67
68
  {
    using type = typename ValueCategory<T>::type;
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
69

70
71
72
73
74
75
76
77
78
79
80
81
82
  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

83
84
85
86
87
88
89
90
91
92
93
94
95
  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>;
  }
96
} // end namespace AMDiS