Py2Cpp 1.6.3; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
py::dict< Key, T > Class Template Reference

Python-like dictionary implementation. More...

#include <dict.hpp>

Inheritance diagram for py::dict< Key, T >:
Inheritance graph
[legend]
Collaboration diagram for py::dict< Key, T >:
Collaboration graph
[legend]

Public Types

using value_type = std::pair< const Key, T >
 
using key_type = Key
 

Public Member Functions

 dict ()
 Default constructor.
 
 dict (std::initializer_list< value_type > init)
 Construct a dict from an initializer list.
 
auto contains (const Key &key) const -> bool
 Check if the dictionary contains a specific key.
 
auto get (const Key &key, const T &default_value) const -> T
 Get a value with a default fallback.
 
auto begin () -> key_iterator< decltype(Base::begin())>
 Get iterator to the beginning of keys.
 
auto end () -> key_iterator< decltype(Base::end())>
 Get iterator to the end of keys.
 
auto begin () const -> key_iterator< decltype(Base::begin())>
 Get const iterator to the beginning of keys.
 
auto end () const -> key_iterator< decltype(Base::end())>
 Get const iterator to the end of keys.
 
auto items () -> Base &
 Get the underlying map for iteration.
 
auto items () const -> const Base &
 Get the underlying map for iteration (const version)
 
auto copy () const -> Self
 Create a copy of the dictionary.
 
auto operator[] (const Key &k) const -> const T &
 Access value by key (const version)
 
auto at (const Key &k) const -> const T &
 Access value by key with bounds checking (const version)
 
auto operator[] (const Key &k) -> T &
 Access or insert value by key.
 
auto operator= (const Self &) -> Self &=delete
 Copy assignment operator (deleted)
 
auto operator= (Self &&) noexcept -> dict &=default
 Move assignment operator.
 
 dict (dict< Key, T > &&) noexcept=default
 Move Constructor (default)
 
 ~dict ()=default
 
 dict (const dict< Key, T > &)=default
 Construct a new dict object.
 

Detailed Description

template<typename Key, typename T>
class py::dict< Key, T >

Python-like dictionary implementation.

A dictionary class that extends std::unordered_map with Python-like convenience methods and functionality.

Template Parameters
KeyThe key type
TThe value type

Member Typedef Documentation

◆ key_type

template<typename Key , typename T >
using py::dict< Key, T >::key_type = Key

◆ value_type

template<typename Key , typename T >
using py::dict< Key, T >::value_type = std::pair<const Key, T>

Constructor & Destructor Documentation

◆ dict() [1/4]

template<typename Key , typename T >
py::dict< Key, T >::dict ( )
inline

Default constructor.

Creates an empty dictionary.

◆ dict() [2/4]

template<typename Key , typename T >
py::dict< Key, T >::dict ( std::initializer_list< value_type init)
inline

Construct a dict from an initializer list.

Creates a dictionary from a list of key-value pairs.

Parameters
[in]initInitializer list of key-value pairs

◆ dict() [3/4]

template<typename Key , typename T >
py::dict< Key, T >::dict ( dict< Key, T > &&  )
defaultnoexcept

Move Constructor (default)

◆ ~dict()

template<typename Key , typename T >
py::dict< Key, T >::~dict ( )
default

◆ dict() [4/4]

template<typename Key , typename T >
py::dict< Key, T >::dict ( const dict< Key, T > &  )
default

Construct a new dict object.

Copy through explicitly the public copy() function!!!

Member Function Documentation

◆ at()

template<typename Key , typename T >
auto py::dict< Key, T >::at ( const Key &  k) const -> const T&
inline

Access value by key with bounds checking (const version)

Returns a const reference to the value associated with the key. Throws std::out_of_range if the key is not found.

Parameters
[in]kThe key to look up
Returns
const T& Reference to the value

◆ begin() [1/2]

template<typename Key , typename T >
auto py::dict< Key, T >::begin ( ) -> key_iterator<decltype(Base::begin())>
inline

Get iterator to the beginning of keys.

Returns an iterator that yields keys from the dictionary.

Returns
auto Iterator to the first key

◆ begin() [2/2]

template<typename Key , typename T >
auto py::dict< Key, T >::begin ( ) const -> key_iterator<decltype(Base::begin())>
inline

Get const iterator to the beginning of keys.

Returns a const iterator that yields keys from the dictionary.

Returns
auto Const iterator to the first key

◆ contains()

template<typename Key , typename T >
auto py::dict< Key, T >::contains ( const Key &  key) const -> bool
inline

Check if the dictionary contains a specific key.

Parameters
[in]keyThe key to look up
Returns
true if the key is contained in the dictionary, false otherwise

◆ copy()

template<typename Key , typename T >
auto py::dict< Key, T >::copy ( ) const -> Self
inline

Create a copy of the dictionary.

Returns
Self A copy of this dictionary

◆ end() [1/2]

template<typename Key , typename T >
auto py::dict< Key, T >::end ( ) -> key_iterator<decltype(Base::end())>
inline

Get iterator to the end of keys.

Returns an iterator past the last key in the dictionary.

Returns
auto Iterator past the last key

◆ end() [2/2]

template<typename Key , typename T >
auto py::dict< Key, T >::end ( ) const -> key_iterator<decltype(Base::end())>
inline

Get const iterator to the end of keys.

Returns a const iterator past the last key in the dictionary.

Returns
auto Const iterator past the last key

◆ get()

template<typename Key , typename T >
auto py::dict< Key, T >::get ( const Key &  key,
const T &  default_value 
) const -> T
inline

Get a value with a default fallback.

Returns the value associated with the key, or the default value if the key is not found in the dictionary.

Parameters
[in]keyThe key to look up
[in]default_valueThe default value to return if key is not found
Returns
T The value associated with the key, or the default value

◆ items() [1/2]

template<typename Key , typename T >
auto py::dict< Key, T >::items ( ) -> Base&
inline

Get the underlying map for iteration.

Returns
std::unordered_map<Key, T>& Reference to the underlying map

◆ items() [2/2]

template<typename Key , typename T >
auto py::dict< Key, T >::items ( ) const -> const Base&
inline

Get the underlying map for iteration (const version)

Returns
const std::unordered_map<Key, T>& Const reference to the underlying map

◆ operator=() [1/2]

template<typename Key , typename T >
auto py::dict< Key, T >::operator= ( const Self ) -> Self &=delete
delete

Copy assignment operator (deleted)

Use copy() method instead for explicit copying.

◆ operator=() [2/2]

template<typename Key , typename T >
auto py::dict< Key, T >::operator= ( Self &&  ) -> dict &=default
defaultnoexcept

Move assignment operator.

Returns
Self& Reference to this dictionary

◆ operator[]() [1/2]

template<typename Key , typename T >
auto py::dict< Key, T >::operator[] ( const Key &  k) -> T&
inline

Access or insert value by key.

Returns a reference to the value associated with the key. If the key does not exist, inserts a default-constructed value.

Parameters
[in]kThe key to look up
Returns
T& Reference to the value

◆ operator[]() [2/2]

template<typename Key , typename T >
auto py::dict< Key, T >::operator[] ( const Key &  k) const -> const T&
inline

Access value by key (const version)

Returns a const reference to the value associated with the key. Throws std::out_of_range if the key is not found.

Parameters
[in]kThe key to look up
Returns
const T& Reference to the value

The documentation for this class was generated from the following file: