SDK Base Utils

Header helpers classes and functions

template<typename EventType>
void Metavision::write_DAT_header(std::ostream &os, const GenericHeader::HeaderMap &header_map)

Writes the DAT header of a file.

Template Parameters
  • EventType: The type of events stored in the DAT file

Parameters
  • os: The stream used to store the DAT data

  • header_map: An optional map of data to be stored in the header

template<typename EventType>
std::string Metavision::get_DAT_header_as_string(const GenericHeader::HeaderMap &header_map = GenericHeader::HeaderMap())

Convenience function to get the DAT header as string.

Return

The string representing the DAT header

Template Parameters
  • EventType: The type of events stored in the DAT file

Parameters
  • header_map: Additional information to be stored in the header

GenericHeader Metavision::make_DAT_header_map_with_geometry(int width, int height)

Convenience function to create a DAT header map to be added in the header using the camera geometry.

Return

The DAT header map

Parameters
  • width: Width of the camera sensor

  • height: Height of the camera sensor

class Metavision::GenericHeader

A utility class to hold and format headers information.

A header is composed of fields composed of key (string) / value (string) pairs

Public Types

using HeaderMap = std::map<std::string, std::string>

Alias for the internal map holding the header information.

Public Functions

GenericHeader()

Default constructor.

GenericHeader(std::istream &stream)

Builds the header map by parsing the input stream.

The stream internal is expected to point to the start of the header (if any) This method effectively places the stream cursor at the end of header. The position of the cursor remains unchanged if no header is actually present.

Parameters
  • stream: The input stream to parse a header from

GenericHeader(const HeaderMap &header_map)

Builds the header map using a copy of the input HeaderMap.

Parameters
  • header_map: The header map to use for initialization

bool empty() const

Returns if the header is empty.

Return

True if the header is empty, false otherwise

void add_date()

Adds the current date and time to the header (in the format Y-m-d H:M:S).

Note

The date is updated at each call to this method

void remove_date()

Removes the date if there was any in the header.

std::string get_date() const

Gets the date and time at which the associated file was recorded.

Return

The current date and time in string format if it is found, or an empty string otherwise.

void set_field(const std::string &key, const std::string &value)

Adds a new field in the header.

Parameters
  • key: The key of the field in the header

  • value: The value of the field

void remove_field(const std::string &key)

Remove the input field (if exists)

std::string get_field(const std::string &key) const

Gets the value associated to the input key.

Return

The value associated to the input key if it exists, or an empty string otherwise

const HeaderMap &get_header_map() const

Gets the HeaderMap holding the header information.

Return

The header map

std::string to_string() const

Serializes the header map.

Return

The header in a string format

Various

template<class T, bool acquire_shared_ptr = false>
class Metavision::ObjectPool

Class that creates a reusable pool of heap allocated objects.

The ObjectPool allocates objects that are returned to the pool upon destruction. acquire is used to allocate or re-use a previously allocated object instance. The object are made available through a std::unique_ptr or a std::shared_ptr according to the template acquire_shared_ptr. The smart pointers are given a custom deleter, which automatically adds the object back to the pool upon destruction.

Template Parameters
  • T: the type of object stored

  • acquire_shared_ptr: if true, the object are wrapped by a std::shared_ptr, otherwise a std::unique_ptr is returned instead

Public Functions

ObjectPool()

Default constructor that builds an unbounded object pool with an initial number of objects allocated make_unbounded.

void add(std::unique_ptr<T> t)

Adds an object to the pool.

Parameters
  • t: A unique_ptr storing the object

template<typename ...Args>
ptr_type acquire(Args&&... args)

Allocates or re-use a previously allocated object.

Return

A unique or shared pointer to the allocated object

Parameters
  • args: Optional arguments to be passed when allocating the object

bool empty() const

Checks if the pool is empty.

Return

true if the pool is empty, false if the pool contains object ready to be re-used

size_t size() const

Gets the number of objects in the pool.

Return

The number of previously allocated and ready to-reuse objects in the pool

bool is_bounded() const

Checks the memory pool type i.e. bounded or unbounded.

Return

true if the memory pool is bounded, false if it is unbounded

Public Static Functions

ObjectPool<T, acquire_shared_ptr> make_bounded(size_t num_initial_objects = 64)

Creates an object pool with limited number of objects that can be allocated.

There won’t be memory allocation upon call to acquire if all objects in the memory pool are already used.

Return

An object pool with bounded memory

Parameters
  • num_initial_objects: Number of objects initially allocated in the pool

template<typename ...Args>
ObjectPool<T, acquire_shared_ptr> make_bounded(size_t num_initial_objects, Args&&... args)

Creates an object pool with limited number of objects that can be allocated.

There won’t be memory allocation upon call to acquire if all objects in the memory pool are already used.

Return

An object pool with bounded memory

Parameters
  • num_initial_objects: Number of objects initially allocated in the pool

  • args: The arguments forwarded to the object constructor during allocation

template<typename ...Args>
ObjectPool<T, acquire_shared_ptr> make_unbounded(size_t num_initial_objects = 64)

Creates an object pool with expendable memory usage.

A pool with unbounded memory will allocate a new object when all objects in the pool are already used and acquire is called.

Return

An object pool with unbounded memory

Parameters
  • num_initial_objects: Number of objects initially allocated in the pool

template<typename ...Args>
ObjectPool<T, acquire_shared_ptr> make_unbounded(size_t num_initial_objects, Args&&... args)

Creates an object pool with expendable memory usage.

A pool with unbounded memory will allocate a new object when all objects in the pool are already used and acquire is called.

Return

An object pool with unbounded memory

Parameters
  • num_initial_objects: Number of objects initially allocated in the pool

  • args: The arguments forwarded to the object constructor during allocation

template<typename T>
using Metavision::SharedObjectPool = ObjectPool<T, true>

Convenience alias to use a ObjectPool returning shared pointers.

Template Parameters
  • T: the type of object stored in the pool

typedef long long Metavision::timestamp

Type to represent time in microseconds.

Logging

MV_LOG_DEBUG(prefixFmt…)

Returns a logging operation of DEBUG level.

Convenience function to return a logging operation of DEBUG level using the current logging stream, an optional prefix format prefixFmt and automatically adding an end of line token at the end of the logging operation

Return

A logging operation of DEBUG level

Parameters
  • prefixFmt: (Optional) a format string that will be be output as the first message token

MV_LOG_TRACE(prefixFmt…)

Returns a logging operation of TRACE level.

Convenience macro to return a logging operation of TRACE level using the current logging stream, an optional prefix format prefixFmt and automatically adding an end of line token at the end of the logging operation

Return

A logging operation of TRACE level

Parameters
  • prefixFmt: (Optional) a format string that will be be output as the first message token

MV_LOG_INFO(prefixFmt…)

Returns a logging operation of INFO level.

Convenience function to return a logging operation of INFO level using the current logging stream, an optional prefix format prefixFmt and automatically adding an end of line token at the end of the logging operation

Return

A logging operation of INFO level

Parameters
  • prefixFmt: (Optional) a format string that will be be output as the first message token

MV_LOG_WARNING(prefixFmt…)

Returns a logging operation of WARNING level.

Convenience function to return a logging operation of WARNING level using the current logging stream, an optional prefix format prefixFmt and automatically adding an end of line token at the end of the logging operation

Return

A logging operation of WARNING level

Parameters
  • prefixFmt: (Optional) a format string that will be be output as the first message token

MV_LOG_ERROR(prefixFmt…)

Returns a logging operation of ERROR level.

Convenience function to return a logging operation of ERROR level using the current logging stream, an optional prefix format prefixFmt and automatically adding an end of line token at the end of the logging operation

Return

A logging operation of ERROR level

Parameters
  • prefixFmt: (Optional) a format string that will be be output as the first message token

namespace Metavision::Log

Functions

template<LogLevel Level>
LoggingOperation<Level> &space(LoggingOperation<Level> &op)

Stream manipulator enabling the automatic addition of spaces between message tokens.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&space(LoggingOperation<Level> &&op)

Stream manipulator enabling the automatic addition of spaces between message tokens.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &no_space(LoggingOperation<Level> &op)

Stream manipulator disabling the automatic addition of spaces between message tokens.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&no_space(LoggingOperation<Level> &&op)

Stream manipulator disabling the automatic addition of spaces between message tokens.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &endline(LoggingOperation<Level> &op)

Stream manipulator enabling the automatic addition of and end of line token at the end of the operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&endline(LoggingOperation<Level> &&op)

Stream manipulator enabling the automatic addition of and end of line token at the end of the operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &no_endline(LoggingOperation<Level> &op)

Stream manipulator disabling the automatic addition of and end of line token at the end of the operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&no_endline(LoggingOperation<Level> &&op)

Stream manipulator disabling the automatic addition of and end of line token at the end of the operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &function(LoggingOperation<Level> &op)

Stream manipulator that outputs the function name associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&function(LoggingOperation<Level> &&op)

Stream manipulator that outputs the function name associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &file(LoggingOperation<Level> &op)

Stream manipulator that outputs the file name associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&file(LoggingOperation<Level> &&op)

Stream manipulator that outputs the file name associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &line(LoggingOperation<Level> &op)

Stream manipulator that outputs the line associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&line(LoggingOperation<Level> &&op)

Stream manipulator that outputs the line associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &prefix(LoggingOperation<Level> &op)

Stream manipulator that outputs the prefix associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier

template<LogLevel Level>
LoggingOperation<Level> &&prefix(LoggingOperation<Level> &&op)

Stream manipulator that outputs the prefix associated to the logging operation.

Return

The modified logging operation

Template Parameters
  • Level: The level of the logging operation

Parameters
  • op: The logging operation modified by this modifier