Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Classes | Functions
Utilities

Provides a number of helper functions for the datamanager class. More...

Collaboration diagram for Utilities:

Classes

struct  Utopia::DataIO::_DMUtils::all_callable< T >
 Metafunction for use with boost hana, check if all T are callable types. More...
 

Functions

template<class ValType , class KVPairs , class ObjMap >
void Utopia::DataIO::_DMUtils::unpack_shared (KVPairs &&kv_pairs, ObjMap &&map)
 Helper function to unpack (key, value) container into a map of shared pointers of a type.
 
template<class ObjMap , class ValType , class KVPairs >
ObjMap Utopia::DataIO::_DMUtils::unpack_shared (KVPairs &&kv_pairs)
 Helper function to unpack (key, value) container into an empty map of shared pointers of a type.
 
template<class AssocsMap , class NamedTasks , class NamedDTMap , class Assocs >
AssocsMap Utopia::DataIO::_DMUtils::build_task_association_map (const NamedTasks &tasks, const NamedDTMap &named_dts, Assocs assocs=Assocs{})
 Build an association map, i.e., a map that associates a decider/trigger name with a collection of tasknames.
 

Detailed Description

Provides a number of helper functions for the datamanager class.

Here some utility functions for the datamanager are implemented.

Function Documentation

◆ build_task_association_map()

template<class AssocsMap , class NamedTasks , class NamedDTMap , class Assocs >
AssocsMap Utopia::DataIO::_DMUtils::build_task_association_map ( const NamedTasks tasks,
const NamedDTMap named_dts,
Assocs  assocs = Assocs{} 
)

Build an association map, i.e., a map that associates a decider/trigger name with a collection of tasknames.

The Association Map is built from a map that associates names to tasks, a map that associates names and deciders/triggers, and a map or vector of pairs that associates each taskname with the name of a trigger/decider functor. If this last argument is not given, then a bijective association is attempted in which each task is associated with a trigger/decider that correponds to its position in the "named_dts" argument. This means that "tasks" and "named_dts" needs to be of equal length. If this is violated the function throws. If the "assocs" argument is given, the requirement of equal length is not necessary, because it specifies this bijective mapping. Note that this means that the "assocs" argument maps one decider/triggername to a taskname, and tasknames may repeat, while decider/triggernames do not.

Template Parameters
AssocsMapFinal map that maps names of deciders/triggers to a collection of task names
NamedTasksautomatically determined
NamedDTMapautomatically determined
Assocsautomatically determined
Parameters
tasksmap or vector of pairs containing (name, task).
named_dtsmap or vector of pairs containing (name, decider/trigger)
assocsmap or vector of pairs containing (taskname, decider-/triggername).
Returns
AssocsMap Map that maps a name of a decider/trigger to a vector of tasknames. This argument is optional: If it is not given, then the function will try to associate tasks and deciders/triggers one by one in a bijective way in the order given. If that fails, an error is thrown.
151 {})
152{
153
154 AssocsMap map;
155
156 // Check if helpers and tasks can be associated one by one bijectivly, if
157 // not, demand a map explicitly giving associations to be given, which can
158 // be used for association
159
160 if (tasks.size() != named_dts.size())
161 {
162 if (assocs.size() == 0)
163 {
164 throw std::invalid_argument(
165 "Error, explicit associations have to be given when mapping "
166 "unequal numbers of decider or trigger functions and tasks.");
167 }
168 else
169 {
170 for (auto&& [taskname, dt_name] : assocs)
171 {
172 map[dt_name].push_back(taskname);
173 }
174 }
175 }
176 else
177 {
178
179 for (auto&& [namedtask, namedhelper] : Itertools::zip(tasks, named_dts))
180 {
181 map[namedhelper.first].push_back(namedtask.first);
182 }
183 }
184
185 return map;
186}

◆ unpack_shared() [1/2]

ObjMap Utopia::DataIO::_DMUtils::unpack_shared ( KVPairs &&  kv_pairs)

Helper function to unpack (key, value) container into an empty map of shared pointers of a type.

Template Parameters
ObjMapThe name -> object map to create
ValTypeUsed in make_shared<ValType> call
KVPairsContainer of (key, value) pairs. Can also be a tuple.
Parameters
kv_pairsThe container of (key, value) pairs to unpack into a new map of type ObjMap
Returns
ObjMap The newly created and populated map
109{
110 ObjMap map;
111 unpack_shared< ValType >(kv_pairs, map);
112 return map;
113}

◆ unpack_shared() [2/2]

void Utopia::DataIO::_DMUtils::unpack_shared ( KVPairs &&  kv_pairs,
ObjMap &&  map 
)

Helper function to unpack (key, value) container into a map of shared pointers of a type.

Template Parameters
ValTypeUsed in make_shared<ValType> call
KVPairsContainer of (key, value) pairs. Can also be a tuple.
ObjMapThe name -> object map type
Parameters
kv_pairsThe container of (key, value) pairs to unpack into a new map of type ObjMap
56{
57 using namespace Utopia::Utils;
58
59 // Distinguish between tuple-like key value pairs and others
60 if constexpr (has_static_size_v< remove_qualifier_t< KVPairs > >)
61 {
62 using std::get; // enable ADL
63
64 // Build map from given key value pairs
65 boost::hana::for_each(kv_pairs, [&](auto&& kv) {
66 // Check if the deduced kv type is a base class of the
67 // given target ValType
68 if constexpr (std::is_base_of_v<
69 ValType,
70 std::tuple_element_t<
71 1,
72 remove_qualifier_t< decltype(kv) > > >)
73 {
74 map[get< 0 >(kv)] = std::make_shared< std::tuple_element_t<
75 1,
76 remove_qualifier_t< decltype(kv) > > >(get< 1 >(kv));
77 }
78 else
79 {
80 map[get< 0 >(kv)] = std::make_shared< ValType >(get< 1 >(kv));
81 }
82 });
83 }
84 else
85 {
86 for (const auto& [k, v] : kv_pairs)
87 {
88 map[k] = std::make_shared< ValType >(v);
89 }
90 }
91}
Definition metaprogramming.hh:43
typename remove_qualifier< T >::type remove_qualifier_t
Shorthand for 'typename remove_qualifier::value'.
Definition type_traits.hh:97