@@ -57,105 +57,105 @@ namespace mbed {
57
57
* The size of the sequence can be encoded in the type itself or in the value of
58
58
* the instance with the help of the template parameter Extent:
59
59
*
60
- * - Span<uint8_t, 6>: Span over a sequence of 6 element
60
+ * - Span<uint8_t, 6>: Span over a sequence of 6 elements.
61
61
* - Span<uint8_t>: Span over an arbitrary long sequence.
62
62
*
63
63
* When the size is encoded in the type itself, it is guaranteed that the Span
64
- * view a valid sequence (not empty() and not NULL). The type system also prevent
65
- * automatic conversion from Span of different sizes. Finally, the size of the
66
- * span object is a single pointer.
64
+ * view a valid sequence (not empty() and not NULL) - unless Extent equals 0.
65
+ * The type system also prevents automatic conversion from Span of different
66
+ * sizes. Finally, the Span object is internally represented as a single pointer.
67
67
*
68
- * When the size of the sequence viewed is encoded in the Span value, span
69
- * instances can view invalid sequence ( empty and NULL pointer) . The function
70
- * empty() helps client code to decide if valid content is being viewed or not.
68
+ * When the size of the sequence viewed is encoded in the Span value, Span
69
+ * instances can view an empty sequence . The function empty() helps client code
70
+ * to decide if valid content is being viewed or not.
71
71
*
72
72
* @par Example
73
73
*
74
74
* - Encoding fixed size array: Array values in parameter decays automatically
75
75
* to pointer which leaves room for subtitle bugs:
76
76
*
77
77
* @code
78
- * typedef uint8_t mac_address_t[6];
79
- * void process_mac(mac_address_t);
80
- *
81
- * // compile just fine
82
- * uint8_t* invalid_value = NULL;
83
- * process_mac(invalid_value);
84
- *
85
- *
86
- * // correct way
87
- * typedef Span<uint8_t, 6> mac_address_t;
88
- * void process_mac(mac_address_t);
89
- *
90
- * // compilation error
91
- * uint8_t* invalid_value = NULL;
92
- * process_mac(invalid_value);
93
- *
94
- * // compilation ok
95
- * uint8_t valid_value[6];
96
- * process_mac(valid_value);
78
+ typedef uint8_t mac_address_t[6];
79
+ void process_mac(mac_address_t);
80
+
81
+ // compile just fine
82
+ uint8_t* invalid_value = NULL;
83
+ process_mac(invalid_value);
84
+
85
+
86
+ // correct way
87
+ typedef Span<uint8_t, 6> mac_address_t;
88
+ void process_mac(mac_address_t);
89
+
90
+ // compilation error
91
+ uint8_t* invalid_value = NULL;
92
+ process_mac(invalid_value);
93
+
94
+ // compilation ok
95
+ uint8_t valid_value[6];
96
+ process_mac(valid_value);
97
97
* @endcode
98
98
*
99
99
* - Arbitrary buffer: When dealing with multiple buffers, it becomes painful to
100
100
* keep track of every buffer size and pointer.
101
101
*
102
102
* @code
103
- * const uint8_t options_tag[OPTIONS_TAG_SIZE];
104
- *
105
- * struct parsed_value_t {
106
- * uint8_t* header;
107
- * uint8_t* options;
108
- * uint8_t* payload;
109
- * size_t payload_size;
110
- * }
111
- *
112
- * parsed_value_t parse(uint8_t* buffer, size_t buffer_size) {
113
- * parsed_value_t parsed_value { 0 };
114
- *
115
- * if (buffer != NULL && buffer_size <= MINIMAL_BUFFER_SIZE) {
116
- * return parsed_value;
117
- * }
118
- *
119
- * parsed_value.header = buffer;
120
- * parsed_value.header_size = BUFFER_HEADER_SIZE;
121
- *
122
- * if (memcmp(buffer + HEADER_OPTIONS_INDEX, options_tag, sizeof(options_tag)) == 0) {
123
- * options = buffer + BUFFER_HEADER_SIZE;
124
- * payload = buffer + BUFFER_HEADER_SIZE + OPTIONS_SIZE;
125
- * payload_size = buffer_size - BUFFER_HEADER_SIZE + OPTIONS_SIZE;
126
- * } else {
127
- * payload = buffer + BUFFER_HEADER_SIZE;
128
- * payload_size = buffer_size - BUFFER_HEADER_SIZE;
129
- * }
130
- *
131
- * return parsed_value;
132
- * }
133
- *
134
- *
135
- * //with span
136
- * struct parsed_value_t {
137
- * Span<uint8_t> header;
138
- * Span<uint8_t> options;
139
- * Span<uint8_t> payload;
140
- * }
141
- *
142
- * parsed_value_t parse(Span<uint8_t> buffer) {
143
- * parsed_value_t parsed_value;
144
- *
145
- * if (buffer.size() <= MINIMAL_BUFFER_SIZE) {
146
- * return parsed_value;
147
- * }
148
- *
149
- * parsed_value.header = buffer.first(BUFFER_HEADER_SIZE);
150
- *
151
- * if (buffer.subspan<HEADER_OPTIONS_INDEX, sizeof(options_tag)>() == option_tag) {
152
- * options = buffer.supspan(parsed_value.header.size(), OPTIONS_SIZE);
153
- * }
154
- *
155
- * payload = buffer.subspan(parsed_value.header.size() + parsed_value.options.size());
156
- *
157
- * return parsed_value;
158
- * }
103
+ const uint8_t options_tag[OPTIONS_TAG_SIZE];
104
+
105
+ struct parsed_value_t {
106
+ uint8_t* header;
107
+ uint8_t* options;
108
+ uint8_t* payload;
109
+ size_t payload_size;
110
+ }
111
+
112
+ parsed_value_t parse(uint8_t* buffer, size_t buffer_size) {
113
+ parsed_value_t parsed_value { 0 };
114
+
115
+ if (buffer != NULL && buffer_size <= MINIMAL_BUFFER_SIZE) {
116
+ return parsed_value;
117
+ }
118
+
119
+ parsed_value.header = buffer;
120
+ parsed_value.header_size = BUFFER_HEADER_SIZE;
121
+
122
+ if (memcmp(buffer + HEADER_OPTIONS_INDEX, options_tag, sizeof(options_tag)) == 0) {
123
+ options = buffer + BUFFER_HEADER_SIZE;
124
+ payload = buffer + BUFFER_HEADER_SIZE + OPTIONS_SIZE;
125
+ payload_size = buffer_size - BUFFER_HEADER_SIZE + OPTIONS_SIZE;
126
+ } else {
127
+ payload = buffer + BUFFER_HEADER_SIZE;
128
+ payload_size = buffer_size - BUFFER_HEADER_SIZE;
129
+ }
130
+
131
+ return parsed_value;
132
+ }
133
+
134
+
135
+ //with span
136
+ struct parsed_value_t {
137
+ Span<uint8_t> header;
138
+ Span<uint8_t> options;
139
+ Span<uint8_t> payload;
140
+ }
141
+
142
+ parsed_value_t parse(Span<uint8_t> buffer) {
143
+ parsed_value_t parsed_value;
144
+
145
+ if (buffer.size() <= MINIMAL_BUFFER_SIZE) {
146
+ return parsed_value;
147
+ }
148
+
149
+ parsed_value.header = buffer.first(BUFFER_HEADER_SIZE);
150
+
151
+ if (buffer.subspan<HEADER_OPTIONS_INDEX, sizeof(options_tag)>() == option_tag) {
152
+ options = buffer.supspan(parsed_value.header.size(), OPTIONS_SIZE);
153
+ }
154
+
155
+ payload = buffer.subspan(parsed_value.header.size() + parsed_value.options.size());
156
+
157
+ return parsed_value;
158
+ }
159
159
* @endcode
160
160
*
161
161
* @note You can create Span instances with the help of the function template
0 commit comments