Kyle Stachowicz | 4addac6 | 2017-12-02 23:12:34 -0800 | [diff] [blame] | 1 | #ifndef AOS_COMMON_MESSAGE_H_ |
| 2 | #define AOS_COMMON_MESSAGE_H_ |
| 3 | |
| 4 | #include "aos/common/time.h" |
| 5 | |
| 6 | namespace aos { |
| 7 | |
| 8 | struct MessageType; |
| 9 | |
| 10 | // This class is a base class for all messages sent over queues. |
| 11 | // All of the methods are overloaded in (generated) subclasses to do the same |
| 12 | // thing for the whole thing. |
| 13 | class Message { |
| 14 | public: |
| 15 | // The time that the message was sent at. |
| 16 | monotonic_clock::time_point sent_time; |
| 17 | |
| 18 | Message() : sent_time(monotonic_clock::min_time) {} |
| 19 | |
| 20 | // Zeros out the time. |
| 21 | void Zero(); |
| 22 | // Returns the size of the common fields. |
| 23 | static size_t Size() { return sizeof(sent_time); } |
| 24 | |
| 25 | // Deserializes the common fields from the buffer. |
| 26 | size_t Deserialize(const char *buffer); |
| 27 | // Serializes the common fields into the buffer. |
| 28 | size_t Serialize(char *buffer) const; |
| 29 | |
| 30 | // Populates sent_time with the current time. |
Austin Schuh | f907f2e | 2018-01-02 01:15:27 -0800 | [diff] [blame^] | 31 | void SetTimeToNow() { |
| 32 | if (sent_time == monotonic_clock::min_time) { |
| 33 | sent_time = monotonic_clock::now(); |
| 34 | } |
| 35 | } |
Kyle Stachowicz | 4addac6 | 2017-12-02 23:12:34 -0800 | [diff] [blame] | 36 | |
| 37 | // Writes the contents of the message to the provided buffer. |
| 38 | size_t Print(char *buffer, int length) const; |
| 39 | |
| 40 | // Compares two messages for equality, excluding their sent_time. |
| 41 | bool EqualsNoTime(const Message & /*other*/) const { return true; } |
| 42 | |
| 43 | static const MessageType *GetType(); |
| 44 | }; |
| 45 | |
| 46 | // Specializations for the Builders will be automatically generated in the .q.h |
| 47 | // header files with all of the handy builder methods. |
| 48 | template <class T> |
| 49 | class MessageBuilder { |
| 50 | public: |
| 51 | typedef T Message; |
| 52 | bool Send(); |
| 53 | }; |
| 54 | |
| 55 | } // namespace aos |
| 56 | |
| 57 | #endif // AOS_COMMON_MESSAGE_H_ |