From Jason Turner

[cpp.embed.gen]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpq_hxyo5v/{from.md → to.md} +208 -0
tmp/tmpq_hxyo5v/{from.md → to.md} RENAMED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### General <a id="cpp.embed.gen">[[cpp.embed.gen]]</a>
2
+
3
+ A *bracket resource search* for a sequence of characters searches a
4
+ sequence of places for a resource identified uniquely by that sequence
5
+ of characters. How the places are determined or the resource identified
6
+ is *implementation-defined*.
7
+
8
+ A *quote resource search* for a sequence of characters attempts to
9
+ identify a resource that is named by the sequence of characters. The
10
+ named resource is searched for in an *implementation-defined* manner. If
11
+ the implementation does not support a quote resource search for that
12
+ sequence of characters, or if the search fails, the result of the quote
13
+ resource search is the result of a bracket resource search for the same
14
+ sequence of characters.
15
+
16
+ A preprocessing directive of the form
17
+
18
+ ``` bnf
19
+ '# embed' header-name pp-tokensₒₚₜ new-line
20
+ ```
21
+
22
+ causes the replacement of that directive by preprocessing tokens derived
23
+ from data in the resource identified by *header-name*, as specified
24
+ below.
25
+
26
+ If the *header-name* is of the form
27
+
28
+ ``` bnf
29
+ '<' h-char-sequence '>'
30
+ ```
31
+
32
+ the resource is identified by a bracket resource search for the sequence
33
+ of characters of the *h-char-sequence*.
34
+
35
+ If the *header-name* is of the form
36
+
37
+ ``` bnf
38
+ '"' q-char-sequence '"'
39
+ ```
40
+
41
+ the resource is identified by a quote resource search for the sequence
42
+ of characters of the *q-char-sequence*.
43
+
44
+ If a bracket resource search fails, or if a quote or bracket resource
45
+ search identifies a resource that cannot be processed by the
46
+ implementation, the program is ill-formed.
47
+
48
+ [*Note 1*: If the resource cannot be processed, the program is
49
+ ill-formed even when processing `#embed` with `limit(0)`
50
+ [[cpp.embed.param.limit]] or evaluating `__has_embed`. — *end note*]
51
+
52
+ *Recommended practice:* A mechanism similar to, but distinct from, the
53
+ *implementation-defined* search paths used for `#include`
54
+ [[cpp.include]] is encouraged.
55
+
56
+ Either form of the `#embed` directive processes the *pp-tokens*, if
57
+ present, just as in normal text. The *pp-tokens* shall then have the
58
+ form *embed-parameter-seq*.
59
+
60
+ A resource is a source of data accessible from the translation
61
+ environment. A resource has an *implementation-resource-width*, which is
62
+ the *implementation-defined* size in bits of the resource. If the
63
+ implementation-resource-width is not an integral multiple of `CHAR_BIT`,
64
+ the program is ill-formed. Let *implementation-resource-count* be
65
+ implementation-resource-width divided by `CHAR_BIT`. Every resource also
66
+ has a *resource-count*, which is
67
+
68
+ - the value as computed from the optionally-provided `limit`
69
+ *embed-parameter* [[cpp.embed.param.limit]], if present;
70
+ - otherwise, the implementation-resource-count.
71
+
72
+ A resource is empty if the resource-count is zero.
73
+
74
+ [*Example 1*:
75
+
76
+ ``` cpp
77
+ // ill-formed if the implementation-resource-width is 6 bits
78
+ #embed "6_bits.bin"
79
+ ```
80
+
81
+ — *end example*]
82
+
83
+ The `#embed` directive is replaced by a comma-separated list of integer
84
+ literals of type `int`, unless otherwise modified by embed parameters
85
+ [[cpp.embed.param]].
86
+
87
+ The integer literals in the comma-separated list correspond to
88
+ resource-count consecutive calls to `std::fgetc` [[cstdio.syn]] from the
89
+ resource, as a binary file. If any call to `std::fgetc` returns `EOF`,
90
+ the program is ill-formed.
91
+
92
+ *Recommended practice:* The value of each integer literal should closely
93
+ represent the bit stream of the resource unmodified. This can require an
94
+ implementation to consider potential differences between translation and
95
+ execution environments, as well as any other applicable sources of
96
+ mismatch.
97
+
98
+ [*Example 2*:
99
+
100
+ ``` cpp
101
+ #include <cstring>
102
+ #include <cstddef>
103
+ #include <fstream>
104
+ #include <vector>
105
+ #include <cassert>
106
+
107
+ int main() {
108
+ // If the file is the same as the resource in the translation environment, no assert in this program should fail.
109
+ constexpr unsigned char d[] = {
110
+ #embed <data.dat>
111
+ };
112
+ const std::vector<unsigned char> vec_d = {
113
+ #embed <data.dat>
114
+ };
115
+
116
+ constexpr std::size_t expected_size = sizeof(d);
117
+
118
+ // same file in execution environment as was embedded
119
+ std::ifstream f_source("data.dat", std::ios::binary | std::ios::in);
120
+ unsigned char runtime_d[expected_size];
121
+ char* ifstream_ptr = reinterpret_cast<char*>(runtime_d);
122
+ assert(!f_source.read(ifstream_ptr, expected_size));
123
+ std::size_t ifstream_size = f_source.gcount();
124
+ assert (ifstream_size == expected_size);
125
+ int is_same = std::memcmp(&d[0], ifstream_ptr, ifstream_size);
126
+ assert(is_same == 0);
127
+ int is_same_vec = std::memcmp(vec_d.data(), ifstream_ptr, ifstream_size);
128
+ assert(is_same_vec == 0);
129
+ }
130
+ ```
131
+
132
+ — *end example*]
133
+
134
+ [*Example 3*:
135
+
136
+ ``` cpp
137
+ int i = {
138
+ #embed "i.dat"
139
+ }; // well-formed if i.dat produces a single value
140
+ int i2 =
141
+ #embed "i.dat"
142
+ ; // also well-formed if i.dat produces a single value
143
+ struct s {
144
+ double a, b, c;
145
+ struct { double e, f, g; } x;
146
+ double h, i, j;
147
+ };
148
+ s x = {
149
+ // well-formed if the directive produces nine or fewer values
150
+ #embed "s.dat"
151
+ };
152
+ ```
153
+
154
+ — *end example*]
155
+
156
+ A preprocessing directive of the form
157
+
158
+ ``` bnf
159
+ '# embed' pp-tokens new-line
160
+ ```
161
+
162
+ (that does not match the previous form) is permitted. The preprocessing
163
+ tokens after `embed` in the directive are processed just as in normal
164
+ text (i.e., each identifier currently defined as a macro name is
165
+ replaced by its replacement list of preprocessing tokens). Then, an
166
+ attempt is made to form a *header-name* preprocessing token
167
+ [[lex.header]] from the whitespace and the characters of the spellings
168
+ of the resulting sequence of preprocessing tokens immediately after
169
+ `embed`; the treatment of whitespace is *implementation-defined*. If the
170
+ attempt succeeds, the directive with the so-formed *header-name* is
171
+ processed as specified for the previous form. Otherwise, the program is
172
+ ill-formed.
173
+
174
+ [*Note 2*: Adjacent *string-literal*s are not concatenated into a
175
+ single *string-literal* (see the translation phases in  [[lex.phases]]);
176
+ thus, an expansion that results in two *string-literal*s is an invalid
177
+ directive. — *end note*]
178
+
179
+ Any further processing as in normal text described for the previous form
180
+ is not performed.
181
+
182
+ [*Note 3*: That is, processing as in normal text happens once and only
183
+ once for the entire directive. — *end note*]
184
+
185
+ [*Example 4*:
186
+
187
+ If the directive matches the second form, the whole directive is
188
+ replaced. If the directive matches the first form, everything after the
189
+ *header-name* is replaced.
190
+
191
+ ``` cpp
192
+ #define EMPTY
193
+ #define X myfile
194
+ #define Y rsc
195
+ #define Z 42
196
+ #embed <myfile.rsc> prefix(Z)
197
+ #embed EMPTY <X.Y> prefix(Z)
198
+ ```
199
+
200
+ is equivalent to:
201
+
202
+ ``` cpp
203
+ #embed <myfile.rsc> prefix(42)
204
+ #embed <myfile.rsc> prefix(42)
205
+ ```
206
+
207
+ — *end example*]
208
+