pure-cpp 1.0.0
A C++ physics simulation benchmark comparing performance with Python implementations
main.cpp
Go to the documentation of this file.
1/**
2 * \file main.cpp
3 * \brief Main entry point for the n-body simulation benchmark.
4 * \author Le Bars, Yoann
5 *
6 * This file is part of the pure C++ benchmark.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation, either version 3 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <https://www.gnu.org/licenses/>.
20 *
21 * This program simulates the gravitational interaction of multiple bodies in 3D
22 * space.
23 *
24 * For a full list of command-line options and their descriptions, run the
25 * executable with the `--help` flag:
26 * \code{.sh}
27 * ./pure-cpp --help
28 * \endcode
29 */
30
31#include <QApplication>
32#include <QDebug>
33#include <QObject>
34#include <boost/program_options.hpp>
35#include <exception>
36
37#include "app_manager.hpp"
38#include "app_profiler.hpp"
39#include "cmd_line_parser.hpp"
40
41/**
42 * \brief Namespace for application-wide utility functions, primarily for
43 * exception handling.
44 */
45namespace AppUtils {
46 /**
47 * \brief Handles logging an exception message and returning the appropriate
48 * exit code.
49 *
50 * \param message The translated error message to be logged.
51 * \param code The exit code to be returned.
52 *
53 * \return The integer value of the exit code.
54 */
55 int handleException(const QString& message, CmdLine::ExitCode code) {
56 qCritical().noquote() << message;
57 return static_cast<int>(code);
58 }
59
60 // Overload for exceptions that don’t have a .what() message.
61 /**
62 * \brief Overload for handling exceptions with a simple C-string message.
63 *
64 * \param message The non-translated error message.
65 * \param code The exit code to be returned.
66 *
67 * \return The integer value of the exit code.
68 */
69 int handleException(const char* message, CmdLine::ExitCode code) {
70 // Use the QObject::tr context for translation.
71 return handleException(QObject::tr(message), code);
72 }
73}
74
75/**
76 * \brief The main entry point for the application.
77 *
78 * \param argc Count of arguments transmitted to the program.
79 * \param argv Values of arguments transmitted to the program.
80 *
81 * \return 0 if everything went well,
82 * otherwise, a negative value corresponding to an ExitCode.
83 */
84int main(int argc, char* argv[]) {
85 // The QApplication must be created first, as it initializes Qt's core.
87 QApplication app(argc, argv);
89
90 /* This top-level try-catch block acts as a final exception boundary for
91 the entire application. */
92 try {
93 App::AppManager manager; // NOLINT
94 if (!manager.setup()) {
95 return static_cast<int>(CmdLine::ExitCode::Success);
96 }
97 return manager.run();
98 } catch (const po::error& e) {
99 // This catch block is now mainly for errors during parsing itself.
101 QObject::tr("Command line error: %1").arg(e.what()),
102 CmdLine::ExitCode::CmdLineError);
103 } catch (const std::out_of_range& e) {
105 QObject::tr("Out of range index error: "
106 "%1")
107 .arg(e.what()),
108 CmdLine::ExitCode::OutOfRange);
109 } catch (const std::bad_alloc& e) {
110 return AppUtils::handleException("Error: not enough memory.",
111 CmdLine::ExitCode::BadAlloc);
112 } catch (const std::exception& e) {
114 QObject::tr("An unexpected error occurred: "
115 "%1")
116 .arg(e.what()),
117 CmdLine::ExitCode::StdException);
118 } catch (...) {
119 return AppUtils::handleException("An unknown exception occurred.",
120 CmdLine::ExitCode::UnknownException);
121 }
122}
Global application profiling instrumentation.
Orchestrates the application's lifecycle: setup, execution, and cleanup.
Definition: app_manager.hpp:25
int run()
Runs the main application event loop.
Definition: app_manager.cpp:73
bool setup()
Sets up all application components before running.
Definition: app_manager.cpp:39
static void startQtInit()
Starts timing Qt initialization.
static void stopQtInit()
Stops timing Qt initialization.
Command-line argument parsing and validation.
int main(int argc, char *argv[])
The main entry point for the application.
Definition: main.cpp:84
Namespace for application-wide utility functions, primarily for exception handling.
int handleException(const QString &message, CmdLine::ExitCode code)
Handles logging an exception message and returning the appropriate exit code.
Definition: main.cpp:55
ExitCode
Exit codes for the application.