[mrpt-containers]

Overview

Container and STL-helper classes.

Library mrpt-containers

This C++ library is part of MRPT and can be installed in Debian-based systems with:

sudo apt install libmrpt-containers-dev

Read also how to import MRPT into your CMake scripts.

Find below some examples of use.

Overview

mrpt-containers provides generic containers that complement the C++ standard library, plus a YAML/JSON document class used throughout MRPT to pass and store parameters. It only depends on [mrpt-core] and [mrpt-typemeta].

Key classes

YAML-like parameter container

See mrpt::containers::yaml, and the examples therein.

#include <mrpt/containers/yaml.h>

const auto p = mrpt::containers::yaml::FromText(R"(
robot:
  name: rover1
  max_speed: 0.8
  sensors: [lidar, imu]
)");

const auto& robot = p["robot"];
const double v = robot["max_speed"].as<double>();
const int n = robot.getOrDefault<int>("num_wheels", 4);  // not present: 4
for (const auto& s : robot["sensors"].asSequence())
{
  std::cout << s.as<std::string>() << "\n";
}

mrpt::containers::yaml out = mrpt::containers::yaml::Map();
out["v"] = v;
out["n"] = n;
out.printAsYAML(std::cout);

Note that references to nodes of a map or sequence are invalidated when new siblings are added to it.

Examples

  • containers_yaml_example: parsing, building, iterating and printing YAML documents, including comments.

  • containers_params_by_name: passing a variable set of named parameters to a function.

  • Python: mrpt_containers_example.py.

Library contents

// typedefs

typedef internal::generic_copier_ptr<T, internal::CopyCloner<T>> mrpt::containers::poly_ptr;
typedef internal::generic_copier_ptr<T, internal::CopyStatic<T>> mrpt::containers::copy_ptr;

// structs

struct mrpt::containers::ci_less;

// classes

template <class T>
class mrpt::containers::CDynamicGrid;

template <class T, class coord_t = double>
class mrpt::containers::CDynamicGrid3D;

template <class T>
class mrpt::containers::CThreadSafeQueue;

template <class T>
class mrpt::containers::NonCopiableData;

template <class T>
class mrpt::containers::PerThreadDataHolder;

template <typename KEY, typename VALUE>
class mrpt::containers::bimap;

template <typename T>
class mrpt::containers::circular_buffer;

template <typename UNDERLYING_IT>
class mrpt::containers::deep_const_iterator;

template <typename T>
class mrpt::containers::deepcopy_poly_ptr;

template <class T>
class mrpt::containers::list_searchable;

template <
    typename KEY,
    typename VALUE,
    typename VECTOR_T = typename std::vector<std::pair<KEY, VALUE>>
    >
class mrpt::containers::map_as_vector;

template <typename VAL, size_t small_size, size_t alignment = 16>
class mrpt::containers::vector_with_small_size_optimization;

// global functions

template <class Visitor, class... T>
void mrpt::visit_each(
    const Visitor& vis,
    T&&... t
    );

template <typename src_container, typename dst_container>
void mrpt::containers::copy_container_typecasting(
    const src_container& src,
    dst_container& trg
    );

Typedefs

typedef internal::generic_copier_ptr<T, internal::CopyCloner<T>> mrpt::containers::poly_ptr

Smart pointer for polymorphic classes with a clone() method.

No shared copies, that is, each poly_ptr<T> owns a unique instance of T. Copying a poly_ptr<T> invokes the copy operator for T.

See also:

copy_ptr<T>

typedef internal::generic_copier_ptr<T, internal::CopyStatic<T>> mrpt::containers::copy_ptr

Smart pointer for non-polymorphic classes.

No shared copies, that is, each copy_ptr<T> owns a unique instance of T. Copying a copy_ptr<T> invokes the copy operator for T.

See also:

poly_ptr<T>

Global Functions

template <typename src_container, typename dst_container>
void mrpt::containers::copy_container_typecasting(
    const src_container& src,
    dst_container& trg
    )

Copy all the elements in a container (vector, deque, list) into a different one performing the appropriate typecasting.

The target container is automatically resized to the appropriate size, and previous contents are lost. This can be used to assign std::vector’s of different types:

std::vector<int>    vi(10);
std::vector<float>  vf;
vf = vi;   // Compiler error
mrpt::containers::copy_container_typecasting(v1,vf);  // Ok