KiCad PCB EDA Suite
Loading...
Searching...
No Matches
coroutines.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <functional>
21
22#include <tool/coroutine.h>
23
25
26#include <common.h>
27
28#include <wx/cmdline.h>
29
30#include <cstdio>
31#include <string>
32
33
35
44{
45public:
46 CoroutineExample( int aCount ) : m_count( aCount )
47 {
48 }
49
50 int CountTo( int n )
51 {
52 for( int i = 1; i <= n; i++ )
53 {
54 m_cofunc->KiYield( i );
55 }
56
57 return 0;
58 }
59
60 void Run()
61 {
62 m_cofunc = std::make_unique<MyCoroutine>( this, &CoroutineExample::CountTo );
63 m_cofunc->Call( m_count );
64
65 while( m_cofunc->Running() )
66 {
67 m_cofunc->Resume();
68 }
69 }
70
71 std::unique_ptr<MyCoroutine> m_cofunc;
73};
74
75
76static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
77 {
78 wxCMD_LINE_SWITCH,
79 "h",
80 "help",
81 _( "displays help on the command line parameters" ).mb_str(),
82 wxCMD_LINE_VAL_NONE,
83 wxCMD_LINE_OPTION_HELP,
84 },
85 {
86 wxCMD_LINE_OPTION,
87 "c",
88 "count",
89 _( "how high to count" ).mb_str(),
90 wxCMD_LINE_VAL_NUMBER,
91 wxCMD_LINE_PARAM_OPTIONAL,
92 },
93 { wxCMD_LINE_NONE }
94};
95
96
97static int coroutine_main_func( int argc, char** argv )
98{
99 wxCmdLineParser cl_parser( argc, argv );
100 cl_parser.SetDesc( g_cmdLineDesc );
101 cl_parser.AddUsageText( _( "Test a simple coroutine that yields a given number of times" ) );
102
103 int cmd_parsed_ok = cl_parser.Parse();
104 if( cmd_parsed_ok != 0 )
105 {
106 // Help and invalid input both stop here
107 return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
108 }
109
110 long count = 5;
111 cl_parser.Found( "count", &count );
112
113 CoroutineExample obj( (int) count );
114
115 obj.Run();
116
118}
119
120
121/*
122 * Define the tool interface
123 */
125 "coroutine",
126 "Test a simple coroutine",
128} );
Implement a coroutine.
Definition coroutine.h:80
A simple harness that counts to a preset value in a couroutine, yielding each value.
CoroutineExample(int aCount)
std::unique_ptr< MyCoroutine > m_cofunc
int CountTo(int n)
static bool Register(const KI_TEST::UTILITY_PROGRAM &aProgInfo)
Register a utility program factory function against an ID string.
The common library.
static bool registered
COROUTINE< int, int > MyCoroutine
static int coroutine_main_func(int argc, char **argv)
static const wxCmdLineEntryDesc g_cmdLineDesc[]
#define _(s)
@ OK
Tool exited OK.
@ BAD_CMDLINE
The command line was not correct for the tool.