netxsimdg
Loading...
Searching...
No Matches
EnumWrapper.hpp
Go to the documentation of this file.
1
8#ifndef ENUMWRAPPER_HPP
9#define ENUMWRAPPER_HPP
10
11#include <boost/program_options.hpp>
12#include <map>
13#include <string>
14
15// A helper class to make configuring enums easier with boost::program_options.
16
17namespace EnumWrap {
18template <typename E> class EnumWrapper;
19template <typename E> std::istream& operator>>(std::istream&, EnumWrapper<E>&);
20
58template <typename E> class EnumWrapper {
59public:
60 typedef std::map<std::string, E> MapType;
61
69 E operator()(const std::string& key)
70 {
71 value = map.at(key);
72 return value;
73 }
75 operator E() const { return value; }
76
82 static void setMap(const MapType& inMap)
83 {
84 map.clear();
85 map = inMap;
86 }
87
88 friend std::istream& operator>><E>(std::istream& is, EnumWrapper<E>&);
89
90private:
92 static MapType map;
93
94 E value;
95};
96
97template <typename E> typename EnumWrapper<E>::MapType EnumWrapper<E>::map;
98
101template <typename E> std::istream& operator>>(std::istream& is, EnumWrapper<E>& e)
102{
103 std::string tok;
104 is >> tok;
105 try {
106 e(tok);
107 } catch (const std::out_of_range& oor) {
108 throw boost::program_options::validation_error(
109 boost::program_options::validation_error::invalid_option);
110 }
111 return is;
112}
113}
114
115#endif // ndef ENUMWRAPPER_HPP
std::istream & operator>>(std::istream &, EnumWrapper< E > &)
A templated input operator that uses the defined map to set the value of the wrapped enum.
E operator()(const std::string &key)
static void setMap(const MapType &inMap)