-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCoTaskWithReturn.cpp
More file actions
99 lines (72 loc) · 1.7 KB
/
SimpleCoTaskWithReturn.cpp
File metadata and controls
99 lines (72 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright (C) 2025 Martin Pietsch <@pmfoss>
SPDX-License-Identifier: BSD-3-Clause */
#include <iostream>
#include <CoTask.h>
using namespace CoRoutines;
CoTask<int, int, int> addPositivNumbers(int pStart, int pMax)
{
if(pStart < 0)
{
co_return -1;
}
if(pStart >= pMax)
{
co_return -2;
}
int lValue = pStart;
while(true)
{
int lSummand = co_await int{};
if(lSummand < 0)
{
co_return -3;
}
lValue += lSummand;
if(lValue > pMax)
{
co_return 0;
}
co_yield lValue;
}
}
void printTaskReturnCode(CoTask<int, int, int>& pTask)
{
std::cout << "return code of task: ";
if(pTask.done())
{
std::cout << pTask.returnValue() << "\n";
}
else
{
std::cout << "The task is still running.\n";
}
}
int main()
{
/*sucessful task execution*/
CoTask lTask1 = addPositivNumbers(2,40);
for(int i = 0; i < 10; ++i)
{
int lValue = lTask1.run(i);
if(lTask1.done())
{
break;
}
std::cout << lValue << " ";
}
std::cout << "\n";
printTaskReturnCode(lTask1);
/*failed task execution with a negativ starting value*/
CoTask lTask2 = addPositivNumbers(-2,40);
lTask2.run(2);
printTaskReturnCode(lTask2);
/*failed task execution with a start value that is too large*/
CoTask lTask3 = addPositivNumbers(40,2);
lTask3.run(2);
printTaskReturnCode(lTask3);
/*failed task execution with a negativ running value*/
CoTask lTask4 = addPositivNumbers(2,40);
lTask4.run(-2);
printTaskReturnCode(lTask4);
return 0;
}