39#include <boost/type_index.hpp>
86template <
typename... Scalars >
89 template <
typename... Extras >
90 using VariantType = std::variant< std::monostate, Scalars..., std::vector< Scalars >..., Extras... >;
117 std::vector< std::vector< double > >,
118 std::vector< std::vector< std::vector< double > > >,
119 std::vector< std::vector< std::vector< long > > >,
120 std::shared_ptr< nest::Parameter >,
140 using vectype_::vectype_;
170std::ostream&
operator<<( std::ostream& os,
const std::monostate& );
191template <
typename T >
192concept Integer = std::integral< T > and not std::same_as< T, bool > and not std::same_as< T, char >;
200template <
typename T >
223 const any_type&
at(
const std::string& key )
const;
241 auto find(
const std::string& key )
const;
252 bool known(
const std::string& key )
const;
289 all_entries_accessed(
const std::string& where,
const std::string& what,
const bool thread_local_dict =
false )
const;
300 template < DictionaryEntryType T >
301 T
get(
const std::string& key )
const;
308 template <
typename T >
322 template <
typename T >
323 bool update_value(
const std::string& key, T& value )
const;
334 template < Integer T >
352template <
typename T >
357 return boost::typeindex::type_id< T >().pretty_name();
359 return typeid( T ).name();
400 return item == other.item;
414 using maptype_ = std::map< std::string, DictEntry_ >;
415 using maptype_::maptype_;
426 template <
typename T >
432 return std::get< T >( value );
434 catch (
const std::bad_variant_access& )
437 String::compose(
"Failed to cast '%1' from %2 to type %3", key,
get_typename( value ), pretty_typename< T >() );
450 template < Integer RetT >
455 [ key ](
auto&& arg ) -> RetT
457 using T = std::decay_t<
decltype( arg ) >;
463 if ( std::in_range< RetT >( arg ) )
465 return static_cast< RetT
>( arg );
468 throw std::out_of_range(
"Value causes data loss or overflow." );
472 throw std::runtime_error(
473 String::compose(
"The dictionary value with key %1 does not hold a numeric integer type.", key ) );
486 template < DictionaryEntryType T >
488 get(
const std::string& key )
const
490 return cast_value_< T >(
at( key ), key );
496 template <
typename T >
501 auto [ iter, success ] = maptype_::try_emplace( key, std::vector< T >() );
503 return std::get< std::vector< T > >( iter->second.item );
509 template <
typename T >
513 auto it =
find( key );
516 value = cast_value_< T >( it->second.item, key );
525 template < Integer T >
529 auto it =
find( key );
532 value = cast_to_integer_< T >( it->second.item, key );
544 for (
const auto& [ key, value ] : *this )
546 dict_out[ key ] = value.item;
555 known(
const std::string& key )
const
557 return maptype_::contains( key );
575 return maptype_::at( key ).accessed;
589 return not( *
this == other );
601 all_entries_accessed(
const std::string& where,
const std::string& what,
const bool thread_local_dict =
false )
const;
607 const any_type&
at(
const std::string& key )
const;
608 iterator
find(
const std::string& key );
609 const_iterator
find(
const std::string& key )
const;
615double dictionary_::cast_value_< double >(
const any_type& value,
const std::string& key )
const;
620 return ( *this )->begin();
626 return ( *this )->end();
636std::vector< double > dictionary_::cast_value_< std::vector< double > >(
const any_type& value,
637 const std::string& key )
const;
643std::vector< std::string > dictionary_::cast_value_< std::vector< std::string > >(
const any_type& value,
644 const std::string& key )
const;
649 return **
this == *other;
655 return ( *this )->size();
661 return ( *this )->empty();
673 return ( *this )->find( key );
679 return ( *this )->known( key );
685 ( *this )->mark_as_accessed( key );
691 return ( *this )->has_been_accessed( key );
697 ( *this )->init_access_flags( thread_local_dict );
702 const std::string& what,
703 const bool thread_local_dict )
const
705 ( *this )->all_entries_accessed( where, what, thread_local_dict );
708template < DictionaryEntryType T >
712 return ( *this )->get< T >( key );
714template <
typename T >
718 return ( *this )->get_or_create_vector< T >( key );
721template <
typename T >
725 return ( *this )->update_value( key, value );
728template < Integer T >
732 return ( *this )->update_integer_value( key, value );
738 return ( *this )->update_dictionary( out_dict );
Vector of any_type.
Definition dictionary.h:138
std::vector< any_type > vectype_
Definition dictionary.h:139
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
bool update_integer_value(const std::string &key, T &value) const
Update the specified value if there exists an integer value at key.
Definition dictionary.h:730
bool known(const std::string &key) const
Check whether there exists a value with specified key in the dictionary.
Definition dictionary.h:677
void mark_as_accessed(const std::string &key) const
Mark to support checking for unaccessed entries.
Definition dictionary.h:683
auto begin() const
Definition dictionary.h:618
void clear()
Definition dictionary.h:665
bool update_value(const std::string &key, T &value) const
Update the specified non-vector value if there exists a value at key.
Definition dictionary.h:723
any_type & operator[](const std::string &key)
Definition dictionary.cpp:57
void all_entries_accessed(const std::string &where, const std::string &what, const bool thread_local_dict=false) const
Confirm that all entries in dictionary have been accessed.
Definition dictionary.h:701
auto empty() const
Definition dictionary.h:659
bool update_dictionary(Dictionary &dict_out) const
Update the provided dictionary with all key-value pairs in this dictionary.
Definition dictionary.h:736
any_type & at(const std::string &key)
Definition dictionary.cpp:67
bool has_been_accessed(const std::string &key) const
Return true if mark_as_accessed has been called at least once for key.
Definition dictionary.h:689
auto end() const
Definition dictionary.h:624
std::vector< T > & get_or_create_vector(const std::string &key)
Return reference to vector of type T stored under key.
Definition dictionary.h:716
auto size() const
Definition dictionary.h:653
void init_access_flags(const bool thread_local_dict=false) const
Initialize access flags to prepare for later error checking with all_entries_accessed.
Definition dictionary.h:695
T get(const std::string &key) const
Get the value at key in the specified type.
Definition dictionary.h:710
Dictionary()
Definition dictionary.h:215
bool operator==(const Dictionary &other) const
Check whether the dictionary is equal to another dictionary.
Definition dictionary.h:647
auto find(const std::string &key) const
Definition dictionary.h:671
A Python-like dictionary_, based on std::map.
Definition dictionary.h:413
bool update_dictionary(Dictionary &dict_out) const
See Dictionary::update_dictionary.
Definition dictionary.h:542
T cast_value_(const any_type &value, const std::string &key) const
Cast the specified non-vector value to the specified type.
Definition dictionary.h:428
iterator find(const std::string &key)
Definition dictionary.cpp:357
any_type & at(const std::string &key)
Definition dictionary.cpp:339
void init_access_flags(const bool thread_local_dict=false) const
See Dictionary::init_access_flags.
Definition dictionary.cpp:379
RetT cast_to_integer_(const any_type &value, const std::string &key) const
Cast the specified value to an integer.
Definition dictionary.h:452
void all_entries_accessed(const std::string &where, const std::string &what, const bool thread_local_dict=false) const
See Dictionary::all_entries_accessedinit_access_flags.
Definition dictionary.cpp:392
std::map< std::string, DictEntry_ > maptype_
Definition dictionary.h:414
std::vector< T > & get_or_create_vector(const std::string &key)
See Dictionary::get_or_create_vector.
Definition dictionary.h:498
void mark_as_accessed(const std::string &key) const
See Dictionary::marked_as_accessed.
Definition dictionary.h:564
void register_access_(const DictEntry_ &entry) const
Mark dictionary entry as accessed.
Definition dictionary.cpp:309
bool update_integer_value(const std::string &key, T &value) const
See Dictionary::update_integer_value.
Definition dictionary.h:527
bool operator==(const dictionary_ &other) const
See Dictionary::operator==.
Definition dictionary.cpp:278
bool operator!=(const dictionary_ &other) const
See Dictionary::operator!=.
Definition dictionary.h:587
T get(const std::string &key) const
See Dictionary::get.
Definition dictionary.h:488
bool known(const std::string &key) const
See Dictionary::known.
Definition dictionary.h:555
bool has_been_accessed(const std::string &key) const
See Dictionary::has_been_accessed.
Definition dictionary.h:573
bool update_value(const std::string &key, T &value) const
See Dictionary::update_value.
Definition dictionary.h:511
any_type & operator[](const std::string &key)
Definition dictionary.cpp:321
Exception to be thrown if a given type does not match the expected type.
Definition exceptions.h:128
A concept for data types that can be stored in a Dictionary as any_type .
Definition dictionary.h:201
A concept for "integer integers", excluding bool and char.
Definition dictionary.h:192
std::string pretty_typename()
Return typename boost-prettified if available, otherwise as returned by typename().
Definition dictionary.h:354
DictionarySchema::VariantType< std::shared_ptr< nest::NodeCollection >, std::vector< std::vector< double > >, std::vector< std::vector< std::vector< double > > >, std::vector< std::vector< std::vector< long > > >, std::shared_ptr< nest::Parameter >, AnyVector, nest::VerbosityLevel, EmptyList > any_type
Complete any_type by adding specific data types.
Definition dictionary.h:123
std::ostream & operator<<(std::ostream &os, const EmptyList &)
Print EmptyList.
Definition dictionary.cpp:271
std::string get_dict_typenames(const Dictionary &dict)
Return multi-line string displaying dictionary content.
Definition dictionary.cpp:168
std::string get_typename(const any_type &operand)
Return typename of operand.
Definition dictionary.cpp:156
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
VerbosityLevel
Report only messages at levels higher than chosen level to user or logs. Default INFO.
Definition logging.h:40
Represent value in dictionary entry with access information.
Definition dictionary.h:383
bool accessed
initially false, set to true once entry is accessed
Definition dictionary.h:404
any_type item
actual item stored
Definition dictionary.h:403
bool operator==(const DictEntry_ &other) const
Definition dictionary.h:398
DictEntry_(const any_type &item)
Definition dictionary.h:391
DictEntry_()
Constructor without arguments needed by std::map::operator[].
Definition dictionary.h:385
Add a set of standard datatypes to any_type variant.
Definition dictionary.h:88
std::variant< std::monostate, Scalars..., std::vector< Scalars >..., Extras... > VariantType
Definition dictionary.h:90
Datatype to allow empty Python lists to be represented without defaulting to a datatype.
Definition dictionary.h:154
bool operator==(const EmptyList &) const =default