KiCad PCB EDA Suite
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 (C) 2018-2020 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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <functional>
25 
26 #include <tool/coroutine.h>
27 
29 
30 #include <common.h>
31 
32 #include <wx/cmdline.h>
33 
34 #include <cstdio>
35 #include <string>
36 
37 
39 
48 {
49 public:
50  CoroutineExample( int aCount ) : m_count( aCount )
51  {
52  }
53 
54  int CountTo( int n )
55  {
56  for( int i = 1; i <= n; i++ )
57  {
58  m_cofunc->KiYield( i );
59  }
60 
61  return 0;
62  }
63 
64  void Run()
65  {
66  m_cofunc = std::make_unique<MyCoroutine>( this, &CoroutineExample::CountTo );
67  m_cofunc->Call( m_count );
68 
69  while( m_cofunc->Running() )
70  {
71  m_cofunc->Resume();
72  }
73  }
74 
75  std::unique_ptr<MyCoroutine> m_cofunc;
76  int m_count;
77 };
78 
79 
80 static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
81  {
82  wxCMD_LINE_SWITCH,
83  "h",
84  "help",
85  _( "displays help on the command line parameters" ).mb_str(),
86  wxCMD_LINE_VAL_NONE,
87  wxCMD_LINE_OPTION_HELP,
88  },
89  {
90  wxCMD_LINE_OPTION,
91  "c",
92  "count",
93  _( "how high to count" ).mb_str(),
94  wxCMD_LINE_VAL_NUMBER,
95  wxCMD_LINE_PARAM_OPTIONAL,
96  },
97  { wxCMD_LINE_NONE }
98 };
99 
100 
101 static int coroutine_main_func( int argc, char** argv )
102 {
103  wxCmdLineParser cl_parser( argc, argv );
104  cl_parser.SetDesc( g_cmdLineDesc );
105  cl_parser.AddUsageText( _( "Test a simple coroutine that yields a given number of times" ) );
106 
107  int cmd_parsed_ok = cl_parser.Parse();
108  if( cmd_parsed_ok != 0 )
109  {
110  // Help and invalid input both stop here
111  return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
112  }
113 
114  long count = 5;
115  cl_parser.Found( "count", &count );
116 
117  CoroutineExample obj( (int) count );
118 
119  obj.Run();
120 
121  return KI_TEST::RET_CODES::OK;
122 }
123 
124 
125 /*
126  * Define the tool interface
127  */
129  "coroutine",
130  "Test a simple coroutine",
132 } );
int CountTo(int n)
Definition: coroutines.cpp:54
Implement a coroutine.
Definition: coroutine.h:75
COROUTINE< int, int > MyCoroutine
Definition: coroutines.cpp:38
#define OK
The command line was not correct for the tool.
static bool registered
Definition: coroutines.cpp:128
static const wxCmdLineEntryDesc g_cmdLineDesc[]
Definition: coroutines.cpp:80
CoroutineExample(int aCount)
Definition: coroutines.cpp:50
static bool Register(const KI_TEST::UTILITY_PROGRAM &aProgInfo)
Register a utility program factory function against an ID string.
std::unique_ptr< MyCoroutine > m_cofunc
Definition: coroutines.cpp:75
#define _(s)
A simple harness that counts to a preset value in a couroutine, yielding each value.
Definition: coroutines.cpp:47
static int coroutine_main_func(int argc, char **argv)
Definition: coroutines.cpp:101
The common library.