Silicium
then.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_THEN_HPP
2 #define SILICIUM_THEN_HPP
3 
4 #include <utility>
5 #include <type_traits>
6 
7 namespace Si
8 {
9  namespace detail
10  {
11  template <class T>
12  void default_construct(std::true_type)
13  {
14  }
15 
16  template <class T>
17  T default_construct(std::false_type)
18  {
19  return T{};
20  }
21  }
22 
23  template <class T>
25  {
26  return detail::default_construct<T>(std::is_same<T, void>());
27  }
28 
29  namespace detail
30  {
31  template <class Error>
32  struct then_impl
33  {
34  Error operator()() const
35  {
36  return Error();
37  }
38 
39  template <class First, class ...Tail>
40  Error operator()(First &&first, Tail &&...tail) const
41  {
42  auto error = std::forward<First>(first)();
43  if (error)
44  {
45  return error;
46  }
47  return (*this)(std::forward<Tail>(tail)...);
48  }
49  };
50 
51  template <>
52  struct then_impl<void>
53  {
54  void operator()() const
55  {
56  }
57 
58  template <class First, class ...Tail>
59  void operator()(First &&first, Tail &&...tail) const
60  {
61  std::forward<First>(first)();
62  return (*this)(std::forward<Tail>(tail)...);
63  }
64  };
65  }
66 
67  template <class First, class ...Sequence>
68  auto then(First &&first, Sequence &&...actions)
69  -> decltype(std::forward<First>(first)())
70  {
71  typedef decltype(std::forward<First>(first)()) result_type;
72  return detail::then_impl<result_type>()(std::forward<First>(first), std::forward<Sequence>(actions)...);
73  }
74 }
75 
76 #endif
auto then(First &&first, Sequence &&...actions) -> decltype(std::forward< First >(first)())
Definition: then.hpp:68
Definition: variant.hpp:176
Definition: absolute_path.hpp:19
void operator()() const
Definition: then.hpp:54
Error operator()(First &&first, Tail &&...tail) const
Definition: then.hpp:40
Error operator()() const
Definition: then.hpp:34
T default_construct()
Definition: then.hpp:24
void operator()(First &&first, Tail &&...tail) const
Definition: then.hpp:59
void default_construct(std::true_type)
Definition: then.hpp:12
Definition: then.hpp:32