MEL
Microthread & Execution library
ExFuture.h
1 #pragma once
2 /*
3  * SPDX-FileCopyrightText: 2022 Daniel Barrientos <danivillamanin@gmail.com>
4  *
5  * SPDX-License-Identifier: MIT
6  */
7 #include <core/Future.h>
8 namespace mel
9 {
10  namespace execution
11  {
12  using mel::core::Future;
13  template <class ExecutorAgent> class Executor; // predeclarationj
19  template <typename ExecutorAgent, typename ResultType>
20  class ExFuture : public Future<ResultType>
21  {
22  public:
23  ExFuture() = default;
24  ExFuture( const ExFuture& ob ) noexcept
25  : Future<ResultType>( ob ), agent( ob.agent )
26  {
27  }
28  ExFuture( ExFuture&& ob ) noexcept
29  : Future<ResultType>( std::move( ob ) ),
30  agent( std::move( ob.agent ) )
31  {
32  }
33  ExFuture( Executor<ExecutorAgent> aEx ) noexcept : agent( aEx ) {}
34  ExFuture( Executor<ExecutorAgent> aEx, const ResultType& val )
35  : Future<ResultType>( val ), agent( aEx )
36  {
37  }
38  ExFuture( Executor<ExecutorAgent> aEx, ResultType&& val )
39  : Future<ResultType>( std::move( val ) ), agent( aEx )
40  {
41  }
42 
43  ExFuture& operator=( const ExFuture& f ) noexcept
44  {
46  agent = f.agent;
47  return *this;
48  };
49  ExFuture& operator=( ExFuture&& f ) noexcept
50  {
51  Future<ResultType>::operator=( std::move( f ) );
52  agent = std::move( f.agent );
53  return *this;
54  };
57  };
58 
60  // for reference type
61  template <typename ExecutorAgent, typename ResultType>
62  class ExFuture<ExecutorAgent, ResultType&> : public Future<ResultType&>
63  {
64  public:
65  ExFuture() = default;
66  ExFuture( const ExFuture& ob )
67  : Future<ResultType&>( ob ), agent( ob.agent )
68  {
69  }
70  ExFuture( ExFuture&& ob )
71  : Future<ResultType&>( std::move( ob ) ),
72  agent( std::move( ob.agent ) )
73  {
74  }
75  ExFuture( Executor<ExecutorAgent> aEx ) : agent( aEx ) {}
76  ExFuture( Executor<ExecutorAgent> aEx, ResultType& val )
77  : Future<ResultType&>( val ), agent( aEx )
78  {
79  }
80 
81  ExFuture& operator=( const ExFuture& f )
82  {
83  Future<ResultType&>::operator=( f );
84  agent = f.agent;
85  return *this;
86  };
87  ExFuture& operator=( ExFuture&& f )
88  {
89  Future<ResultType&>::operator=( std::move( f ) );
90  agent = std::move( f.agent );
91  return *this;
92  };
93  Executor<ExecutorAgent>
94  agent;
95  };
96  // specialization for void
97  template <typename ExecutorAgent>
98  class ExFuture<ExecutorAgent, void> : public Future<void>
99  {
100  public:
101  ExFuture() = default;
102  ExFuture( const ExFuture& ob )
103  : Future<void>( ob ), agent( ob.agent )
104  {
105  }
106  ExFuture( ExFuture&& ob )
107  : Future<void>( std::move( ob ) ),
108  agent( std::move( ob.agent ) )
109  {
110  }
111  ExFuture( Executor<ExecutorAgent> aEx ) : agent( aEx ) {}
112  ExFuture( Executor<ExecutorAgent> aEx, int dummy )
113  : Future<void>( dummy ), agent( aEx )
114  {
115  }
116  ExFuture& operator=( const ExFuture& f )
117  {
118  Future<void>::operator=( f );
119  agent = f.agent;
120  return *this;
121  };
122  ExFuture& operator=( ExFuture&& f )
123  {
124  Future<void>::operator=( std::move( f ) );
125  agent = std::move( f.agent );
126  return *this;
127  };
128 
129  Executor<ExecutorAgent>
130  agent;
131  };
133  } // namespace execution
134 } // namespace mel
Represents a value that maybe is not present at the current moment.
Definition: Future.h:750
Extension of mel::core::Future to apply to executors.
Definition: ExFuture.h:21
Executor< ExecutorAgent > agent
execution agent associated with this instance
Definition: ExFuture.h:54
Definition: Executor.h:26
Definition: Callback_Impl.h:11