AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
Common Utilities

Fundamental helper classes and utility functions. More...

Namespaces

namespace  auto_apms_util
 Fundamental helper classes and utility functions.
 

Classes

class  ActionClientWrapper< ActionT >
 Convenience wrapper for a rclcpp_action::Client that introduces synchronous goal handling functions. More...
 
class  ActionContext< ActionT >
 Helper class that stores contextual information related to a ROS 2 action. More...
 
class  ActionWrapper< ActionT >
 Generic base class for implementing robot skills using the ROS 2 action concept. More...
 
class  PluginClassLoader< BaseT >
 Class for loading plugin resources registered according to the conventions defined by the pluginlib package. More...
 

Enumerations

enum class  ActionGoalStatus : uint8_t
 Enum for indicating a ROS 2 action goal's current status. More...
 
enum class  ActionStatus : uint8_t
 Status of the auto_apms_util::ActionWrapper execution process. More...
 
std::string makeColoredText (const std::string &text, TextColor color)
 Add ANSI color escape sequences to display the text in color when printed to console.
 
std::vector< std::string > splitString (const std::string &str, const std::string &delimiter, bool remove_empty=true)
 Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multiple characters).
 
std::string printMap (const std::map< std::string, std::string > &map, const std::string &key_val_sep="=", const std::string &entry_sep=", ")
 Converts a map to a string representation that is suited for printing to console.
 
std::string trimWhitespaces (const std::string &str)
 Trim whitespaces from both ends of a string.
 
std::string toCamelCase (const std::string &str)
 Transform a string to camelCase.
 
std::string toSnakeCase (const std::string &str)
 Transform a string to snake_case.
 
template<typename ValueT, typename AllocatorT, template< typename T, class A > class ContainerT>
bool contains (const ContainerT< ValueT, AllocatorT > &c, const ValueT &val)
 Check whether a particular container structure contains a value.
 
template<typename KeyT, typename CompareT, typename AllocatorT>
std::set< KeyT, CompareT, AllocatorT > getCommonElements (std::set< KeyT, CompareT, AllocatorT > c1, std::set< KeyT, CompareT, AllocatorT > c2)
 Assemble common elements of two sets.
 
template<typename KeyT, typename AllocatorT>
std::vector< KeyT, AllocatorT > getCommonElements (std::vector< KeyT, AllocatorT > c1, std::vector< KeyT, AllocatorT > c2)
 Assemble common elements of two vectors.
 
bool isFileEmpty (const std::string &path)
 Determine if a file is empty.
 
void exposeToGlobalDebugLogging (const rclcpp::Logger &logger)
 Enable ROS 2 debug logging, if the C preprocessor flag _AUTO_APMS_DEBUG_LOGGING is set.
 
void setLoggingSeverity (const rclcpp::Logger &logger, const std::string &severity)
 Set the logging severity of a ROS 2 logger.
 
void setLoggingSeverity (const rclcpp::Logger &logger, rclcpp::Logger::Level severity)
 Set the logging severity of a ROS 2 logger.
 
std::set< std::string > getPackagesWithResourceType (const std::string &resource_type, const std::set< std::string > &exclude_packages={})
 Get a list of all package names that register a certain type of ament_index resources.
 
std::set< std::string > getPackagesWithPluginResources (const std::set< std::string > &exclude_packages={})
 Get a list of all package names that register AutoAPMS plugin resources.
 
std::string getPluginXMLPath (const std::string &package)
 Get the path of a plugin.xml manifest file used for initializing pluginlib::ClassLoader objects.
 
std::vector< std::string > collectPluginXMLPaths (const std::set< std::string > &exclude_packages={})
 Collect the paths of plugin.xml manifest files used for initializing pluginlib::ClassLoader objects.
 
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType)
 Macro for defining YAML encode/decode methods for a class.
 

Detailed Description

Fundamental helper classes and utility functions.

Macro Definition Documentation

◆ AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS

#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS ( ClassType)
Value:
static ClassType fromFile(const std::string & path) \
{ \
if (auto_apms_util::isFileEmpty(path)) return ClassType(); \
try { \
return YAML::LoadFile(path).as<ClassType>(); \
} catch (const YAML::ParserException & e) { \
throw auto_apms_util::exceptions::YAMLFormatError( \
"Format error when creating " + boost::core::demangle(typeid(ClassType).name()) + \
" from file: " + std::string(e.what())); \
} \
} \
static ClassType decode(const std::string & str) \
{ \
const bool empty = std::all_of(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); }); \
return empty ? ClassType() : YAML::Load(str).as<ClassType>(); \
} \
std::string encode() const \
{ \
YAML::Node root; \
root = *this; \
YAML::Emitter out; \
out << root; \
if (!out.good()) { \
throw std::runtime_error( \
"Error trying to encode " + boost::core::demangle(typeid(ClassType).name()) + ": " + out.GetLastError()); \
} \
return out.c_str(); \
}
bool isFileEmpty(const std::string &path)
Determine if a file is empty.

Macro for defining YAML encode/decode methods for a class.

Parameters
ClassTypeFully qualified name of the class.

Definition at line 33 of file yaml.hpp.

Enumeration Type Documentation

◆ ActionGoalStatus

enum class ActionGoalStatus : uint8_t
strong

Enum for indicating a ROS 2 action goal's current status.

It is used to represent the internal state of auto_apms_util::ActionClientWrapper.

Definition at line 42 of file action_client_wrapper.hpp.

◆ ActionStatus

enum class ActionStatus : uint8_t
strong

Status of the auto_apms_util::ActionWrapper execution process.

Definition at line 34 of file action_wrapper.hpp.

Function Documentation

◆ contains()

template<typename ValueT, typename AllocatorT, template< typename T, class A > class ContainerT>
bool contains ( const ContainerT< ValueT, AllocatorT > & c,
const ValueT & val )

Check whether a particular container structure contains a value.

Template Parameters
ValueTType of the values inside the container.
AllocatorTContainer allocator type.
Parameters
cContainer to be searched.
valComparison value.
Returns
true if c contains val, false otherwise.

Definition at line 36 of file container.hpp.

◆ isFileEmpty()

bool isFileEmpty ( const std::string & path)

Determine if a file is empty.

A file is considered empty if it has no content or the content consists of whitespace characters only. If there is any non-whitespace character, this function returns false.

Parameters
pathPath to the file.
Returns
true if the file has no content or consists of whitespaces only, false otherwise.

Definition at line 24 of file filesystem.cpp.

◆ exposeToGlobalDebugLogging()

void exposeToGlobalDebugLogging ( const rclcpp::Logger & logger)

Enable ROS 2 debug logging, if the C preprocessor flag _AUTO_APMS_DEBUG_LOGGING is set.

Parameters
loggerLogger instance.

Definition at line 25 of file logging.cpp.

◆ getPackagesWithResourceType()

std::set< std::string > getPackagesWithResourceType ( const std::string & resource_type,
const std::set< std::string > & exclude_packages = {} )

Get a list of all package names that register a certain type of ament_index resources.

Note
This function determines what packages register resources by parsing the install directory, so any resources that are not installed at the time this function is called won't be considered.
Parameters
resource_typeName of the resource type.
exclude_packagesPackages to exclude when searching for resources.
Returns
List of all packages that register resources of type resource_type excluding exclude_packages.
Exceptions
auto_apms_util::exceptions::ResourceErrorif no resources of type resource_type were found in any of the intalled packages.

Definition at line 28 of file resource.cpp.

◆ makeColoredText()

std::string makeColoredText ( const std::string & text,
TextColor color )

Add ANSI color escape sequences to display the text in color when printed to console.

The text color will be reset to default after the text ends.

Parameters
textText to be displayed.
colorDesired color of the text.
Returns
String including corresponding ANSI color escape sequences.

Definition at line 65 of file string.cpp.

◆ getCommonElements() [1/2]

template<typename KeyT, typename CompareT, typename AllocatorT>
std::set< KeyT, CompareT, AllocatorT > getCommonElements ( std::set< KeyT, CompareT, AllocatorT > c1,
std::set< KeyT, CompareT, AllocatorT > c2 )

Assemble common elements of two sets.

Template Parameters
KeyTType of the keys within the set.
CompareTComparator type.
AllocatorTAllocator type.
Parameters
c1First set.
c2Second set.
Returns
Set of common elements present in c1 as well as c2.

Definition at line 53 of file container.hpp.

◆ getCommonElements() [2/2]

template<typename KeyT, typename AllocatorT>
std::vector< KeyT, AllocatorT > getCommonElements ( std::vector< KeyT, AllocatorT > c1,
std::vector< KeyT, AllocatorT > c2 )

Assemble common elements of two vectors.

Template Parameters
KeyTType of the values within the vector.
AllocatorTAllocator type.
Parameters
c1First vector.
c2Second vector.
Returns
Vector of common elements present in c1 as well as c2.

Definition at line 70 of file container.hpp.

◆ setLoggingSeverity() [1/2]

void setLoggingSeverity ( const rclcpp::Logger & logger,
const std::string & severity )

Set the logging severity of a ROS 2 logger.

Parameters
loggerLogger instance.
severityDesired severity level encoded as a string. Must be one of DEBUG, INFO, WARN, ERROR, FATAL or UNSET.

Definition at line 38 of file logging.cpp.

◆ setLoggingSeverity() [2/2]

void setLoggingSeverity ( const rclcpp::Logger & logger,
rclcpp::Logger::Level severity )

Set the logging severity of a ROS 2 logger.

Parameters
loggerLogger instance.
severityDesired severity level.

Definition at line 60 of file logging.cpp.

◆ getPackagesWithPluginResources()

std::set< std::string > getPackagesWithPluginResources ( const std::set< std::string > & exclude_packages = {})

Get a list of all package names that register AutoAPMS plugin resources.

Note
This function determines what packages register resources by parsing the install directory, so any resources that are not installed at the time this function is called won't be considered.
Parameters
exclude_packagesPackages to exclude when searching for resources.
Returns
List of all packages that register AutoAPMS plugins excluding exclude_packages.
Exceptions
auto_apms_util::exceptions::ResourceErrorif no AutoAPMS plugin resources were found in any of the installed packages.

Definition at line 52 of file resource.cpp.

◆ getPluginXMLPath()

std::string getPluginXMLPath ( const std::string & package)

Get the path of a plugin.xml manifest file used for initializing pluginlib::ClassLoader objects.

This function requires packages to install plugins.xml manifest files and register them as an ament_index resource using the CMake macro auto_apms_util_register_plugins().

Parameters
packageName of the package registering AutoAPMS plugin resources.
Returns
Absolute path to the pluginlib-style plugin manifest xml file.
Exceptions
auto_apms_util::exceptions::ResourceErrorif failed to find a plugin manifest file.
auto_apms_util::exceptions::ResourceErrorif an ament_index resource marker file is invalid.

Definition at line 57 of file resource.cpp.

◆ collectPluginXMLPaths()

std::vector< std::string > collectPluginXMLPaths ( const std::set< std::string > & exclude_packages = {})

Collect the paths of plugin.xml manifest files used for initializing pluginlib::ClassLoader objects.

This function requires packages to install plugins.xml manifest files and register them as an ament_index resource using the CMake macro auto_apms_util_register_plugins().

Parameters
exclude_packagesPackages to exclude when searching for plugins.xml file paths.
Returns
Absolute paths to the pluginlib-style plugin manifest xml files.
Exceptions
auto_apms_util::exceptions::ResourceErrorif failed to find a plugin manifest file.
auto_apms_util::exceptions::ResourceErrorif an ament_index resource marker file is invalid.

Definition at line 76 of file resource.cpp.

◆ splitString()

std::vector< std::string > splitString ( const std::string & str,
const std::string & delimiter,
bool remove_empty = true )

Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multiple characters).

Will remove empty strings if remove_empty is true (Default). So for example with the delimiter being :: passing the string ::foo will output a vector with one elements {"foo"}. If you want {"", "foo"}, you must set remove_empty to false.

Parameters
[in]strString to split into multiple tokens.
[in]delimiterDelimiter string at which the string shall be split.
[in]remove_emptyRemove empty string tokens in the result vector.
Returns
Vector of string representing the string's tokens without the delimiter.

Definition at line 24 of file string.cpp.

◆ printMap()

std::string printMap ( const std::map< std::string, std::string > & map,
const std::string & key_val_sep = "=",
const std::string & entry_sep = ", " )

Converts a map to a string representation that is suited for printing to console.

Parameters
mapMap to be converted to string.
key_val_sepSeparator for the key-value pairs.
entry_sepSeparator for the map's entries.
Returns
String that encodes map.

Definition at line 50 of file string.cpp.

◆ trimWhitespaces()

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

Trim whitespaces from both ends of a string.

Parameters
strString to trim.
Returns
Trimmed string.

Definition at line 84 of file string.cpp.

◆ toCamelCase()

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

Transform a string to camelCase.

Parameters
[in]strString to transform.
Returns
Transformed string.

Definition at line 97 of file string.cpp.

◆ toSnakeCase()

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

Transform a string to snake_case.

Parameters
[in]strString to transform.
Returns
Transformed string.

Definition at line 116 of file string.cpp.