ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkPipeline.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkPipeline_h
19#define itkPipeline_h
20
21#include <CLI/App.hpp>
22#include <CLI/Config.hpp>
23#include <CLI/Error.hpp>
24
25#include "itkMacro.h"
26#include "itkImage.h"
27#include "itkVectorImage.h"
28
29#include "glaze/glaze.hpp"
30
31#include "WebAssemblyInterfaceExport.h"
32
33
34// Short circuit help output without raising an exception (currently not
35// available in WASI)
36#define ITK_WASM_PARSE(pipeline) \
37 try \
38 { \
39 const auto iwpArgc = (pipeline).get_argc(); \
40 const auto iwpArgv = (pipeline).get_argv(); \
41 for (int ii = 0; ii < iwpArgc; ++ii) \
42 { \
43 const std::string arg(iwpArgv[ii]); \
44 if (arg == "-h" || arg == "--help") \
45 { \
46 (pipeline).exit(CLI::CallForAllHelp()); \
47 std::exit(0); \
48 } \
49 if (arg == "--interface-json") \
50 { \
51 (pipeline).interface_json(); \
52 std::exit(0); \
53 } \
54 if (arg == "--version") \
55 { \
56 std::cout << "Version: " << (pipeline).version() << std::endl; \
57 std::exit(0); \
58 } \
59 } \
60 (pipeline).parse(); \
61 } \
62 catch (const CLI::ParseError & e) \
63 { \
64 return (pipeline).exit(e); \
65 }
66
67// Parse options while allowing extra flags, not exiting with help flags, and clearing parse state after finished.
68// Use this to parse some positionals or options before all options have been added.
69// WARNING: It is best to only add the pre-parse options and read them through ITK_WASM_PRE_PARSE before adding other
70// options, as you may face issues(EXCEPTIONS) generating bindings (bindgen), if you add "required" options/flags before
71// the PRE_PARSE.
72#define ITK_WASM_PRE_PARSE(pipeline) \
73 try \
74 { \
75 (pipeline).set_help_flag(); \
76 (pipeline).allow_extras(true); \
77 (pipeline).parse(); \
78 } \
79 catch (const CLI::CallForHelp & e) \
80 {} \
81 catch (const CLI::CallForAllHelp & e) \
82 {} \
83 catch (const CLI::ParseError & e) \
84 { \
85 return (pipeline).exit(e); \
86 } \
87 (pipeline).allow_extras(false); \
88 (pipeline).set_help_flag("-h,--help", "Print this help message and exit"); \
89 (pipeline).clear();
90
91#define ITK_WASM_CATCH_EXCEPTION(pipeline, command) \
92 try \
93 { \
94 command; \
95 } \
96 catch (const itk::ExceptionObject & excp) \
97 { \
98 std::ostringstream ostrm; \
99 ostrm << excp; \
100 ostrm << " In " __FILE__ ", line " << __LINE__ << std::endl; \
101 CLI::Error err("Runtime error", ostrm.str(), 1); \
102 return (pipeline).exit(err); \
103 } \
104 catch (const std::exception & excp) \
105 { \
106 std::ostringstream ostrm; \
107 ostrm << excp.what(); \
108 ostrm << " In " __FILE__ ", line " << __LINE__ << std::endl; \
109 CLI::Error err("Runtime error", ostrm.str(), 1); \
110 return (pipeline).exit(err); \
111 }
112
113namespace itk
114{
115namespace wasm
116{
117
118// Importing into the itk namespace the main classes from CLI11
119using CLI::App;
120using CLI::ExistingDirectory;
121using CLI::ExistingFile;
122using CLI::ExitCodes;
123using CLI::FileError;
124using CLI::NonexistentPath;
125using CLI::Option;
126using CLI::ParseError;
127using CLI::Success;
128using CLI::Config;
129
137template <typename TIteratorable>
138glz::json_t::array_t
139getArrayJson(TIteratorable container)
140{
141 glz::json_t::array_t value(container.begin(), container.end());
142 return value;
143}
144
145class WebAssemblyInterface_EXPORT Pipeline : public CLI::App
146{
147public:
149 Pipeline(std::string name, std::string description, int argc, char ** argv);
150
152 auto
153 exit(const CLI::Error & e) -> int;
154
155 void
157 {
158 CLI::App::parse(m_argc, m_argv);
159 }
160
161 static auto
163 {
164 return m_UseMemoryIO;
165 }
166
167 int
168 get_argc() const
169 {
170 return m_argc;
171 }
172
173 char **
174 get_argv() const
175 {
176 return m_argv;
177 }
178
179 void
180 set_version(const char * version)
181 {
183 }
184
185 std::string
186 version() const
187 {
188 return m_Version;
189 }
190
191 void
193
194 ~Pipeline() override;
195
196private:
197 static bool m_UseMemoryIO;
199 char ** m_argv;
200 std::string m_Version;
201};
202
203
204} // end namespace wasm
205} // end namespace itk
206
207#endif
int get_argc() const
static auto get_use_memory_io()
Pipeline(std::string name, std::string description, int argc, char **argv)
void set_version(const char *version)
std::string version() const
char ** get_argv() const
~Pipeline() override
auto exit(const CLI::Error &e) -> int
static bool m_UseMemoryIO
std::string m_Version
glz::json_t::array_t getArrayJson(TIteratorable container)
Create a glaze arrat_t value from an STL style container.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....