Support Class Library
A set of tools providing classes and utility
payload.h
Go to the documentation of this file.
1 #pragma once
4 #include <scl/utils/Either.h>
5 #include <scl/utils/Optional.h>
6 #include <functional>
7 
8 namespace scl{
9  namespace stream{
10  namespace details{
15  template <class T>
17  protected:
21  void ensureGenerated() const{
22  if(!generated){
23  this->alt = gen();
24  generated = true;
25  }
26  }
27 
28  public:
32  using value_type = T;
33 
38 
42  using producer = std::function<alternative(void)>;
43 
48  explicit StreamIteratorPayload(producer prod) : gen{prod}, alt{}{
49  }
50 
55  const alternative& value() const{
57  return alt;
58  }
59 
64  bool isInvalid() const{
66  return !alt.hasValue();
67  }
68 
73  bool isValid() const{
74  return !this->isInvalid();
75  }
76 
82  constexpr static StreamIteratorPayload withValue(const T& value){
83  return StreamIteratorPayload{[=]{
84  return alternative::ref(value);
85  }};
86  }
87 
92  constexpr static StreamIteratorPayload withoutValue(){
93  return StreamIteratorPayload{[]{
94  return scl::utils::none;
95  }};
96  }
97 
98  protected:
104 
109  mutable alternative alt;
110 
115  mutable bool generated = false;
116  };
117  }
118  }
119 }
const alternative & value() const
Retrieve the underlying sum type.
Definition: payload.h:55
Global namespace of the SCL.
Definition: alias.hpp:3
static Optional ref(const value_type &ref)
Construct an optional from a reference to an object.
Definition: Optional.h:109
std::function< alternative(void)> producer
The type of function that produces the alternative.
Definition: payload.h:42
void ensureGenerated() const
Ensure that the payload has been generated before use.
Definition: payload.h:21
StreamIteratorPayload(producer prod)
Construct a payload with its producer function.
Definition: payload.h:48
bool isValid() const
Determine whether or not the underlying sum type stores a value.
Definition: payload.h:73
constexpr None none
A constant global variable of type None.
Definition: Optional.h:41
bool hasValue() const
Determines whether or not this Optional<T> is empty.
Definition: Optional.h:125
bool isInvalid() const
Determine whether or not the underlying sum type store an invalid tag.
Definition: payload.h:64
T value_type
The type of value stored.
Definition: payload.h:32
producer gen
The producer function that gives an optional result.
Definition: payload.h:103
static constexpr StreamIteratorPayload withoutValue()
Create a payload with an invalid tag set.
Definition: payload.h:92
A class representing a stream iterator&#39;s payload.
Definition: payload.h:16
static constexpr StreamIteratorPayload withValue(const T &value)
Create a payload with a value set.
Definition: payload.h:82
alternative alt
The optional result (as a cache)
Definition: payload.h:109
bool generated
Computation cache flag.
Definition: payload.h:115