Dev: improve robustness of CTags.cmake
[marnav.git] / src / marnav / nmea / tfi.cpp
blob405dafae9113e71247f5b2976ce68e54a2719f97
1 #include <marnav/nmea/tfi.hpp>
2 #include <marnav/nmea/io.hpp>
3 #include <stdexcept>
5 namespace marnav
7 namespace nmea
9 namespace
11 /// Converts data read from the NMEA string to the corresponding
12 /// enumerator.
13 ///
14 /// @param[in] value The numerical value to convert.
15 /// @return The corresponding enumerator.
16 /// @exception std::invalid_argument The specified value to convert is unknown.
17 ///
18 static tfi::state state_mapping(typename std::underlying_type<tfi::state>::type value)
20 switch (value) {
21 case 0:
22 return tfi::state::off;
23 case 1:
24 return tfi::state::on;
25 case 2:
26 return tfi::state::no_answer;
28 throw std::invalid_argument{"invaild value for conversion to tfi::state"};
32 std::string to_string(tfi::state value)
34 switch (value) {
35 case tfi::state::off:
36 return "0";
37 case tfi::state::on:
38 return "1";
39 case tfi::state::no_answer:
40 return "2";
42 throw std::invalid_argument{"invaild value for conversion of tfi::state"};
45 constexpr sentence_id tfi::ID;
46 constexpr const char * tfi::TAG;
47 constexpr const int tfi::num_sensors;
49 tfi::tfi()
50 : sentence(ID, TAG, talker::global_positioning_system)
52 for (auto & t : sensors_)
53 t = state::no_answer;
56 tfi::tfi(talker talk, fields::const_iterator first, fields::const_iterator last)
57 : sentence(ID, TAG, talk)
59 if (std::distance(first, last) != 3)
60 throw std::invalid_argument{"invalid number of fields in tfi"};
62 for (size_t i = 0; i < num_sensors; ++i)
63 read(*(first + i), sensors_[i], state_mapping);
66 void tfi::check_index(int index) const
68 if ((index < 0) || (index >= num_sensors)) {
69 throw std::out_of_range{"sensor index out of range"};
73 tfi::state tfi::get_sensor(int index) const
75 check_index(index);
76 return sensors_[index];
79 void tfi::set_sensor(int index, state t)
81 check_index(index);
82 sensors_[index] = t;
85 void tfi::append_data_to(std::string & s) const
87 for (auto const & t : sensors_)
88 append(s, to_string(t));