Fixposition SDK 0.0.0-heads/main-0-g7b59b93
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
app.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: Utilities for apps
12 *
13 * @page FPSDK_COMMON_APPS Utilities for apps
14 *
15 * **API**: fpsdk_common/app.hpp and fpsdk::common::app
16 *
17 */
18#ifndef __FPSDK_COMMON_APP_HPP__
19#define __FPSDK_COMMON_APP_HPP__
20
21/* LIBC/STL */
22#include <cstdint>
23
24/* EXTERNAL */
25
26/* PACKAGE */
27#include "logging.hpp"
28
29namespace fpsdk {
30namespace common {
31/**
32 * @brief Utilities for apps
33 */
34namespace app {
35/* ****************************************************************************************************************** */
36
37/**
38 * @brief Helper to catch SIGINT (CTRL-c)
39 *
40 * On construction this installs a handler for SIGINT. On destruction it sets the handler back to its previous state.
41 * Note that signal handlers are global and therefore you can only use one SigIntHelper in a app.
42 *
43 * Example:
44 *
45 * @code{.cpp}
46 * SigIntHelper sigint;
47 * while (!sigint.ShouldAbort()) {
48 * // do stuff..
49 * }
50 *
51 * if (sigint.ShouldAbort()) {
52 * INFO("We've been asked to stop");
53 * }
54 * @endcode
55 */
57{
58 public:
61 /**
62 * @brief Check if signal was raised and we should abort
63 *
64 * @returns true if signal was raised and we should abort, false otherwise
65 */
67
68 /**
69 * @brief Wait (block) until signal is raised and we should abort
70 *
71 * @param[in] millis Wait at most this long [ms], 0 = forever
72 *
73 * @returns true if the signal was raised, fals if timeout expired
74 */
75 bool WaitAbort(const uint32_t millis = 0);
76};
77
78/**
79 * @brief Helper to print a strack trace on SIGSEGV and SIGABRT
80 *
81 * On construction this installs a handler for SIGSEGV and SIGABRT, which prints a stack trace.
82 * Note that signal handlers are global and therefore you can only use one StacktraceHelper in a app.
83 * It is probably a good idea to only include this in non-Release builds.
84 *
85 * Example:
86 *
87 * @code{.cpp}
88 * int main(int, char**) {
89 * #ifndef NDEBUG
90 * StacktraceHelper stacktrace;
91 * #endif
92 *
93 * // Do stuff...
94 *
95 * return 0;
96 * }
97 * @endcode
98 */
100{
101 public:
104};
105
106/**
107 * @brief Prints a stacktrace to stderr
108 */
110
111/**
112 * @brief Program options
113 */
115{
116 public:
117 /**
118 * @brief A program option
119 */
120 struct Option
121 {
122 char flag; //!< The flag (reserved: 'h', 'V', 'v', 'q', '?', '*', ':')
123 bool has_argument; //!< True if flag requires an an argument, false if not
124 };
125
126 /**
127 * @brief Constructor
128 */
129 ProgramOptions(const std::string& app_name, const std::vector<Option>& options);
130
131 /**
132 * @brief Destructor
133 */
135
136 /**
137 * @brief Load arguments from argv[]
138 *
139 * @param[in,out] argc Number of arguments
140 * @param[in,out] argv Command-line arguments
141 * @return
142 */
143 bool LoadFromArgv(int argc, char** argv);
144
145 /**
146 * @brief Print the help screen and exit(0)
147 */
148 virtual void PrintHelp() = 0;
149
150 /**
151 * @brief Handle a command-line flag argument
152 *
153 * @param[in] option The option
154 * @param[in] argument Optional argument
155 *
156 * @returns true if option was accepted, false otherwise
157 */
158 virtual bool HandleOption(const Option& option, const std::string& argument) = 0;
159
160 /**
161 * @brief Check options, and handle non-flag arguments
162 *
163 * @param[in] args The non-flag arguments
164 *
165 * @returns true if options are good, false otherwise
166 */
167 virtual bool CheckOptions(const std::vector<std::string>& args)
168 {
169 (void)args;
170 return true;
171 }
172
173 //! Help screen for common options @hideinitializer
174 static constexpr const char* COMMON_FLAGS_HELP = /* clang-format off */
175 " -h -- Print program help screen, and exit\n"
176 " -V -- Print program, version and license information, and exit\n"
177 " -v / -q -- Increase / decrease logging verbosity, multiple flags accumulate\n"; // clang-format on
178
179 std::string app_name_; //!< App name
181 std::vector<std::string> argv_; //!< argv[] of program
182
183 private:
184 std::vector<Option> options_; //!< Program options
185 void PrintVersion();
186};
187
188/* ****************************************************************************************************************** */
189} // namespace app
190} // namespace common
191} // namespace fpsdk
192#endif // __FPSDK_COMMON_APP_HPP__
std::vector< std::string > argv_
argv[] of program
Definition app.hpp:181
virtual void PrintHelp()=0
Print the help screen and exit(0)
std::string app_name_
App name.
Definition app.hpp:179
ProgramOptions(const std::string &app_name, const std::vector< Option > &options)
Constructor.
virtual bool CheckOptions(const std::vector< std::string > &args)
Check options, and handle non-flag arguments.
Definition app.hpp:167
bool LoadFromArgv(int argc, char **argv)
Load arguments from argv[].
static constexpr const char * COMMON_FLAGS_HELP
Help screen for common options.
Definition app.hpp:174
virtual bool HandleOption(const Option &option, const std::string &argument)=0
Handle a command-line flag argument.
logging::LoggingLevel logging_
Logging verbosity level.
Definition app.hpp:180
virtual ~ProgramOptions()
Destructor.
Helper to catch SIGINT (CTRL-c)
Definition app.hpp:57
bool ShouldAbort()
Check if signal was raised and we should abort.
bool WaitAbort(const uint32_t millis=0)
Wait (block) until signal is raised and we should abort.
Helper to print a strack trace on SIGSEGV and SIGABRT.
Definition app.hpp:100
Fixposition SDK: Logging.
void PrintStacktrace()
Prints a stacktrace to stderr.
LoggingLevel
Logging verbosity levels, default is INFO.
Definition logging.hpp:152
@ INFO
[6/info] Interesting stuff, the default level (for apps)
Fixposition SDK.
Definition fpsdk_doc.hpp:20
bool has_argument
True if flag requires an an argument, false if not.
Definition app.hpp:123
char flag
The flag (reserved: 'h', 'V', 'v', 'q', '?', '*', ':')
Definition app.hpp:122