Message.hpp
Go to the documentation of this file.
1 #ifndef MESSAGE_HPP
2 #define MESSAGE_HPP
3 
4 #include <iostream>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 #include "ByteVector.hpp"
9 #include "FirmwareVariants.hpp"
10 
11 namespace msp {
12 
13 enum class ID : uint16_t;
14 
15 class Message {
16 public:
21  virtual ID id() const = 0;
22 
28  Message(FirmwareVariant v) : fw_variant(v) {}
29 
33  virtual ~Message() {}
34 
40  void setFirmwareVariant(FirmwareVariant v) { fw_variant = v; }
41 
46  FirmwareVariant getFirmwareVariant() const { return fw_variant; }
47 
53  virtual bool decode(const ByteVector& /*data*/) { return false; }
54 
59  virtual ByteVectorUptr encode() const { return ByteVectorUptr(); }
60 
61  virtual std::ostream& print(std::ostream& s) const {
62  s << "Print method for message ID " << uint16_t(id())
63  << " is not implemented" << std::endl;
64  return s;
65  }
66 
67 protected:
69 };
70 
71 } // namespace msp
72 
73 inline std::ostream& operator<<(std::ostream& s, const msp::ID& id) {
74  s << int(id);
75  return s;
76 }
77 
78 inline std::ostream& operator<<(std::ostream& s, const msp::Message& val) {
79  return val.print(s);
80 }
81 
82 #endif // TYPES_HPP
Message(FirmwareVariant v)
Message constructor accepting a FirmwareVariant.
Definition: Message.hpp:28
FirmwareVariant getFirmwareVariant() const
Queries the firmware variant the message works with.
Definition: Message.hpp:46
std::ostream & operator<<(std::ostream &s, const msp::ID &id)
Definition: Message.hpp:73
virtual ByteVectorUptr encode() const
Encode all data into a ByteVector.
Definition: Message.hpp:59
ID
Definition: msp_msg.hpp:30
FirmwareVariant
Enum of firmware variants.
std::unique_ptr< ByteVector > ByteVectorUptr
Definition: ByteVector.hpp:465
virtual ~Message()
Message destructor.
Definition: Message.hpp:33
virtual bool decode(const ByteVector &)
Decode message contents from a ByteVector.
Definition: Message.hpp:53
virtual std::ostream & print(std::ostream &s) const
Definition: Message.hpp:61
void setFirmwareVariant(FirmwareVariant v)
Set the firmware the message should work with.
Definition: Message.hpp:40
FirmwareVariant fw_variant
Definition: Message.hpp:68


msp
Author(s): Christian Rauch
autogenerated on Tue Oct 6 2020 03:38:57