FakeContainer.hpp 1.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once

#include <cstddef>

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

    template <class T>
    FakeAssigner& operator+=(T&&) { return *this; }

    template <class T>
    FakeAssigner& operator-=(T&&) { return *this; }

    template <class T>
    FakeAssigner& operator*=(T&&) { return *this; }

    template <class T>
    FakeAssigner& operator/=(T&&) { return *this; }
  };

  /// A container-like data-structure not storing anything and with empty
  /// implementations in many container-interface functions.
  /**
   * This container that *does nothing* can be used as a dummy argument to
   * functions expecting a container, in order to ommit specializations of
   * these functions for not providing a container.
   **/
  class FakeContainer
      : public FakeAssigner
  {
  public:
    using size_type = std::size_t;

    template <class... Args>
    FakeContainer(Args&&...) {}

    template <class Arg>
    void push_back(Arg&&) {}

    template <class... Args>
    void emplace_back(Args&&...) {}

    void reserve(size_type) {}
    void resize(size_type) {}

    /// This container is always empty
    bool empty() const { return true; }
    size_type size() const { return 0u; }

    /// Mutable *element* access does return the container itself
    /// This allows to emulate nested containers
    FakeContainer& operator[](size_type) { return *this; }

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

    FakeAssigner front() { return {}; }
    FakeAssigner back() { return {}; }

    friend inline FakeAssigner front(FakeContainer&) { return {}; }
    friend inline FakeAssigner back(FakeContainer&) { return {}; }
  };

} // end namespace AMDiS