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