MarlinMT  0.1.0
QueueElement.h
Go to the documentation of this file.
1 #ifndef MARLINMT_CONCURRENCY_QUEUEELEMENT_h
2 #define MARLINMT_CONCURRENCY_QUEUEELEMENT_h 1
3 
4 // -- std headers
5 #include <memory>
6 #include <future>
7 
8 namespace marlinmt {
9 
10  namespace concurrency {
11 
19  template <typename IN, typename OUT>
20  class QueueElement {
21  public:
22  // no copy
23  QueueElement() = default ;
24  QueueElement( const QueueElement<IN,OUT> & ) = delete ;
25  QueueElement &operator=( const QueueElement<IN,OUT> & ) = delete ;
26 
32  QueueElement( IN && input ) :
33  _input(input) {
34  /* nop */
35  }
36 
41  *this = std::move(rhs) ;
42  }
43 
48  _promise = std::move(rhs._promise) ;
49  _input = std::move(rhs._input) ;
50  return *this ;
51  }
52 
56  std::shared_ptr<std::promise<OUT>> promise() const {
57  return _promise ;
58  }
59 
65  void setValue( OUT && output ) {
66  _promise->set_value( output ) ;
67  }
68 
73  IN takeInput() {
74  return std::move(_input) ;
75  }
76 
77  private:
79  std::shared_ptr<std::promise<OUT>> _promise {std::make_shared<std::promise<OUT>>()} ;
81  IN _input {} ;
82  };
83 
84  template <typename OUT>
85  class QueueElement<void, OUT> {
86  public:
87  // no copy
88  QueueElement( const QueueElement<void,OUT> & ) = delete ;
89  QueueElement &operator=( const QueueElement<void,OUT> & ) = delete ;
90  QueueElement() = default ;
91  QueueElement( QueueElement<void,OUT> &&rhs ) { *this = std::move(rhs) ; }
92  QueueElement &operator=( QueueElement<void,OUT> &&rhs ) { _promise = std::move(rhs._promise) ; return *this ; }
93  std::shared_ptr<std::promise<OUT>> promise() const { return _promise ; }
94  void setValue( OUT && output ) { _promise->set_value( output ) ; }
95  private:
96  std::shared_ptr<std::promise<OUT>> _promise {std::make_shared<std::promise<OUT>>()} ;
97  };
98 
99  template <typename IN>
100  class QueueElement<IN, void> {
101  public:
102  // no copy
103  QueueElement() = default ;
104  QueueElement( const QueueElement<IN,void> & ) = delete ;
105  QueueElement &operator=( const QueueElement<IN,void> & ) = delete ;
106  QueueElement( IN && input ) : _input(input) {}
107  QueueElement( QueueElement<IN,void> &&rhs ) { *this = std::move(rhs) ; }
109  _promise = std::move(rhs._promise) ;
110  _input = std::move(rhs._input) ;
111  return *this ;
112  }
113  std::shared_ptr<std::promise<void>> promise() const { return _promise ; }
114  void setValue() { _promise->set_value() ; }
115  IN takeInput() { return std::move(_input) ; }
116  private:
117  std::shared_ptr<std::promise<void>> _promise {std::make_shared<std::promise<void>>()} ;
118  IN _input {} ;
119  };
120 
121  template <>
122  class QueueElement<void, void> {
123  public:
124  // no copy
125  QueueElement( const QueueElement<void,void> & ) = delete ;
126  QueueElement &operator=( const QueueElement<void,void> & ) = delete ;
127  QueueElement() = default ;
128  QueueElement( QueueElement<void,void> &&rhs ) { *this = std::move(rhs) ; }
129  QueueElement &operator=( QueueElement<void,void> &&rhs ) { _promise = std::move(rhs._promise) ; return *this ; }
130  std::shared_ptr<std::promise<void>> promise() const { return _promise ; }
131  void setValue() { _promise->set_value() ; }
132  private:
133  std::shared_ptr<std::promise<void>> _promise {std::make_shared<std::promise<void>>()} ;
134  };
135 
136  } // end namespace concurrency
137 
138 } // end namespace marlinmt
139 
140 #endif
void setValue(OUT &&output)
Set the value to be retrieved in the future variable.
Definition: QueueElement.h:65
std::shared_ptr< std::promise< void > > promise() const
Definition: QueueElement.h:113
QueueElement(IN &&input)
Constructor with input data.
Definition: QueueElement.h:32
std::shared_ptr< std::promise< void > > promise() const
Definition: QueueElement.h:130
QueueElement & operator=(QueueElement< IN, void > &&rhs)
Definition: QueueElement.h:108
std::shared_ptr< std::promise< OUT > > _promise
< The promise for getting the output data
Definition: QueueElement.h:79
QueueElement & operator=(QueueElement< void, void > &&rhs)
Definition: QueueElement.h:129
QueueElement(QueueElement< IN, void > &&rhs)
Definition: QueueElement.h:107
std::shared_ptr< std::promise< OUT > > promise() const
Definition: QueueElement.h:93
QueueElement & operator=(QueueElement< IN, OUT > &&rhs)
Move assignement operator.
Definition: QueueElement.h:47
QueueElement(QueueElement< void, OUT > &&rhs)
Definition: QueueElement.h:91
QueueElement & operator=(const QueueElement< IN, OUT > &)=delete
std::shared_ptr< std::promise< OUT > > promise() const
Get the output promise.
Definition: QueueElement.h:56
QueueElement(QueueElement< IN, OUT > &&rhs)
Move constructor.
Definition: QueueElement.h:40
IN takeInput()
Take the input data.
Definition: QueueElement.h:73
QueueElement class A template queue element used in the thread pool.
Definition: QueueElement.h:20
QueueElement & operator=(QueueElement< void, OUT > &&rhs)
Definition: QueueElement.h:92
QueueElement(QueueElement< void, void > &&rhs)
Definition: QueueElement.h:128