Pogosim
Loading...
Searching...
No Matches
utils.h File Reference
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/fmt/ostr.h>
#include <box2d/box2d.h>
#include <random>
#include <algorithm>
#include "configuration.h"
#include <vector>

Go to the source code of this file.

Macros

#define _USE_MATH_DEFINES

Typedefs

template<class T>
using span_t = const std::vector<T>&

Functions

template<typename T>
constexpr bool contains (const std::vector< T > &vec, const T &value)
 Check whether a vector contains a given value.
constexpr bool all_true (const std::vector< bool > &flags)
 Check whether every element in a std::vector<bool> is true.
void init_logger (Configuration &config)
 Initializes the global loggers.
void loggers_add_file_sink (std::string const &filename)
 Adds a file sink to the global loggers.
bool string_to_bool (std::string const &str)
 Converts a string to a boolean value.
std::string to_lowercase (std::string const &str)
 Converts a string to lowercase.
void ensure_directories_exist (const std::string &filename)
 Ensures that the directory for a given file exists.
void delete_files_with_extension (const std::string &path, const std::string &extension, bool recursive=false)
 Deletes files with a specified extension in a given directory.
std::string resolve_path (const std::string &inputPath)
 Resolves an input file path to an absolute path.

Variables

std::shared_ptr< spdlog::logger > glogger
 Global logger for general logging.
std::shared_ptr< spdlog::logger > robotlogger
 Global logger for robot-specific logging.
std::random_device rd
 Random device used for seeding the random generator.
std::mt19937 rnd_gen
 Random number generator seeded with rd.

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

Typedef Documentation

◆ span_t

template<class T>
using span_t = const std::vector<T>&

Function Documentation

◆ all_true()

bool all_true ( const std::vector< bool > & flags)
nodiscardconstexpr

Check whether every element in a std::vector<bool> is true.

Uses std::ranges::all_of to test each element. Stops early on the first false, so the complexity is O(n) in the worst case, O(1) if the first value is false.

Parameters
flagsVector of bool (or any range of bool-convertible values).
Returns
true if all elements evaluate to true, otherwise false.
Note
Works with the std::vector<bool> specialisation because the proxy object returned by its iterator is convertible to bool.
Since
C++20

◆ contains()

template<typename T>
bool contains ( const std::vector< T > & vec,
const T & value )
nodiscardconstexpr

Check whether a vector contains a given value.

Traverses the vector and returns true as soon as the first element equal to value is found.

Template Parameters
TElement type stored in the vector and of the searched value.
Parameters
vecVector to inspect.
valueValue to look for inside vec.
Returns
true if value is present in vec, otherwise false.
Note
Complexity: O(n) comparisons, where n is vec.size().
Since
C++20 — std::find is constexpr‐friendly when the iterators (and operator==) are constexpr.

◆ delete_files_with_extension()

void delete_files_with_extension ( const std::string & path,
const std::string & extension,
bool recursive = false )

Deletes files with a specified extension in a given directory.

This function deletes all regular files with the specified extension within the given directory. If the recursive flag is true, it searches subdirectories as well; otherwise, it only searches the specified directory. Deletion operations and any errors encountered are logged.

Parameters
pathThe directory path where files will be deleted.
extensionThe file extension (including the dot) to match for deletion.
recursiveIf true, deletion is performed recursively in subdirectories.

◆ ensure_directories_exist()

void ensure_directories_exist ( const std::string & filename)

Ensures that the directory for a given file exists.

This function checks the parent directory of the provided filename. If the directory does not exist, it attempts to create it. Successful creation is logged using glogger. If an error occurs during directory creation, the error is logged.

Parameters
filenameThe file path for which the directories should be ensured.

◆ init_logger()

void init_logger ( Configuration & config)

Initializes the global loggers.

This function creates a console sink with color support and attaches it to two loggers: one for global logging (glogger) and one for robot-specific logging (robotlogger). The log level for both loggers is set to info and a specific log message format is defined.

◆ loggers_add_file_sink()

void loggers_add_file_sink ( std::string const & filename)

Adds a file sink to the global loggers.

This function ensures that the directory for the given filename exists, then creates a file sink that logs messages with debug level and a specified format. The file sink is then added to both the global logger (glogger) and the robot logger (robotlogger).

Parameters
filenameThe file path where the log file will be created.

◆ resolve_path()

std::string resolve_path ( const std::string & inputPath)

Resolves an input file path to an absolute path.

This function attempts to resolve the provided input path by first checking if the file exists in the current directory. If not found and if the DATA_DIR macro is defined, it checks relative to DATA_DIR. If the file is found in either location, the absolute path is returned; otherwise, an exception is thrown.

Parameters
inputPathThe relative or absolute path of the file.
Returns
std::string The resolved absolute path of the file.
Exceptions
std::runtime_errorIf the file cannot be found in the current directory or DATA_DIR.

◆ string_to_bool()

bool string_to_bool ( std::string const & str)

Converts a string to a boolean value.

The conversion is case-insensitive. The function returns true if the input string is "true" or "1", and false if it is "false" or "0". For any other input, an std::invalid_argument exception is thrown.

Parameters
strThe input string to convert.
Returns
true if the string represents a boolean true.
false if the string represents a boolean false.
Exceptions
std::invalid_argumentIf the input string is not a valid boolean representation.

◆ to_lowercase()

std::string to_lowercase ( std::string const & str)

Converts a string to lowercase.

This function creates and returns a lowercase copy of the input string.

Parameters
strThe string to convert.
Returns
std::string A new string where all characters have been converted to lowercase.

Variable Documentation

◆ glogger

std::shared_ptr<spdlog::logger> glogger
extern

Global logger for general logging.

◆ rd

std::random_device rd
extern

Random device used for seeding the random generator.

◆ rnd_gen

std::mt19937 rnd_gen
extern

Random number generator seeded with rd.

◆ robotlogger

std::shared_ptr<spdlog::logger> robotlogger
extern

Global logger for robot-specific logging.