robot_cpp/include/robot/init.h
2026-01-31 17:20:07 +07:00

135 lines
3.5 KiB
C++

#ifndef ROBOT_INIT_H_INCLUDED
#define ROBOT_INIT_H_INCLUDED
#include <atomic>
#include <csignal>
#include <string>
namespace robot
{
/**
* @brief Check if the robot system is still running
*
* Similar to ros::ok(), this function returns false when the system
* has finished shutting down. Use this in main loops and thread loops
* to check if the system should continue running.
*
* @return true if system is running, false if shutdown is complete
*
* @note This returns false only after shutdown is complete, not when
* shutdown is requested. Use isShuttingDown() to check if shutdown
* has been requested.
*
* Example:
* @code
* while (robot::ok())
* {
* // Do work
* }
* @endcode
*/
bool ok();
/**
* @brief Check if shutdown has been requested
*
* Similar to ros::isShuttingDown(), this function returns true as soon
* as shutdown() is called, not when shutdown is complete. Use this in
* long-running callbacks to check if they should exit early.
*
* @return true if shutdown has been requested, false otherwise
*
* @note This returns true immediately when shutdown() is called, while
* ok() returns false only after shutdown is complete.
*
* Example:
* @code
* void longCallback()
* {
* for (int i = 0; i < 1000000; i++)
* {
* if (robot::isShuttingDown())
* return; // Exit early
* // Do work...
* }
* }
* @endcode
*/
bool isShuttingDown();
/**
* @brief Shutdown the robot system
*
* Similar to ros::shutdown(), this function initiates the shutdown process.
* After calling this:
* - isShuttingDown() will return true immediately
* - ok() will return false after shutdown is complete
*
* @note This function is thread-safe and can be called multiple times.
* Subsequent calls have no effect.
*
* Example:
* @code
* // Custom signal handler
* void mySigintHandler(int sig)
* {
* // Do custom cleanup
* robot::shutdown();
* }
* @endcode
*/
void shutdown();
/**
* @brief Initialize the robot system
*
* Similar to ros::init(), this function initializes the robot system.
* It sets up signal handlers and initializes internal state.
*
* @param argc Number of command line arguments (can be modified)
* @param argv Command line arguments (can be modified)
* @param node_name Name of the node (optional, for logging purposes)
* @param install_sigint_handler If true, install SIGINT handler (default: true)
*
* @note If install_sigint_handler is true, Ctrl-C will automatically
* call shutdown(). Set to false if you want custom signal handling.
*
* Example:
* @code
* int main(int argc, char** argv)
* {
* robot::init(argc, argv, "my_node");
* // ... use robot system ...
* return 0;
* }
* @endcode
*/
void init(int& argc, char** argv, const std::string& node_name = "", bool install_sigint_handler = true);
/**
* @brief Initialize the robot system without command line arguments
*
* Simplified version of init() that doesn't require argc/argv.
*
* @param node_name Name of the node (optional, for logging purposes)
* @param install_sigint_handler If true, install SIGINT handler (default: true)
*/
void init(const std::string& node_name = "", bool install_sigint_handler = true);
namespace detail
{
/**
* @brief Internal signal handler for SIGINT
*
* This is called automatically when SIGINT is received (Ctrl-C).
* It calls shutdown() to gracefully shut down the system.
*/
void sigintHandler(int sig);
}
} // namespace robot
#endif // ROBOT_INIT_H_INCLUDED