rcpputils  master
C++ API providing common utilities and data structures.
rcpputils Features

This package includes the following convenience functions for generally cross-platform c++ functionality:

Assertion Functions

The rcpputils/asserts.hpp header provides the helper functions:

  • require_true: for validating function inputs. Throws an std::invalid_argument exception if the condition fails.
  • check_true: for checking states. Throws an rcpputils::InvalidStateException if the condition fails.
  • assert_true: for verifying results. Throws an rcpputils::AssertionException if the condition fails. This function becomes a no-op in release builds.

These helper functions can be used to improve readability of C++ functions. Example usage:

ResultType some_function(ArgType arg)
// Check if the required internal state for calling `some_function` is valid.
// There's usually no reason to operate on the input if the state is invalid.
// Check if the arguments are valid.
// It's usually best practice to only process valid inputs.
auto result = do_logic(arg);
// Assert that the result is valid.
// This will only throw when ran in debug mode if result is invalid.
// Throwing early when invalid may help find points of error when debugging.
return result

Clang Thread Safety Annotation Macros

the rcpputils/thread_safety_annotations.hpp header provides macros for Clang's Thread Safety Analysis feature.

The macros allow you to annotate your code, but expand to nothing when using a non-clang compiler, so they are safe for cross-platform use.

To use thread safety annotation in your package (in a Clang+libcxx build), enable the -Wthread-safety compiler flag.

For example usage, see the documentation of this feature and the tests in test/test_basic.cpp

Endianness helpers

In rcpputils/endian.hpp

Emulates the features of std::endian if it is not available. See cppreference for more information.

Library Discovery

In rcpputils/find_library.hpp:

  • find_library(library_name): Namely used for dynamically loading RMW implementations.
    • For dynamically loading user-defined plugins in C++, please use pluginlib instead.

String Helpers

In rcpputils/join.hpp and rcpputils/split.hpp

These headers include simple functions for joining a container into a single string and splitting a string into a container of values.

File system helpers

rcpputils/filesystem_helper.hpp provides std::filesystem-like functionality on systems that do not yet include those features. See the cppreference for more information.

Type traits helpers

rcpputils/pointer_traits.hpp provides several type trait definitions for pointers and smart pointers.

Visibility definitions and macros

rcpputils/visibility_control.hpp provides macros and definitions for controlling the visibility of class members. The logic was borrowed and then namespaced from https://gcc.gnu.org/wiki/Visibility.

Shared Libraries

rcpputils/shared_library.hpp provides dynamically loads, unloads and get symbols from shared libraries at run-time.