FakeContainer.hpp 4.13 KB
Newer Older
1
2
3
#pragma once

#include <cstddef>
4
#include <iterator>
5
6
7
8
9
10

namespace AMDiS
{
  struct FakeAssigner
  {
    template <class T>
11
    constexpr FakeAssigner& operator=(T&&) noexcept { return *this; }
12
13

    template <class T>
14
    constexpr FakeAssigner& operator+=(T&&) noexcept { return *this; }
15
16

    template <class T>
17
    constexpr FakeAssigner& operator-=(T&&) noexcept { return *this; }
18
19

    template <class T>
20
    constexpr FakeAssigner& operator*=(T&&) noexcept { return *this; }
21
22

    template <class T>
23
    constexpr FakeAssigner& operator/=(T&&) noexcept { return *this; }
24
25
  };

26
  /// \brief A container-like data-structure not storing anything and with empty
27
28
29
  /// implementations in many container-interface functions.
  /**
   * This container that *does nothing* can be used as a dummy argument to
30
   * functions expecting a container, in order to omit specializations of
31
32
   * these functions for not providing a container.
   **/
33
  template <class T, T value>
34
35
36
37
  class FakeContainer
      : public FakeAssigner
  {
  public:
38
    using value_type = T;
39
40
    using size_type = std::size_t;

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    struct const_iterator
    {
      using value_type = T;
      using reference = T;
      using difference_type = std::ptrdiff_t;
      using iterator_category = std::forward_iterator_tag;

      constexpr T operator*() const noexcept { return value; }
      constexpr const_iterator& operator++() noexcept { return *this; }
      constexpr const_iterator  operator++(int) noexcept { return *this; }

      /// Comparison of the iterator is always true
      constexpr bool operator==(const_iterator) const noexcept { return true; }
      constexpr bool operator!=(const_iterator) const noexcept { return false; }
    };


  public: // constructor

60
    template <class... Args>
61
62
    constexpr explicit FakeContainer(Args&&...) noexcept {}

63
64
    template <class S>
    constexpr void init(S const&, bool) noexcept { /* do nothing */ }
65
66
67
68
69
70
71
    constexpr void finish() noexcept { /* do nothing */ }


  public: // modifiers and capacity

    template <class... Args>
    constexpr void scatter(Args&&...) noexcept { /* do nothing */ }
72
73

    template <class Arg>
74
    constexpr void push_back(Arg&&) noexcept { /* do nothing */ }
75
76

    template <class... Args>
77
78
79
80
81
82
83
    constexpr void emplace_back(Args&&...) noexcept { /* do nothing */ }

    template <class Size>
    constexpr void reserve(Size&&) noexcept { /* do nothing */ }

    template <class Size>
    constexpr void resize(Size&&) noexcept { /* do nothing */ }
84

85
86
    /// This container is never empty
    constexpr bool empty() const noexcept { return false; }
87

88
89

  public: // element access
90
91
92

    /// Mutable *element* access does return the container itself
    /// This allows to emulate nested containers
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    template <class Index>
    constexpr FakeContainer& operator[](Index&&) noexcept { return *this; }

    /// Const *element* access does return the container itself
    /// This allows to emulate nested containers
    template <class Index>
    constexpr FakeContainer const& operator[](Index&&) const noexcept { return *this; }

    /// Mutable *element* access
    template <class Index>
    constexpr FakeContainer& at(Index&&) noexcept { return *this; }

    /// Const *element* access
    template <class Index>
    constexpr FakeContainer const& at(Index&&) const noexcept { return *this; }


    constexpr FakeContainer&       front()       noexcept { return *this; }
    constexpr FakeContainer const& front() const noexcept { return *this; }


    /// Container can be cast to the constant integral value
    constexpr operator T() const noexcept { return value; }


  public: // iterators

    /// Return iterator that always redirects to a bool true.
    constexpr const_iterator begin() const noexcept { return const_iterator{}; }

    /// Return iterator that always redirects to a bool true.
    constexpr const_iterator cbegin() const noexcept { return const_iterator{}; }


  public: // assignment operators
128
129
130
131
132
133
134
135
136
137

    /// Assignment operators from a fake assigner
    using FakeAssigner::operator=;
    using FakeAssigner::operator+=;
    using FakeAssigner::operator-=;
    using FakeAssigner::operator*=;
    using FakeAssigner::operator/=;
  };

} // end namespace AMDiS