Skip to content

Commit 33ca101

Browse files
committed
Span: Fix documentation.
1 parent 6b08320 commit 33ca101

File tree

1 file changed

+82
-82
lines changed

1 file changed

+82
-82
lines changed

platform/Span.h

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -57,105 +57,105 @@ namespace mbed {
5757
* The size of the sequence can be encoded in the type itself or in the value of
5858
* the instance with the help of the template parameter Extent:
5959
*
60-
* - Span<uint8_t, 6>: Span over a sequence of 6 element
60+
* - Span<uint8_t, 6>: Span over a sequence of 6 elements.
6161
* - Span<uint8_t>: Span over an arbitrary long sequence.
6262
*
6363
* 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.
6767
*
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.
7171
*
7272
* @par Example
7373
*
7474
* - Encoding fixed size array: Array values in parameter decays automatically
7575
* to pointer which leaves room for subtitle bugs:
7676
*
7777
* @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);
9797
* @endcode
9898
*
9999
* - Arbitrary buffer: When dealing with multiple buffers, it becomes painful to
100100
* keep track of every buffer size and pointer.
101101
*
102102
* @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+
}
159159
* @endcode
160160
*
161161
* @note You can create Span instances with the help of the function template

0 commit comments

Comments
 (0)