Utopia  2
Framework for studying models of complex & adaptive systems.
utils.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_DATAIO_DATA_MANAGER_UTILS_HH
2 #define UTOPIA_DATAIO_DATA_MANAGER_UTILS_HH
3 
4 #include "../../core/type_traits.hh"
5 #include "../../core/zip.hh"
6 #include "../cfg_utils.hh"
7 
8 namespace Utopia
9 {
10 namespace DataIO
11 {
12 namespace _DMUtils
13 {
14 
36 template < typename... T >
38  : std::conjunction< Utopia::Utils::is_callable< T >... >
39 {
40 };
41 
53 template < class ValType, class KVPairs, class ObjMap >
54 void
55 unpack_shared(KVPairs&& kv_pairs, ObjMap&& map)
56 {
57  using namespace Utopia::Utils;
58 
59  // Distinguish between tuple-like key value pairs and others
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 }
92 
106 template < class ObjMap, class ValType, class KVPairs >
107 ObjMap
108 unpack_shared(KVPairs&& kv_pairs)
109 {
110  ObjMap map;
111  unpack_shared< ValType >(kv_pairs, map);
112  return map;
113 }
114 
147 template < class AssocsMap, class NamedTasks, class NamedDTMap, class Assocs >
148 AssocsMap
149 build_task_association_map(const NamedTasks& tasks,
150  const NamedDTMap& named_dts,
151  Assocs assocs = Assocs{})
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 }
187 
190 } // namespace _DMUtils
191 } // namespace DataIO
192 } // namespace Utopia
193 
194 #endif // UTOPIA_DATAIO_DATA_MANAGER_UTILS_HH
void for_each(const Utopia::ExecPolicy policy, InputIt first, InputIt last, UnaryFunction f)
Apply a function to a range.
Definition: parallel.hh:346
void unpack_shared(KVPairs &&kv_pairs, ObjMap &&map)
Helper function to unpack (key, value) container into a map of shared pointers of a type.
Definition: utils.hh:55
AssocsMap 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 tas...
Definition: utils.hh:149
Definition: metaprogramming.hh:43
constexpr bool has_static_size_v
Shorthand for has_static_size::value.
Definition: type_traits.hh:675
typename remove_qualifier< T >::type remove_qualifier_t
Shorthand for 'typename remove_qualifier::value'.
Definition: type_traits.hh:97
Definition: agent.hh:11
Metafunction for use with boost hana, check if all T are callable types.
Definition: utils.hh:39