General: reorganization of header files
[marnav.git] / src / marnav / nmea / checks.hpp
blobb809f52c10418c3da1fb34b5a631712d2c7cece7
1 #ifndef MARNAV__NMEA__CHECKS__HPP
2 #define MARNAV__NMEA__CHECKS__HPP
4 #include <marnav/nmea/constants.hpp>
5 #include <marnav/nmea/string.hpp>
6 #include <marnav/utils/optional.hpp>
7 #include <algorithm>
8 #include <stdexcept>
9 #include <string>
11 namespace marnav
13 namespace nmea
15 /// @cond DEV
16 namespace
18 template <typename T>
19 static void throw_elaborated_invalid_argument(
20 T value, std::initializer_list<T> options, const char * name = nullptr)
22 std::string text;
23 text.reserve(64);
24 text += "invalid argument, value '";
25 text += to_string(value);
26 text += "' not in options:{";
27 for (auto const & opt : options) {
28 text += ' ';
29 text += to_string(opt);
31 text += " }";
32 if (name) {
33 text += " for '";
34 text += name;
35 text += '\'';
37 throw std::invalid_argument{text};
40 /// @endcond
42 /// Checks the value agains a list of possible values.
43 ///
44 /// @param[in] value Value to check.
45 /// @param[in] options Possible values to check against.
46 /// @param[in] name Optional name of the value to check. This name will be mentioned
47 /// in thrown exception, if the value is invalid.
48 ///
49 /// @exception std::invalid_argument The value is not listed in the options.
50 template <class T>
51 void check_value(T value, std::initializer_list<T> options, const char * name = nullptr)
53 using namespace std;
54 if (find_if(begin(options), end(options), [value](T opt) { return value == opt; })
55 != end(options))
56 return;
58 throw_elaborated_invalid_argument(value, options, name);
61 template <class T>
62 void check_value(const utils::optional<T> & value, std::initializer_list<T> options,
63 const char * name = nullptr)
65 if (value)
66 check_value(value.value(), options, name);
69 void check_status(status value, const char * name = nullptr);
71 void check_status(const utils::optional<status> & value, const char * name = nullptr);
75 #endif