Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Functor.h
Go to the documentation of this file.
1 
8 #ifndef INCLUDE_TEMPLE_FUNCTOR_H
9 #define INCLUDE_TEMPLE_FUNCTOR_H
10 
11 #include <algorithm>
12 #include <stdexcept>
14 
15 namespace Scine {
16 namespace Molassembler {
17 namespace Temple {
18 
20 namespace Functor {
21 
25 struct Identity {
27  template<typename U>
28  constexpr auto operator()(U&& v) const noexcept {
29  return std::forward<U>(v);
30  }
31 };
32 
38 template<class Container>
39 struct At : public Binding<Container> {
42  // Bring constructor into scope
43  using ContainerBinding::ContainerBinding;
44 
46  template<typename T>
47  auto operator() (const T& index) const {
48  return ContainerBinding::value.at(index);
49  }
50 };
51 
57 template<class Container>
58 auto at(Container&& container) {
59  return At<Container&&> {std::forward<Container>(container)};
60 }
61 
63 template<class Container>
64 struct IndexIn : public Binding<Container> {
67  // Bring constructor into scope
68  using ContainerBinding::ContainerBinding;
69 
76  template<typename T>
77  auto operator() (const T& item) const {
78  auto findIter = std::find(
79  std::begin(ContainerBinding::value),
80  std::end(ContainerBinding::value),
81  item
82  );
83 
84  if(findIter == std::end(ContainerBinding::value)) {
85  throw std::out_of_range("Item not found in container");
86  }
87 
88  return findIter - std::begin(ContainerBinding::value);
89  }
90 };
91 
97 template<class Container>
98 auto indexIn(Container&& container) {
99  return IndexIn<Container&&> {std::forward<Container>(container)};
100 }
101 
103 template<unsigned i>
104 struct Get {
106  template<typename T>
107  constexpr auto operator() (const T& t) const {
108  return std::get<i>(t);
109  }
110 };
111 
119 template<unsigned i>
120 constexpr auto get() { return Get<i> {}; }
121 
123 constexpr Get<0> first;
125 constexpr Get<1> second;
126 
127 // TODO this doesn't curry, it chains
128 template<typename UnaryF, typename UnaryG>
129 struct Currier {
130  Currier(UnaryF&& outer, UnaryG&& inner) : f(outer), g(inner) {}
131 
132  template<typename T>
133  auto operator() (T&& t) -> decltype(auto) {
134  return f(g(t));
135  }
136 
137  UnaryF f;
138  UnaryG g;
139 };
140 
141 template<typename UnaryF, typename UnaryG>
142 auto curry(UnaryF&& outer, UnaryG&& inner) {
143  return Currier<UnaryF, UnaryG>(std::forward<UnaryF>(outer), std::forward<UnaryG>(inner));
144 }
145 
146 } // namespace Functor
147 } // namespace Temple
148 } // namespace Molassembler
149 } // namespace Scine
150 
151 #endif
constexpr auto operator()(const T &t) const
Call std::get&lt;i&gt; on the argument.
Definition: Functor.h:107
auto at(Container &&container)
Make functor calling at on arguments.
Definition: Functor.h:58
Provides an identity functor.
constexpr Get< 1 > second
Calls std::get&lt;1&gt; on any argument it is invoked with.
Definition: Functor.h:125
Metafunction required for default arguments.
Definition: Functor.h:25
Metafunction calling at on a bound container.
Definition: Functor.h:39
Metafunction determining the index of an item in a bound container.
Definition: Functor.h:64
constexpr Get< 0 > first
Calls std::get&lt;0&gt; on any argument it is invoked with.
Definition: Functor.h:123
constexpr auto operator()(U &&v) const noexcept
Returns its arguments (perfect forwarding)
Definition: Functor.h:28
auto operator()(const T &item) const
Determine index of an item in the container.
Definition: Functor.h:77
Type that will own rvalues, reference lvalues.
Definition: Binding.h:37
auto operator()(const T &index) const
Return value in container at specified position.
Definition: Functor.h:47
Metafunction calling std::get on an argument with a bound index.
Definition: Functor.h:104
auto indexIn(Container &&container)
Make functor determining the index of an item in a bound container.
Definition: Functor.h:98
auto find(const Container &container, const T &needle)
std::find shorthand
Definition: Functional.h:346