1 | #ifndef DATAMESSAGE_H_ |
---|
2 | #define DATAMESSAGE_H_ |
---|
3 | |
---|
4 | #define USE_MESSAGE_UTILITY |
---|
5 | |
---|
6 | #include <memory> |
---|
7 | #include <inttypes.h> |
---|
8 | |
---|
9 | // use message utility |
---|
10 | #ifdef USE_MESSAGE_UTILITY |
---|
11 | #include "ariba/utility/messages.h" |
---|
12 | namespace ariba { |
---|
13 | typedef utility::Message Message; |
---|
14 | } |
---|
15 | #endif |
---|
16 | |
---|
17 | namespace ariba { |
---|
18 | |
---|
19 | // define sequence number type |
---|
20 | typedef uint16_t seqnum_t; |
---|
21 | |
---|
22 | /** |
---|
23 | * This class wraps different representations of a message. In its current |
---|
24 | * version is allows to specify binary data (as void*) with a size specifying |
---|
25 | * the number of bytes of data or an message object that can be |
---|
26 | * serialized if necessary. The main idea is, that simulation environments |
---|
27 | * do not necessarily need to serialize messages. |
---|
28 | * |
---|
29 | * For performance reasons methods of this class are inlined where possible! |
---|
30 | * |
---|
31 | * @author Sebastian Mies <mies@tm.uka.de> |
---|
32 | */ |
---|
33 | class DataMessage { |
---|
34 | private: |
---|
35 | void* data; //< internal buffer pointer |
---|
36 | size_t size; //< internal buffer pointer size |
---|
37 | public: |
---|
38 | static const DataMessage UNSPECIFIED; //< default implementation of a data message |
---|
39 | |
---|
40 | /** |
---|
41 | * Default constructor for a data message |
---|
42 | */ |
---|
43 | inline DataMessage() { |
---|
44 | this->data = NULL; |
---|
45 | this->size = 0; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Constructor for a data message |
---|
50 | * @param data Data buffer to carry in the message |
---|
51 | * @param size Size of the buffer pointed to |
---|
52 | */ |
---|
53 | inline DataMessage( const void* data, const size_t size ) { |
---|
54 | this->data = const_cast<void*>(data); |
---|
55 | this->size = size; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Copy constructor for a data message |
---|
60 | * @param message The other message to copy from |
---|
61 | */ |
---|
62 | inline DataMessage(const DataMessage& message){ |
---|
63 | this->data = message.data; |
---|
64 | this->size = message.size; |
---|
65 | } |
---|
66 | |
---|
67 | #ifdef USE_MESSAGE_UTILITY |
---|
68 | /** |
---|
69 | * Construct a data message from a normal message |
---|
70 | * @param message The normal message to store |
---|
71 | */ |
---|
72 | inline DataMessage( const Message* message ) { |
---|
73 | this->data = (void*)const_cast<Message*>(message); |
---|
74 | this->size = ~0; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Construct a data message from a normal message |
---|
79 | * @param message The normal message to store |
---|
80 | */ |
---|
81 | inline DataMessage( const Message& message ) { |
---|
82 | this->data = (void*)const_cast<Message*>(&message); |
---|
83 | this->size = ~0; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Get the internal message when constructued through one |
---|
88 | * @return pointer to the message |
---|
89 | */ |
---|
90 | inline Message* getMessage() const { |
---|
91 | if (isData()) { |
---|
92 | return new Message( Data((uint8_t*)data,size*8) ); |
---|
93 | } |
---|
94 | return (Message*)data; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Conversion function to convert to Message* |
---|
99 | * @return internal message |
---|
100 | */ |
---|
101 | inline operator Message* () const { |
---|
102 | return getMessage(); |
---|
103 | } |
---|
104 | #endif |
---|
105 | |
---|
106 | /** |
---|
107 | * Is the data message a normal message? |
---|
108 | * @return true, if the data message is a normal message |
---|
109 | */ |
---|
110 | inline bool isMessage() const { |
---|
111 | return size == ~(size_t)0; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Is the data message a data message |
---|
116 | * @return true, if the data message is not a normal message |
---|
117 | */ |
---|
118 | inline bool isData() const { |
---|
119 | return !isMessage(); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Directly access the internal data pointer |
---|
124 | * @return internal data pointer |
---|
125 | */ |
---|
126 | inline void* getData() const { |
---|
127 | return data; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Get the size of the internal buffer |
---|
132 | * @return internal buffer size |
---|
133 | */ |
---|
134 | inline size_t getSize() const { |
---|
135 | return size; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Is the data message invalid? |
---|
140 | * @return true, if data message is invalid |
---|
141 | */ |
---|
142 | inline bool isUnspecified() const { |
---|
143 | return data == NULL; |
---|
144 | } |
---|
145 | |
---|
146 | }; |
---|
147 | |
---|
148 | } // namespace ariba |
---|
149 | |
---|
150 | #endif /* DATAMESSAGE_H_ */ |
---|