13std::unique_ptr<HDM> HDM::create(std::unique_ptr<Message0183> baseMessage) {
14 std::string context =
"HDM::create";
15 if (baseMessage->getSentenceType() !=
"HDM") {
16 NMEALIB_RETURN_ERROR(NotHDMException(context,
"Expected sentence type 'HDM', got " + baseMessage->getSentenceType()));
19 std::string payload = baseMessage->getPayload();
20 std::istringstream ss(payload);
22 std::vector<std::string> fields;
24 while (std::getline(ss, token,
',')) {
25 fields.push_back(token);
28 if (!payload.empty() && payload.back() ==
',') {
32 if (!fields.empty()) {
33 fields.erase(fields.begin());
36 if (fields.size() != 2) {
37 NMEALIB_RETURN_ERROR(NotHDMException(context,
"Invalid fields in HDM payload: expected 2, got " + std::to_string(fields.size()) +
". Payload: " + payload));
45 char indicator = fields[1].empty() ?
'\0' : fields[1][0];
47 return std::unique_ptr<HDM>(
new HDM(std::move(*baseMessage),
52HDM::HDM(Message0183 baseMessage,
54 char indicator) noexcept
55 : Message0183(std::move(baseMessage)),
57 indicator_(indicator) {}
59HDM::HDM(std::string talkerId,
64 indicator_(indicator) {}
67 return std::unique_ptr<HDM>(
new HDM(*
this));
71 std::ostringstream ss;
72 ss << this->toString(verbose);
73 ss << std::fixed << std::setprecision(1);
76 ss <<
"\tHeading: " << heading_ <<
" " << indicator_;
79 ss <<
"Heading=" << heading_ << indicator_;
85std::string HDM::composeRaw(
const std::string& talkerId,
88 std::ostringstream payloadStream;
89 payloadStream << talkerId <<
"HDM,";
90 payloadStream << std::fixed << std::setprecision(1) << heading <<
",";
91 payloadStream << indicator;
93 std::string payload = payloadStream.str();
94 return "$" + payload +
"\r\n";
Represents a parsed NMEA 0183 HDM (Heading – Magnetic) sentence.
double getHeading() const noexcept
Get the vessel heading in degrees magnetic.
std::string getStringContent(bool verbose) const noexcept override
Return a human-readable string representation of this message.
char getIndicator() const noexcept
Get the magnetic heading indicator character.
std::unique_ptr< nmealib::Message > clone() const override
Create a polymorphic copy of this HDM message.
bool operator==(const HDM &other) const noexcept
Compare two HDM messages for equality.
HDM(std::string talkerId, double heading, char indicator)
Construct an HDM message from individual field values.
Represents an NMEA 0183 sentence.
bool operator==(const Message0183 &other) const noexcept
Compares two Message0183 objects for equality based on their content and timestamp.
#define NMEALIB_RETURN_ERROR(exceptionExpr)
bool parseOptionalDouble(const std::string &text, double &value) noexcept