RcsPySim
A robot control and simulation library
pybind_dict_utils.h File Reference
#include <pybind11/pybind11.h>
#include <dictobject.h>
Include dependency graph for pybind_dict_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

py::object get (const py::dict &dict, const char *key, py::handle default_=py::none(), bool noneIsEmpty=true)
 
template<typename T , typename std::enable_if<!std::is_pointer< T >::value, int >::type = 0>
get_cast (const py::dict &dict, const char *key, T default_, bool noneIsEmpty=true)
 
template<typename T , typename std::enable_if< std::is_pointer< T >::value, int >::type = 0>
detail::cast_pointer_proxy< typename std::remove_pointer< T >::type > get_cast (const py::dict &dict, const char *key, T &&default_=NULL)
 
template<typename T >
bool try_get (const py::dict &dict, const char *key, T &var)
 
template<typename T >
bool try_get (const py::dict &dict, const std::string &key, T &var)
 

Function Documentation

◆ get()

py::object get ( const py::dict &  dict,
const char *  key,
py::handle  default_ = py::none(),
bool  noneIsEmpty = true 
)
inline

Get the string-labeled item from the dict and return it as python object. Returns default_ if item not found.

This is designed to work similar to python's dict.get()

Parameters
dictpython dictionary
keykey string
default_default value to return if not found. defaults to None.
noneIsEmptyset to false to return None values explicitly. If true, None is replaced with default_.
Returns
the value for key if found, else default.

Definition at line 55 of file pybind_dict_utils.h.

◆ get_cast() [1/2]

template<typename T , typename std::enable_if<!std::is_pointer< T >::value, int >::type = 0>
T get_cast ( const py::dict &  dict,
const char *  key,
default_,
bool  noneIsEmpty = true 
)
inline

Get the string-labeled item from the dict and return it as converted c++ value. Returns default_ if item not found.

This is designed to work similar to python's dict.get().

A special overload of this function handles pointer types, which are normally not allowed as cast return type.

Parameters
dictpython dictionary
keykey string
default_default value to return if not found.
noneIsEmptyset to false to return None values explicitly. If true, None is replaced with default_.
Returns
the value for key if found, else default.

Definition at line 82 of file pybind_dict_utils.h.

◆ get_cast() [2/2]

template<typename T , typename std::enable_if< std::is_pointer< T >::value, int >::type = 0>
detail::cast_pointer_proxy<typename std::remove_pointer<T>::type> get_cast ( const py::dict &  dict,
const char *  key,
T &&  default_ = NULL 
)
inline

Get the string-labeled item from the dict and return it as converted c++ pointer value value. Returns default_ if item not found.

This is designed to work similar to python's dict.get().

The return value is actually a wrapper to make sure the pointer's target does not go out of scope. It has operator overloads to be usable like a regular pointer.

Parameters
dictpython dictionary
keykey string
default_default value to return if not found. defaults to NULL.
Returns
the value for key if found, else default.

Definition at line 155 of file pybind_dict_utils.h.

◆ try_get() [1/2]

template<typename T >
bool try_get ( const py::dict &  dict,
const char *  key,
T &  var 
)
inline

Get the string-labeled item from the dict, convert it and set it to the var parameter Returns false if item not found.

This is designed to work similar to python's dict.get().

Parameters
dictpython dictionary
keykey string
[out]varoutput var ref. not modified if the parameter is not found.
Returns
true if found

Definition at line 179 of file pybind_dict_utils.h.

◆ try_get() [2/2]

template<typename T >
bool try_get ( const py::dict &  dict,
const std::string &  key,
T &  var 
)
inline

Get the string-labeled item from the dict, convert it and set it to the var parameter Returns false if item not found.

This is designed to work similar to python's dict.get().

Parameters
dictpython dictionary
keykey string
[out]varoutput var ref. not modified if the parameter is not found.
Returns
true if found

Definition at line 203 of file pybind_dict_utils.h.