bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
std_map_utils.h
Go to the documentation of this file.
1#ifndef BITSIM_UTILS_STD_MAP_UTILS_H
2#define BITSIM_UTILS_STD_MAP_UTILS_H
3
8
9#include <any>
10#include <map>
11#include <stdexcept>
12#include <unordered_map>
13
14namespace bitsim
15{
16namespace utils
17{
18
23template <typename OutT>
24OutT resolve(const std::string &name, const std::map<std::string, std::any> &input)
25{
26
27 auto itr = input.find(name);
28
29 if (itr == input.end())
30 {
31 throw std::logic_error("Property: " + name + " not in input");
32 }
33
34 return std::any_cast<OutT>(itr->second);
35}
36
41template <typename OutT>
42OutT resolve(const std::string &name, const std::unordered_map<std::string, std::any> &input)
43{
44 auto itr = input.find(name);
45
46 if (itr == input.end())
47 {
48 throw std::logic_error("Property: " + name + " not in input");
49 }
50
51 try
52 {
53 return std::any_cast<OutT>(itr->second);
54 }
55 catch (const std::bad_any_cast &)
56 {
57 throw std::logic_error("Property: " + name + " has unexpected type");
58 }
59}
60
61} // namespace utils
62} // namespace bitsim
63
64#endif // BITSIM_UTILS_STD_MAP_UTILS_H
Definition bitsim_utils.h:11
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:24
Definition dc_motor_backend_base.h:9