NMEA: detectio of the optional tag block in front of a sentence, ignoring it for...
[marnav.git] / src / marnav / nmea / checks.hpp
blobeb4201d37310cb968645186a06cd44e89dd20370
1 #ifndef __MARNAV__NMEA__CHECKS__HPP__
2 #define __MARNAV__NMEA__CHECKS__HPP__
4 #include <algorithm>
5 #include <stdexcept>
6 #include <string>
7 #include <marnav/nmea/constants.hpp>
8 #include <marnav/nmea/string.hpp>
9 #include <marnav/utils/optional.hpp>
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