blob: 8ae7f1644e9dab0d97eb51fd7a5e9a12005fb588 [file] [log] [blame]
Kyle Stachowicz4addac62017-12-02 23:12:34 -08001#ifndef AOS_COMMON_MESSAGE_H_
2#define AOS_COMMON_MESSAGE_H_
3
4#include "aos/common/time.h"
5
6namespace aos {
7
8struct 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.
13class 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.
31 void SetTimeToNow() { sent_time = monotonic_clock::now(); }
32
33 // Writes the contents of the message to the provided buffer.
34 size_t Print(char *buffer, int length) const;
35
36 // Compares two messages for equality, excluding their sent_time.
37 bool EqualsNoTime(const Message & /*other*/) const { return true; }
38
39 static const MessageType *GetType();
40};
41
42// Specializations for the Builders will be automatically generated in the .q.h
43// header files with all of the handy builder methods.
44template <class T>
45class MessageBuilder {
46 public:
47 typedef T Message;
48 bool Send();
49};
50
51} // namespace aos
52
53#endif // AOS_COMMON_MESSAGE_H_