NMEA: AIS helper functions moved into its separate files.
[marnav.git] / src / marnav / nmea / ais_helper.hpp
blob821fb4d38b54eb5d35cce71cea24b752e50d4b07
1 #ifndef __MARNAV__NMEA__AIS_HELPER__HPP__
2 #define __MARNAV__NMEA__AIS_HELPER__HPP__
4 #include <vector>
5 #include <marnav/nmea/vdm.hpp>
7 namespace marnav
9 namespace nmea
11 /// @{
13 /// @brief Collects payload from proper NMEA sentences.
14 ///
15 /// @note This function assumes, that all sentences in the specified range are
16 /// providing payload (VDM or descendents).
17 ///
18 /// @param[in] begin Iterator pointing to the beginning of the messges to process.
19 /// @param[in] end Iterator pointing after the messages to process (will not be
20 /// processed).
21 /// @return The container with all payload and padding bit information.
22 ///
23 /// std::unique_ptr<nmea::sentence> variant. Example:
24 /// @code
25 /// std::vector<std::unique_ptr<nmea::sentence>> v;
26 /// v.push_back(nmea::make_sentence("..."));
27 /// v.push_back(nmea::make_sentence("..."));
28 /// auto data = nmea::collect_payload(v.begin(), v.end());
29 /// @endcode
30 template <class InputIt, typename std::enable_if<std::is_class<InputIt>::value
31 && std::is_convertible<typename InputIt::value_type,
32 std::unique_ptr<sentence>>::value,
33 int>::type
34 = 0>
35 std::vector<std::pair<std::string, uint32_t>> collect_payload(InputIt begin, InputIt end)
37 std::vector<std::pair<std::string, uint32_t>> v;
38 v.reserve(std::distance(begin, end));
40 for (; begin != end; ++begin) {
41 const auto & s = sentence_cast<nmea::vdm>(*begin);
42 v.push_back(make_pair(s->get_payload(), s->get_n_fill_bits()));
45 return v;
48 /// Object iterator variant. Example:
49 /// @code
50 /// std::vector<nmea::vdm> v; // collect data, for example:
51 /// v.push_back(nmea::vdm{});
52 /// v.push_back(nmea::vdm{});
53 /// auto data = nmea::collect_payload(v.begin(), v.end());
54 /// @endcode
55 template <class InputIt, typename std::enable_if<std::is_class<InputIt>::value
56 && !std::is_convertible<typename InputIt::value_type,
57 std::unique_ptr<sentence>>::value,
58 int>::type
59 = 0>
60 std::vector<std::pair<std::string, uint32_t>> collect_payload(InputIt begin, InputIt end)
62 std::vector<std::pair<std::string, uint32_t>> v;
63 v.reserve(std::distance(begin, end));
64 for (; begin != end; ++begin) {
65 v.push_back(make_pair(begin->get_payload(), begin->get_n_fill_bits()));
67 return v;
70 /// Pointer variant. Example:
71 /// @code
72 /// nmea::vdm v[3]; // initialize them in any way...
73 /// auto data = nmea::collect_payload(v, v + 3);
74 /// @endcode
75 /// or more modern:
76 /// @code
77 /// nmea::vdm v[3]; // initialize them in any way...
78 /// auto data = nmea::collect_payload(std::begin(v), std::end(v));
79 /// @endcode
80 /// this will not compile, because MTW does not provide payload:
81 /// @code
82 /// nmea::mtw v[3];
83 /// auto data = nmea::collect_payload(std::begin(v), std::end(v));
84 /// @endcode
85 template <class InputIt,
86 typename std::enable_if<std::is_pointer<InputIt>::value, int>::type = 0>
87 std::vector<std::pair<std::string, uint32_t>> collect_payload(InputIt begin, InputIt end)
89 std::vector<std::pair<std::string, uint32_t>> v;
90 v.reserve(std::distance(begin, end));
91 for (; begin != end; ++begin) {
92 v.push_back(make_pair(begin->get_payload(), begin->get_n_fill_bits()));
94 return v;
97 /// @}
99 std::vector<std::unique_ptr<nmea::sentence>> make_vdms(
100 const std::vector<std::pair<std::string, uint32_t>> & payload,
101 utils::optional<uint32_t> seq_msg_id = utils::optional<uint32_t>{},
102 ais_channel radio_channel = ais_channel::B);
106 #endif