Compare commits
617 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9a295fecf | ||
|
|
5c01ad387f | ||
|
|
42ca1b9140 | ||
|
|
6ed780dbd5 | ||
|
|
3412fab8ee | ||
|
|
8a7d013f85 | ||
|
|
170af59788 | ||
|
|
c8c16de167 | ||
|
|
4525cd9c56 | ||
|
|
47361baf61 | ||
|
|
8a582883ac | ||
|
|
a4bb8f0c2e | ||
|
|
82aed82b84 | ||
|
|
60ff76630d | ||
|
|
a27c7d8093 | ||
|
|
3632208233 | ||
|
|
6b44c605b8 | ||
|
|
d3cd78a87c | ||
|
|
8fa3dfdb5d | ||
|
|
29379e8e49 | ||
|
|
14725d6c3b | ||
|
|
16836ff95a | ||
|
|
c87179e73e | ||
|
|
c24031c36b | ||
|
|
4ccc52c7a0 | ||
|
|
b82fe07384 | ||
|
|
1e7f6c8c7c | ||
|
|
84714b109b | ||
|
|
1045d7dd44 | ||
|
|
0c77775966 | ||
|
|
c43ba17520 | ||
|
|
da3bb64ef6 | ||
|
|
151900ba96 | ||
|
|
c012f29f91 | ||
|
|
df2df21ec1 | ||
|
|
408e4db4af | ||
|
|
4d2364f342 | ||
|
|
261cf3b204 | ||
|
|
cd67261bba | ||
|
|
1aa0c2f6a9 | ||
|
|
4133a39df8 | ||
|
|
1c26d2a1a0 | ||
|
|
276b1bc342 | ||
|
|
124654ffc4 | ||
|
|
3b7d1e86b4 | ||
|
|
78f0c0d1d9 | ||
|
|
c992eafb5b | ||
|
|
ef8dd7792a | ||
|
|
69b329fc87 | ||
|
|
6543ba5297 | ||
|
|
0e453ac352 | ||
|
|
e9b4ae69dc | ||
|
|
fc4fffea41 | ||
|
|
b240ab704d | ||
|
|
9a4f1f434b | ||
|
|
fac64918dc | ||
|
|
a69815f72c | ||
|
|
4033ff5892 | ||
|
|
a083572512 | ||
|
|
5319dedb1a | ||
|
|
bd4e0b30a7 | ||
|
|
bf90612007 | ||
|
|
8142fedd19 | ||
|
|
c0be1cb7a5 | ||
|
|
8cccdfba53 | ||
|
|
cbbbaa61b3 | ||
|
|
ffc2ef77ca | ||
|
|
1da6f4f18b | ||
|
|
b5da526e6d | ||
|
|
3b5365762d | ||
|
|
e2f5438ac1 | ||
|
|
5e3613f732 | ||
|
|
e6b911d40c | ||
|
|
0c7ae58164 | ||
|
|
ae603b9770 | ||
|
|
7f47718b6d | ||
|
|
54dc09e8ac | ||
|
|
334c6be496 | ||
|
|
4174c10e7a | ||
|
|
a20f606c29 | ||
|
|
a72a208272 | ||
|
|
86401e078d | ||
|
|
6af37e6729 | ||
|
|
1b88655b00 | ||
|
|
0b15916e50 | ||
|
|
60eed0ca66 | ||
|
|
0f83367f57 | ||
|
|
fee095410b | ||
|
|
6f3e45eca1 | ||
|
|
815d3e820d | ||
|
|
76e7a0ff55 | ||
|
|
6d91096a2f | ||
|
|
13d9e35858 | ||
|
|
6effe431bb | ||
|
|
efcbdc7698 | ||
|
|
e581013e3d | ||
|
|
0984d4328d | ||
|
|
786f69b248 | ||
|
|
1da0a2dfac | ||
|
|
796ed68faf | ||
|
|
7b1ee31d80 | ||
|
|
4aff1198dd | ||
|
|
ad3a729f96 | ||
|
|
52e2177069 | ||
|
|
760c657551 | ||
|
|
75c859e98f | ||
|
|
91b0958c43 | ||
|
|
8008dde117 | ||
|
|
c81cf82492 | ||
|
|
8573108bbe | ||
|
|
7abe612b59 | ||
|
|
408cf58024 | ||
|
|
39e115fdb4 | ||
|
|
85719669cb | ||
|
|
809fe49c7a | ||
|
|
41253e574e | ||
|
|
08d2ce844b | ||
|
|
a15a8d930d | ||
|
|
83ce29cc22 | ||
|
|
4363c1d2cb | ||
|
|
1bf1ec0270 | ||
|
|
0800976533 | ||
|
|
795408115a | ||
|
|
46545e6273 | ||
|
|
0168178a17 | ||
|
|
82836a62be | ||
|
|
e7430bbebd | ||
|
|
24dd85fd28 | ||
|
|
57f68e2896 | ||
|
|
44cf2bde19 | ||
|
|
be37d4da14 | ||
|
|
4e79d129cb | ||
|
|
05192553f4 | ||
|
|
f2511d7d49 | ||
|
|
f8b203c9c4 | ||
|
|
8ab7c7e2c8 | ||
|
|
7e00390289 | ||
|
|
65c415911a | ||
|
|
a9e91116d2 | ||
|
|
80a745d9b0 | ||
|
|
9fca5e4f42 | ||
|
|
92a806b4e8 | ||
|
|
9c9baf6d58 | ||
|
|
aafc5dc950 | ||
|
|
442949bc11 | ||
|
|
9064072e8c | ||
|
|
fd4c1b5ff7 | ||
|
|
bc7eb8adeb | ||
|
|
fc960f3670 | ||
|
|
f437f0f7ed | ||
|
|
7f33cf682a | ||
|
|
8d9eae9ac9 | ||
|
|
2046bffa40 | ||
|
|
338393f854 | ||
|
|
c27bc2d76f | ||
|
|
bc518a5127 | ||
|
|
100c59054c | ||
|
|
e58c182443 | ||
|
|
69a8b2a579 | ||
|
|
25eba6f35f | ||
|
|
e1f0f75ba6 | ||
|
|
faeb04fbe1 | ||
|
|
537212afee | ||
|
|
6764f25d91 | ||
|
|
d6a8dbd26f | ||
|
|
ed391e1777 | ||
|
|
a49531414b | ||
|
|
de1f0342c8 | ||
|
|
d0d51e2a5c | ||
|
|
33ab26017d | ||
|
|
febb9d87c9 | ||
|
|
8778dc7c2b | ||
|
|
aae376e9a8 | ||
|
|
0ff0471488 | ||
|
|
46a8c7e958 | ||
|
|
bc56c553ec | ||
|
|
07d7cd78a7 | ||
|
|
cdef70e246 | ||
|
|
9dd44df35c | ||
|
|
c9b29d0885 | ||
|
|
fba93e0abb | ||
|
|
d1a545b1fb | ||
|
|
ea92a668d8 | ||
|
|
6034de286a | ||
|
|
b08b0a4402 | ||
|
|
17ae48decc | ||
|
|
fc80979253 | ||
|
|
55658f523a | ||
|
|
14ecfe4236 | ||
|
|
a0182cdb11 | ||
|
|
0dfcc0a378 | ||
|
|
f9a18ea635 | ||
|
|
c7586e85aa | ||
|
|
914c646014 | ||
|
|
42d7c79977 | ||
|
|
e68e8d7de9 | ||
|
|
84809be7e7 | ||
|
|
1606fb6375 | ||
|
|
fe8e3c7e5d | ||
|
|
8f6fa4b715 | ||
|
|
b46db38f57 | ||
|
|
9fa1d27059 | ||
|
|
a402b3abae | ||
|
|
0e1415b996 | ||
|
|
5cd7137103 | ||
|
|
5be777e1d4 | ||
|
|
a49d440ecd | ||
|
|
4ec5e8db90 | ||
|
|
04bec23a37 | ||
|
|
77d57fd075 | ||
|
|
543c1bbeba | ||
|
|
cb971eece8 | ||
|
|
7b9e61fccf | ||
|
|
3359e3042f | ||
|
|
187a4787f9 | ||
|
|
08943aa26f | ||
|
|
5975658ebd | ||
|
|
5d3cf440e5 | ||
|
|
8ec8322f09 | ||
|
|
bbcc85fd4c | ||
|
|
0bdf2fa156 | ||
|
|
2eedc769d5 | ||
|
|
ab01ae1620 | ||
|
|
689bfafa7e | ||
|
|
641309a5bf | ||
|
|
52e3628794 | ||
|
|
dca12522a9 | ||
|
|
e0bbaa6f9c | ||
|
|
ab139d6bea | ||
|
|
34d67b425e | ||
|
|
96d5e35977 | ||
|
|
eb686a86f3 | ||
|
|
750281630b | ||
|
|
fb4e1c34f9 | ||
|
|
8c67b5b129 | ||
|
|
6228b66d3d | ||
|
|
e1be8aaadd | ||
|
|
94873e595c | ||
|
|
b8e87fafe4 | ||
|
|
f96d1ef744 | ||
|
|
89435303b7 | ||
|
|
c75ae24293 | ||
|
|
338944d3d9 | ||
|
|
f5ab24bc41 | ||
|
|
92a8c1a0f2 | ||
|
|
6cea45dcd3 | ||
|
|
fec58aa129 | ||
|
|
71aca81ff9 | ||
|
|
04d87ffec3 | ||
|
|
bb25956f09 | ||
|
|
49f4948f06 | ||
|
|
eeacc53d22 | ||
|
|
f3003e08d0 | ||
|
|
d713a00843 | ||
|
|
77f966f89f | ||
|
|
e86d5b8e97 | ||
|
|
db2aa9b4ec | ||
|
|
63cc0eec4e | ||
|
|
c30a87de6f | ||
|
|
a0fb30575c | ||
|
|
77c18c1d69 | ||
|
|
f1f23d08ed | ||
|
|
f1025b2847 | ||
|
|
5d052f4e55 | ||
|
|
18b015d25a | ||
|
|
d76e93f277 | ||
|
|
82fac326c5 | ||
|
|
043b52bd4a | ||
|
|
c8fa0afdfc | ||
|
|
6d0aae73cd | ||
|
|
ff1b73128d | ||
|
|
2e48c8dd31 | ||
|
|
6942704f2b | ||
|
|
9ecd2e16c2 | ||
|
|
33e2d80791 | ||
|
|
969d0f7a63 | ||
|
|
515a4052a7 | ||
|
|
36fbe6f13e | ||
|
|
b69fc8cc95 | ||
|
|
ab6af18d9f | ||
|
|
37a5dee105 | ||
|
|
8a721f69a1 | ||
|
|
e810635eaa | ||
|
|
4995e15273 | ||
|
|
60b6066feb | ||
|
|
35d45cac7a | ||
|
|
165a6e3d1e | ||
|
|
13d3fb2ea6 | ||
|
|
d64078eb27 | ||
|
|
ca1190a3d8 | ||
|
|
7571b2ac56 | ||
|
|
e5a8f76a4b | ||
|
|
413bb9b553 | ||
|
|
f35184aef9 | ||
|
|
b124b76258 | ||
|
|
0ec7600c67 | ||
|
|
14baf45c90 | ||
|
|
9abb2ec2cc | ||
|
|
2e57d80b13 | ||
|
|
cfc7753a4c | ||
|
|
12ddc8a920 | ||
|
|
24ad35709d | ||
|
|
cc44a44427 | ||
|
|
9ab4a5c0e2 | ||
|
|
6682cfe870 | ||
|
|
64922904bc | ||
|
|
8e505cb677 | ||
|
|
a28357d7ac | ||
|
|
7cb4762a61 | ||
|
|
4e45f7c9e8 | ||
|
|
7ac0264053 | ||
|
|
108e981dbe | ||
|
|
94a78e3853 | ||
|
|
53fb453e04 | ||
|
|
17c1f35fa0 | ||
|
|
2eaf57778c | ||
|
|
666800da36 | ||
|
|
38ed69eb3d | ||
|
|
d026e6f071 | ||
|
|
988164f6e1 | ||
|
|
7179a5a8ba | ||
|
|
a0da0c08c6 | ||
|
|
ac203b2092 | ||
|
|
8dd1bf25b5 | ||
|
|
5aa443d98c | ||
|
|
0fa087657e | ||
|
|
424a473e1f | ||
|
|
c3faa83463 | ||
|
|
91399ad055 | ||
|
|
32782e4ad1 | ||
|
|
e7f3b16905 | ||
|
|
12ed1fe4a9 | ||
|
|
85ee4df7a2 | ||
|
|
de89bd1933 | ||
|
|
8be05f6bd4 | ||
|
|
870ecbc09a | ||
|
|
c2da8d5d85 | ||
|
|
e84cbff673 | ||
|
|
f94e6c84e0 | ||
|
|
f12cca8bcb | ||
|
|
7e4124d6e6 | ||
|
|
a875d247ad | ||
|
|
6e9f5d9810 | ||
|
|
ff1c78233d | ||
|
|
2e9a196734 | ||
|
|
e3cb07d321 | ||
|
|
712866d57b | ||
|
|
44c919a9e8 | ||
|
|
99aa1ef21d | ||
|
|
40ba170c94 | ||
|
|
cb4d0f72e3 | ||
|
|
003e164057 | ||
|
|
21cf300f4c | ||
|
|
9655e12d6d | ||
|
|
fb96fadc20 | ||
|
|
408f11fbdd | ||
|
|
a83caf5910 | ||
|
|
925fab6b15 | ||
|
|
d9fecc3327 | ||
|
|
e9d4532401 | ||
|
|
c37c989ed6 | ||
|
|
6b271b7ecb | ||
|
|
90f3b8e8c7 | ||
|
|
3af7359345 | ||
|
|
c4231c3cb9 | ||
|
|
9657df184e | ||
|
|
97ffc590e3 | ||
|
|
8b52af65bc | ||
|
|
9b034eee12 | ||
|
|
3e9ac3cff9 | ||
|
|
697147a2e6 | ||
|
|
6df40a2471 | ||
|
|
0dba63909f | ||
|
|
0e3fdd0eea | ||
|
|
45a2b07cbd | ||
|
|
d10c163142 | ||
|
|
35abb7f89b | ||
|
|
9954e09ab7 | ||
|
|
95a21327fc | ||
|
|
89b6183ee9 | ||
|
|
5a98d65e86 | ||
|
|
f73d205bc7 | ||
|
|
7c37abe92d | ||
|
|
4749e77b0e | ||
|
|
6ff1898413 | ||
|
|
c9a30c9ca2 | ||
|
|
8c02d17bea | ||
|
|
34305c4ce4 | ||
|
|
cd88e6b2aa | ||
|
|
3ec7a53c62 | ||
|
|
6d44cede70 | ||
|
|
cc08c0835b | ||
|
|
54f8b787cb | ||
|
|
17557f9131 | ||
|
|
d54af8cd43 | ||
|
|
173e10fdf1 | ||
|
|
8f56990f6c | ||
|
|
6400c9b054 | ||
|
|
7418d85872 | ||
|
|
c580fa284c | ||
|
|
f2a1272303 | ||
|
|
316d7c2089 | ||
|
|
47026ea6ba | ||
|
|
3f677f2414 | ||
|
|
a593a11e59 | ||
|
|
7cdfc8475e | ||
|
|
bab2b0db48 | ||
|
|
89418eb848 | ||
|
|
9cadf05d89 | ||
|
|
6da1cf79d9 | ||
|
|
bee1df96dc | ||
|
|
01189d7edd | ||
|
|
c4b2b0a25d | ||
|
|
a4b2884e4e | ||
|
|
04d80f255d | ||
|
|
55686100aa | ||
|
|
718351831d | ||
|
|
d1b34f0f28 | ||
|
|
21b7061963 | ||
|
|
35daaf83d3 | ||
|
|
3b458f7a17 | ||
|
|
a5d9d0f7d3 | ||
|
|
3cd9b6434a | ||
|
|
355dfd48d1 | ||
|
|
bcd58a159b | ||
|
|
a2c12900aa | ||
|
|
901b89e733 | ||
|
|
31f8799083 | ||
|
|
8023d99e21 | ||
|
|
b4154405d1 | ||
|
|
04c17c7a76 | ||
|
|
62ec7d52ce | ||
|
|
7de668053f | ||
|
|
3a70e0b308 | ||
|
|
9b13201356 | ||
|
|
5e3916050c | ||
|
|
c957550511 | ||
|
|
44bf719883 | ||
|
|
3e8f15df90 | ||
|
|
602721a735 | ||
|
|
13c05f4da3 | ||
|
|
ce3a1c43a2 | ||
|
|
aa75e5734b | ||
|
|
2790fee257 | ||
|
|
eddebec1b6 | ||
|
|
030fee36ab | ||
|
|
f9724d1bde | ||
|
|
b20801ca40 | ||
|
|
a8e800bd7c | ||
|
|
d7530ae961 | ||
|
|
99d11e279f | ||
|
|
4fd8eb214b | ||
|
|
65f8703572 | ||
|
|
75823cc275 | ||
|
|
58e279244c | ||
|
|
3c964e10ab | ||
|
|
c3c32ec942 | ||
|
|
075e8d676b | ||
|
|
bcf1bd5c9e | ||
|
|
136d75fa65 | ||
|
|
091fa1fd1b | ||
|
|
ff3781dc2d | ||
|
|
6beb9f49cb | ||
|
|
80988ea869 | ||
|
|
0f2ff7eaa9 | ||
|
|
dda095023d | ||
|
|
adbcbba5d1 | ||
|
|
cbbd6aca04 | ||
|
|
405c64e07d | ||
|
|
42c08cbca6 | ||
|
|
33d5dd9bdd | ||
|
|
105dd528e9 | ||
|
|
f0f0efe7b8 | ||
|
|
e837d5a296 | ||
|
|
9834ee9787 | ||
|
|
44b2ab087c | ||
|
|
46ae3f80a6 | ||
|
|
7b38aa71e6 | ||
|
|
661bedd837 | ||
|
|
8526e12d73 | ||
|
|
3c7b660d62 | ||
|
|
964365ba69 | ||
|
|
32254b7acd | ||
|
|
521e255ad9 | ||
|
|
1b85292fd3 | ||
|
|
480815447a | ||
|
|
8d5e424c65 | ||
|
|
b4774d2354 | ||
|
|
26f238c248 | ||
|
|
e93c8c46e6 | ||
|
|
e21516b9d7 | ||
|
|
fbc11e8aec | ||
|
|
e9d29c21a7 | ||
|
|
8bfafc76de | ||
|
|
df3e8bf4a7 | ||
|
|
5665cfe492 | ||
|
|
5797540ed0 | ||
|
|
7f1af7cb02 | ||
|
|
32f47ad247 | ||
|
|
842f672baf | ||
|
|
d4cae0a623 | ||
|
|
f1147f65bb | ||
|
|
69d3fec488 | ||
|
|
cfb4ecf6f0 | ||
|
|
a92039687a | ||
|
|
625338d095 | ||
|
|
3f8ce99c50 | ||
|
|
0798b7b698 | ||
|
|
cbdf82e2fb | ||
|
|
e365c502ff | ||
|
|
97f3aa9174 | ||
|
|
2f5bb2eec4 | ||
|
|
917687c7a6 | ||
|
|
f9277e691d | ||
|
|
2706381eef | ||
|
|
b5560fcd52 | ||
|
|
782b865c55 | ||
|
|
3bfc86eaff | ||
|
|
c0282873fb | ||
|
|
4b870aca98 | ||
|
|
d0e3870c0f | ||
|
|
fb25eb87f2 | ||
|
|
cb35d3a0e5 | ||
|
|
8e6cabb31b | ||
|
|
bd31dd2425 | ||
|
|
65b67d2132 | ||
|
|
1fbb711324 | ||
|
|
cd75a36587 | ||
|
|
ec6b0bf297 | ||
|
|
c11b5d7447 | ||
|
|
4525c91be3 | ||
|
|
b97b342f59 | ||
|
|
c1058a903b | ||
|
|
3030449348 | ||
|
|
a2485d4ecc | ||
|
|
a20e71ac96 | ||
|
|
acc9990abd | ||
|
|
2d5315ff0e | ||
|
|
7de1a5e347 | ||
|
|
a4e3ad808e | ||
|
|
c953fa572b | ||
|
|
0c86085a5b | ||
|
|
a1f7ecd148 | ||
|
|
a20c55bea5 | ||
|
|
d7c2b388ef | ||
|
|
9fa8245e81 | ||
|
|
a5ca8bee4d | ||
|
|
09dea79a22 | ||
|
|
b632061eff | ||
|
|
da88be05e1 | ||
|
|
d4fa984f1d | ||
|
|
a0c0131e36 | ||
|
|
de9aa0cdee | ||
|
|
a752d1b88c | ||
|
|
db972be264 | ||
|
|
e304f8c115 | ||
|
|
47c7aa0361 | ||
|
|
7a63792929 | ||
|
|
7d7d796cd0 | ||
|
|
550b386995 | ||
|
|
5479adc80f | ||
|
|
b7012484f3 | ||
|
|
92e9f33036 | ||
|
|
ff1a22a05f | ||
|
|
9fb195cce8 | ||
|
|
7836e65dd4 | ||
|
|
123c7a4890 | ||
|
|
e635141d5b | ||
|
|
0d2cebccfe | ||
|
|
a80db8538c | ||
|
|
a6be1d0d74 | ||
|
|
a7e20b1996 | ||
|
|
4eb3efc221 | ||
|
|
a807fa9567 | ||
|
|
b80ad7e439 | ||
|
|
16aef8ac0d | ||
|
|
b59a1ca2f8 | ||
|
|
4fdfe0d468 | ||
|
|
95004218f7 | ||
|
|
bc7ede8fb3 | ||
|
|
b652fcc3a7 | ||
|
|
c978b9ef1f | ||
|
|
3a88e1031b | ||
|
|
51dd733ba4 | ||
|
|
79f0df3dfc | ||
|
|
9d92fd92e1 | ||
|
|
93f74c0363 | ||
|
|
43dbac5d25 | ||
|
|
53ea1ab1bd | ||
|
|
b10b050ab9 | ||
|
|
563dcd6893 | ||
|
|
30ac512a54 | ||
|
|
b04736f9bd | ||
|
|
bc240b3004 | ||
|
|
0f7e7fd209 | ||
|
|
766ed04422 | ||
|
|
c5e2d37337 | ||
|
|
fe83b68ac6 | ||
|
|
718ddea558 | ||
|
|
8d86b5347f | ||
|
|
39bd667fd0 | ||
|
|
0bb3ce6935 | ||
|
|
b56d60f058 | ||
|
|
bff7ffbc51 | ||
|
|
107c08988a | ||
|
|
f9ebfcb9c4 | ||
|
|
b701c7d56e | ||
|
|
103f61b685 | ||
|
|
e47ca7ab40 | ||
|
|
d79f4e9717 | ||
|
|
5d67693e8f | ||
|
|
af74f87ccd | ||
|
|
b8ef8c1521 | ||
|
|
6cc30b3272 | ||
|
|
e5b6125fa2 | ||
|
|
ac14c8906f | ||
|
|
9936adf473 |
@@ -19,6 +19,7 @@ call generate_code.bat -b %buildtype% || goto FAIL
|
||||
|
||||
:: TODO: Release and Debug builds produce differences here for some reason.
|
||||
git checkout HEAD -- monster_test.bfbs
|
||||
git checkout HEAD -- arrays_test.bfbs
|
||||
|
||||
git -c core.autocrlf=true diff --exit-code --quiet || goto :DIFFFOUND
|
||||
goto SUCCESS
|
||||
|
||||
13
.eslintrc.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/* eslint-env node */
|
||||
|
||||
module.exports = {
|
||||
root: true,
|
||||
parser: '@typescript-eslint/parser',
|
||||
plugins: [
|
||||
'@typescript-eslint',
|
||||
],
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
]
|
||||
};
|
||||
8
.github/PULL_REQUEST_TEMPLATE.md
vendored
@@ -1,16 +1,20 @@
|
||||
Thank you for submitting a PR!
|
||||
|
||||
Please make sure you include the names of the affected language(s) in your PR title.
|
||||
Please delete this standard text once you've created your own description.
|
||||
|
||||
Make sure you include the names of the affected language(s) in your PR title.
|
||||
This helps us get the correct maintainers to look at your issue.
|
||||
|
||||
If you make changes to any of the code generators, be sure to run
|
||||
`cd tests && sh generate_code.sh` (or equivalent .bat) and include the generated
|
||||
`cd tests && bash generate_code.sh` (or equivalent .bat) and include the generated
|
||||
code changes in the PR. This allows us to better see the effect of the PR.
|
||||
|
||||
If your PR includes C++ code, please adhere to the Google C++ Style Guide,
|
||||
and don't forget we try to support older compilers (e.g. VS2010, GCC 4.6.3),
|
||||
so only some C++11 support is available.
|
||||
|
||||
For any C++ changes, please make sure to run `sh src/clang-format-git.sh`
|
||||
|
||||
Include other details as appropriate.
|
||||
|
||||
Thanks!
|
||||
|
||||
96
.github/labeler.yml
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
# Configuration for Auto Labeler during pull request
|
||||
#
|
||||
# See https://github.com/actions/labeler for file format
|
||||
# and https://github.com/google/flatbuffers/labels for a list of valid labels
|
||||
#
|
||||
# See .github/workflows/label.yml for Github Action workflow script
|
||||
|
||||
c#:
|
||||
- '**/*.cs'
|
||||
- net/**/*
|
||||
- tests/FlatBuffers.Test/**/*
|
||||
- tests/FlatBuffers.Benchmarks/**/*
|
||||
- src/idl_gen_csharp.cpp
|
||||
|
||||
swift:
|
||||
- '**/*.swift'
|
||||
- swift/**/*
|
||||
- tests/FlatBuffers.GRPC.Swift/**/*
|
||||
- tests/FlatBuffers.Benchmarks.swift/**/*
|
||||
- tests/FlatBuffers.Test.Swift/**/*
|
||||
- src/idl_gen_swift.cpp
|
||||
|
||||
javascript:
|
||||
- '**/*.js'
|
||||
- src/idl_gen_ts.cpp
|
||||
|
||||
typescript:
|
||||
- '**/*.ts'
|
||||
- src/idl_gen_ts.cpp
|
||||
- grpc/flatbuffers-js-grpc/**/*.ts
|
||||
|
||||
golang:
|
||||
- '**/*.go'
|
||||
- src/idl_gen_go.cpp
|
||||
|
||||
python:
|
||||
- '**/*.py'
|
||||
- src/idl_gen_python.cpp
|
||||
|
||||
java:
|
||||
- '**/*.java'
|
||||
- src/idl_gen_java.cpp
|
||||
|
||||
kotlin:
|
||||
- '**/*.kt'
|
||||
- src/idl_gen_kotlin.cpp
|
||||
|
||||
lua:
|
||||
- '**/*.lua'
|
||||
- lua/**/*
|
||||
- src/idl_gen_lua.cpp
|
||||
|
||||
lobster:
|
||||
- '**/*.lobster'
|
||||
- src/idl_gen_lobster.cpp
|
||||
|
||||
php:
|
||||
- '**/*.php'
|
||||
- src/idl_gen_php.cpp
|
||||
|
||||
rust:
|
||||
- '**/*.rs'
|
||||
- rust/**/*
|
||||
- src/idl_gen_rust.cpp
|
||||
|
||||
dart:
|
||||
- '**/*.dart'
|
||||
- src/idl_gen_dart.cpp
|
||||
|
||||
c++:
|
||||
- '**/*.cc'
|
||||
- '**/*.cpp'
|
||||
- '**/*.h'
|
||||
|
||||
json:
|
||||
- '**/*.json'
|
||||
- src/idl_gen_json_schema.cpp
|
||||
|
||||
codegen:
|
||||
- src/**/*
|
||||
|
||||
documentation:
|
||||
- docs/**/*
|
||||
- '**/*.md'
|
||||
|
||||
CI:
|
||||
- '.github/**/*'
|
||||
- '.appveyor/**/*'
|
||||
- '.travis/**/*'
|
||||
- '.bazelci/**/*'
|
||||
- .travis.yml
|
||||
- appveyor.yml
|
||||
|
||||
grpc:
|
||||
- grpc/**/*
|
||||
- src/idl_gen_grpc.cpp
|
||||
229
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build-linux:
|
||||
name: Build Linux
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
cxx: [g++-9, clang++-9]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: cmake
|
||||
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release .
|
||||
- name: build
|
||||
run: make -j4
|
||||
- name: test
|
||||
run: ./flattests
|
||||
- name: upload build artifacts
|
||||
uses: actions/upload-artifact@v1
|
||||
with:
|
||||
name: Linux flatc binary ${{ matrix.cxx }}
|
||||
path: flatc
|
||||
|
||||
build-windows:
|
||||
name: Build Windows
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Add msbuild to PATH
|
||||
uses: microsoft/setup-msbuild@v1.0.2
|
||||
- name: cmake
|
||||
run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_CPP17=ON .
|
||||
- name: build
|
||||
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64
|
||||
- name: test
|
||||
run: Release\flattests.exe
|
||||
- name: upload build artifacts
|
||||
uses: actions/upload-artifact@v1
|
||||
with:
|
||||
name: Windows flatc binary
|
||||
path: Release\flatc.exe
|
||||
|
||||
build-windows-2017:
|
||||
name: Build Windows 2017
|
||||
runs-on: windows-2016
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Add msbuild to PATH
|
||||
uses: microsoft/setup-msbuild@v1.0.2
|
||||
- name: cmake
|
||||
run: cmake -G "Visual Studio 15 2017" -A x64 -DCMAKE_BUILD_TYPE=Release .
|
||||
- name: build
|
||||
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64
|
||||
- name: test
|
||||
run: Release\flattests.exe
|
||||
|
||||
build-mac:
|
||||
name: Build Mac
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: cmake
|
||||
run: cmake -G "Xcode" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_FLATC_EXECUTABLE=_build/Release/flatc .
|
||||
- name: build
|
||||
# NOTE: we need this _build dir to not have xcodebuild's default ./build dir clash with the BUILD file.
|
||||
run: xcodebuild -toolchain clang -configuration Release -target flattests SYMROOT=$(PWD)/_build
|
||||
- name: test
|
||||
run: _build/Release/flattests
|
||||
- name: upload build artifacts
|
||||
uses: actions/upload-artifact@v1
|
||||
with:
|
||||
name: Mac flatc binary
|
||||
path: _build/Release/flatc
|
||||
|
||||
build-android:
|
||||
name: Build Android (on Linux)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: set up JDK 1.8
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
- name: set up flatc
|
||||
run: |
|
||||
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .
|
||||
make
|
||||
echo "${PWD}" >> $GITHUB_PATH
|
||||
- name: build
|
||||
working-directory: android
|
||||
run: bash ./gradlew clean build
|
||||
|
||||
build-generator:
|
||||
name: Check Generated Code
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
cxx: [g++-9, clang++-9]
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: cmake
|
||||
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release . && make -j4
|
||||
- name: Generate
|
||||
run: bash scripts/check-generate-code.sh && bash scripts/check-grpc-generated-code.sh
|
||||
|
||||
build-java:
|
||||
name: Build Java
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: test
|
||||
working-directory: tests
|
||||
run: bash JavaTest.sh
|
||||
|
||||
build-kotlin:
|
||||
name: Build Kotlin
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v1
|
||||
- name: Build
|
||||
working-directory: kotlin
|
||||
run: ./gradlew clean build allTests
|
||||
- name: Run Benchmark
|
||||
working-directory: kotlin
|
||||
run: ./gradlew benchmark
|
||||
- name: Generate Benchmark Report
|
||||
working-directory: kotlin
|
||||
run: |
|
||||
./gradlew jmhReport;
|
||||
mv benchmark/build/reports/benchmarks/main/* benchmark_latest
|
||||
- name: Archive benchmark report
|
||||
uses: actions/upload-artifact@v1
|
||||
with:
|
||||
name: Kotlin Benchmark Report
|
||||
path: kotlin/benchmark_latest
|
||||
|
||||
build-rust:
|
||||
name: Build Rust
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: test
|
||||
working-directory: tests
|
||||
run: bash RustTest.sh
|
||||
|
||||
#build-js:
|
||||
# name: Build JS
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
# - uses: actions/checkout@v1
|
||||
# - name: flatc
|
||||
# # FIXME: make test script not rely on flatc
|
||||
# run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j4
|
||||
# - name: test
|
||||
# working-directory: tests
|
||||
# run: bash JavaScriptTest.sh
|
||||
|
||||
build-python:
|
||||
name: Build Python
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: test
|
||||
working-directory: tests
|
||||
run: bash PythonTest.sh
|
||||
|
||||
build-go:
|
||||
name: Build Go
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: flatc
|
||||
# FIXME: make test script not rely on flatc
|
||||
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j4
|
||||
- name: test
|
||||
working-directory: tests
|
||||
run: bash GoTest.sh
|
||||
|
||||
#build-csharp:
|
||||
# name: Build CSharp
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
# - uses: actions/checkout@v1
|
||||
# - name: test
|
||||
# working-directory: tests/FlatBuffers.Test
|
||||
# run: bash NetTest.sh
|
||||
|
||||
#build-php:
|
||||
# name: Build PHP
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
# - uses: actions/checkout@v1
|
||||
# - name: flatc
|
||||
# # FIXME: make test script not rely on flatc
|
||||
# run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j4
|
||||
# - name: test
|
||||
# working-directory: tests
|
||||
# run: |
|
||||
# php phpTest.php
|
||||
# sh phpUnionVectorTest.sh
|
||||
|
||||
build-swift:
|
||||
name: Build Swift
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: test
|
||||
working-directory: tests/FlatBuffers.Test.Swift
|
||||
run: sh SwiftTest.sh
|
||||
|
||||
build-ts:
|
||||
name: Build TS
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: compile
|
||||
run: npm run compile
|
||||
- name: test
|
||||
working-directory: tests
|
||||
run: sh TypeScriptTest.sh
|
||||
19
.github/workflows/label.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
# This workflow will triage pull requests and apply a label based on the
|
||||
# paths that are modified in the pull request.
|
||||
#
|
||||
# To use this workflow, you will need to set up a .github/labeler.yml
|
||||
# file with configuration. For more information, see:
|
||||
# https://github.com/actions/labeler
|
||||
|
||||
name: Labeler
|
||||
on: [pull_request_target]
|
||||
|
||||
jobs:
|
||||
label:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/labeler@main
|
||||
with:
|
||||
repo-token: "${{ secrets.GITHUB_TOKEN }}"
|
||||
20
.github/workflows/stale.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
name: Mark stale issues and pull requests
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "30 20 * * *"
|
||||
|
||||
jobs:
|
||||
stale:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/stale@v3
|
||||
with:
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
stale-pr-message: 'This pull request is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.'
|
||||
stale-issue-message: 'This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.'
|
||||
days-before-stale: 182 # 6 months
|
||||
days-before-close: 14
|
||||
operations-per-run: 1500
|
||||
29
.gitignore
vendored
@@ -1,6 +1,13 @@
|
||||
*_wire.txt
|
||||
*_wire.bin
|
||||
.DS_Store
|
||||
**/.build
|
||||
build
|
||||
**/Packages
|
||||
/*.xcodeproj
|
||||
**/xcuserdata/
|
||||
**/xcshareddata/
|
||||
**/.swiftpm/
|
||||
*.o
|
||||
*.o.d
|
||||
*.class
|
||||
@@ -63,8 +70,6 @@ tests/monsterdata_go_wire.mon
|
||||
tests/monsterdata_javascript_wire.mon
|
||||
tests/monsterdata_lobster_wire.mon
|
||||
tests/monsterdata_rust_wire.mon
|
||||
tests/unicode_test.mon
|
||||
tests/ts/
|
||||
tests/php/
|
||||
CMakeLists.txt.user
|
||||
CMakeScripts/**
|
||||
@@ -116,3 +121,23 @@ dart/doc/api/
|
||||
Cargo.lock
|
||||
.corpus**
|
||||
.seed**
|
||||
.crash**
|
||||
grpc/google/
|
||||
**/Package.resolved
|
||||
.clangd/**
|
||||
package-lock.json
|
||||
/*.ilk
|
||||
/*.pdb
|
||||
.clwb
|
||||
js/**/*.js
|
||||
js/**/*.d.ts
|
||||
mjs/**/*.js
|
||||
mjs/**/*.d.ts
|
||||
yarn-error.log
|
||||
.cache/
|
||||
/flatbuffers.lib
|
||||
.cmake/
|
||||
**/dist
|
||||
**/vendor
|
||||
**/go.sum
|
||||
flatbuffers.pc
|
||||
|
||||
77
.travis.yml
@@ -22,6 +22,21 @@ conan-linux: &conan-linux
|
||||
- ./conan/travis/build.sh
|
||||
if: tag IS present
|
||||
|
||||
conan-linux-master: &conan-linux-master
|
||||
os: linux
|
||||
dist: xenial
|
||||
language: python
|
||||
python: "3.7"
|
||||
services:
|
||||
- docker
|
||||
install:
|
||||
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./conan/travis/install.sh; fi'
|
||||
script:
|
||||
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./conan/travis/build.sh; fi'
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
|
||||
conan-osx: &conan-osx
|
||||
os: osx
|
||||
language: generic
|
||||
@@ -78,7 +93,7 @@ matrix:
|
||||
env:
|
||||
matrix:
|
||||
- BUILD_TYPE=Debug
|
||||
- BUILD_TYPE=Release CONAN=true
|
||||
- BUILD_TYPE=Release
|
||||
|
||||
before_install:
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
|
||||
@@ -89,6 +104,7 @@ matrix:
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which gcc-$GCC_VERSION) /usr/bin/gcc; fi
|
||||
|
||||
script:
|
||||
- pip install cmake
|
||||
- bash .travis/check-sources.sh
|
||||
- bash grpc/build_grpc.sh
|
||||
- cmake .
|
||||
@@ -97,10 +113,9 @@ matrix:
|
||||
-DGRPC_INSTALL_PATH=$TRAVIS_BUILD_DIR/google/grpc/install
|
||||
-DPROTOBUF_DOWNLOAD_PATH=$TRAVIS_BUILD_DIR/google/grpc/third_party/protobuf
|
||||
-DFLATBUFFERS_CODE_SANITIZE=ON
|
||||
- cmake --build . -- -j${JOBS}
|
||||
- cmake --build . --target all --clean-first -- -j${JOBS}
|
||||
- LD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/google/grpc/install/lib ctest --extra-verbose --output-on-failure
|
||||
- bash .travis/check-generate-code.sh
|
||||
- if [ "$CONAN" == "true" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo pip install conan && conan create . flatbuffers/${TRAVIS_BRANCH}@google/testing -s build_type=$BUILD_TYPE -tf conan/test_package; fi
|
||||
- bash scripts/check-generate-code.sh
|
||||
|
||||
- language: cpp
|
||||
os: osx
|
||||
@@ -111,6 +126,11 @@ matrix:
|
||||
- BUILD_TYPE=Release
|
||||
|
||||
script:
|
||||
- pip install --user cmake
|
||||
- mkdir ~/cmake_path
|
||||
- ln -s $(find ~/Library/Python -name cmake -type f | head -n 1) ~/cmake_path/cmake
|
||||
- ln -s $(find ~/Library/Python -name ctest -type f | head -n 1) ~/cmake_path/ctest
|
||||
- export PATH=~/cmake_path:${PATH}
|
||||
- bash grpc/build_grpc.sh
|
||||
- cmake .
|
||||
-DCMAKE_BUILD_TYPE=$BUILD_TYPE
|
||||
@@ -120,8 +140,10 @@ matrix:
|
||||
-DFLATBUFFERS_CODE_SANITIZE=ON
|
||||
- cmake --build . -- -j${JOBS}
|
||||
- DYLD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/google/grpc/install/lib ctest --extra-verbose --output-on-failure
|
||||
- bash .travis/check-generate-code.sh
|
||||
- bash scripts/check-generate-code.sh
|
||||
|
||||
- <<: *conan-linux-master
|
||||
env: CONAN_GCC_VERSIONS=8 CONAN_DOCKER_IMAGE=conanio/gcc8
|
||||
- <<: *conan-linux
|
||||
env: CONAN_GCC_VERSIONS=4.9 CONAN_DOCKER_IMAGE=conanio/gcc49
|
||||
- <<: *conan-linux
|
||||
@@ -132,6 +154,8 @@ matrix:
|
||||
env: CONAN_GCC_VERSIONS=7 CONAN_DOCKER_IMAGE=conanio/gcc7
|
||||
- <<: *conan-linux
|
||||
env: CONAN_GCC_VERSIONS=8 CONAN_DOCKER_IMAGE=conanio/gcc8
|
||||
- <<: *conan-linux
|
||||
env: CONAN_GCC_VERSIONS=9 CONAN_DOCKER_IMAGE=conanio/gcc9
|
||||
- <<: *conan-linux
|
||||
env: CONAN_CLANG_VERSIONS=3.9 CONAN_DOCKER_IMAGE=conanio/clang39
|
||||
- <<: *conan-linux
|
||||
@@ -140,6 +164,10 @@ matrix:
|
||||
env: CONAN_CLANG_VERSIONS=5.0 CONAN_DOCKER_IMAGE=conanio/clang50
|
||||
- <<: *conan-linux
|
||||
env: CONAN_CLANG_VERSIONS=6.0 CONAN_DOCKER_IMAGE=conanio/clang60
|
||||
- <<: *conan-linux
|
||||
env: CONAN_CLANG_VERSIONS=7.0 CONAN_DOCKER_IMAGE=conanio/clang7
|
||||
- <<: *conan-linux
|
||||
env: CONAN_CLANG_VERSIONS=8 CONAN_DOCKER_IMAGE=conanio/clang8
|
||||
- <<: *conan-osx
|
||||
osx_image: xcode7.3
|
||||
env: CONAN_APPLE_CLANG_VERSIONS=7.3
|
||||
@@ -153,34 +181,37 @@ matrix:
|
||||
osx_image: xcode9.4
|
||||
env: CONAN_APPLE_CLANG_VERSIONS=9.1
|
||||
- <<: *conan-osx
|
||||
osx_image: xcode10
|
||||
osx_image: xcode10.2
|
||||
env: CONAN_APPLE_CLANG_VERSIONS=10.0
|
||||
|
||||
- language: android
|
||||
sudo: true
|
||||
dist: trusty
|
||||
android:
|
||||
components:
|
||||
- tools
|
||||
- platform-tools
|
||||
- build-tools-25.0.2
|
||||
- android-25
|
||||
- extra-android-m2repository
|
||||
licenses:
|
||||
- 'android-sdk-preview-license-52d11cd2'
|
||||
- 'android-sdk-license-.+'
|
||||
- 'google-gdk-license-.+'
|
||||
compiler:
|
||||
- gcc
|
||||
|
||||
before_install:
|
||||
# Output something every 10 minutes or Travis kills the job
|
||||
- while sleep 540; do echo "=====[ $SECONDS seconds still running ]====="; done &
|
||||
- git clone https://github.com/urho3d/android-ndk.git $HOME/android-ndk-root
|
||||
- export ANDROID_NDK_HOME=$HOME/android-ndk-root
|
||||
# Setup environment for Linux build which is required to build the sample.
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq g++-$GCC_VERSION; fi
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq gcc-$GCC_VERSION; fi
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which g++-$GCC_VERSION) /usr/bin/g++; fi
|
||||
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which gcc-$GCC_VERSION) /usr/bin/gcc; fi
|
||||
- echo y | sdkmanager "platforms;android-30"
|
||||
- echo y | sdkmanager "build-tools;30.0.2"
|
||||
- echo y | sdkmanager "ndk-bundle"
|
||||
- echo y | sdkmanager "cmake;3.6.4111459"
|
||||
script:
|
||||
- failed=0; for build_gradle in $(git ls-files | grep build.gradle); do ( cd "$(dirname "${build_gradle}")" && ./gradlew build ) || failed=1; done; exit $((failed))
|
||||
# Kill the sleep loop
|
||||
- kill %1
|
||||
- cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .; make; export PATH="$PATH:${PWD}"
|
||||
- cd android; ./gradlew clean build
|
||||
|
||||
- language: generic
|
||||
if: type IN (pull_request)
|
||||
os: linux
|
||||
install:
|
||||
- bash .travis/format_install.sh
|
||||
|
||||
script:
|
||||
- bash .travis/format_check.sh
|
||||
|
||||
@@ -15,10 +15,15 @@
|
||||
# limitations under the License.
|
||||
set -e
|
||||
|
||||
# build flatc on debian once to speed up the test loop below
|
||||
docker build -t build_flatc_debian_stretch -f tests/docker/Dockerfile.testing.build_flatc_debian_stretch .
|
||||
BUILD_CONTAINER_ID=$(docker create --read-only build_flatc_debian_stretch)
|
||||
docker cp ${BUILD_CONTAINER_ID}:/code/flatc flatc_debian_stretch
|
||||
docker build -t build_cpp_image -f tests/docker/Dockerfile.testing.cpp.debian_buster .
|
||||
# Run tests with sanitizers (--cap-add SYS_PTRACE), both GCC and Clang.
|
||||
cpp_test_args="--cap-add SYS_PTRACE build_cpp_image sh ./tests/docker/cpp_test.run.sh Debug"
|
||||
docker run --rm $cpp_test_args
|
||||
docker run --rm --env CC=/usr/bin/clang --env CXX=/usr/bin/clang++ $cpp_test_args
|
||||
# Build flatc on debian once to speed up the test loop below.
|
||||
docker run --name flatc_container build_cpp_image sh ./tests/docker/build_flatc.run.sh Debug
|
||||
# All dependent dockers refer to 'flatc_debian_stretch'.
|
||||
docker cp flatc_container:/flatbuffers/flatc flatc_debian_stretch
|
||||
|
||||
for f in $(ls tests/docker/languages | sort)
|
||||
do
|
||||
|
||||
52
.travis/format_check.sh
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2021 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set -e
|
||||
|
||||
# HACKY solution to make nodejs work.
|
||||
source ~/.nvm/nvm.sh
|
||||
nvm alias default node
|
||||
nvm use default
|
||||
|
||||
sh src/clang-format-git.sh
|
||||
|
||||
# Check formatting for go lang
|
||||
|
||||
cd go
|
||||
gofmt -w .
|
||||
cd ..
|
||||
cd grpc/examples/go
|
||||
sh format.sh
|
||||
cd ../../..
|
||||
|
||||
node_modules/.bin/eslint ts/** --ext .ts --quiet --fix
|
||||
|
||||
#PYTHON IS DISABLED UNTIL WE CREATE A .pylintrc FILE FOR IT
|
||||
pylint python/** --disable=all
|
||||
|
||||
swiftformat --config swift.swiftformat .
|
||||
|
||||
|
||||
if ! git diff --quiet; then
|
||||
echo >&2
|
||||
echo "ERROR: ********************************************************" >&2
|
||||
echo "ERROR: The following differences were found after running" >&2
|
||||
echo "ERROR: .travis/format_check.sh script. Maybe you forgot to format" >&2
|
||||
echo "ERROR: the code after making changes? please check Formatters.md" >&2
|
||||
echo "ERROR: ********************************************************" >&2
|
||||
echo >&2
|
||||
git diff --binary --exit-code
|
||||
fi
|
||||
90
.travis/format_install.sh
Normal file
@@ -0,0 +1,90 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2020 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set -e
|
||||
set -x
|
||||
|
||||
# install devtools
|
||||
install_languages() {
|
||||
sudo apt update
|
||||
|
||||
# Install nodeJS and yarn
|
||||
wget https://raw.githubusercontent.com/creationix/nvm/v0.31.0/nvm.sh -O ~/.nvm/nvm.sh
|
||||
source ~/.nvm/nvm.sh
|
||||
nvm install node
|
||||
node --version
|
||||
curl -o- -L https://yarnpkg.com/install.sh | bash
|
||||
export PATH="$HOME/.yarn/bin:$PATH"
|
||||
yarn config set prefix ~/.yarn -g
|
||||
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
|
||||
|
||||
# Install swift
|
||||
sudo apt-get install \
|
||||
binutils \
|
||||
git \
|
||||
libc6-dev \
|
||||
libcurl3 \
|
||||
libedit2 \
|
||||
libgcc-5-dev \
|
||||
libpython2.7 \
|
||||
libsqlite3-0 \
|
||||
libstdc++-5-dev \
|
||||
libxml2 \
|
||||
pkg-config \
|
||||
tzdata \
|
||||
zlib1g-dev
|
||||
|
||||
SWIFT_URL=https://swift.org/builds/swift-5.3.1-release/ubuntu1604/swift-5.3.1-RELEASE/swift-5.3.1-RELEASE-ubuntu16.04.tar.gz
|
||||
curl -fSsL "$SWIFT_URL" -o swift.tar.gz
|
||||
|
||||
mkdir ~/swiftbuild
|
||||
tar -xvzf swift.tar.gz -C ~/swiftbuild
|
||||
|
||||
export PATH="~/swiftbuild/swift-5.3.1-RELEASE-ubuntu16.04/usr/bin:$PATH"
|
||||
|
||||
|
||||
mkdir ~/gobuild
|
||||
wget -c https://golang.org/dl/go1.15.2.linux-amd64.tar.gz
|
||||
tar -xvzf go1.15.2.linux-amd64.tar.gz -C ~/gobuild
|
||||
|
||||
export PATH="~/gobuild/go/bin:$PATH"
|
||||
|
||||
swift --version
|
||||
go version
|
||||
yarn -v
|
||||
node -v
|
||||
}
|
||||
|
||||
install_formatters() {
|
||||
# installing swift formatter
|
||||
git clone --depth 1 --branch 0.47.4 https://github.com/nicklockwood/SwiftFormat.git
|
||||
cd SwiftFormat
|
||||
swift build -c release
|
||||
sudo cp .build/release/swiftformat /usr/local/bin/swiftformat
|
||||
cd ..
|
||||
|
||||
which yarn
|
||||
which node
|
||||
yarn -v
|
||||
node -v
|
||||
|
||||
yarn install
|
||||
pip install pylint
|
||||
}
|
||||
|
||||
install_languages
|
||||
export PATH="~/swift/swift/usr/bin:$PATH"
|
||||
install_formatters
|
||||
186
BUILD
@@ -1,186 +0,0 @@
|
||||
licenses(["notice"])
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
features = [
|
||||
"-layering_check",
|
||||
"-parse_headers",
|
||||
],
|
||||
)
|
||||
|
||||
exports_files([
|
||||
"LICENSE",
|
||||
])
|
||||
|
||||
load(":build_defs.bzl", "flatbuffer_cc_library")
|
||||
|
||||
# Public flatc library to compile flatbuffer files at runtime.
|
||||
cc_library(
|
||||
name = "flatbuffers",
|
||||
srcs = [
|
||||
"src/code_generators.cpp",
|
||||
"src/idl_gen_fbs.cpp",
|
||||
"src/idl_gen_general.cpp",
|
||||
"src/idl_gen_text.cpp",
|
||||
"src/idl_parser.cpp",
|
||||
"src/reflection.cpp",
|
||||
"src/util.cpp",
|
||||
],
|
||||
hdrs = [":public_headers"],
|
||||
includes = ["include/"],
|
||||
linkstatic = 1,
|
||||
)
|
||||
|
||||
# Public C++ headers for the Flatbuffers library.
|
||||
filegroup(
|
||||
name = "public_headers",
|
||||
srcs = [
|
||||
"include/flatbuffers/base.h",
|
||||
"include/flatbuffers/code_generators.h",
|
||||
"include/flatbuffers/flatbuffers.h",
|
||||
"include/flatbuffers/flexbuffers.h",
|
||||
"include/flatbuffers/hash.h",
|
||||
"include/flatbuffers/idl.h",
|
||||
"include/flatbuffers/minireflect.h",
|
||||
"include/flatbuffers/reflection.h",
|
||||
"include/flatbuffers/reflection_generated.h",
|
||||
"include/flatbuffers/stl_emulation.h",
|
||||
"include/flatbuffers/util.h",
|
||||
],
|
||||
)
|
||||
|
||||
# Public flatc compiler library.
|
||||
cc_library(
|
||||
name = "flatc_library",
|
||||
srcs = [
|
||||
"src/code_generators.cpp",
|
||||
"src/flatc.cpp",
|
||||
"src/idl_gen_fbs.cpp",
|
||||
"src/idl_parser.cpp",
|
||||
"src/reflection.cpp",
|
||||
"src/util.cpp",
|
||||
],
|
||||
hdrs = [
|
||||
"include/flatbuffers/flatc.h",
|
||||
":public_headers",
|
||||
],
|
||||
includes = [
|
||||
"grpc/",
|
||||
"include/",
|
||||
],
|
||||
)
|
||||
|
||||
# Public flatc compiler.
|
||||
cc_binary(
|
||||
name = "flatc",
|
||||
srcs = [
|
||||
"grpc/src/compiler/config.h",
|
||||
"grpc/src/compiler/cpp_generator.cc",
|
||||
"grpc/src/compiler/cpp_generator.h",
|
||||
"grpc/src/compiler/go_generator.cc",
|
||||
"grpc/src/compiler/go_generator.h",
|
||||
"grpc/src/compiler/java_generator.cc",
|
||||
"grpc/src/compiler/java_generator.h",
|
||||
"grpc/src/compiler/schema_interface.h",
|
||||
"src/flatc_main.cpp",
|
||||
"src/idl_gen_cpp.cpp",
|
||||
"src/idl_gen_dart.cpp",
|
||||
"src/idl_gen_general.cpp",
|
||||
"src/idl_gen_go.cpp",
|
||||
"src/idl_gen_grpc.cpp",
|
||||
"src/idl_gen_js_ts.cpp",
|
||||
"src/idl_gen_json_schema.cpp",
|
||||
"src/idl_gen_lobster.cpp",
|
||||
"src/idl_gen_lua.cpp",
|
||||
"src/idl_gen_php.cpp",
|
||||
"src/idl_gen_python.cpp",
|
||||
"src/idl_gen_rust.cpp",
|
||||
"src/idl_gen_text.cpp",
|
||||
],
|
||||
includes = [
|
||||
"grpc/",
|
||||
"include/",
|
||||
],
|
||||
deps = [
|
||||
":flatc_library",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "runtime_cc",
|
||||
hdrs = [
|
||||
"include/flatbuffers/base.h",
|
||||
"include/flatbuffers/flatbuffers.h",
|
||||
"include/flatbuffers/flexbuffers.h",
|
||||
"include/flatbuffers/stl_emulation.h",
|
||||
"include/flatbuffers/util.h",
|
||||
],
|
||||
includes = ["include/"],
|
||||
linkstatic = 1,
|
||||
)
|
||||
|
||||
# Test binary.
|
||||
cc_test(
|
||||
name = "flatbuffers_test",
|
||||
testonly = 1,
|
||||
srcs = [
|
||||
"include/flatbuffers/minireflect.h",
|
||||
"include/flatbuffers/registry.h",
|
||||
"src/code_generators.cpp",
|
||||
"src/idl_gen_fbs.cpp",
|
||||
"src/idl_gen_general.cpp",
|
||||
"src/idl_gen_text.cpp",
|
||||
"src/idl_parser.cpp",
|
||||
"src/reflection.cpp",
|
||||
"src/util.cpp",
|
||||
"tests/namespace_test/namespace_test1_generated.h",
|
||||
"tests/namespace_test/namespace_test2_generated.h",
|
||||
"tests/test.cpp",
|
||||
"tests/test_assert.cpp",
|
||||
"tests/test_assert.h",
|
||||
"tests/test_builder.cpp",
|
||||
"tests/test_builder.h",
|
||||
"tests/union_vector/union_vector_generated.h",
|
||||
":public_headers",
|
||||
],
|
||||
copts = [
|
||||
"-DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE",
|
||||
"-DBAZEL_TEST_DATA_PATH",
|
||||
],
|
||||
data = [
|
||||
":tests/include_test/include_test1.fbs",
|
||||
":tests/include_test/sub/include_test2.fbs",
|
||||
":tests/monster_test.bfbs",
|
||||
":tests/monster_test.fbs",
|
||||
":tests/monsterdata_test.golden",
|
||||
":tests/prototest/imported.proto",
|
||||
":tests/prototest/test.golden",
|
||||
":tests/prototest/test.proto",
|
||||
":tests/prototest/test_union.golden",
|
||||
":tests/unicode_test.json",
|
||||
":tests/union_vector/union_vector.fbs",
|
||||
":tests/union_vector/union_vector.json",
|
||||
],
|
||||
includes = ["include/"],
|
||||
deps = [
|
||||
":monster_extra_cc_fbs",
|
||||
":monster_test_cc_fbs",
|
||||
],
|
||||
)
|
||||
|
||||
# Test bzl rules
|
||||
|
||||
flatbuffer_cc_library(
|
||||
name = "monster_test_cc_fbs",
|
||||
srcs = ["tests/monster_test.fbs"],
|
||||
include_paths = ["tests/include_test"],
|
||||
includes = [
|
||||
"tests/include_test/include_test1.fbs",
|
||||
"tests/include_test/sub/include_test2.fbs",
|
||||
],
|
||||
)
|
||||
|
||||
flatbuffer_cc_library(
|
||||
name = "monster_extra_cc_fbs",
|
||||
srcs = ["tests/monster_extra.fbs"],
|
||||
)
|
||||
79
BUILD.bazel
Normal file
@@ -0,0 +1,79 @@
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
exports_files([
|
||||
"LICENSE",
|
||||
])
|
||||
|
||||
# Public flatc library to compile flatbuffer files at runtime.
|
||||
cc_library(
|
||||
name = "flatbuffers",
|
||||
hdrs = ["//:public_headers"],
|
||||
linkstatic = 1,
|
||||
strip_include_prefix = "/include",
|
||||
deps = ["//src:flatbuffers"],
|
||||
)
|
||||
|
||||
# Public C++ headers for the Flatbuffers library.
|
||||
filegroup(
|
||||
name = "public_headers",
|
||||
srcs = [
|
||||
"include/flatbuffers/base.h",
|
||||
"include/flatbuffers/code_generators.h",
|
||||
"include/flatbuffers/flatbuffers.h",
|
||||
"include/flatbuffers/flexbuffers.h",
|
||||
"include/flatbuffers/grpc.h",
|
||||
"include/flatbuffers/hash.h",
|
||||
"include/flatbuffers/idl.h",
|
||||
"include/flatbuffers/minireflect.h",
|
||||
"include/flatbuffers/reflection.h",
|
||||
"include/flatbuffers/reflection_generated.h",
|
||||
"include/flatbuffers/registry.h",
|
||||
"include/flatbuffers/stl_emulation.h",
|
||||
"include/flatbuffers/util.h",
|
||||
],
|
||||
)
|
||||
|
||||
# Public flatc compiler library.
|
||||
cc_library(
|
||||
name = "flatc_library",
|
||||
linkstatic = 1,
|
||||
deps = [
|
||||
"//src:flatc_library",
|
||||
],
|
||||
)
|
||||
|
||||
# Public flatc compiler.
|
||||
cc_binary(
|
||||
name = "flatc",
|
||||
deps = [
|
||||
"//src:flatc",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "flatc_headers",
|
||||
srcs = [
|
||||
"include/flatbuffers/flatc.h",
|
||||
],
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
# Library used by flatbuffer_cc_library rules.
|
||||
cc_library(
|
||||
name = "runtime_cc",
|
||||
hdrs = [
|
||||
"include/flatbuffers/base.h",
|
||||
"include/flatbuffers/flatbuffers.h",
|
||||
"include/flatbuffers/flexbuffers.h",
|
||||
"include/flatbuffers/stl_emulation.h",
|
||||
"include/flatbuffers/util.h",
|
||||
],
|
||||
linkstatic = 1,
|
||||
strip_include_prefix = "/include",
|
||||
)
|
||||
@@ -95,7 +95,7 @@ function(build_flatbuffers flatbuffers_schemas
|
||||
set(generated_include ${generated_includes_dir}/${filename}_generated.h)
|
||||
add_custom_command(
|
||||
OUTPUT ${generated_include}
|
||||
COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS}
|
||||
COMMAND ${FLATC} ${FLATC_ARGS}
|
||||
-o ${generated_includes_dir}
|
||||
${include_params}
|
||||
-c ${schema}
|
||||
@@ -150,3 +150,254 @@ function(build_flatbuffers flatbuffers_schemas
|
||||
${copy_text_schemas_dir})
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Creates a target that can be linked against that generates flatbuffer headers.
|
||||
#
|
||||
# This function takes a target name and a list of schemas. You can also specify
|
||||
# other flagc flags using the FLAGS option to change the behavior of the flatc
|
||||
# tool.
|
||||
#
|
||||
# Arguments:
|
||||
# TARGET: The name of the target to generate.
|
||||
# SCHEMAS: The list of schema files to generate code for.
|
||||
# BINARY_SCHEMAS_DIR: Optional. The directory in which to generate binary
|
||||
# schemas. Binary schemas will only be generated if a path is provided.
|
||||
# INCLUDE: Optional. Search for includes in the specified paths. (Use this
|
||||
# instead of "-I <path>" and the FLAGS option so that CMake is aware of
|
||||
# the directories that need to be searched).
|
||||
# INCLUDE_PREFIX: Optional. The directory in which to place the generated
|
||||
# files. Use this instead of the --include-prefix option.
|
||||
# FLAGS: Optional. A list of any additional flags that you would like to pass
|
||||
# to flatc.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# flatbuffers_generate_headers(
|
||||
# TARGET my_generated_headers_target
|
||||
# INCLUDE_PREFIX ${MY_INCLUDE_PREFIX}"
|
||||
# SCHEMAS ${MY_SCHEMA_FILES}
|
||||
# BINARY_SCHEMAS_DIR "${MY_BINARY_SCHEMA_DIRECTORY}"
|
||||
# FLAGS --gen-object-api)
|
||||
#
|
||||
# target_link_libraries(MyExecutableTarget
|
||||
# PRIVATE my_generated_headers_target
|
||||
# )
|
||||
function(flatbuffers_generate_headers)
|
||||
# Parse function arguments.
|
||||
set(options)
|
||||
set(one_value_args
|
||||
"TARGET"
|
||||
"INCLUDE_PREFIX"
|
||||
"BINARY_SCHEMAS_DIR")
|
||||
set(multi_value_args
|
||||
"SCHEMAS"
|
||||
"INCLUDE"
|
||||
"FLAGS")
|
||||
cmake_parse_arguments(
|
||||
PARSE_ARGV 0
|
||||
FLATBUFFERS_GENERATE_HEADERS
|
||||
"${options}"
|
||||
"${one_value_args}"
|
||||
"${multi_value_args}")
|
||||
|
||||
# Test if including from FindFlatBuffers
|
||||
if(FLATBUFFERS_FLATC_EXECUTABLE)
|
||||
set(FLATC_TARGET "")
|
||||
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
|
||||
else()
|
||||
set(FLATC_TARGET flatc)
|
||||
set(FLATC flatc)
|
||||
endif()
|
||||
|
||||
set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
# Generate the include files parameters.
|
||||
set(include_params "")
|
||||
foreach (include_dir ${FLATBUFFERS_GENERATE_HEADERS_INCLUDE})
|
||||
set(include_params -I ${include_dir} ${include_params})
|
||||
endforeach()
|
||||
|
||||
# Create a directory to place the generated code.
|
||||
set(generated_target_dir "${CMAKE_CURRENT_BINARY_DIR}/${FLATBUFFERS_GENERATE_HEADERS_TARGET}")
|
||||
set(generated_include_dir "${generated_target_dir}")
|
||||
if (NOT ${FLATBUFFERS_GENERATE_HEADERS_INCLUDE_PREFIX} STREQUAL "")
|
||||
set(generated_include_dir "${generated_include_dir}/${FLATBUFFERS_GENERATE_HEADERS_INCLUDE_PREFIX}")
|
||||
list(APPEND FLATBUFFERS_GENERATE_HEADERS_FLAGS
|
||||
"--include-prefix" ${FLATBUFFERS_GENERATE_HEADERS_INCLUDE_PREFIX})
|
||||
endif()
|
||||
|
||||
# Create rules to generate the code for each schema.
|
||||
foreach(schema ${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
|
||||
get_filename_component(filename ${schema} NAME_WE)
|
||||
set(generated_include "${generated_include_dir}/${filename}_generated.h")
|
||||
add_custom_command(
|
||||
OUTPUT ${generated_include}
|
||||
COMMAND ${FLATC} ${FLATC_ARGS}
|
||||
-o ${generated_include_dir}
|
||||
${include_params}
|
||||
-c ${schema}
|
||||
${FLATBUFFERS_GENERATE_HEADERS_FLAGS}
|
||||
DEPENDS ${FLATC_TARGET} ${schema}
|
||||
WORKING_DIRECTORY "${working_dir}")
|
||||
list(APPEND all_generated_header_files ${generated_include})
|
||||
|
||||
# Geneate the binary flatbuffers schemas if instructed to.
|
||||
if (NOT ${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR} STREQUAL "")
|
||||
set(binary_schema
|
||||
"${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR}/${filename}.bfbs")
|
||||
add_custom_command(
|
||||
OUTPUT ${binary_schema}
|
||||
COMMAND ${FLATC} -b --schema
|
||||
-o ${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR}
|
||||
${include_params}
|
||||
${schema}
|
||||
DEPENDS ${FLATC_TARGET} ${schema}
|
||||
WORKING_DIRECTORY "${working_dir}")
|
||||
list(APPEND all_generated_binary_files ${binary_schema})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# Set up interface library
|
||||
add_library(${FLATBUFFERS_GENERATE_HEADERS_TARGET} INTERFACE)
|
||||
target_sources(
|
||||
${FLATBUFFERS_GENERATE_HEADERS_TARGET}
|
||||
INTERFACE
|
||||
${all_generated_header_files}
|
||||
${all_generated_binary_files}
|
||||
${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
|
||||
add_dependencies(
|
||||
${FLATBUFFERS_GENERATE_HEADERS_TARGET}
|
||||
${FLATC}
|
||||
${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
|
||||
target_include_directories(
|
||||
${FLATBUFFERS_GENERATE_HEADERS_TARGET}
|
||||
INTERFACE ${generated_target_dir})
|
||||
|
||||
# Organize file layout for IDEs.
|
||||
source_group(
|
||||
TREE "${generated_target_dir}"
|
||||
PREFIX "Flatbuffers/Generated/Headers Files"
|
||||
FILES ${all_generated_header_files})
|
||||
source_group(
|
||||
TREE ${working_dir}
|
||||
PREFIX "Flatbuffers/Schemas"
|
||||
FILES ${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
|
||||
if (NOT ${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR} STREQUAL "")
|
||||
source_group(
|
||||
TREE "${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR}"
|
||||
PREFIX "Flatbuffers/Generated/Binary Schemas"
|
||||
FILES ${all_generated_binary_files})
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Creates a target that can be linked against that generates flatbuffer binaries
|
||||
# from json files.
|
||||
#
|
||||
# This function takes a target name and a list of schemas and Json files. You
|
||||
# can also specify other flagc flags and options to change the behavior of the
|
||||
# flatc compiler.
|
||||
#
|
||||
# Adding this target to your executable ensurses that the flatbuffer binaries
|
||||
# are compiled before your executable is run.
|
||||
#
|
||||
# Arguments:
|
||||
# TARGET: The name of the target to generate.
|
||||
# JSON_FILES: The list of json files to compile to flatbuffers binaries.
|
||||
# SCHEMA: The flatbuffers schema of the Json files to be compiled.
|
||||
# INCLUDE: Optional. Search for includes in the specified paths. (Use this
|
||||
# instead of "-I <path>" and the FLAGS option so that CMake is aware of
|
||||
# the directories that need to be searched).
|
||||
# OUTPUT_DIR: The directly where the generated flatbuffers binaries should be
|
||||
# placed.
|
||||
# FLAGS: Optional. A list of any additional flags that you would like to pass
|
||||
# to flatc.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# flatbuffers_generate_binary_files(
|
||||
# TARGET my_binary_data
|
||||
# SCHEMA "${MY_SCHEMA_DIR}/my_example_schema.fbs"
|
||||
# JSON_FILES ${MY_JSON_FILES}
|
||||
# OUTPUT_DIR "${MY_BINARY_DATA_DIRECTORY}"
|
||||
# FLAGS --strict-json)
|
||||
#
|
||||
# target_link_libraries(MyExecutableTarget
|
||||
# PRIVATE my_binary_data
|
||||
# )
|
||||
function(flatbuffers_generate_binary_files)
|
||||
# Parse function arguments.
|
||||
set(options)
|
||||
set(one_value_args
|
||||
"TARGET"
|
||||
"SCHEMA"
|
||||
"OUTPUT_DIR")
|
||||
set(multi_value_args
|
||||
"JSON_FILES"
|
||||
"INCLUDE"
|
||||
"FLAGS")
|
||||
cmake_parse_arguments(
|
||||
PARSE_ARGV 0
|
||||
FLATBUFFERS_GENERATE_BINARY_FILES
|
||||
"${options}"
|
||||
"${one_value_args}"
|
||||
"${multi_value_args}")
|
||||
|
||||
# Test if including from FindFlatBuffers
|
||||
if(FLATBUFFERS_FLATC_EXECUTABLE)
|
||||
set(FLATC_TARGET "")
|
||||
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
|
||||
else()
|
||||
set(FLATC_TARGET flatc)
|
||||
set(FLATC flatc)
|
||||
endif()
|
||||
|
||||
set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
# Generate the include files parameters.
|
||||
set(include_params "")
|
||||
foreach (include_dir ${FLATBUFFERS_GENERATE_BINARY_FILES_INCLUDE})
|
||||
set(include_params -I ${include_dir} ${include_params})
|
||||
endforeach()
|
||||
|
||||
# Create rules to generate the flatbuffers binary for each json file.
|
||||
foreach(json_file ${FLATBUFFERS_GENERATE_BINARY_FILES_JSON_FILES})
|
||||
get_filename_component(filename ${json_file} NAME_WE)
|
||||
set(generated_binary_file "${FLATBUFFERS_GENERATE_BINARY_FILES_OUTPUT_DIR}/${filename}.bin")
|
||||
add_custom_command(
|
||||
OUTPUT ${generated_binary_file}
|
||||
COMMAND ${FLATC} ${FLATC_ARGS}
|
||||
-o ${FLATBUFFERS_GENERATE_BINARY_FILES_OUTPUT_DIR}
|
||||
${include_params}
|
||||
-b ${FLATBUFFERS_GENERATE_BINARY_FILES_SCHEMA} ${json_file}
|
||||
${FLATBUFFERS_GENERATE_BINARY_FILES_FLAGS}
|
||||
DEPENDS ${FLATC_TARGET} ${json_file}
|
||||
WORKING_DIRECTORY "${working_dir}")
|
||||
list(APPEND all_generated_binary_files ${generated_binary_file})
|
||||
endforeach()
|
||||
|
||||
# Set up interface library
|
||||
add_library(${FLATBUFFERS_GENERATE_BINARY_FILES_TARGET} INTERFACE)
|
||||
target_sources(
|
||||
${FLATBUFFERS_GENERATE_BINARY_FILES_TARGET}
|
||||
INTERFACE
|
||||
${all_generated_binary_files}
|
||||
${FLATBUFFERS_GENERATE_BINARY_FILES_JSON_FILES}
|
||||
${FLATBUFFERS_GENERATE_BINARY_FILES_SCHEMA})
|
||||
add_dependencies(
|
||||
${FLATBUFFERS_GENERATE_BINARY_FILES_TARGET}
|
||||
${FLATC})
|
||||
|
||||
# Organize file layout for IDEs.
|
||||
source_group(
|
||||
TREE ${working_dir}
|
||||
PREFIX "Flatbuffers/JSON Files"
|
||||
FILES ${FLATBUFFERS_GENERATE_BINARY_FILES_JSON_FILES})
|
||||
source_group(
|
||||
TREE ${working_dir}
|
||||
PREFIX "Flatbuffers/Schemas"
|
||||
FILES ${FLATBUFFERS_GENERATE_BINARY_FILES_SCHEMA})
|
||||
source_group(
|
||||
TREE ${FLATBUFFERS_GENERATE_BINARY_FILES_OUTPUT_DIR}
|
||||
PREFIX "Flatbuffers/Generated/Binary Files"
|
||||
FILES ${all_generated_binary_files})
|
||||
endfunction()
|
||||
|
||||
@@ -30,7 +30,7 @@ find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc)
|
||||
find_path(FLATBUFFERS_INCLUDE_DIR NAMES flatbuffers/flatbuffers.h)
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(flatbuffers
|
||||
find_package_handle_standard_args(FlatBuffers
|
||||
DEFAULT_MSG FLATBUFFERS_FLATC_EXECUTABLE FLATBUFFERS_INCLUDE_DIR)
|
||||
|
||||
if(FLATBUFFERS_FOUND)
|
||||
@@ -58,4 +58,4 @@ else()
|
||||
set(FLATBUFFERS_INCLUDE_DIR)
|
||||
endif()
|
||||
|
||||
include("${FLATBUFFERS_CMAKE_DIR}/BuildFlatBuffers.cmake")
|
||||
include("${FLATBUFFERS_CMAKE_DIR}/BuildFlatBuffers.cmake")
|
||||
|
||||
@@ -31,4 +31,14 @@ if (UNIX)
|
||||
set(CPACK_RPM_PACKAGE_NAME "flatbuffers")
|
||||
set(CPACK_PACKAGE_FILE_NAME
|
||||
"${CPACK_RPM_PACKAGE_NAME}_${CPACK_RPM_PACKAGE_VERSION}_${CPACK_RPM_PACKAGE_ARCHITECTURE}")
|
||||
if(NOT DEFINED ${CPACK_PACKAGING_INSTALL_PREFIX})
|
||||
# Default packaging install prefix on RedHat systems is /usr.
|
||||
# This is the assumed value when this variable is not defined.
|
||||
# There is currently a conflict with
|
||||
# /usr/${CMAKE_INSTALL_LIBDIR}/cmake which is installed by default
|
||||
# by other packages on RedHat (most notably cmake-filesystem). Ensure
|
||||
# that on these systems, flatbuffers does not package this path.
|
||||
# This patch is required for cmake pre-3.17.
|
||||
list(APPEND CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/usr/${CMAKE_INSTALL_LIBDIR}/cmake")
|
||||
endif()
|
||||
endif(UNIX)
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
set(VERSION_MAJOR 2)
|
||||
set(VERSION_MINOR 0)
|
||||
set(VERSION_PATCH 0)
|
||||
set(VERSION_COMMIT 0)
|
||||
|
||||
find_program(GIT git)
|
||||
execute_process(
|
||||
COMMAND ${GIT} describe
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
)
|
||||
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")
|
||||
if(GIT)
|
||||
execute_process(
|
||||
COMMAND ${GIT} describe
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
RESULT_VARIABLE GIT_DESCRIBE_RESULT
|
||||
)
|
||||
|
||||
if(GIT_DESCRIBE_RESULT EQUAL 0)
|
||||
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
|
||||
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")
|
||||
else()
|
||||
message(WARNING "git describe failed with exit code: ${GIT_DESCRIBE_RESULT}")
|
||||
endif()
|
||||
else()
|
||||
message(WARNING "git is not found")
|
||||
endif()
|
||||
|
||||
message(STATUS "Proceeding with version: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_COMMIT}")
|
||||
|
||||
9
CMake/flatbuffers.pc.in
Normal file
@@ -0,0 +1,9 @@
|
||||
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
|
||||
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
|
||||
|
||||
Name: FlatBuffers
|
||||
Description: Memory Efficient Serialization Library
|
||||
Version: @VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@
|
||||
|
||||
Libs: -L${libdir} -lflatbuffers
|
||||
Cflags: -I${includedir}
|
||||
313
CMakeLists.txt
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
cmake_minimum_required(VERSION 2.8.12)
|
||||
# generate compile_commands.json
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
include(CheckCXXSymbolExists)
|
||||
@@ -13,6 +13,8 @@ option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library"
|
||||
ON)
|
||||
option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
|
||||
ON)
|
||||
option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
|
||||
OFF)
|
||||
option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
|
||||
option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF)
|
||||
option(FLATBUFFERS_BUILD_SHAREDLIB
|
||||
@@ -29,6 +31,18 @@ option(FLATBUFFERS_PACKAGE_REDHAT
|
||||
option(FLATBUFFERS_PACKAGE_DEBIAN
|
||||
"Build an deb using the 'package' target."
|
||||
OFF)
|
||||
option(FLATBUFFERS_BUILD_CPP17
|
||||
"Enable the build of c++17 test target. \"
|
||||
Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher."
|
||||
OFF)
|
||||
option(FLATBUFFERS_BUILD_LEGACY
|
||||
"Run C++ code generator with '--cpp-std c++0x' switch."
|
||||
OFF)
|
||||
option(FLATBUFFERS_ENABLE_PCH
|
||||
"Enable precompile headers support for 'flatbuffers' and 'flatc'. \"
|
||||
Only work if CMake supports 'target_precompile_headers'. \"
|
||||
This can speed up compilation time."
|
||||
OFF)
|
||||
|
||||
if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
|
||||
message(WARNING
|
||||
@@ -42,18 +56,23 @@ if(DEFINED FLATBUFFERS_MAX_PARSING_DEPTH)
|
||||
message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}")
|
||||
endif()
|
||||
|
||||
# Auto-detect locale-narrow 'strtod_l' function.
|
||||
# Auto-detect locale-narrow 'strtod_l' and 'strtoull_l' functions.
|
||||
if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
|
||||
set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
|
||||
if(MSVC)
|
||||
check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_LOCALE_INDEPENDENT)
|
||||
check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
|
||||
check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
|
||||
else()
|
||||
check_cxx_symbol_exists(strtof_l stdlib.h FLATBUFFERS_LOCALE_INDEPENDENT)
|
||||
check_cxx_symbol_exists(strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
|
||||
check_cxx_symbol_exists(strtoull_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
|
||||
endif()
|
||||
if(FLATBUFFERS_HAS_STRTOF_L AND FLATBUFFERS_HAS_STRTOULL_L)
|
||||
set(FLATBUFFERS_LOCALE_INDEPENDENT 1)
|
||||
endif()
|
||||
endif()
|
||||
add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)
|
||||
|
||||
set(FlatBuffers_Library_SRCS
|
||||
include/flatbuffers/code_generators.h
|
||||
include/flatbuffers/base.h
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/hash.h
|
||||
@@ -65,7 +84,6 @@ set(FlatBuffers_Library_SRCS
|
||||
include/flatbuffers/flexbuffers.h
|
||||
include/flatbuffers/registry.h
|
||||
include/flatbuffers/minireflect.h
|
||||
src/code_generators.cpp
|
||||
src/idl_parser.cpp
|
||||
src/idl_gen_text.cpp
|
||||
src/reflection.cpp
|
||||
@@ -75,10 +93,12 @@ set(FlatBuffers_Library_SRCS
|
||||
set(FlatBuffers_Compiler_SRCS
|
||||
${FlatBuffers_Library_SRCS}
|
||||
src/idl_gen_cpp.cpp
|
||||
src/idl_gen_csharp.cpp
|
||||
src/idl_gen_dart.cpp
|
||||
src/idl_gen_general.cpp
|
||||
src/idl_gen_kotlin.cpp
|
||||
src/idl_gen_go.cpp
|
||||
src/idl_gen_js_ts.cpp
|
||||
src/idl_gen_java.cpp
|
||||
src/idl_gen_ts.cpp
|
||||
src/idl_gen_php.cpp
|
||||
src/idl_gen_python.cpp
|
||||
src/idl_gen_lobster.cpp
|
||||
@@ -87,8 +107,11 @@ set(FlatBuffers_Compiler_SRCS
|
||||
src/idl_gen_fbs.cpp
|
||||
src/idl_gen_grpc.cpp
|
||||
src/idl_gen_json_schema.cpp
|
||||
src/idl_gen_swift.cpp
|
||||
src/flatc.cpp
|
||||
src/flatc_main.cpp
|
||||
include/flatbuffers/code_generators.h
|
||||
src/code_generators.cpp
|
||||
grpc/src/compiler/schema_interface.h
|
||||
grpc/src/compiler/cpp_generator.h
|
||||
grpc/src/compiler/cpp_generator.cc
|
||||
@@ -96,6 +119,12 @@ set(FlatBuffers_Compiler_SRCS
|
||||
grpc/src/compiler/go_generator.cc
|
||||
grpc/src/compiler/java_generator.h
|
||||
grpc/src/compiler/java_generator.cc
|
||||
grpc/src/compiler/python_generator.h
|
||||
grpc/src/compiler/python_generator.cc
|
||||
grpc/src/compiler/swift_generator.h
|
||||
grpc/src/compiler/swift_generator.cc
|
||||
grpc/src/compiler/ts_generator.h
|
||||
grpc/src/compiler/ts_generator.cc
|
||||
)
|
||||
|
||||
set(FlatHash_SRCS
|
||||
@@ -111,8 +140,39 @@ set(FlatBuffers_Tests_SRCS
|
||||
tests/test_assert.cpp
|
||||
tests/test_builder.h
|
||||
tests/test_builder.cpp
|
||||
tests/native_type_test_impl.h
|
||||
tests/native_type_test_impl.cpp
|
||||
include/flatbuffers/code_generators.h
|
||||
src/code_generators.cpp
|
||||
# file generate by running compiler on tests/monster_test.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
|
||||
# file generate by running compiler on namespace_test/namespace_test1.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h
|
||||
# file generate by running compiler on union_vector/union_vector.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h
|
||||
# file generate by running compiler on tests/arrays_test.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h
|
||||
# file generate by running compiler on tests/native_type_test.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h
|
||||
# file generate by running compiler on tests/monster_extra.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h
|
||||
# file generate by running compiler on tests/monster_test.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h
|
||||
# file generate by running compiler on tests/optional_scalars.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
|
||||
)
|
||||
|
||||
set(FlatBuffers_Tests_CPP17_SRCS
|
||||
${FlatBuffers_Library_SRCS}
|
||||
tests/test_assert.h
|
||||
tests/test_assert.cpp
|
||||
tests/cpp17/test_cpp17.cpp
|
||||
# file generate by running compiler on tests/monster_test.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
|
||||
)
|
||||
|
||||
set(FlatBuffers_Sample_Binary_SRCS
|
||||
@@ -131,7 +191,6 @@ set(FlatBuffers_Sample_Text_SRCS
|
||||
|
||||
set(FlatBuffers_Sample_BFBS_SRCS
|
||||
${FlatBuffers_Library_SRCS}
|
||||
src/idl_gen_general.cpp
|
||||
samples/sample_bfbs.cpp
|
||||
# file generated by running compiler on samples/monster.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
|
||||
@@ -140,6 +199,8 @@ set(FlatBuffers_Sample_BFBS_SRCS
|
||||
set(FlatBuffers_GRPCTest_SRCS
|
||||
include/flatbuffers/flatbuffers.h
|
||||
include/flatbuffers/grpc.h
|
||||
include/flatbuffers/util.h
|
||||
src/util.cpp
|
||||
tests/monster_test.grpc.fb.h
|
||||
tests/test_assert.h
|
||||
tests/test_builder.h
|
||||
@@ -148,8 +209,8 @@ set(FlatBuffers_GRPCTest_SRCS
|
||||
tests/test_builder.cpp
|
||||
grpc/tests/grpctest.cpp
|
||||
grpc/tests/message_builder_test.cpp
|
||||
# file generated by running compiler on samples/monster.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
|
||||
# file generate by running compiler on tests/monster_test.fbs
|
||||
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
|
||||
)
|
||||
|
||||
# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
|
||||
@@ -217,8 +278,18 @@ elseif(MSVC)
|
||||
# warning C4512: assignment operator could not be generated
|
||||
# warning C4316: object allocated on the heap may not be aligned
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX /wd4512 /wd4316")
|
||||
|
||||
# multi-core build.
|
||||
add_definitions("/MP")
|
||||
endif()
|
||||
|
||||
# Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS.
|
||||
if(DEFINED FLATBUFFERS_CXX_FLAGS AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
|
||||
message(STATUS "extend CXX_FLAGS with ${FLATBUFFERS_CXX_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATBUFFERS_CXX_FLAGS}")
|
||||
endif()
|
||||
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
|
||||
|
||||
if(FLATBUFFERS_CODE_COVERAGE)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_EXE_LINKER_FLAGS
|
||||
@@ -226,23 +297,37 @@ if(FLATBUFFERS_CODE_COVERAGE)
|
||||
endif()
|
||||
|
||||
function(add_fsanitize_to_target _target _sanitizer)
|
||||
# FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
|
||||
# List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
|
||||
if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
|
||||
((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
|
||||
)
|
||||
set(_sanitizer_flags "=address,undefined")
|
||||
if(_sanitizer MATCHES "=.*")
|
||||
# override default by user-defined sanitizer list
|
||||
set(_sanitizer_flags ${_sanitizer})
|
||||
if(WIN32)
|
||||
target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
|
||||
message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
|
||||
else()
|
||||
# FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
|
||||
# List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
|
||||
if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
|
||||
((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
|
||||
)
|
||||
set(_sanitizer_flags "=address,undefined")
|
||||
if(_sanitizer MATCHES "=.*")
|
||||
# override default by user-defined sanitizer list
|
||||
set(_sanitizer_flags ${_sanitizer})
|
||||
endif()
|
||||
target_compile_options(${_target} PRIVATE
|
||||
-g -fsigned-char -fno-omit-frame-pointer
|
||||
"-fsanitize${_sanitizer_flags}")
|
||||
target_link_libraries(${_target} PRIVATE
|
||||
"-fsanitize${_sanitizer_flags}")
|
||||
set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
|
||||
endif()
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(add_pch_to_target _target _pch_header)
|
||||
if(COMMAND target_precompile_headers)
|
||||
target_precompile_headers(${_target} PRIVATE ${_pch_header})
|
||||
if(NOT MSVC)
|
||||
set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
|
||||
endif()
|
||||
target_compile_options(${_target} PRIVATE
|
||||
-g -fsigned-char -fno-omit-frame-pointer
|
||||
"-fsanitize${_sanitizer_flags}")
|
||||
target_link_libraries(${_target} PRIVATE
|
||||
"-fsanitize${_sanitizer_flags}")
|
||||
set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||
message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
@@ -256,14 +341,20 @@ include_directories(grpc)
|
||||
|
||||
if(FLATBUFFERS_BUILD_FLATLIB)
|
||||
add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
|
||||
# CMake > 2.8.11: Attach header directory for when build via add_subdirectory().
|
||||
# Attach header directory for when build via add_subdirectory().
|
||||
target_include_directories(flatbuffers INTERFACE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
|
||||
target_compile_options(flatbuffers PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
|
||||
if(FLATBUFFERS_ENABLE_PCH)
|
||||
add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_FLATC)
|
||||
add_executable(flatc ${FlatBuffers_Compiler_SRCS})
|
||||
if(FLATBUFFERS_ENABLE_PCH)
|
||||
add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h)
|
||||
endif()
|
||||
target_compile_options(flatc PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
|
||||
if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
|
||||
add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE})
|
||||
@@ -275,6 +366,9 @@ if(FLATBUFFERS_BUILD_FLATC)
|
||||
# Make flatc.exe not depend on runtime dlls for easy distribution.
|
||||
target_compile_options(flatc PUBLIC $<$<CONFIG:Release>:/MT>)
|
||||
endif()
|
||||
if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC)
|
||||
target_link_libraries(flatc PRIVATE -static)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_FLATHASH)
|
||||
@@ -288,58 +382,153 @@ if(FLATBUFFERS_BUILD_SHAREDLIB)
|
||||
# - micro updated every release when there is no API/ABI changes
|
||||
# - minor updated when there are additions in API/ABI
|
||||
# - major (ABI number) updated when there are changes in ABI (or removals)
|
||||
set(FlatBuffers_Library_SONAME_MAJOR "1")
|
||||
set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.11.0")
|
||||
set(FlatBuffers_Library_SONAME_MAJOR "2")
|
||||
set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.0.0")
|
||||
set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
|
||||
SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
|
||||
VERSION "${FlatBuffers_Library_SONAME_FULL}")
|
||||
if(FLATBUFFERS_ENABLE_PCH)
|
||||
add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
function(compile_flatbuffers_schema_to_cpp SRC_FBS)
|
||||
# Global list of generated files.
|
||||
# Use the global property to be independent of PARENT_SCOPE.
|
||||
set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
|
||||
|
||||
function(get_generated_output generated_files)
|
||||
get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
|
||||
set(${generated_files} ${tmp} PARENT_SCOPE)
|
||||
endfunction(get_generated_output)
|
||||
|
||||
function(register_generated_output file_name)
|
||||
get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
|
||||
list(APPEND tmp ${file_name})
|
||||
set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp})
|
||||
endfunction(register_generated_output)
|
||||
|
||||
function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
|
||||
if(FLATBUFFERS_BUILD_LEGACY)
|
||||
set(OPT ${OPT};--cpp-std c++0x)
|
||||
else()
|
||||
# --cpp-std is defined by flatc default settings.
|
||||
endif()
|
||||
message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'")
|
||||
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
|
||||
string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
|
||||
add_custom_command(
|
||||
OUTPUT ${GEN_HEADER}
|
||||
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -c --no-includes --gen-mutable
|
||||
--gen-object-api --gen-compare -o "${SRC_FBS_DIR}"
|
||||
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
|
||||
--cpp --gen-mutable --gen-object-api --reflect-names
|
||||
--cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
|
||||
--reflect-names
|
||||
${OPT}
|
||||
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
|
||||
-o "${SRC_FBS_DIR}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
|
||||
DEPENDS flatc)
|
||||
DEPENDS flatc
|
||||
COMMENT "Run generation: '${GEN_HEADER}'")
|
||||
register_generated_output(${GEN_HEADER})
|
||||
endfunction()
|
||||
|
||||
function(compile_flatbuffers_schema_to_cpp SRC_FBS)
|
||||
compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare")
|
||||
endfunction()
|
||||
|
||||
function(compile_flatbuffers_schema_to_binary SRC_FBS)
|
||||
message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema")
|
||||
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
|
||||
string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
|
||||
# For details about flags see generate_code.bat(sh)
|
||||
add_custom_command(
|
||||
OUTPUT ${GEN_BINARY_SCHEMA}
|
||||
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -b --schema -o "${SRC_FBS_DIR}"
|
||||
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
|
||||
-b --schema --bfbs-comments --bfbs-builtins
|
||||
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
|
||||
-o "${SRC_FBS_DIR}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
|
||||
DEPENDS flatc)
|
||||
DEPENDS flatc
|
||||
COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'")
|
||||
register_generated_output(${GEN_BINARY_SCHEMA})
|
||||
endfunction()
|
||||
|
||||
function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
|
||||
if(FLATBUFFERS_BUILD_LEGACY)
|
||||
set(OPT ${OPT};--cpp-std c++0x)
|
||||
else()
|
||||
# --cpp-std is defined by flatc default settings.
|
||||
endif()
|
||||
message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'")
|
||||
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
|
||||
string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS})
|
||||
# For details about flags see generate_code.bat(sh)
|
||||
add_custom_command(
|
||||
OUTPUT ${GEN_BFBS_HEADER}
|
||||
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
|
||||
--cpp --gen-mutable --gen-object-api --reflect-names
|
||||
--cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
|
||||
${OPT}
|
||||
--bfbs-comments --bfbs-builtins --bfbs-gen-embed
|
||||
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
|
||||
-o "${SRC_FBS_DIR}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
|
||||
DEPENDS flatc
|
||||
COMMENT "Run generation: '${GEN_BFBS_HEADER}'")
|
||||
register_generated_output(${GEN_BFBS_HEADER})
|
||||
endfunction()
|
||||
|
||||
if(FLATBUFFERS_BUILD_TESTS)
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
|
||||
# TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon
|
||||
compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
|
||||
compile_flatbuffers_schema_to_binary(tests/monster_test.fbs)
|
||||
compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test1.fbs)
|
||||
compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test2.fbs)
|
||||
compile_flatbuffers_schema_to_cpp(tests/union_vector/union_vector.fbs)
|
||||
compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs)
|
||||
compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "")
|
||||
compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare")
|
||||
compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
|
||||
compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare")
|
||||
if(NOT (MSVC AND (MSVC_VERSION LESS 1900)))
|
||||
compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF.
|
||||
endif()
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
|
||||
add_executable(flattests ${FlatBuffers_Tests_SRCS})
|
||||
add_dependencies(flattests generated_code)
|
||||
set_property(TARGET flattests
|
||||
PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
|
||||
FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1)
|
||||
if(FLATBUFFERS_CODE_SANITIZE)
|
||||
if(WIN32)
|
||||
target_compile_definitions(flattests PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
|
||||
message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to flattests")
|
||||
else()
|
||||
add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
|
||||
endif()
|
||||
add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
|
||||
endif()
|
||||
|
||||
compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
|
||||
compile_flatbuffers_schema_to_binary(samples/monster.fbs)
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
|
||||
add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
|
||||
add_dependencies(flatsamplebinary generated_code)
|
||||
add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
|
||||
add_dependencies(flatsampletext generated_code)
|
||||
add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS})
|
||||
add_dependencies(flatsamplebfbs generated_code)
|
||||
|
||||
if(FLATBUFFERS_BUILD_CPP17)
|
||||
# Don't generate header for flattests_cpp17 target.
|
||||
# This target uses "generated_cpp17/monster_test_generated.h"
|
||||
# produced by direct call of generate_code.bat(sh) script.
|
||||
add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS})
|
||||
add_dependencies(flattests_cpp17 generated_code)
|
||||
target_compile_features(flattests_cpp17 PRIVATE cxx_std_17)
|
||||
target_compile_definitions(flattests_cpp17 PRIVATE
|
||||
FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
|
||||
FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1
|
||||
)
|
||||
if(FLATBUFFERS_CODE_SANITIZE)
|
||||
add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
|
||||
endif()
|
||||
endif(FLATBUFFERS_BUILD_CPP17)
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_GRPCTEST)
|
||||
@@ -354,9 +543,17 @@ if(FLATBUFFERS_BUILD_GRPCTEST)
|
||||
endif()
|
||||
INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include)
|
||||
INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src)
|
||||
LINK_DIRECTORIES(${GRPC_INSTALL_PATH}/lib)
|
||||
find_package(Threads REQUIRED)
|
||||
list(APPEND CMAKE_PREFIX_PATH ${GRPC_INSTALL_PATH})
|
||||
find_package(protobuf CONFIG REQUIRED)
|
||||
find_package(gRPC CONFIG REQUIRED)
|
||||
add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
|
||||
target_link_libraries(grpctest grpc++_unsecure grpc_unsecure gpr pthread dl)
|
||||
add_dependencies(grpctest generated_code)
|
||||
target_link_libraries(grpctest PRIVATE gRPC::grpc++_unsecure gRPC::grpc_unsecure gRPC::gpr pthread dl)
|
||||
if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
|
||||
# GRPC test has problems with alignment and will fail under ASAN/UBSAN.
|
||||
# add_fsanitize_to_target(grpctest ${FLATBUFFERS_CODE_SANITIZE})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
include(CMake/Version.cmake)
|
||||
@@ -434,28 +631,48 @@ if(FLATBUFFERS_INSTALL)
|
||||
DESTINATION ${FB_CMAKE_DIR}
|
||||
)
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB)
|
||||
configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY)
|
||||
install(
|
||||
FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc"
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(FLATBUFFERS_BUILD_TESTS)
|
||||
enable_testing()
|
||||
|
||||
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION
|
||||
"${CMAKE_CURRENT_BINARY_DIR}")
|
||||
add_test(NAME flattests COMMAND flattests)
|
||||
if(FLATBUFFERS_BUILD_CPP17)
|
||||
add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
|
||||
endif()
|
||||
if(FLATBUFFERS_BUILD_GRPCTEST)
|
||||
add_test(NAME grpctest COMMAND grpctest)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# This target is sync-barrier.
|
||||
# Other generate-dependent targets can depend on 'generated_code' only.
|
||||
get_generated_output(fbs_generated)
|
||||
if(fbs_generated)
|
||||
# message(STATUS "Add generated_code target with files:${fbs_generated}")
|
||||
add_custom_target(generated_code
|
||||
DEPENDS ${fbs_generated}
|
||||
COMMENT "All generated files were updated.")
|
||||
endif()
|
||||
|
||||
include(CMake/BuildFlatBuffers.cmake)
|
||||
|
||||
if(UNIX)
|
||||
# Use of CPack only supported on Linux systems.
|
||||
if(FLATBUFFERS_PACKAGE_DEBIAN)
|
||||
include(CMake/PackageDebian.cmake)
|
||||
include(CPack)
|
||||
endif()
|
||||
if (FLATBUFFERS_PACKAGE_REDHAT)
|
||||
include(CMake/PackageRedhat.cmake)
|
||||
include(CPack)
|
||||
endif()
|
||||
include(CPack)
|
||||
endif()
|
||||
|
||||
22
Formatters.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Format Guidelines
|
||||
|
||||
If you are interesting in contributing to the flatbuffers project, please take a second to read this document. Each language has it's own set of rules, that are defined in their respective formatter/linter documents.
|
||||
|
||||
# Notes
|
||||
|
||||
- Run the linter on the language you are working on before making a Pull Request.
|
||||
- DONT format/lint the generated code.
|
||||
|
||||
# Languages
|
||||
|
||||
## C++
|
||||
|
||||
C++ uses `clang-format` as it's formatter. Run the following script `sh src/clang-format-git.sh`, and it should style the C++ code according to [google style guide](https://google.github.io/styleguide/cppguide.html).
|
||||
|
||||
## Swift
|
||||
|
||||
Swift uses swiftformat as it's formatter. Take a look at [how to install here](https://github.com/nicklockwood/SwiftFormat/blob/master/README.md#how-do-i-install-it). Run the following command `swiftformat --config swift.swiftformat .` in the root directory of the project
|
||||
|
||||
## Typescript
|
||||
|
||||
Typescript uses eslint as it's linter. Take a look at [how to install here](https://eslint.org/docs/user-guide/getting-started). Run the following command `eslint ts/** --ext .ts` in the root directory of the project
|
||||
@@ -187,7 +187,7 @@
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2014 Google Inc.
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
35
WORKSPACE
@@ -4,8 +4,11 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
|
||||
http_archive(
|
||||
name = "io_bazel_rules_go",
|
||||
sha256 = "492c3ac68ed9dcf527a07e6a1b2dcbf199c6bf8b35517951467ac32e421c06c1",
|
||||
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.17.0/rules_go-0.17.0.tar.gz"],
|
||||
sha256 = "d1ffd055969c8f8d431e2d439813e42326961d0942bdf734d2c95dc30c369566",
|
||||
urls = [
|
||||
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.5/rules_go-v0.24.5.tar.gz",
|
||||
"https://github.com/bazelbuild/rules_go/releases/download/v0.24.5/rules_go-v0.24.5.tar.gz",
|
||||
],
|
||||
)
|
||||
|
||||
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
|
||||
@@ -13,3 +16,31 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_depe
|
||||
go_rules_dependencies()
|
||||
|
||||
go_register_toolchains()
|
||||
|
||||
##### Protobuf
|
||||
_PROTOBUF_VERSION = "3.15.2"
|
||||
|
||||
http_archive(
|
||||
name = "com_google_protobuf",
|
||||
strip_prefix = "protobuf-" + _PROTOBUF_VERSION,
|
||||
urls = [
|
||||
"https://github.com/protocolbuffers/protobuf/archive/v" + _PROTOBUF_VERSION + ".tar.gz",
|
||||
],
|
||||
)
|
||||
|
||||
##### GRPC
|
||||
_GRPC_VERSION = "1.36.1"
|
||||
|
||||
http_archive(
|
||||
name = "com_github_grpc_grpc",
|
||||
strip_prefix = "grpc-" + _GRPC_VERSION,
|
||||
urls = ["https://github.com/grpc/grpc/archive/v" + _GRPC_VERSION + ".tar.gz"],
|
||||
)
|
||||
|
||||
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
|
||||
|
||||
grpc_deps()
|
||||
|
||||
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
|
||||
|
||||
grpc_extra_deps()
|
||||
|
||||
1
android/app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
144
android/app/build.gradle
Normal file
@@ -0,0 +1,144 @@
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion "30.0.2"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.flatbuffers.app"
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 30
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
arguments "-DFLATBUFFERS_SRC=${rootProject.projectDir}/.."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
ndkVersion "21.3.6528147"
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path "src/main/cpp/CMakeLists.txt"
|
||||
}
|
||||
}
|
||||
|
||||
task generateFbsCpp(type: Exec) {
|
||||
def inputDir = file("$projectDir/src/main/fbs")
|
||||
def outputCppDir = file("$projectDir/src/main/cpp/generated/")
|
||||
def fbsFiles = layout.files { file(inputDir).listFiles() }.filter { File f -> f.name.endsWith(".fbs") }.toList()
|
||||
ignoreExitValue(true)
|
||||
|
||||
standardOutput = new ByteArrayOutputStream()
|
||||
errorOutput = new ByteArrayOutputStream()
|
||||
def commandLineArgs = ['flatc', '-o', outputCppDir, '--cpp']
|
||||
fbsFiles.forEach{
|
||||
commandLineArgs.add(it.path)
|
||||
}
|
||||
commandLine commandLineArgs
|
||||
|
||||
doFirst {
|
||||
delete "$outputCppDir/"
|
||||
mkdir "$outputCppDir/"
|
||||
}
|
||||
doLast {
|
||||
if (execResult.getExitValue() != 0) {
|
||||
println(standardOutput.toString())
|
||||
throw new GradleException("flatc command line failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task generateFbsKotlin(type: Exec) {
|
||||
def inputDir = file("$projectDir/src/main/fbs")
|
||||
def outputKotlinDir = file("$projectDir/src/main/java/generated/")
|
||||
def fbsFiles = layout.files { file(inputDir).listFiles() }.filter { File f -> f.name.endsWith(".fbs") }.toList()
|
||||
ignoreExitValue(true)
|
||||
|
||||
standardOutput = new ByteArrayOutputStream()
|
||||
errorOutput = new ByteArrayOutputStream()
|
||||
def commandLineArgs = ['flatc', '-o', outputKotlinDir, '--kotlin']
|
||||
fbsFiles.forEach{
|
||||
commandLineArgs.add(it.path)
|
||||
}
|
||||
commandLine commandLineArgs
|
||||
|
||||
doFirst {
|
||||
delete "$outputKotlinDir/"
|
||||
mkdir "$outputKotlinDir/"
|
||||
}
|
||||
doLast {
|
||||
if (execResult.getExitValue() != 0) {
|
||||
println(standardOutput.toString())
|
||||
throw new GradleException("flatc command line failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
afterEvaluate {
|
||||
android.applicationVariants.all { variant ->
|
||||
variant.javaCompiler.dependsOn(generateFbsKotlin)
|
||||
variant.javaCompiler.dependsOn(generateFbsCpp)
|
||||
}
|
||||
}
|
||||
|
||||
flavorDimensions "stl-variant"
|
||||
productFlavors {
|
||||
stlport {
|
||||
dimension "stl-variant"
|
||||
applicationIdSuffix ".stlport"
|
||||
versionNameSuffix "-stlport"
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
arguments "APP_STL=stlport_static"
|
||||
}
|
||||
}
|
||||
}
|
||||
gnustl {
|
||||
dimension "stl-variant"
|
||||
applicationIdSuffix ".gnustl"
|
||||
versionNameSuffix "-gnustl"
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
arguments "APP_STL=gnustl_static"
|
||||
}
|
||||
}
|
||||
}
|
||||
libcpp {
|
||||
dimension "stl-variant"
|
||||
applicationIdSuffix ".libcpp"
|
||||
versionNameSuffix "-libcpp"
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
arguments "APP_STL=c++_static"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation 'androidx.core:core-ktx:1.3.2'
|
||||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||
implementation 'com.google.flatbuffers:flatbuffers-java:2.0.0'
|
||||
|
||||
}
|
||||
21
android/app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
21
android/app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.flatbuffers.app">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
54
android/app/src/main/cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,54 @@
|
||||
# For more information about using CMake with Android Studio, read the
|
||||
# documentation: https://d.android.com/studio/projects/add-native-code.html
|
||||
|
||||
# Sets the minimum version of CMake required to build the native library.
|
||||
|
||||
cmake_minimum_required(VERSION 3.4.1)
|
||||
|
||||
# Creates and names a library, sets it as either STATIC
|
||||
# or SHARED, and provides the relative paths to its source code.
|
||||
# You can define multiple libraries, and CMake builds them for you.
|
||||
# Gradle automatically packages shared libraries with your APK.
|
||||
|
||||
include_directories(${FLATBUFFERS_SRC}/include)
|
||||
|
||||
add_subdirectory(flatbuffers)
|
||||
|
||||
FILE(GLOB Generated_SRCS generated/*.h)
|
||||
|
||||
add_library( # Sets the name of the library.
|
||||
native-lib
|
||||
|
||||
# Sets the library as a shared library.
|
||||
SHARED
|
||||
|
||||
# Provides a relative path to your source file(s).
|
||||
animals.cpp
|
||||
${Generated_SRCS}
|
||||
|
||||
)
|
||||
|
||||
# Searches for a specified prebuilt library and stores the path as a
|
||||
# variable. Because CMake includes system libraries in the search path by
|
||||
# default, you only need to specify the name of the public NDK library
|
||||
# you want to add. CMake verifies that the library exists before
|
||||
# completing its build.
|
||||
|
||||
find_library( # Sets the name of the path variable.
|
||||
log-lib
|
||||
|
||||
# Specifies the name of the NDK library that
|
||||
# you want CMake to locate.
|
||||
log )
|
||||
|
||||
# Specifies libraries CMake should link to your target library. You
|
||||
# can link multiple libraries, such as libraries you define in this
|
||||
# build script, prebuilt third-party libraries, or system libraries.
|
||||
|
||||
target_link_libraries( # Specifies the target library.
|
||||
native-lib
|
||||
flatbuffers
|
||||
flatbuffers_tests
|
||||
# Links the target library to the log library
|
||||
# included in the NDK.
|
||||
${log-lib} )
|
||||
39
android/app/src/main/cpp/animals.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2014 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
#include <search.h>
|
||||
#include "generated/animal_generated.h"
|
||||
|
||||
using namespace com::fbs::app;
|
||||
using namespace flatbuffers;
|
||||
|
||||
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_flatbuffers_app_MainActivity_createAnimalFromJNI(
|
||||
JNIEnv* env,
|
||||
jobject /* this */) {
|
||||
// create a new animal flatbuffers
|
||||
auto fb = FlatBufferBuilder(1024);
|
||||
auto tiger = CreateAnimalDirect(fb, "Tiger", "Roar", 300);
|
||||
fb.Finish(tiger);
|
||||
|
||||
// copies it to a Java byte array.
|
||||
auto buf = reinterpret_cast<jbyte*>(fb.GetBufferPointer());
|
||||
int size = fb.GetSize();
|
||||
auto ret = env->NewByteArray(size);
|
||||
env->SetByteArrayRegion (ret, 0, fb.GetSize(), buf);
|
||||
return ret;
|
||||
}
|
||||
59
android/app/src/main/cpp/flatbuffers/CMakeLists.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
# For more information about using CMake with Android Studio, read the
|
||||
# documentation: https://d.android.com/studio/projects/add-native-code.html
|
||||
|
||||
# Sets the minimum version of CMake required to build the native library.
|
||||
|
||||
cmake_minimum_required(VERSION 3.4.1)
|
||||
|
||||
include_directories(${FLATBUFFERS_SRC}/include)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -fexceptions -Wall -DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE")
|
||||
|
||||
# Certain platforms such as ARM do not use signed chars by default
|
||||
# which causes issues with certain bounds checks.
|
||||
set(CMAKE_CXX_FLAGS
|
||||
"${CMAKE_CXX_FLAGS} -fsigned-char")
|
||||
|
||||
set(FlatBuffers_Library_SRCS
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/base.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/flatbuffers.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/hash.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/idl.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/util.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/reflection.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/reflection_generated.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/stl_emulation.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/flexbuffers.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/registry.h
|
||||
${FLATBUFFERS_SRC}/include/flatbuffers/minireflect.h
|
||||
${FLATBUFFERS_SRC}/src/idl_parser.cpp
|
||||
${FLATBUFFERS_SRC}/src/idl_gen_text.cpp
|
||||
${FLATBUFFERS_SRC}/src/reflection.cpp
|
||||
${FLATBUFFERS_SRC}/src/util.cpp
|
||||
${FLATBUFFERS_SRC}/src/idl_gen_fbs.cpp
|
||||
${FLATBUFFERS_SRC}/src/code_generators.cpp
|
||||
)
|
||||
|
||||
set(FlatBuffers_Test_SRCS
|
||||
${FLATBUFFERS_SRC}/tests/test.cpp
|
||||
${FLATBUFFERS_SRC}/tests/test_assert.h
|
||||
${FLATBUFFERS_SRC}/tests/test_builder.h
|
||||
${FLATBUFFERS_SRC}/tests/test_assert.cpp
|
||||
${FLATBUFFERS_SRC}/tests/test_builder.cpp
|
||||
${FLATBUFFERS_SRC}/tests/native_type_test_impl.h
|
||||
${FLATBUFFERS_SRC}/tests/native_type_test_impl.cpp
|
||||
)
|
||||
|
||||
add_library( # Sets the name of the library.
|
||||
flatbuffers
|
||||
|
||||
${FlatBuffers_Library_SRCS}
|
||||
${FlatBuffers_Test_SRCS}
|
||||
${Generated_SRCS}
|
||||
)
|
||||
|
||||
add_library( # Sets the name of the library.
|
||||
flatbuffers_tests
|
||||
|
||||
${FlatBuffers_Test_SRCS}
|
||||
)
|
||||
128
android/app/src/main/cpp/generated/animal_generated.h
Normal file
@@ -0,0 +1,128 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
|
||||
#ifndef FLATBUFFERS_GENERATED_ANIMAL_COM_FBS_APP_H_
|
||||
#define FLATBUFFERS_GENERATED_ANIMAL_COM_FBS_APP_H_
|
||||
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
namespace com {
|
||||
namespace fbs {
|
||||
namespace app {
|
||||
|
||||
struct Animal;
|
||||
struct AnimalBuilder;
|
||||
|
||||
struct Animal FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef AnimalBuilder Builder;
|
||||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
|
||||
VT_NAME = 4,
|
||||
VT_SOUND = 6,
|
||||
VT_WEIGHT = 8
|
||||
};
|
||||
const flatbuffers::String *name() const {
|
||||
return GetPointer<const flatbuffers::String *>(VT_NAME);
|
||||
}
|
||||
const flatbuffers::String *sound() const {
|
||||
return GetPointer<const flatbuffers::String *>(VT_SOUND);
|
||||
}
|
||||
uint16_t weight() const {
|
||||
return GetField<uint16_t>(VT_WEIGHT, 0);
|
||||
}
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyOffset(verifier, VT_NAME) &&
|
||||
verifier.VerifyString(name()) &&
|
||||
VerifyOffset(verifier, VT_SOUND) &&
|
||||
verifier.VerifyString(sound()) &&
|
||||
VerifyField<uint16_t>(verifier, VT_WEIGHT) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
|
||||
struct AnimalBuilder {
|
||||
typedef Animal Table;
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_name(flatbuffers::Offset<flatbuffers::String> name) {
|
||||
fbb_.AddOffset(Animal::VT_NAME, name);
|
||||
}
|
||||
void add_sound(flatbuffers::Offset<flatbuffers::String> sound) {
|
||||
fbb_.AddOffset(Animal::VT_SOUND, sound);
|
||||
}
|
||||
void add_weight(uint16_t weight) {
|
||||
fbb_.AddElement<uint16_t>(Animal::VT_WEIGHT, weight, 0);
|
||||
}
|
||||
explicit AnimalBuilder(flatbuffers::FlatBufferBuilder &_fbb)
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
AnimalBuilder &operator=(const AnimalBuilder &);
|
||||
flatbuffers::Offset<Animal> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Animal>(end);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
inline flatbuffers::Offset<Animal> CreateAnimal(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
flatbuffers::Offset<flatbuffers::String> name = 0,
|
||||
flatbuffers::Offset<flatbuffers::String> sound = 0,
|
||||
uint16_t weight = 0) {
|
||||
AnimalBuilder builder_(_fbb);
|
||||
builder_.add_sound(sound);
|
||||
builder_.add_name(name);
|
||||
builder_.add_weight(weight);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline flatbuffers::Offset<Animal> CreateAnimalDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const char *name = nullptr,
|
||||
const char *sound = nullptr,
|
||||
uint16_t weight = 0) {
|
||||
auto name__ = name ? _fbb.CreateString(name) : 0;
|
||||
auto sound__ = sound ? _fbb.CreateString(sound) : 0;
|
||||
return com::fbs::app::CreateAnimal(
|
||||
_fbb,
|
||||
name__,
|
||||
sound__,
|
||||
weight);
|
||||
}
|
||||
|
||||
inline const com::fbs::app::Animal *GetAnimal(const void *buf) {
|
||||
return flatbuffers::GetRoot<com::fbs::app::Animal>(buf);
|
||||
}
|
||||
|
||||
inline const com::fbs::app::Animal *GetSizePrefixedAnimal(const void *buf) {
|
||||
return flatbuffers::GetSizePrefixedRoot<com::fbs::app::Animal>(buf);
|
||||
}
|
||||
|
||||
inline bool VerifyAnimalBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifyBuffer<com::fbs::app::Animal>(nullptr);
|
||||
}
|
||||
|
||||
inline bool VerifySizePrefixedAnimalBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<com::fbs::app::Animal>(nullptr);
|
||||
}
|
||||
|
||||
inline void FinishAnimalBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<com::fbs::app::Animal> root) {
|
||||
fbb.Finish(root);
|
||||
}
|
||||
|
||||
inline void FinishSizePrefixedAnimalBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<com::fbs::app::Animal> root) {
|
||||
fbb.FinishSizePrefixed(root);
|
||||
}
|
||||
|
||||
} // namespace app
|
||||
} // namespace fbs
|
||||
} // namespace com
|
||||
|
||||
#endif // FLATBUFFERS_GENERATED_ANIMAL_COM_FBS_APP_H_
|
||||
@@ -12,11 +12,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
namespace sample;
|
||||
namespace com.fbs.app;
|
||||
|
||||
table Animal {
|
||||
name:string;
|
||||
sound:string;
|
||||
weight: uint16;
|
||||
}
|
||||
|
||||
root_type Animal;
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.flatbuffers.app
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.widget.TextView
|
||||
import com.fbs.app.Animal
|
||||
import com.google.flatbuffers.FlatBufferBuilder
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
@ExperimentalUnsignedTypes
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
val tiger = Animal.getRootAsAnimal(ByteBuffer.wrap(createAnimalFromJNI()))
|
||||
findViewById<TextView>(R.id.tv_animal_one).text = animalInfo(tiger)
|
||||
|
||||
findViewById<TextView>(R.id.tv_animal_two).text = animalInfo(createAnimalFromKotlin())
|
||||
}
|
||||
|
||||
// This function is a sample of communicating FlatBuffers between JNI (native C++) and Java.
|
||||
// Implementation can be found on animals.cpp file.
|
||||
private external fun createAnimalFromJNI(): ByteArray
|
||||
|
||||
// Create a "Cow" Animal flatbuffers from Kotlin
|
||||
private fun createAnimalFromKotlin():Animal {
|
||||
val fb = FlatBufferBuilder(100)
|
||||
val cowOffset = Animal.createAnimal(
|
||||
builder = fb,
|
||||
nameOffset = fb.createString("Cow"),
|
||||
soundOffset = fb.createString("Moo"),
|
||||
weight = 720u
|
||||
)
|
||||
fb.finish(cowOffset)
|
||||
return Animal.getRootAsAnimal(fb.dataBuffer())
|
||||
}
|
||||
|
||||
private fun animalInfo(animal: Animal): String =
|
||||
"The ${animal.name} sound is ${animal.sound} and it weights ${animal.weight}kg."
|
||||
|
||||
companion object {
|
||||
// Used to load the 'native-lib' library on application startup.
|
||||
init {
|
||||
System.loadLibrary("native-lib")
|
||||
}
|
||||
}
|
||||
}
|
||||
64
android/app/src/main/java/generated/com/fbs/app/Animal.kt
Normal file
@@ -0,0 +1,64 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
package com.fbs.app
|
||||
|
||||
import java.nio.*
|
||||
import kotlin.math.sign
|
||||
import com.google.flatbuffers.*
|
||||
|
||||
@Suppress("unused")
|
||||
@ExperimentalUnsignedTypes
|
||||
class Animal : Table() {
|
||||
|
||||
fun __init(_i: Int, _bb: ByteBuffer) {
|
||||
__reset(_i, _bb)
|
||||
}
|
||||
fun __assign(_i: Int, _bb: ByteBuffer) : Animal {
|
||||
__init(_i, _bb)
|
||||
return this
|
||||
}
|
||||
val name : String?
|
||||
get() {
|
||||
val o = __offset(4)
|
||||
return if (o != 0) __string(o + bb_pos) else null
|
||||
}
|
||||
val nameAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(4, 1)
|
||||
fun nameInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 4, 1)
|
||||
val sound : String?
|
||||
get() {
|
||||
val o = __offset(6)
|
||||
return if (o != 0) __string(o + bb_pos) else null
|
||||
}
|
||||
val soundAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(6, 1)
|
||||
fun soundInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 6, 1)
|
||||
val weight : UShort
|
||||
get() {
|
||||
val o = __offset(8)
|
||||
return if(o != 0) bb.getShort(o + bb_pos).toUShort() else 0u
|
||||
}
|
||||
companion object {
|
||||
fun validateVersion() = Constants.FLATBUFFERS_2_0_0()
|
||||
fun getRootAsAnimal(_bb: ByteBuffer): Animal = getRootAsAnimal(_bb, Animal())
|
||||
fun getRootAsAnimal(_bb: ByteBuffer, obj: Animal): Animal {
|
||||
_bb.order(ByteOrder.LITTLE_ENDIAN)
|
||||
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
|
||||
}
|
||||
fun createAnimal(builder: FlatBufferBuilder, nameOffset: Int, soundOffset: Int, weight: UShort) : Int {
|
||||
builder.startTable(3)
|
||||
addSound(builder, soundOffset)
|
||||
addName(builder, nameOffset)
|
||||
addWeight(builder, weight)
|
||||
return endAnimal(builder)
|
||||
}
|
||||
fun startAnimal(builder: FlatBufferBuilder) = builder.startTable(3)
|
||||
fun addName(builder: FlatBufferBuilder, name: Int) = builder.addOffset(0, name, 0)
|
||||
fun addSound(builder: FlatBufferBuilder, sound: Int) = builder.addOffset(1, sound, 0)
|
||||
fun addWeight(builder: FlatBufferBuilder, weight: UShort) = builder.addShort(2, weight.toShort(), 0)
|
||||
fun endAnimal(builder: FlatBufferBuilder) : Int {
|
||||
val o = builder.endTable()
|
||||
return o
|
||||
}
|
||||
fun finishAnimalBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finish(offset)
|
||||
fun finishSizePrefixedAnimalBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finishSizePrefixed(offset)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="85.84757"
|
||||
android:endY="92.4963"
|
||||
android:startX="42.9492"
|
||||
android:startY="49.59793"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||
android:strokeWidth="1"
|
||||
android:strokeColor="#00000000" />
|
||||
</vector>
|
||||
170
android/app/src/main/res/drawable/ic_launcher_background.xml
Normal file
@@ -0,0 +1,170 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#3DDC84"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
</vector>
|
||||
23
android/app/src/main/res/layout/activity_main.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_animal_one"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="Text Sample"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_animal_two"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="Text Sample 2"/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
BIN
android/app/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 3.5 KiB |
BIN
android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
android/app/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 4.8 KiB |
BIN
android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 7.3 KiB |
BIN
android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
BIN
android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 16 KiB |
6
android/app/src/main/res/values/colors.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#6200EE</color>
|
||||
<color name="colorPrimaryDark">#3700B3</color>
|
||||
<color name="colorAccent">#03DAC5</color>
|
||||
</resources>
|
||||
3
android/app/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">FlatbuffersTestApp</string>
|
||||
</resources>
|
||||
10
android/app/src/main/res/values/styles.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<resources>
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@@ -1,108 +1,35 @@
|
||||
// Copyright (c) 2017 Google, Inc.
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
buildscript {
|
||||
ext.kotlin_version = "1.4.10"
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.3.0'
|
||||
classpath 'com.android.tools.build:gradle:4.1.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.application'
|
||||
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
|
||||
android {
|
||||
compileSdkVersion 25
|
||||
buildToolsVersion '25.0.2'
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
manifest.srcFile 'AndroidManifest.xml'
|
||||
res.srcDirs = ['res']
|
||||
}
|
||||
}
|
||||
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
path "jni/Android.mk"
|
||||
}
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
applicationId 'com.example.FlatBufferTest'
|
||||
// This is the platform API where NativeActivity was introduced.
|
||||
minSdkVersion 9
|
||||
targetSdkVersion 25
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
}
|
||||
}
|
||||
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
targets "FlatBufferTest"
|
||||
arguments "-j" + Runtime.getRuntime().availableProcessors()
|
||||
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
}
|
||||
|
||||
// Build with each STL variant.
|
||||
productFlavors {
|
||||
stlport {
|
||||
applicationIdSuffix ".stlport"
|
||||
versionNameSuffix "-stlport"
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
arguments "APP_STL=stlport_static"
|
||||
}
|
||||
}
|
||||
}
|
||||
gnustl {
|
||||
applicationIdSuffix ".gnustl"
|
||||
versionNameSuffix "-gnustl"
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
arguments "APP_STL=gnustl_static"
|
||||
}
|
||||
}
|
||||
}
|
||||
libcpp {
|
||||
applicationIdSuffix ".libcpp"
|
||||
versionNameSuffix "-libcpp"
|
||||
externalNativeBuild {
|
||||
ndkBuild {
|
||||
arguments "APP_STL=c++_static"
|
||||
}
|
||||
}
|
||||
}
|
||||
compileKotlin {
|
||||
dependsOn flatbuffer
|
||||
}
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
|
||||
21
android/gradle.properties
Normal file
@@ -0,0 +1,21 @@
|
||||
# Project-wide Gradle settings.
|
||||
# IDE (e.g. Android Studio) users:
|
||||
# Gradle settings configured through the IDE *will override*
|
||||
# any settings specified in this file.
|
||||
# For more details on how to configure your build environment visit
|
||||
# http://www.gradle.org/docs/current/userguide/build_environment.html
|
||||
# Specifies the JVM arguments used for the daemon process.
|
||||
# The setting is particularly useful for tweaking memory settings.
|
||||
org.gradle.jvmargs=-Xmx2048m
|
||||
# When configured, Gradle will run in incubating parallel mode.
|
||||
# This option should only be used with decoupled projects. More details, visit
|
||||
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
|
||||
# org.gradle.parallel=true
|
||||
# AndroidX package structure to make it clearer which packages are bundled with the
|
||||
# Android operating system, and which are packaged with your app"s APK
|
||||
# https://developer.android.com/topic/libraries/support-library/androidx-rn
|
||||
android.useAndroidX=true
|
||||
# Automatically convert third-party libraries to use AndroidX
|
||||
android.enableJetifier=true
|
||||
# Kotlin code style for this project: "official" or "obsolete":
|
||||
kotlin.code.style=official
|
||||
@@ -1,6 +1,6 @@
|
||||
#Mon Jun 19 11:54:59 PDT 2017
|
||||
#Thu Oct 29 19:47:23 CET 2020
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
# Copyright (c) 2013 Google, Inc.
|
||||
#
|
||||
# This software is provided 'as-is', without any express or implied
|
||||
# warranty. In no event will the authors be held liable for any damages
|
||||
# arising from the use of this software.
|
||||
# Permission is granted to anyone to use this software for any purpose,
|
||||
# including commercial applications, and to alter it and redistribute it
|
||||
# freely, subject to the following restrictions:
|
||||
# 1. The origin of this software must not be misrepresented; you must not
|
||||
# claim that you wrote the original software. If you use this software
|
||||
# in a product, an acknowledgment in the product documentation would be
|
||||
# appreciated but is not required.
|
||||
# 2. Altered source versions must be plainly marked as such, and must not be
|
||||
# misrepresented as being the original software.
|
||||
# 3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
LOCAL_PATH := $(call my-dir)/../..
|
||||
|
||||
include $(LOCAL_PATH)/android/jni/include.mk
|
||||
LOCAL_PATH := $(call realpath-portable,$(LOCAL_PATH))
|
||||
|
||||
# Empty static library so that other projects can include just the basic
|
||||
# FlatBuffers headers as a module.
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := flatbuffers
|
||||
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
|
||||
LOCAL_EXPORT_CPPFLAGS := -std=c++11 -fexceptions -Wall \
|
||||
-DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
|
||||
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
# static library that additionally includes text parsing/generation/reflection
|
||||
# for projects that want richer functionality.
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := flatbuffers_extra
|
||||
LOCAL_SRC_FILES := src/idl_parser.cpp \
|
||||
src/idl_gen_text.cpp \
|
||||
src/reflection.cpp \
|
||||
src/util.cpp \
|
||||
src/code_generators.cpp
|
||||
LOCAL_STATIC_LIBRARIES := flatbuffers
|
||||
LOCAL_ARM_MODE := arm
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
# FlatBuffers test
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := FlatBufferTest
|
||||
LOCAL_SRC_FILES := android/jni/main.cpp \
|
||||
tests/test.cpp \
|
||||
tests/test_assert.h \
|
||||
tests/test_builder.h \
|
||||
tests/test_assert.cpp \
|
||||
tests/test_builder.cpp \
|
||||
src/idl_gen_fbs.cpp \
|
||||
src/idl_gen_general.cpp
|
||||
LOCAL_LDLIBS := -llog -landroid -latomic
|
||||
LOCAL_STATIC_LIBRARIES := android_native_app_glue flatbuffers_extra
|
||||
LOCAL_ARM_MODE := arm
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
$(call import-module,android/native_app_glue)
|
||||
|
||||
$(call import-add-path,$(LOCAL_PATH)/../..)
|
||||
@@ -1,20 +0,0 @@
|
||||
# Copyright (c) 2014 Google, Inc.
|
||||
#
|
||||
# This software is provided 'as-is', without any express or implied
|
||||
# warranty. In no event will the authors be held liable for any damages
|
||||
# arising from the use of this software.
|
||||
# Permission is granted to anyone to use this software for any purpose,
|
||||
# including commercial applications, and to alter it and redistribute it
|
||||
# freely, subject to the following restrictions:
|
||||
# 1. The origin of this software must not be misrepresented; you must not
|
||||
# claim that you wrote the original software. If you use this software
|
||||
# in a product, an acknowledgment in the product documentation would be
|
||||
# appreciated but is not required.
|
||||
# 2. Altered source versions must be plainly marked as such, and must not be
|
||||
# misrepresented as being the original software.
|
||||
# 3. This notice may not be removed or altered from any source distribution.
|
||||
APP_PLATFORM := android-9
|
||||
APP_PROJECT_PATH := $(call my-dir)/..
|
||||
APP_STL ?= stlport_static
|
||||
APP_ABI := armeabi-v7a
|
||||
APP_CPPFLAGS += -std=c++11
|
||||
@@ -1,68 +0,0 @@
|
||||
@rem Copyright (c) 2013 Google, Inc.
|
||||
@rem
|
||||
@rem This software is provided 'as-is', without any express or implied
|
||||
@rem warranty. In no event will the authors be held liable for any damages
|
||||
@rem arising from the use of this software.
|
||||
@rem Permission is granted to anyone to use this software for any purpose,
|
||||
@rem including commercial applications, and to alter it and redistribute it
|
||||
@rem freely, subject to the following restrictions:
|
||||
@rem 1. The origin of this software must not be misrepresented; you must not
|
||||
@rem claim that you wrote the original software. If you use this software
|
||||
@rem in a product, an acknowledgment in the product documentation would be
|
||||
@rem appreciated but is not required.
|
||||
@rem 2. Altered source versions must be plainly marked as such, and must not be
|
||||
@rem misrepresented as being the original software.
|
||||
@rem 3. This notice may not be removed or altered from any source distribution.
|
||||
@echo off
|
||||
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
set thispath=%~dp0
|
||||
|
||||
rem Path to cmake passed in by caller.
|
||||
set cmake=%1
|
||||
rem Path to cmake project to build.
|
||||
set cmake_project_path=%2
|
||||
|
||||
rem Newest and oldest version of Visual Studio that it's possible to select.
|
||||
set visual_studio_version_max=20
|
||||
set visual_studio_version_min=8
|
||||
|
||||
rem Determine the newest version of Visual Studio installed on this machine.
|
||||
set visual_studio_version=
|
||||
for /L %%a in (%visual_studio_version_max%,-1,%visual_studio_version_min%) do (
|
||||
echo Searching for Visual Studio %%a >&2
|
||||
reg query HKLM\SOFTWARE\Microsoft\VisualStudio\%%a.0 /ve 1>NUL 2>NUL
|
||||
if !ERRORLEVEL! EQU 0 (
|
||||
set visual_studio_version=%%a
|
||||
goto found_vs
|
||||
)
|
||||
)
|
||||
echo Unable to determine whether Visual Studio is installed. >&2
|
||||
exit /B 1
|
||||
:found_vs
|
||||
|
||||
rem Map Visual Studio version to cmake generator name.
|
||||
if "%visual_studio_version%"=="8" (
|
||||
set cmake_generator=Visual Studio 8 2005
|
||||
)
|
||||
if "%visual_studio_version%"=="9" (
|
||||
set cmake_generator=Visual Studio 9 2008
|
||||
)
|
||||
if %visual_studio_version% GEQ 10 (
|
||||
set cmake_generator=Visual Studio %visual_studio_version%
|
||||
)
|
||||
rem Set visual studio version variable for msbuild.
|
||||
set VisualStudioVersion=%visual_studio_version%.0
|
||||
|
||||
rem Generate Visual Studio solution.
|
||||
echo Generating solution for %cmake_generator%. >&2
|
||||
cd "%cmake_project_path%"
|
||||
%cmake% -G"%cmake_generator%"
|
||||
if %ERRORLEVEL% NEQ 0 (
|
||||
exit /B %ERRORLEVEL%
|
||||
)
|
||||
|
||||
rem Build flatc
|
||||
python %thispath%\msbuild.py flatc.vcxproj
|
||||
if ERRORLEVEL 1 exit /B 1
|
||||
@@ -1,237 +0,0 @@
|
||||
# Copyright 2014 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# This file contains utility functions for Android projects using Flatbuffers.
|
||||
# To use this file, include it in your project's Android.mk by calling near the
|
||||
# top of your android makefile like so:
|
||||
#
|
||||
# include $(FLATBUFFERS_DIR)/android/jni/include.mk
|
||||
#
|
||||
# You will also need to import the flatbuffers module using the standard
|
||||
# import-module function.
|
||||
#
|
||||
# The main functionality this file provides are the following functions:
|
||||
# flatbuffers_fbs_to_h: Converts flatbuffer schema paths to header paths.
|
||||
# flatbuffers_header_build_rule:
|
||||
# Creates a build rule for a schema's generated header. This build rule
|
||||
# has a dependency on the flatc compiler which will be built if necessary.
|
||||
# flatbuffers_header_build_rules:
|
||||
# Creates build rules for generated headers for each schema listed and sets
|
||||
# up depenedendies.
|
||||
#
|
||||
# More information and example usage can be found in the comments preceeding
|
||||
# each function.
|
||||
|
||||
# Targets to build the Flatbuffers compiler as well as some utility definitions
|
||||
ifeq (,$(FLATBUFFERS_INCLUDE_MK_))
|
||||
FLATBUFFERS_INCLUDE_MK_ := 1
|
||||
|
||||
# Portable version of $(realpath) that omits drive letters on Windows.
|
||||
realpath-portable = $(join $(filter %:,$(subst :,: ,$1)),\
|
||||
$(realpath $(filter-out %:,$(subst :,: ,$1))))
|
||||
|
||||
PROJECT_OS := $(OS)
|
||||
ifeq (,$(OS))
|
||||
PROJECT_OS := $(shell uname -s)
|
||||
else
|
||||
ifneq ($(findstring Windows,$(PROJECT_OS)),)
|
||||
PROJECT_OS := Windows
|
||||
endif
|
||||
endif
|
||||
|
||||
# The following block generates build rules which result in headers being
|
||||
# rebuilt from flatbuffers schemas.
|
||||
|
||||
FLATBUFFERS_CMAKELISTS_DIR := \
|
||||
$(call realpath-portable,$(dir $(lastword $(MAKEFILE_LIST)))/../..)
|
||||
|
||||
# Directory that contains the FlatBuffers compiler.
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
|
||||
FLATBUFFERS_FLATC := $(lastword \
|
||||
$(wildcard $(FLATBUFFERS_FLATC_PATH)/*/flatc.exe) \
|
||||
$(wildcard $(FLATBUFFERS_FLATC_PATH)/flatc.exe))
|
||||
endif
|
||||
ifeq (Linux,$(PROJECT_OS))
|
||||
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
|
||||
FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/flatc
|
||||
endif
|
||||
ifeq (Darwin,$(PROJECT_OS))
|
||||
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
|
||||
FLATBUFFERS_FLATC := $(lastword \
|
||||
$(wildcard $(FLATBUFFERS_FLATC_PATH)/*/flatc) \
|
||||
$(wildcard $(FLATBUFFERS_FLATC_PATH)/flatc))
|
||||
endif
|
||||
|
||||
FLATBUFFERS_FLATC_ARGS?=
|
||||
|
||||
# Search for cmake.
|
||||
CMAKE_ROOT := \
|
||||
$(call realpath-portable,$(LOCAL_PATH)/../../../../../../prebuilts/cmake)
|
||||
ifeq (,$(CMAKE))
|
||||
ifeq (Linux,$(PROJECT_OS))
|
||||
CMAKE := $(wildcard $(CMAKE_ROOT)/linux-x86/current/bin/cmake*)
|
||||
endif
|
||||
ifeq (Darwin,$(PROJECT_OS))
|
||||
CMAKE := \
|
||||
$(wildcard $(CMAKE_ROOT)/darwin-x86_64/current/*.app/Contents/bin/cmake)
|
||||
endif
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
CMAKE := $(wildcard $(CMAKE_ROOT)/windows/current/bin/cmake*)
|
||||
endif
|
||||
endif
|
||||
ifeq (,$(CMAKE))
|
||||
CMAKE := cmake
|
||||
endif
|
||||
|
||||
# Windows friendly portable local path.
|
||||
# GNU-make doesn't like : in paths, must use relative paths on Windows.
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
PORTABLE_LOCAL_PATH =
|
||||
else
|
||||
PORTABLE_LOCAL_PATH = $(LOCAL_PATH)/
|
||||
endif
|
||||
|
||||
# Generate a host build rule for the flatbuffers compiler.
|
||||
ifeq (Windows,$(PROJECT_OS))
|
||||
define build_flatc_recipe
|
||||
$(FLATBUFFERS_CMAKELISTS_DIR)\android\jni\build_flatc.bat \
|
||||
$(CMAKE) $(FLATBUFFERS_CMAKELISTS_DIR)
|
||||
endef
|
||||
endif
|
||||
ifeq (Linux,$(PROJECT_OS))
|
||||
define build_flatc_recipe
|
||||
+cd $(FLATBUFFERS_CMAKELISTS_DIR) && \
|
||||
$(CMAKE) . && \
|
||||
$(MAKE) flatc
|
||||
endef
|
||||
endif
|
||||
ifeq (Darwin,$(PROJECT_OS))
|
||||
define build_flatc_recipe
|
||||
cd $(FLATBUFFERS_CMAKELISTS_DIR) && "$(CMAKE)" -GXcode . && \
|
||||
xcodebuild -target flatc
|
||||
endef
|
||||
endif
|
||||
ifeq (,$(build_flatc_recipe))
|
||||
ifeq (,$(FLATBUFFERS_FLATC))
|
||||
$(error flatc binary not found!)
|
||||
endif
|
||||
endif
|
||||
|
||||
# Generate a build rule for flatc.
|
||||
ifeq ($(strip $(FLATBUFFERS_FLATC)),)
|
||||
flatc_target := build_flatc
|
||||
.PHONY: $(flatc_target)
|
||||
FLATBUFFERS_FLATC := \
|
||||
python $(FLATBUFFERS_CMAKELISTS_DIR)/android/jni/run_flatc.py \
|
||||
$(FLATBUFFERS_CMAKELISTS_DIR)
|
||||
else
|
||||
flatc_target := $(FLATBUFFERS_FLATC)
|
||||
endif
|
||||
$(flatc_target):
|
||||
$(call build_flatc_recipe)
|
||||
|
||||
# $(flatbuffers_fbs_to_h schema_dir,output_dir,path)
|
||||
#
|
||||
# Convert the specified schema path to a Flatbuffers generated header path.
|
||||
# For example:
|
||||
#
|
||||
# $(call flatbuffers_fbs_to_h,$(MY_PROJ_DIR)/schemas,\
|
||||
# $(MY_PROJ_DIR)/gen/include,$(MY_PROJ_DIR)/schemas/example.fbs)
|
||||
#
|
||||
# This will convert the file path `$(MY_PROJ_DIR)/schemas/example.fbs)` to
|
||||
# `$(MY_PROJ_DIR)/gen/include/example_generated.h`
|
||||
define flatbuffers_fbs_to_h
|
||||
$(subst $(1),$(2),$(patsubst %.fbs,%_generated.h,$(3)))
|
||||
endef
|
||||
|
||||
# $(flatbuffers_header_build_rule schema_file,schema_dir,output_dir,\
|
||||
# schema_include_dirs)
|
||||
#
|
||||
# Generate a build rule that will convert a Flatbuffers schema to a generated
|
||||
# header derived from the schema filename using flatbuffers_fbs_to_h. For
|
||||
# example:
|
||||
#
|
||||
# $(call flatbuffers_header_build_rule,$(MY_PROJ_DIR)/schemas/example.fbs,\
|
||||
# $(MY_PROJ_DIR)/schemas,$(MY_PROJ_DIR)/gen/include)
|
||||
#
|
||||
# The final argument, schema_include_dirs, is optional and is only needed when
|
||||
# the schema files depend on other schema files outside their own directory.
|
||||
define flatbuffers_header_build_rule
|
||||
$(eval \
|
||||
$(call flatbuffers_fbs_to_h,$(2),$(3),$(1)): $(1) $(flatc_target)
|
||||
$(call host-echo-build-step,generic,Generate) \
|
||||
$(subst $(LOCAL_PATH)/,,$(call flatbuffers_fbs_to_h,$(2),$(3),$(1)))
|
||||
$(hide) $$(FLATBUFFERS_FLATC) $(FLATBUFFERS_FLATC_ARGS) \
|
||||
$(foreach include,$(4),-I $(include)) -o $$(dir $$@) -c $$<)
|
||||
endef
|
||||
|
||||
# TODO: Remove when the LOCAL_PATH expansion bug in the NDK is fixed.
|
||||
# Override the default behavior of local-source-file-path to workaround
|
||||
# a bug which prevents the build of deeply nested projects when NDK_OUT is
|
||||
# set.
|
||||
local-source-file-path=\
|
||||
$(if $(call host-path-is-absolute,$1),$1,$(call \
|
||||
realpath-portable,$(LOCAL_PATH)/$1))
|
||||
|
||||
|
||||
# $(flatbuffers_header_build_rules schema_files,schema_dir,output_dir,\
|
||||
# schema_include_dirs,src_files,[build_target],[dependencies]))
|
||||
#
|
||||
# $(1) schema_files: Space separated list of flatbuffer schema files.
|
||||
# $(2) schema_dir: Directory containing the flatbuffer schemas.
|
||||
# $(3) output_dir: Where to place the generated files.
|
||||
# $(4) schema_include_dirs: Directories to include when generating schemas.
|
||||
# $(5) src_files: Files that should depend upon the headers generated from the
|
||||
# flatbuffer schemas.
|
||||
# $(6) build_target: Name of a build target that depends upon all generated
|
||||
# headers.
|
||||
# $(7) dependencies: Space seperated list of additional build targets src_files
|
||||
# should depend upon.
|
||||
#
|
||||
# Use this in your own Android.mk file to generate build rules that will
|
||||
# generate header files for your flatbuffer schemas as well as automatically
|
||||
# set your source files to be dependent on the generated headers. For example:
|
||||
#
|
||||
# $(call flatbuffers_header_build_rules,$(MY_PROJ_SCHEMA_FILES),\
|
||||
# $(MY_PROJ_SCHEMA_DIR),$(MY_PROJ_GENERATED_OUTPUT_DIR),
|
||||
# $(MY_PROJ_SCHEMA_INCLUDE_DIRS),$(LOCAL_SRC_FILES))
|
||||
#
|
||||
# NOTE: Due problesm with path processing in ndk-build when presented with
|
||||
# deeply nested projects must redefine LOCAL_PATH after include this makefile
|
||||
# using:
|
||||
#
|
||||
# LOCAL_PATH := $(call realpath-portable,$(LOCAL_PATH))
|
||||
#
|
||||
define flatbuffers_header_build_rules
|
||||
$(foreach schema,$(1),\
|
||||
$(call flatbuffers_header_build_rule,\
|
||||
$(schema),$(strip $(2)),$(strip $(3)),$(strip $(4))))\
|
||||
$(foreach src,$(strip $(5)),\
|
||||
$(eval $(call local-source-file-path,$(src)): \
|
||||
$(foreach schema,$(strip $(1)),\
|
||||
$(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))))\
|
||||
$(if $(6),\
|
||||
$(foreach schema,$(strip $(1)),\
|
||||
$(eval $(6): \
|
||||
$(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))),)\
|
||||
$(if $(7),\
|
||||
$(foreach src,$(strip $(5)),\
|
||||
$(eval $(call local-source-file-path,$(src)): $(strip $(7)))),)\
|
||||
$(if $(7),\
|
||||
$(foreach dependency,$(strip $(7)),\
|
||||
$(eval $(6): $(dependency))),)
|
||||
endef
|
||||
|
||||
endif # FLATBUFFERS_INCLUDE_MK_
|
||||
@@ -1,77 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# Copyright 2014 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Simple script that locates the newest MSBuild in one of several locations.
|
||||
|
||||
This script will find the highest version number of MSBuild and run it,
|
||||
passing its arguments through to MSBuild.
|
||||
"""
|
||||
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import string
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
SYSTEMROOT = os.getenv("SYSTEMROOT", "c:\\windows")
|
||||
PROGRAM_FILES = os.getenv("ProgramFiles", "c:\\Program Files")
|
||||
PROGRAM_FILES_X86 = os.getenv("ProgramFiles(x86)", "c:\\Program Files (x86)")
|
||||
|
||||
SEARCH_FOLDERS = [ PROGRAM_FILES + "\\MSBuild\\*\\Bin\\MSBuild.exe",
|
||||
PROGRAM_FILES_X86 + "\\MSBuild\\*\\Bin\\MSBuild.exe",
|
||||
SYSTEMROOT + "\\Microsoft.NET\Framework\\*\\MSBuild.exe" ]
|
||||
|
||||
def compare_version(a, b):
|
||||
"""Compare two version number strings of the form W.X.Y.Z.
|
||||
|
||||
The numbers are compared most-significant to least-significant.
|
||||
For example, 12.345.67.89 > 2.987.88.99.
|
||||
|
||||
Args:
|
||||
a: First version number string to compare
|
||||
b: Second version number string to compare
|
||||
|
||||
Returns:
|
||||
0 if the numbers are identical, a positive number if 'a' is larger, and
|
||||
a negative number if 'b' is larger.
|
||||
"""
|
||||
aa = string.split(a, ".")
|
||||
bb = string.split(b, ".")
|
||||
for i in range(0, 4):
|
||||
if aa[i] != bb[i]:
|
||||
return cmp(int(aa[i]), int(bb[i]))
|
||||
return 0
|
||||
|
||||
def main():
|
||||
msbuilds = []
|
||||
|
||||
for folder in SEARCH_FOLDERS:
|
||||
for file in glob.glob(folder):
|
||||
p = subprocess.Popen([file, "/version"], stdout=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
match = re.search("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$", out, re.M)
|
||||
if match:
|
||||
msbuilds.append({ 'ver':match.group(), 'exe':file })
|
||||
msbuilds.sort(lambda x, y: compare_version(x['ver'], y['ver']), reverse=True)
|
||||
if len(msbuilds) == 0:
|
||||
print "Unable to find MSBuild.\n"
|
||||
return -1;
|
||||
cmd = [msbuilds[0]['exe']]
|
||||
cmd.extend(sys.argv[1:])
|
||||
return subprocess.call(cmd)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -1,46 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# Copyright 2015 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
EXECUTABLE_EXTENSION = '.exe' if platform.system() == 'Windows' else ''
|
||||
# Paths to search for flatc relative to the current working directory.
|
||||
FLATC_SEARCH_PATHS = [os.path.curdir, 'Release', 'Debug']
|
||||
|
||||
def main():
|
||||
"""Script that finds and runs flatc built from source."""
|
||||
if len(sys.argv) < 2:
|
||||
sys.stderr.write('Usage: run_flatc.py flatbuffers_dir [flatc_args]\n')
|
||||
return 1
|
||||
cwd = os.getcwd()
|
||||
flatc = ''
|
||||
flatbuffers_dir = sys.argv[1]
|
||||
for path in FLATC_SEARCH_PATHS:
|
||||
current = os.path.join(flatbuffers_dir, path,
|
||||
'flatc' + EXECUTABLE_EXTENSION)
|
||||
if os.path.exists(current):
|
||||
flatc = current
|
||||
break
|
||||
if not flatc:
|
||||
sys.stderr.write('flatc not found\n')
|
||||
return 1
|
||||
command = [flatc] + sys.argv[2:]
|
||||
return subprocess.call(command)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (c) 2014 Google, Inc.
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
-->
|
||||
<resources>
|
||||
<string name="app_name">FlatBufferTest</string>
|
||||
</resources>
|
||||
2
android/settings.gradle
Normal file
@@ -0,0 +1,2 @@
|
||||
include ':app'
|
||||
rootProject.name = "FlatbuffersTest"
|
||||
70
appveyor.yml
@@ -2,23 +2,29 @@ branches:
|
||||
only:
|
||||
- master
|
||||
|
||||
os: Visual Studio 2015
|
||||
|
||||
environment:
|
||||
nodejs_version: "14"
|
||||
|
||||
global:
|
||||
# Workaround for https://github.com/conda/conda-build/issues/636
|
||||
PYTHONIOENCODING: UTF-8
|
||||
CONDA_INSTALL_LOCN: "C:\\Miniconda35-x64"
|
||||
CMAKE_OPTIONS: ""
|
||||
CPP_TEST_OPTIONS: ""
|
||||
|
||||
matrix:
|
||||
- CMAKE_VS_VERSION: "10 2010"
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
CMAKE_VS_VERSION: "10 2010"
|
||||
CMAKE_OPTIONS: "-DFLATBUFFERS_BUILD_LEGACY=1"
|
||||
CPP_TEST_OPTIONS: "--std-cpp c++0x"
|
||||
MONSTER_EXTRA: "skip"
|
||||
|
||||
- CMAKE_VS_VERSION: "12 2013"
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
CMAKE_VS_VERSION: "12 2013"
|
||||
MONSTER_EXTRA: "skip"
|
||||
|
||||
- CMAKE_VS_VERSION: "14 2015"
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
CMAKE_VS_VERSION: "14 2015"
|
||||
MONSTER_EXTRA: ""
|
||||
|
||||
platform:
|
||||
@@ -31,9 +37,9 @@ configuration:
|
||||
|
||||
before_build:
|
||||
- set MONSTER_EXTRA=%MONSTER_EXTRA%
|
||||
- cmake -G"Visual Studio %CMAKE_VS_VERSION%" -DFLATBUFFERS_CODE_SANITIZE=1 .
|
||||
- cmake . -G"Visual Studio %CMAKE_VS_VERSION%" -DFLATBUFFERS_CODE_SANITIZE=1 %CMAKE_OPTIONS%
|
||||
# This cuts down on a lot of noise generated by xamarin warnings.
|
||||
- del "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets"
|
||||
- if exist "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets" del "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets"
|
||||
|
||||
build:
|
||||
project: ALL_BUILD.vcxproj
|
||||
@@ -45,58 +51,36 @@ after_build:
|
||||
|
||||
install:
|
||||
- set PATH=%CONDA_INSTALL_LOCN%;%CONDA_INSTALL_LOCN%\scripts;%PATH%;
|
||||
- curl -sSf -o rustup-init.exe https://win.rustup.rs/
|
||||
- rustup-init.exe -y
|
||||
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
|
||||
- rustc -V
|
||||
- cargo -V
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
|
||||
test_script:
|
||||
- call .appveyor\check-generate-code.bat -b %CONFIGURATION%
|
||||
- "cd tests"
|
||||
- rem "Building all code"
|
||||
- generate_code.bat -b %CONFIGURATION%
|
||||
- generate_code.bat -b %CONFIGURATION% %CPP_TEST_OPTIONS%
|
||||
- 7z a GeneratedMyGameCode.zip MyGame\
|
||||
- rem "---------------- C++ -----------------"
|
||||
- "cd .."
|
||||
- "%CONFIGURATION%\\flattests.exe"
|
||||
- "cd tests"
|
||||
- rem "---------------- Java -----------------"
|
||||
- "java -version"
|
||||
- "JavaTest.bat"
|
||||
- rem "---------------- Rust ----------------"
|
||||
- "RustTest.bat"
|
||||
- rem "---------------- JS -----------------"
|
||||
- "node --version"
|
||||
- "..\\%CONFIGURATION%\\flatc -b -I include_test monster_test.fbs unicode_test.json"
|
||||
- "node JavaScriptTest ./monster_test_generated"
|
||||
- rem "-------------- Python ---------------"
|
||||
- where python
|
||||
- python --version
|
||||
- where pip
|
||||
- pip --version
|
||||
- where conda
|
||||
- conda --version
|
||||
- rem "installing flatbuffers python library"
|
||||
- pip install ../python
|
||||
- rem "testing without installing Numpy"
|
||||
- python py_test.py 0 0 0
|
||||
- rem "testing after installing Numpy - disabled"
|
||||
# FIXME: This has a LOT of unnecessary dependencies and makes the tests fail
|
||||
# with timeouts.
|
||||
# - conda install --yes numpy
|
||||
# - python py_test.py 0 0 0
|
||||
- "npm install"
|
||||
- "npm run compile"
|
||||
- "cd tests"
|
||||
- "TypeScriptTest.bat"
|
||||
- rem "---------------- C# -----------------"
|
||||
# Have to compile this here rather than in "build" above because AppVeyor only
|
||||
# supports building one project??
|
||||
- "cd FlatBuffers.Test"
|
||||
- "msbuild.exe /property:Configuration=Release;OutputPath=tempcs /verbosity:minimal FlatBuffers.Test.csproj"
|
||||
- "tempcs\\FlatBuffers.Test.exe"
|
||||
- "dotnet new sln"
|
||||
- "dotnet sln add FlatBuffers.Test.csproj"
|
||||
- "nuget restore"
|
||||
- "mkdir .tmp"
|
||||
- "msbuild.exe /property:Configuration=Release;OutputPath=.tmp /verbosity:minimal FlatBuffers.Test.csproj"
|
||||
- ".tmp\\FlatBuffers.Test.exe"
|
||||
# Run tests with UNSAFE_BYTEBUFFER
|
||||
- "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=tempcsUnsafe /verbosity:minimal FlatBuffers.Test.csproj"
|
||||
- "tempcsUnsafe\\FlatBuffers.Test.exe"
|
||||
# TODO: add more languages.
|
||||
- "cd ..\\.."
|
||||
- "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=.tmp /verbosity:minimal FlatBuffers.Test.csproj"
|
||||
- ".tmp\\FlatBuffers.Test.exe"
|
||||
|
||||
artifacts:
|
||||
- path: $(CONFIGURATION)\flatc.exe
|
||||
|
||||
0
bazel/BUILD.bazel
Normal file
@@ -5,6 +5,8 @@
|
||||
Rules for building C++ flatbuffers with Bazel.
|
||||
"""
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||
|
||||
flatc_path = "@com_github_google_flatbuffers//:flatc"
|
||||
|
||||
DEFAULT_INCLUDE_PATHS = [
|
||||
@@ -32,7 +34,9 @@ def flatbuffer_library_public(
|
||||
include_paths = DEFAULT_INCLUDE_PATHS,
|
||||
flatc_args = DEFAULT_FLATC_ARGS,
|
||||
reflection_name = "",
|
||||
reflection_visiblity = None,
|
||||
reflection_visibility = None,
|
||||
compatible_with = None,
|
||||
restricted_to = None,
|
||||
output_to_bindir = False):
|
||||
"""Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler.
|
||||
|
||||
@@ -48,7 +52,12 @@ def flatbuffer_library_public(
|
||||
flatc_args: Optional, list of additional arguments to pass to flatc.
|
||||
reflection_name: Optional, if set this will generate the flatbuffer
|
||||
reflection binaries for the schemas.
|
||||
reflection_visiblity: The visibility of the generated reflection Fileset.
|
||||
reflection_visibility: The visibility of the generated reflection Fileset.
|
||||
output_to_bindir: Passed to genrule for output to bin directory.
|
||||
compatible_with: Optional, The list of environments this rule can be
|
||||
built for, in addition to default-supported environments.
|
||||
restricted_to: Optional, The list of environments this rule can be built
|
||||
for, instead of default-supported environments.
|
||||
output_to_bindir: Passed to genrule for output to bin directory.
|
||||
|
||||
|
||||
@@ -82,6 +91,8 @@ def flatbuffer_library_public(
|
||||
output_to_bindir = output_to_bindir,
|
||||
tools = [flatc_path],
|
||||
cmd = genrule_cmd,
|
||||
compatible_with = compatible_with,
|
||||
restricted_to = restricted_to,
|
||||
message = "Generating flatbuffer files for %s:" % (name),
|
||||
)
|
||||
if reflection_name:
|
||||
@@ -107,16 +118,17 @@ def flatbuffer_library_public(
|
||||
outs = reflection_outs,
|
||||
output_to_bindir = output_to_bindir,
|
||||
tools = [flatc_path],
|
||||
compatible_with = compatible_with,
|
||||
restricted_to = restricted_to,
|
||||
cmd = reflection_genrule_cmd,
|
||||
message = "Generating flatbuffer reflection binary for %s:" % (name),
|
||||
)
|
||||
native.Fileset(
|
||||
name = reflection_name,
|
||||
out = "%s_out" % reflection_name,
|
||||
entries = [
|
||||
native.FilesetEntry(files = reflection_outs),
|
||||
],
|
||||
visibility = reflection_visiblity,
|
||||
native.filegroup(
|
||||
name = "%s_out" % reflection_name,
|
||||
srcs = reflection_outs,
|
||||
visibility = reflection_visibility,
|
||||
compatible_with = compatible_with,
|
||||
restricted_to = restricted_to,
|
||||
)
|
||||
|
||||
def flatbuffer_cc_library(
|
||||
@@ -128,6 +140,8 @@ def flatbuffer_cc_library(
|
||||
include_paths = DEFAULT_INCLUDE_PATHS,
|
||||
flatc_args = DEFAULT_FLATC_ARGS,
|
||||
visibility = None,
|
||||
compatible_with = None,
|
||||
restricted_to = None,
|
||||
srcs_filegroup_visibility = None,
|
||||
gen_reflections = False):
|
||||
'''A cc_library with the generated reader/writers for the given flatbuffer definitions.
|
||||
@@ -151,6 +165,10 @@ def flatbuffer_cc_library(
|
||||
By default, use the value of the visibility parameter above.
|
||||
gen_reflections: Optional, if true this will generate the flatbuffer
|
||||
reflection binaries for the schemas.
|
||||
compatible_with: Optional, The list of environments this rule can be built
|
||||
for, in addition to default-supported environments.
|
||||
restricted_to: Optional, The list of environments this rule can be built
|
||||
for, instead of default-supported environments.
|
||||
|
||||
This produces:
|
||||
filegroup([name]_srcs): all generated .h files.
|
||||
@@ -206,10 +224,12 @@ def flatbuffer_cc_library(
|
||||
includes = includes,
|
||||
include_paths = include_paths,
|
||||
flatc_args = flatc_args,
|
||||
compatible_with = compatible_with,
|
||||
restricted_to = restricted_to,
|
||||
reflection_name = reflection_name,
|
||||
reflection_visiblity = visibility,
|
||||
reflection_visibility = visibility,
|
||||
)
|
||||
native.cc_library(
|
||||
cc_library(
|
||||
name = name,
|
||||
hdrs = [
|
||||
":" + srcs_lib,
|
||||
@@ -224,6 +244,8 @@ def flatbuffer_cc_library(
|
||||
"@com_github_google_flatbuffers//:runtime_cc",
|
||||
],
|
||||
includes = [],
|
||||
compatible_with = compatible_with,
|
||||
restricted_to = restricted_to,
|
||||
linkstatic = 1,
|
||||
visibility = visibility,
|
||||
)
|
||||
@@ -233,5 +255,7 @@ def flatbuffer_cc_library(
|
||||
native.filegroup(
|
||||
name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name),
|
||||
srcs = srcs,
|
||||
compatible_with = compatible_with,
|
||||
restricted_to = restricted_to,
|
||||
visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility,
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, non_constant_identifier_names
|
||||
// ignore_for_file: unused_import, unused_field, unused_local_variable
|
||||
|
||||
library my_game.sample;
|
||||
|
||||
@@ -12,7 +12,7 @@ class Color {
|
||||
const Color._(this.value);
|
||||
|
||||
factory Color.fromValue(int value) {
|
||||
if (value == null) return null;
|
||||
if (value == null) value = 0;
|
||||
if (!values.containsKey(value)) {
|
||||
throw new StateError('Invalid value $value for bit flag enum Color');
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class Color {
|
||||
static const Color Red = const Color._(0);
|
||||
static const Color Green = const Color._(1);
|
||||
static const Color Blue = const Color._(2);
|
||||
static get values => {0: Red,1: Green,2: Blue,};
|
||||
static const Map<int,Color> values = {0: Red,1: Green,2: Blue,};
|
||||
|
||||
static const fb.Reader<Color> reader = const _ColorReader();
|
||||
|
||||
@@ -52,7 +52,7 @@ class EquipmentTypeId {
|
||||
const EquipmentTypeId._(this.value);
|
||||
|
||||
factory EquipmentTypeId.fromValue(int value) {
|
||||
if (value == null) return null;
|
||||
if (value == null) value = 0;
|
||||
if (!values.containsKey(value)) {
|
||||
throw new StateError('Invalid value $value for bit flag enum EquipmentTypeId');
|
||||
}
|
||||
@@ -65,7 +65,7 @@ class EquipmentTypeId {
|
||||
|
||||
static const EquipmentTypeId NONE = const EquipmentTypeId._(0);
|
||||
static const EquipmentTypeId Weapon = const EquipmentTypeId._(1);
|
||||
static get values => {0: NONE,1: Weapon,};
|
||||
static const Map<int,EquipmentTypeId> values = {0: NONE,1: Weapon,};
|
||||
|
||||
static const fb.Reader<EquipmentTypeId> reader = const _EquipmentTypeIdReader();
|
||||
|
||||
@@ -184,7 +184,7 @@ class Monster {
|
||||
List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null);
|
||||
Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 2));
|
||||
List<Weapon> get weapons => const fb.ListReader<Weapon>(Weapon.reader).vTableGet(_bc, _bcOffset, 18, null);
|
||||
EquipmentTypeId get equippedType => new EquipmentTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 20, null));
|
||||
EquipmentTypeId get equippedType => new EquipmentTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 20, 0));
|
||||
dynamic get equipped {
|
||||
switch (equippedType?.value) {
|
||||
case 1: return Weapon.reader.vTableGet(_bc, _bcOffset, 22, null);
|
||||
@@ -363,7 +363,7 @@ class Weapon {
|
||||
final int _bcOffset;
|
||||
|
||||
String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 4, null);
|
||||
int get damage => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, null);
|
||||
int get damage => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 0);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
|
||||
@@ -149,6 +149,9 @@ class Builder {
|
||||
reset();
|
||||
}
|
||||
|
||||
/// Calculate the finished buffer size (aligned).
|
||||
int size() => _tail + ((-_tail) % _maxAlign);
|
||||
|
||||
/// Add the [field] with the given boolean [value]. The field is not added if
|
||||
/// the [value] is equal to [def]. Booleans are stored as 8-bit fields with
|
||||
/// `0` for `false` and `1` for `true`.
|
||||
@@ -335,8 +338,7 @@ class Builder {
|
||||
///
|
||||
/// Most clients should prefer calling [finish].
|
||||
Uint8List lowFinish() {
|
||||
int alignedTail = _tail + ((-_tail) % _maxAlign);
|
||||
return _buf.buffer.asUint8List(_buf.lengthInBytes - alignedTail);
|
||||
return _buf.buffer.asUint8List(_buf.lengthInBytes - size());
|
||||
}
|
||||
|
||||
/// Finish off the creation of the buffer. The given [offset] is used as the
|
||||
@@ -346,15 +348,15 @@ class Builder {
|
||||
/// bytes 4-7 of the file.
|
||||
Uint8List finish(int offset, [String fileIdentifier]) {
|
||||
_prepare(max(_sizeofUint32, _maxAlign), fileIdentifier == null ? 1 : 2);
|
||||
int alignedTail = _tail + ((-_tail) % _maxAlign);
|
||||
_setUint32AtTail(_buf, alignedTail, alignedTail - offset);
|
||||
final finishedSize = size();
|
||||
_setUint32AtTail(_buf, finishedSize, finishedSize - offset);
|
||||
if (fileIdentifier != null) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
_setUint8AtTail(_buf, alignedTail - _sizeofUint32 - i,
|
||||
_setUint8AtTail(_buf, finishedSize - _sizeofUint32 - i,
|
||||
fileIdentifier.codeUnitAt(i));
|
||||
}
|
||||
}
|
||||
return _buf.buffer.asUint8List(_buf.lengthInBytes - alignedTail);
|
||||
return _buf.buffer.asUint8List(_buf.lengthInBytes - finishedSize);
|
||||
}
|
||||
|
||||
/// Writes a Float64 to the tail of the buffer after preparing space for it.
|
||||
@@ -443,6 +445,7 @@ class Builder {
|
||||
_maxAlign = 1;
|
||||
_tail = 0;
|
||||
_currentVTable = null;
|
||||
_vTables.clear();
|
||||
if (_strings != null) {
|
||||
_strings = new Map<String, int>();
|
||||
}
|
||||
@@ -492,7 +495,7 @@ class Builder {
|
||||
/// Write the given list of 64-bit float [values].
|
||||
int writeListFloat64(List<double> values) {
|
||||
_ensureNoVTable();
|
||||
_prepare(4, 1 + (2 * values.length));
|
||||
_prepare(_sizeofFloat64, values.length, additionalBytes: _sizeofUint32);
|
||||
final int result = _tail;
|
||||
int tail = _tail;
|
||||
_setUint32AtTail(_buf, tail, values.length);
|
||||
@@ -522,7 +525,7 @@ class Builder {
|
||||
/// Write the given list of signed 64-bit integer [values].
|
||||
int writeListInt64(List<int> values) {
|
||||
_ensureNoVTable();
|
||||
_prepare(_sizeofUint32, 2 * values.length);
|
||||
_prepare(_sizeofInt64, values.length, additionalBytes: _sizeofUint32);
|
||||
final int result = _tail;
|
||||
int tail = _tail;
|
||||
_setUint32AtTail(_buf, tail, values.length);
|
||||
@@ -537,7 +540,7 @@ class Builder {
|
||||
/// Write the given list of signed 64-bit integer [values].
|
||||
int writeListUint64(List<int> values) {
|
||||
_ensureNoVTable();
|
||||
_prepare(_sizeofUint32, 2 * values.length);
|
||||
_prepare(_sizeofUint64, values.length, additionalBytes: _sizeofUint32);
|
||||
final int result = _tail;
|
||||
int tail = _tail;
|
||||
_setUint32AtTail(_buf, tail, values.length);
|
||||
@@ -669,6 +672,7 @@ class Builder {
|
||||
for (int i = 0; i < length; i++) {
|
||||
_buf.setUint8(offset++, bytes[i]);
|
||||
}
|
||||
_buf.setUint8(offset, 0); // trailing zero
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
2
dart/lib/flex_buffers.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
export 'src/builder.dart';
|
||||
export 'src/reference.dart';
|
||||
666
dart/lib/src/builder.dart
Normal file
@@ -0,0 +1,666 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'types.dart';
|
||||
|
||||
/// The main builder class for creation of a FlexBuffer.
|
||||
class Builder {
|
||||
ByteData _buffer;
|
||||
List<_StackValue> _stack;
|
||||
List<_StackPointer> _stackPointers;
|
||||
int _offset;
|
||||
bool _finished;
|
||||
Map<String, _StackValue> _stringCache;
|
||||
Map<String, _StackValue> _keyCache;
|
||||
Map<_KeysHash, _StackValue> _keyVectorCache;
|
||||
Map<int, _StackValue> _indirectIntCache;
|
||||
Map<double, _StackValue> _indirectDoubleCache;
|
||||
|
||||
/// Instantiate the builder if you intent to gradually build up the buffer by calling
|
||||
/// add... methods and calling [finish] to receive the the resulting byte array.
|
||||
///
|
||||
/// The default size of internal buffer is set to 2048. Provide a different value in order to avoid buffer copies.
|
||||
Builder({int size = 2048}) {
|
||||
_buffer = ByteData(size);
|
||||
_stack = [];
|
||||
_stackPointers = [];
|
||||
_offset = 0;
|
||||
_finished = false;
|
||||
_stringCache = {};
|
||||
_keyCache = {};
|
||||
_keyVectorCache = {};
|
||||
_indirectIntCache = {};
|
||||
_indirectDoubleCache = {};
|
||||
}
|
||||
|
||||
/// Use this method in order to turn an object into a FlexBuffer directly.
|
||||
///
|
||||
/// Use the manual instantiation of the [Builder] and gradual addition of values, if performance is more important than convenience.
|
||||
static ByteBuffer buildFromObject(Object value) {
|
||||
final builder = Builder();
|
||||
builder._add(value);
|
||||
final buffer = builder.finish();
|
||||
final byteData = ByteData(buffer.lengthInBytes);
|
||||
byteData.buffer.asUint8List().setAll(0, buffer);
|
||||
return byteData.buffer;
|
||||
}
|
||||
|
||||
void _add(Object value) {
|
||||
if (value == null) {
|
||||
addNull();
|
||||
} else if (value is bool) {
|
||||
addBool(value);
|
||||
} else if (value is int) {
|
||||
addInt(value);
|
||||
} else if (value is double) {
|
||||
addDouble(value);
|
||||
} else if (value is ByteBuffer) {
|
||||
addBlob(value);
|
||||
} else if (value is String) {
|
||||
addString(value);
|
||||
} else if (value is List<dynamic>) {
|
||||
startVector();
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
_add(value[i]);
|
||||
}
|
||||
end();
|
||||
} else if (value is Map<String, dynamic>) {
|
||||
startMap();
|
||||
value.forEach((key, value) {
|
||||
addKey(key);
|
||||
_add(value);
|
||||
});
|
||||
end();
|
||||
} else {
|
||||
throw UnsupportedError('Value of unexpected type: $value');
|
||||
}
|
||||
}
|
||||
|
||||
/// Use this method if you want to store a null value.
|
||||
///
|
||||
/// Specifically useful when building up a vector where values can be null.
|
||||
void addNull() {
|
||||
_integrityCheckOnValueAddition();
|
||||
_stack.add(_StackValue.WithNull());
|
||||
}
|
||||
|
||||
/// Adds a string value.
|
||||
void addInt(int value) {
|
||||
_integrityCheckOnValueAddition();
|
||||
_stack.add(_StackValue.WithInt(value));
|
||||
}
|
||||
|
||||
/// Adds a bool value.
|
||||
void addBool(bool value) {
|
||||
_integrityCheckOnValueAddition();
|
||||
_stack.add(_StackValue.WithBool(value));
|
||||
}
|
||||
|
||||
/// Adds a double value.
|
||||
void addDouble(double value) {
|
||||
_integrityCheckOnValueAddition();
|
||||
_stack.add(_StackValue.WithDouble(value));
|
||||
}
|
||||
|
||||
/// Adds a string value.
|
||||
void addString(String value) {
|
||||
_integrityCheckOnValueAddition();
|
||||
if (_stringCache.containsKey(value)) {
|
||||
_stack.add(_stringCache[value]);
|
||||
return;
|
||||
}
|
||||
final utf8String = utf8.encode(value);
|
||||
final length = utf8String.length;
|
||||
final bitWidth = BitWidthUtil.uwidth(length);
|
||||
final byteWidth = _align(bitWidth);
|
||||
_writeUInt(length, byteWidth);
|
||||
final stringOffset = _offset;
|
||||
final newOffset = _newOffset(length + 1);
|
||||
_pushBuffer(utf8String);
|
||||
_offset = newOffset;
|
||||
final stackValue = _StackValue.WithOffset(stringOffset, ValueType.String, bitWidth);
|
||||
_stack.add(stackValue);
|
||||
_stringCache[value] = stackValue;
|
||||
}
|
||||
|
||||
/// This methods adds a key to a map and should be followed by an add... value call.
|
||||
///
|
||||
/// It also implies that you call this method only after you called [startMap].
|
||||
void addKey(String value) {
|
||||
_integrityCheckOnKeyAddition();
|
||||
if (_keyCache.containsKey(value)) {
|
||||
_stack.add(_keyCache[value]);
|
||||
return;
|
||||
}
|
||||
final utf8String = utf8.encode(value);
|
||||
final length = utf8String.length;
|
||||
final keyOffset = _offset;
|
||||
final newOffset = _newOffset(length + 1);
|
||||
_pushBuffer(utf8String);
|
||||
_offset = newOffset;
|
||||
final stackValue = _StackValue.WithOffset(keyOffset, ValueType.Key, BitWidth.width8);
|
||||
_stack.add(stackValue);
|
||||
_keyCache[value] = stackValue;
|
||||
}
|
||||
|
||||
/// Adds a byte array.
|
||||
///
|
||||
/// This method can be used to store any generic BLOB.
|
||||
void addBlob(ByteBuffer value) {
|
||||
_integrityCheckOnValueAddition();
|
||||
final length = value.lengthInBytes;
|
||||
final bitWidth = BitWidthUtil.uwidth(length);
|
||||
final byteWidth = _align(bitWidth);
|
||||
_writeUInt(length, byteWidth);
|
||||
final blobOffset = _offset;
|
||||
final newOffset = _newOffset(length);
|
||||
_pushBuffer(value.asUint8List());
|
||||
_offset = newOffset;
|
||||
final stackValue = _StackValue.WithOffset(blobOffset, ValueType.Blob, bitWidth);
|
||||
_stack.add(stackValue);
|
||||
}
|
||||
|
||||
/// Stores int value indirectly in the buffer.
|
||||
///
|
||||
/// Adding large integer values indirectly might be beneficial if those values suppose to be store in a vector together with small integer values.
|
||||
/// This is due to the fact that FlexBuffers will add padding to small integer values, if they are stored together with large integer values.
|
||||
/// When we add integer indirectly the vector of ints will contain not the value itself, but only the relative offset to the value.
|
||||
/// By setting the [cache] parameter to true, you make sure that the builder tracks added int value and performs deduplication.
|
||||
void addIntIndirectly(int value, {bool cache = false}) {
|
||||
_integrityCheckOnValueAddition();
|
||||
if (_indirectIntCache.containsKey(value)) {
|
||||
_stack.add(_indirectIntCache[value]);
|
||||
return;
|
||||
}
|
||||
final stackValue = _StackValue.WithInt(value);
|
||||
final byteWidth = _align(stackValue.width);
|
||||
final newOffset = _newOffset(byteWidth);
|
||||
final valueOffset = _offset;
|
||||
_pushBuffer(stackValue.asU8List(stackValue.width));
|
||||
final stackOffset = _StackValue.WithOffset(valueOffset, ValueType.IndirectInt, stackValue.width);
|
||||
_stack.add(stackOffset);
|
||||
_offset = newOffset;
|
||||
if (cache) {
|
||||
_indirectIntCache[value] = stackOffset;
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores double value indirectly in the buffer.
|
||||
///
|
||||
/// Double are stored as 8 or 4 byte values in FlexBuffers. If they are stored in a mixed vector, values which are smaller than 4 / 8 bytes will be padded.
|
||||
/// When we add double indirectly, the vector will contain not the value itself, but only the relative offset to the value. Which could occupy only 1 or 2 bytes, reducing the odds for unnecessary padding.
|
||||
/// By setting the [cache] parameter to true, you make sure that the builder tracks already added double value and performs deduplication.
|
||||
void addDoubleIndirectly(double value, {bool cache = false}) {
|
||||
_integrityCheckOnValueAddition();
|
||||
if (cache && _indirectDoubleCache.containsKey(value)) {
|
||||
_stack.add(_indirectDoubleCache[value]);
|
||||
return;
|
||||
}
|
||||
final stackValue = _StackValue.WithDouble(value);
|
||||
final byteWidth = _align(stackValue.width);
|
||||
final newOffset = _newOffset(byteWidth);
|
||||
final valueOffset = _offset;
|
||||
_pushBuffer(stackValue.asU8List(stackValue.width));
|
||||
final stackOffset = _StackValue.WithOffset(valueOffset, ValueType.IndirectFloat, stackValue.width);
|
||||
_stack.add(stackOffset);
|
||||
_offset = newOffset;
|
||||
if (cache) {
|
||||
_indirectDoubleCache[value] = stackOffset;
|
||||
}
|
||||
}
|
||||
|
||||
/// This method starts a vector definition and needs to be followed by 0 to n add... value calls.
|
||||
///
|
||||
/// The vector definition needs to be finished with an [end] call.
|
||||
/// It is also possible to add nested vector or map by calling [startVector] / [startMap].
|
||||
void startVector() {
|
||||
_integrityCheckOnValueAddition();
|
||||
_stackPointers.add(_StackPointer(_stack.length, true));
|
||||
}
|
||||
|
||||
/// This method starts a map definition.
|
||||
///
|
||||
/// This method call needs to be followed by 0 to n [addKey] + add... value calls.
|
||||
/// The map definition needs to be finished with an [end] call.
|
||||
/// It is also possible to add nested vector or map by calling [startVector] / [startMap] after calling [addKey].
|
||||
void startMap() {
|
||||
_integrityCheckOnValueAddition();
|
||||
_stackPointers.add(_StackPointer(_stack.length, false));
|
||||
}
|
||||
|
||||
/// Marks that the addition of values to the last vector, or map have ended.
|
||||
void end() {
|
||||
final pointer = _stackPointers.removeLast();
|
||||
if (pointer.isVector) {
|
||||
_endVector(pointer);
|
||||
} else {
|
||||
_sortKeysAndEndMap(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
/// Finish building the FlatBuffer and return array of bytes.
|
||||
///
|
||||
/// Can be called multiple times, to get the array of bytes.
|
||||
/// After the first call, adding values, or starting vectors / maps will result in an exception.
|
||||
Uint8List finish() {
|
||||
if (_finished == false) {
|
||||
_finish();
|
||||
}
|
||||
return _buffer.buffer.asUint8List(0, _offset);
|
||||
}
|
||||
|
||||
/// Builds a FlatBuffer with current state without finishing the builder.
|
||||
///
|
||||
/// Creates an internal temporary copy of current builder and finishes the copy.
|
||||
/// Use this method, when the state of a long lasting builder need to be persisted periodically.
|
||||
ByteBuffer snapshot() {
|
||||
final tmp = Builder(size: _offset + 200);
|
||||
tmp._offset = _offset;
|
||||
tmp._stack = List.from(_stack);
|
||||
tmp._stackPointers = List.from(_stackPointers);
|
||||
tmp._buffer.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List(0, _offset));
|
||||
for (var i = 0; i < tmp._stackPointers.length; i++){
|
||||
tmp.end();
|
||||
}
|
||||
final buffer = tmp.finish();
|
||||
final bd = ByteData(buffer.lengthInBytes);
|
||||
bd.buffer.asUint8List().setAll(0, buffer);
|
||||
return bd.buffer;
|
||||
}
|
||||
|
||||
void _integrityCheckOnValueAddition() {
|
||||
if (_finished) {
|
||||
throw StateError('Adding values after finish is prohibited');
|
||||
}
|
||||
if (_stackPointers.isNotEmpty && _stackPointers.last.isVector == false) {
|
||||
if (_stack.last.type != ValueType.Key) {
|
||||
throw StateError('Adding value to a map before adding a key is prohibited');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _integrityCheckOnKeyAddition() {
|
||||
if (_finished) {
|
||||
throw StateError('Adding values after finish is prohibited');
|
||||
}
|
||||
if (_stackPointers.isEmpty || _stackPointers.last.isVector) {
|
||||
throw StateError('Adding key before staring a map is prohibited');
|
||||
}
|
||||
}
|
||||
|
||||
void _finish() {
|
||||
if (_stack.length != 1) {
|
||||
throw StateError('Stack has to be exactly 1, but is ${_stack.length}. You have to end all started vectors and maps, before calling [finish]');
|
||||
}
|
||||
final value = _stack[0];
|
||||
final byteWidth = _align(value.elementWidth(_offset, 0));
|
||||
_writeStackValue(value, byteWidth);
|
||||
_writeUInt(value.storedPackedType(), 1);
|
||||
_writeUInt(byteWidth, 1);
|
||||
_finished = true;
|
||||
}
|
||||
|
||||
_StackValue _createVector(int start, int vecLength, int step, [_StackValue keys]) {
|
||||
var bitWidth = BitWidthUtil.uwidth(vecLength);
|
||||
var prefixElements = 1;
|
||||
if (keys != null) {
|
||||
var elemWidth = keys.elementWidth(_offset, 0);
|
||||
if (elemWidth.index > bitWidth.index) {
|
||||
bitWidth = elemWidth;
|
||||
}
|
||||
prefixElements += 2;
|
||||
}
|
||||
var vectorType = ValueType.Key;
|
||||
var typed = keys == null;
|
||||
for (var i = start; i < _stack.length; i += step) {
|
||||
final elemWidth = _stack[i].elementWidth(_offset, i + prefixElements);
|
||||
if (elemWidth.index > bitWidth.index) {
|
||||
bitWidth = elemWidth;
|
||||
}
|
||||
if (i == start) {
|
||||
vectorType = _stack[i].type;
|
||||
typed &= ValueTypeUtils.isTypedVectorElement(vectorType);
|
||||
} else {
|
||||
if (vectorType != _stack[i].type) {
|
||||
typed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
final byteWidth = _align(bitWidth);
|
||||
final fix = typed & ValueTypeUtils.isNumber(vectorType) && vecLength >= 2 && vecLength <= 4;
|
||||
if (keys != null) {
|
||||
_writeStackValue(keys, byteWidth);
|
||||
_writeUInt(1 << keys.width.index, byteWidth);
|
||||
}
|
||||
if (fix == false) {
|
||||
_writeUInt(vecLength, byteWidth);
|
||||
}
|
||||
final vecOffset = _offset;
|
||||
for (var i = start; i < _stack.length; i += step) {
|
||||
_writeStackValue(_stack[i], byteWidth);
|
||||
}
|
||||
if (typed == false) {
|
||||
for (var i = start; i < _stack.length; i += step) {
|
||||
_writeUInt(_stack[i].storedPackedType(), 1);
|
||||
}
|
||||
}
|
||||
if (keys != null) {
|
||||
return _StackValue.WithOffset(vecOffset, ValueType.Map, bitWidth);
|
||||
}
|
||||
if (typed) {
|
||||
final vType = ValueTypeUtils.toTypedVector(vectorType, fix ? vecLength : 0);
|
||||
return _StackValue.WithOffset(vecOffset, vType, bitWidth);
|
||||
}
|
||||
return _StackValue.WithOffset(vecOffset, ValueType.Vector, bitWidth);
|
||||
}
|
||||
|
||||
void _endVector(_StackPointer pointer) {
|
||||
final vecLength = _stack.length - pointer.stackPosition;
|
||||
final vec = _createVector(pointer.stackPosition, vecLength, 1);
|
||||
_stack.removeRange(pointer.stackPosition, _stack.length);
|
||||
_stack.add(vec);
|
||||
}
|
||||
|
||||
void _sortKeysAndEndMap(_StackPointer pointer) {
|
||||
if (((_stack.length - pointer.stackPosition) & 1) == 1) {
|
||||
throw StateError('The stack needs to hold key value pairs (even number of elements). Check if you combined [addKey] with add... method calls properly.');
|
||||
}
|
||||
|
||||
var sorted = true;
|
||||
for (var i = pointer.stackPosition; i < _stack.length - 2; i += 2) {
|
||||
if (_shouldFlip(_stack[i], _stack[i+2])) {
|
||||
sorted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sorted == false) {
|
||||
for (var i = pointer.stackPosition; i < _stack.length; i += 2) {
|
||||
var flipIndex = i;
|
||||
for (var j = i + 2; j < _stack.length; j += 2) {
|
||||
if (_shouldFlip(_stack[flipIndex], _stack[j])) {
|
||||
flipIndex = j;
|
||||
}
|
||||
}
|
||||
if (flipIndex != i) {
|
||||
var k = _stack[flipIndex];
|
||||
var v = _stack[flipIndex + 1];
|
||||
_stack[flipIndex] = _stack[i];
|
||||
_stack[flipIndex + 1] = _stack[i + 1];
|
||||
_stack[i] = k;
|
||||
_stack[i + 1] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
_endMap(pointer);
|
||||
}
|
||||
|
||||
void _endMap(_StackPointer pointer) {
|
||||
final vecLength = (_stack.length - pointer.stackPosition) >> 1;
|
||||
final offsets = <int>[];
|
||||
for (var i = pointer.stackPosition; i < _stack.length; i += 2) {
|
||||
offsets.add(_stack[i].offset);
|
||||
}
|
||||
final keysHash = _KeysHash(offsets);
|
||||
var keysStackValue;
|
||||
if (_keyVectorCache.containsKey(keysHash)) {
|
||||
keysStackValue = _keyVectorCache[keysHash];
|
||||
} else {
|
||||
keysStackValue = _createVector(pointer.stackPosition, vecLength, 2);
|
||||
_keyVectorCache[keysHash] = keysStackValue;
|
||||
}
|
||||
final vec = _createVector(pointer.stackPosition + 1, vecLength, 2, keysStackValue);
|
||||
_stack.removeRange(pointer.stackPosition, _stack.length);
|
||||
_stack.add(vec);
|
||||
}
|
||||
|
||||
bool _shouldFlip(_StackValue v1, _StackValue v2) {
|
||||
if (v1.type != ValueType.Key || v2.type != ValueType.Key) {
|
||||
throw StateError('Stack values are not keys $v1 | $v2. Check if you combined [addKey] with add... method calls properly.');
|
||||
}
|
||||
|
||||
var c1, c2;
|
||||
var index = 0;
|
||||
do {
|
||||
c1 = _buffer.getUint8(v1.offset + index);
|
||||
c2 = _buffer.getUint8(v2.offset + index);
|
||||
if (c2 < c1) return true;
|
||||
if (c1 < c2) return false;
|
||||
index += 1;
|
||||
} while (c1 != 0 && c2 != 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
int _align(BitWidth width) {
|
||||
final byteWidth = BitWidthUtil.toByteWidth(width);
|
||||
_offset += BitWidthUtil.paddingSize(_offset, byteWidth);
|
||||
return byteWidth;
|
||||
}
|
||||
|
||||
void _writeStackValue(_StackValue value, int byteWidth) {
|
||||
final newOffset = _newOffset(byteWidth);
|
||||
if (value.isOffset) {
|
||||
final relativeOffset = _offset - value.offset;
|
||||
if (byteWidth == 8 || relativeOffset < (1 << (byteWidth * 8))) {
|
||||
_writeUInt(relativeOffset, byteWidth);
|
||||
} else {
|
||||
throw StateError('Unexpected size $byteWidth. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
|
||||
}
|
||||
} else {
|
||||
_pushBuffer(value.asU8List(BitWidthUtil.fromByteWidth(byteWidth)));
|
||||
}
|
||||
_offset = newOffset;
|
||||
}
|
||||
|
||||
void _writeUInt(int value, int byteWidth) {
|
||||
final newOffset = _newOffset(byteWidth);
|
||||
_pushUInt(value, BitWidthUtil.fromByteWidth(byteWidth));
|
||||
_offset = newOffset;
|
||||
}
|
||||
|
||||
int _newOffset(int newValueSize) {
|
||||
final newOffset = _offset + newValueSize;
|
||||
var size = _buffer.lengthInBytes;
|
||||
final prevSize = size;
|
||||
while (size < newOffset) {
|
||||
size <<= 1;
|
||||
}
|
||||
if (prevSize < size) {
|
||||
final newBuf = ByteData(size);
|
||||
newBuf.buffer
|
||||
.asUint8List()
|
||||
.setAll(0, _buffer.buffer.asUint8List());
|
||||
}
|
||||
return newOffset;
|
||||
}
|
||||
|
||||
void _pushInt(int value, BitWidth width) {
|
||||
switch (width) {
|
||||
|
||||
case BitWidth.width8:
|
||||
_buffer.setInt8(_offset, value);
|
||||
break;
|
||||
case BitWidth.width16:
|
||||
_buffer.setInt16(_offset, value, Endian.little);
|
||||
break;
|
||||
case BitWidth.width32:
|
||||
_buffer.setInt32(_offset, value, Endian.little);
|
||||
break;
|
||||
case BitWidth.width64:
|
||||
_buffer.setInt64(_offset, value, Endian.little);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _pushUInt(int value, BitWidth width) {
|
||||
switch (width) {
|
||||
|
||||
case BitWidth.width8:
|
||||
_buffer.setUint8(_offset, value);
|
||||
break;
|
||||
case BitWidth.width16:
|
||||
_buffer.setUint16(_offset, value, Endian.little);
|
||||
break;
|
||||
case BitWidth.width32:
|
||||
_buffer.setUint32(_offset, value, Endian.little);
|
||||
break;
|
||||
case BitWidth.width64:
|
||||
_buffer.setUint64(_offset, value, Endian.little);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _pushBuffer(List<int> value) {
|
||||
_buffer.buffer.asUint8List().setAll(_offset, value);
|
||||
}
|
||||
}
|
||||
|
||||
class _StackValue {
|
||||
Object _value;
|
||||
int _offset;
|
||||
ValueType _type;
|
||||
BitWidth _width;
|
||||
_StackValue.WithNull() {
|
||||
_type = ValueType.Null;
|
||||
_width = BitWidth.width8;
|
||||
}
|
||||
_StackValue.WithInt(int value) {
|
||||
_type = value != null ? ValueType.Int : ValueType.Null;
|
||||
_width = BitWidthUtil.width(value);
|
||||
_value = value;
|
||||
}
|
||||
_StackValue.WithBool(bool value) {
|
||||
_type = value != null ? ValueType.Bool : ValueType.Null;
|
||||
_width = BitWidth.width8;
|
||||
_value = value;
|
||||
}
|
||||
_StackValue.WithDouble(double value) {
|
||||
_type = value != null ? ValueType.Float : ValueType.Null;
|
||||
_width = BitWidthUtil.width(value);
|
||||
_value = value;
|
||||
}
|
||||
_StackValue.WithOffset(int value, ValueType type, BitWidth width) {
|
||||
_offset = value;
|
||||
_type = type;
|
||||
_width = width;
|
||||
}
|
||||
|
||||
BitWidth storedWidth({BitWidth width = BitWidth.width8}) {
|
||||
return ValueTypeUtils.isInline(_type) ? BitWidthUtil.max(_width, width) : _width;
|
||||
}
|
||||
|
||||
int storedPackedType({BitWidth width = BitWidth.width8}) {
|
||||
return ValueTypeUtils.packedType(_type, storedWidth(width: width));
|
||||
}
|
||||
|
||||
BitWidth elementWidth(int size, int index) {
|
||||
if (ValueTypeUtils.isInline(_type)) return _width;
|
||||
for(var i = 0; i < 4; i++) {
|
||||
final width = 1 << i;
|
||||
final offsetLoc = size + BitWidthUtil.paddingSize(size, width) + index * width;
|
||||
final offset = offsetLoc - _offset;
|
||||
final bitWidth = BitWidthUtil.uwidth(offset);
|
||||
if (1 << bitWidth.index == width) {
|
||||
return bitWidth;
|
||||
}
|
||||
}
|
||||
throw StateError('Element is of unknown. Size: $size at index: $index. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
|
||||
}
|
||||
|
||||
List<int> asU8List(BitWidth width) {
|
||||
if (ValueTypeUtils.isNumber(_type)) {
|
||||
if (_type == ValueType.Float) {
|
||||
if (width == BitWidth.width32) {
|
||||
final result = ByteData(4);
|
||||
result.setFloat32(0, _value, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
} else {
|
||||
final result = ByteData(8);
|
||||
result.setFloat64(0, _value, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
}
|
||||
} else {
|
||||
switch(width) {
|
||||
case BitWidth.width8:
|
||||
final result = ByteData(1);
|
||||
result.setInt8(0, _value);
|
||||
return result.buffer.asUint8List();
|
||||
case BitWidth.width16:
|
||||
final result = ByteData(2);
|
||||
result.setInt16(0, _value, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
case BitWidth.width32:
|
||||
final result = ByteData(4);
|
||||
result.setInt32(0, _value, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
case BitWidth.width64:
|
||||
final result = ByteData(8);
|
||||
result.setInt64(0, _value, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_type == ValueType.Null) {
|
||||
final result = ByteData(1);
|
||||
result.setInt8(0, 0);
|
||||
return result.buffer.asUint8List();
|
||||
}
|
||||
if (_type == ValueType.Bool) {
|
||||
final result = ByteData(1);
|
||||
result.setInt8(0, _value ? 1 : 0);
|
||||
return result.buffer.asUint8List();
|
||||
}
|
||||
|
||||
throw StateError('Unexpected type: $_type. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
|
||||
}
|
||||
|
||||
ValueType get type {
|
||||
return _type;
|
||||
}
|
||||
|
||||
BitWidth get width {
|
||||
return _width;
|
||||
}
|
||||
|
||||
bool get isOffset {
|
||||
return !ValueTypeUtils.isInline(_type);
|
||||
}
|
||||
int get offset => _offset;
|
||||
|
||||
bool get isFloat32 {
|
||||
return _type == ValueType.Float && _width == BitWidth.width32;
|
||||
}
|
||||
}
|
||||
|
||||
class _StackPointer {
|
||||
int stackPosition;
|
||||
bool isVector;
|
||||
_StackPointer(this.stackPosition, this.isVector);
|
||||
}
|
||||
|
||||
class _KeysHash {
|
||||
final List<int> keys;
|
||||
|
||||
const _KeysHash(this.keys);
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (other is _KeysHash) {
|
||||
if (keys.length != other.keys.length) return false;
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != other.keys[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
var result = 17;
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
result = result * 23 + keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
446
dart/lib/src/reference.dart
Normal file
@@ -0,0 +1,446 @@
|
||||
import 'dart:collection';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'types.dart';
|
||||
|
||||
/// Main class to read a value out of a FlexBuffer.
|
||||
///
|
||||
/// This class let you access values stored in the buffer in a lazy fashion.
|
||||
class Reference {
|
||||
final ByteData _buffer;
|
||||
final int _offset;
|
||||
final BitWidth _parentWidth;
|
||||
final String _path;
|
||||
int _byteWidth;
|
||||
ValueType _valueType;
|
||||
int _length;
|
||||
|
||||
Reference._(this._buffer, this._offset, this._parentWidth, int packedType, this._path) {
|
||||
_byteWidth = 1 << (packedType & 3);
|
||||
_valueType = ValueTypeUtils.fromInt(packedType >> 2);
|
||||
}
|
||||
|
||||
/// Use this method to access the root value of a FlexBuffer.
|
||||
static Reference fromBuffer(ByteBuffer buffer) {
|
||||
final len = buffer.lengthInBytes;
|
||||
if (len < 3) {
|
||||
throw UnsupportedError('Buffer needs to be bigger than 3');
|
||||
}
|
||||
final byteData = ByteData.view(buffer);
|
||||
final byteWidth = byteData.getUint8(len - 1);
|
||||
final packedType = byteData.getUint8(len - 2);
|
||||
final offset = len - byteWidth - 2;
|
||||
return Reference._(ByteData.view(buffer), offset, BitWidthUtil.fromByteWidth(byteWidth), packedType, "/");
|
||||
}
|
||||
|
||||
/// Returns true if the underlying value is null.
|
||||
bool get isNull => _valueType == ValueType.Null;
|
||||
/// Returns true if the underlying value can be represented as [num].
|
||||
bool get isNum => ValueTypeUtils.isNumber(_valueType) || ValueTypeUtils.isIndirectNumber(_valueType);
|
||||
/// Returns true if the underlying value was encoded as a float (direct or indirect).
|
||||
bool get isDouble => _valueType == ValueType.Float || _valueType == ValueType.IndirectFloat;
|
||||
/// Returns true if the underlying value was encoded as an int or uint (direct or indirect).
|
||||
bool get isInt => isNum && !isDouble;
|
||||
/// Returns true if the underlying value was encoded as a string or a key.
|
||||
bool get isString => _valueType == ValueType.String || _valueType == ValueType.Key;
|
||||
/// Returns true if the underlying value was encoded as a bool.
|
||||
bool get isBool => _valueType == ValueType.Bool;
|
||||
/// Returns true if the underlying value was encoded as a blob.
|
||||
bool get isBlob => _valueType == ValueType.Blob;
|
||||
/// Returns true if the underlying value points to a vector.
|
||||
bool get isVector => ValueTypeUtils.isAVector(_valueType);
|
||||
/// Returns true if the underlying value points to a map.
|
||||
bool get isMap => _valueType == ValueType.Map;
|
||||
|
||||
/// If this [isBool], returns the bool value. Otherwise, returns null.
|
||||
bool get boolValue {
|
||||
if(_valueType == ValueType.Bool) {
|
||||
return _readInt(_offset, _parentWidth) != 0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns an [int], if the underlying value can be represented as an int.
|
||||
///
|
||||
/// Otherwise returns [null].
|
||||
int get intValue {
|
||||
if (_valueType == ValueType.Int) {
|
||||
return _readInt(_offset, _parentWidth);
|
||||
}
|
||||
if (_valueType == ValueType.UInt) {
|
||||
return _readUInt(_offset, _parentWidth);
|
||||
}
|
||||
if (_valueType == ValueType.IndirectInt) {
|
||||
return _readInt(_indirect, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
}
|
||||
if (_valueType == ValueType.IndirectUInt) {
|
||||
return _readUInt(_indirect, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns [double], if the underlying value [isDouble].
|
||||
///
|
||||
/// Otherwise returns [null].
|
||||
double get doubleValue {
|
||||
if (_valueType == ValueType.Float) {
|
||||
return _readFloat(_offset, _parentWidth);
|
||||
}
|
||||
if (_valueType == ValueType.IndirectFloat) {
|
||||
return _readFloat(_indirect, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns [num], if the underlying value is numeric, be it int uint, or float (direct or indirect).
|
||||
///
|
||||
/// Otherwise returns [null].
|
||||
num get numValue => doubleValue ?? intValue;
|
||||
|
||||
/// Returns [String] value or null otherwise.
|
||||
///
|
||||
/// This method performers a utf8 decoding, as FlexBuffers format stores strings in utf8 encoding.
|
||||
String get stringValue {
|
||||
if (_valueType == ValueType.String || _valueType == ValueType.Key) {
|
||||
return utf8.decode(_buffer.buffer.asUint8List(_indirect, length));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns [Uint8List] value or null otherwise.
|
||||
Uint8List get blobValue {
|
||||
if (_valueType == ValueType.Blob) {
|
||||
return _buffer.buffer.asUint8List(_indirect, length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Can be used with an [int] or a [String] value for key.
|
||||
/// If the underlying value in FlexBuffer is a vector, then use [int] for access.
|
||||
/// If the underlying value in FlexBuffer is a map, then use [String] for access.
|
||||
/// Returns [Reference] value. Throws an exception when [key] is not applicable.
|
||||
Reference operator [](Object key) {
|
||||
if (key is int && ValueTypeUtils.isAVector(_valueType)) {
|
||||
final index = key;
|
||||
if(index >= length || index < 0) {
|
||||
throw ArgumentError('Key: [$key] is not applicable on: $_path of: $_valueType length: $length');
|
||||
}
|
||||
final elementOffset = _indirect + index * _byteWidth;
|
||||
final reference = Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), 0, "$_path[$index]");
|
||||
reference._byteWidth = 1;
|
||||
if (ValueTypeUtils.isTypedVector(_valueType)) {
|
||||
reference._valueType = ValueTypeUtils.typedVectorElementType(_valueType);
|
||||
return reference;
|
||||
}
|
||||
if(ValueTypeUtils.isFixedTypedVector(_valueType)) {
|
||||
reference._valueType = ValueTypeUtils.fixedTypedVectorElementType(_valueType);
|
||||
return reference;
|
||||
}
|
||||
final packedType = _buffer.getUint8(_indirect + length * _byteWidth + index);
|
||||
return Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path[$index]");
|
||||
}
|
||||
if (key is String && _valueType == ValueType.Map) {
|
||||
final index = _keyIndex(key);
|
||||
if (index != null) {
|
||||
return _valueForIndexWithKey(index, key);
|
||||
}
|
||||
}
|
||||
throw ArgumentError('Key: [$key] is not applicable on: $_path of: $_valueType');
|
||||
}
|
||||
|
||||
/// Get an iterable if the underlying flexBuffer value is a vector.
|
||||
/// Otherwise throws an exception.
|
||||
Iterable<Reference> get vectorIterable {
|
||||
if(isVector == false) {
|
||||
throw UnsupportedError('Value is not a vector. It is: $_valueType');
|
||||
}
|
||||
return _VectorIterator(this);
|
||||
}
|
||||
|
||||
/// Get an iterable for keys if the underlying flexBuffer value is a map.
|
||||
/// Otherwise throws an exception.
|
||||
Iterable<String> get mapKeyIterable {
|
||||
if(isMap == false) {
|
||||
throw UnsupportedError('Value is not a map. It is: $_valueType');
|
||||
}
|
||||
return _MapKeyIterator(this);
|
||||
}
|
||||
|
||||
/// Get an iterable for values if the underlying flexBuffer value is a map.
|
||||
/// Otherwise throws an exception.
|
||||
Iterable<Reference> get mapValueIterable {
|
||||
if(isMap == false) {
|
||||
throw UnsupportedError('Value is not a map. It is: $_valueType');
|
||||
}
|
||||
return _MapValueIterator(this);
|
||||
}
|
||||
|
||||
/// Returns the length of the the underlying FlexBuffer value.
|
||||
/// If the underlying value is [null] the length is 0.
|
||||
/// If the underlying value is a number, or a bool, the length is 1.
|
||||
/// If the underlying value is a vector, or map, the length reflects number of elements / element pairs.
|
||||
/// If the values is a string or a blob, the length reflects a number of bytes the value occupies (strings are encoded in utf8 format).
|
||||
int get length {
|
||||
if (_length != null) {
|
||||
return _length;
|
||||
}
|
||||
// needs to be checked before more generic isAVector
|
||||
if(ValueTypeUtils.isFixedTypedVector(_valueType)) {
|
||||
_length = ValueTypeUtils.fixedTypedVectorElementSize(_valueType);
|
||||
} else if(_valueType == ValueType.Blob || ValueTypeUtils.isAVector(_valueType) || _valueType == ValueType.Map){
|
||||
_length = _readUInt(_indirect - _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
} else if (_valueType == ValueType.Null) {
|
||||
_length = 0;
|
||||
} else if (_valueType == ValueType.String) {
|
||||
final indirect = _indirect;
|
||||
var size_byte_width = _byteWidth;
|
||||
var size = _readUInt(indirect - size_byte_width, BitWidthUtil.fromByteWidth(size_byte_width));
|
||||
while (_buffer.getInt8(indirect + size) != 0) {
|
||||
size_byte_width <<= 1;
|
||||
size = _readUInt(indirect - size_byte_width, BitWidthUtil.fromByteWidth(size_byte_width));
|
||||
}
|
||||
_length = size;
|
||||
} else if (_valueType == ValueType.Key) {
|
||||
final indirect = _indirect;
|
||||
var size = 1;
|
||||
while (_buffer.getInt8(indirect + size) != 0) {
|
||||
size += 1;
|
||||
}
|
||||
_length = size;
|
||||
} else {
|
||||
_length = 1;
|
||||
}
|
||||
return _length;
|
||||
}
|
||||
|
||||
|
||||
/// Returns a minified JSON representation of the underlying FlexBuffer value.
|
||||
///
|
||||
/// This method involves materializing the entire object tree, which may be
|
||||
/// expensive. It is more efficient to work with [Reference] and access only the needed data.
|
||||
/// Blob values are represented as base64 encoded string.
|
||||
String get json {
|
||||
if(_valueType == ValueType.Bool) {
|
||||
return boolValue ? 'true' : 'false';
|
||||
}
|
||||
if (_valueType == ValueType.Null) {
|
||||
return 'null';
|
||||
}
|
||||
if(ValueTypeUtils.isNumber(_valueType)) {
|
||||
return jsonEncode(numValue);
|
||||
}
|
||||
if (_valueType == ValueType.String) {
|
||||
return jsonEncode(stringValue);
|
||||
}
|
||||
if (_valueType == ValueType.Blob) {
|
||||
return jsonEncode(base64Encode(blobValue));
|
||||
}
|
||||
if (ValueTypeUtils.isAVector(_valueType)) {
|
||||
final result = StringBuffer();
|
||||
result.write('[');
|
||||
for (var i = 0; i < length; i++) {
|
||||
result.write(this[i].json);
|
||||
if (i < length - 1) {
|
||||
result.write(',');
|
||||
}
|
||||
}
|
||||
result.write(']');
|
||||
return result.toString();
|
||||
}
|
||||
if (_valueType == ValueType.Map) {
|
||||
final result = StringBuffer();
|
||||
result.write('{');
|
||||
for (var i = 0; i < length; i++) {
|
||||
result.write(jsonEncode(_keyForIndex(i)));
|
||||
result.write(':');
|
||||
result.write(_valueForIndex(i).json);
|
||||
if (i < length - 1) {
|
||||
result.write(',');
|
||||
}
|
||||
}
|
||||
result.write('}');
|
||||
return result.toString();
|
||||
}
|
||||
throw UnsupportedError('Type: $_valueType is not supported for JSON conversion');
|
||||
}
|
||||
|
||||
/// Computes the indirect offset of the value.
|
||||
///
|
||||
/// To optimize for the more common case of being called only once, this
|
||||
/// value is not cached. Callers that need to use it more than once should
|
||||
/// cache the return value in a local variable.
|
||||
int get _indirect {
|
||||
final step = _readUInt(_offset, _parentWidth);
|
||||
return _offset - step;
|
||||
}
|
||||
|
||||
int _readInt(int offset, BitWidth width) {
|
||||
_validateOffset(offset, width);
|
||||
if (width == BitWidth.width8) {
|
||||
return _buffer.getInt8(offset);
|
||||
}
|
||||
if (width == BitWidth.width16) {
|
||||
return _buffer.getInt16(offset, Endian.little);
|
||||
}
|
||||
if (width == BitWidth.width32) {
|
||||
return _buffer.getInt32(offset, Endian.little);
|
||||
}
|
||||
return _buffer.getInt64(offset, Endian.little);
|
||||
}
|
||||
|
||||
int _readUInt(int offset, BitWidth width) {
|
||||
_validateOffset(offset, width);
|
||||
if (width == BitWidth.width8) {
|
||||
return _buffer.getUint8(offset);
|
||||
}
|
||||
if (width == BitWidth.width16) {
|
||||
return _buffer.getUint16(offset, Endian.little);
|
||||
}
|
||||
if (width == BitWidth.width32) {
|
||||
return _buffer.getUint32(offset, Endian.little);
|
||||
}
|
||||
return _buffer.getUint64(offset, Endian.little);
|
||||
}
|
||||
|
||||
double _readFloat(int offset, BitWidth width) {
|
||||
_validateOffset(offset, width);
|
||||
if (width.index < BitWidth.width32.index) {
|
||||
throw StateError('Bad width: $width');
|
||||
}
|
||||
|
||||
if (width == BitWidth.width32) {
|
||||
return _buffer.getFloat32(offset, Endian.little);
|
||||
}
|
||||
|
||||
return _buffer.getFloat64(offset, Endian.little);
|
||||
}
|
||||
|
||||
void _validateOffset(int offset, BitWidth width) {
|
||||
if (_offset < 0 || _buffer.lengthInBytes <= offset + width.index || offset & (BitWidthUtil.toByteWidth(width) - 1) != 0) {
|
||||
throw StateError('Bad offset: $offset, width: $width');
|
||||
}
|
||||
}
|
||||
|
||||
int _keyIndex(String key) {
|
||||
final input = utf8.encode(key);
|
||||
final keysVectorOffset = _indirect - _byteWidth * 3;
|
||||
final indirectOffset = keysVectorOffset - _readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
final byteWidth = _readUInt(keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
var low = 0;
|
||||
var high = length - 1;
|
||||
while (low <= high) {
|
||||
final mid = (high + low) >> 1;
|
||||
final dif = _diffKeys(input, mid, indirectOffset, byteWidth);
|
||||
if (dif == 0) return mid;
|
||||
if (dif < 0) {
|
||||
high = mid - 1;
|
||||
} else {
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
int _diffKeys(List<int> input, int index, int indirect_offset, int byteWidth) {
|
||||
final keyOffset = indirect_offset + index * byteWidth;
|
||||
final keyIndirectOffset = keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth));
|
||||
for (var i = 0; i < input.length; i++) {
|
||||
final dif = input[i] - _buffer.getUint8(keyIndirectOffset + i);
|
||||
if (dif != 0) {
|
||||
return dif;
|
||||
}
|
||||
}
|
||||
return (_buffer.getUint8(keyIndirectOffset + input.length) == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
Reference _valueForIndexWithKey(int index, String key) {
|
||||
final indirect = _indirect;
|
||||
final elementOffset = indirect + index * _byteWidth;
|
||||
final packedType = _buffer.getUint8(indirect + length * _byteWidth + index);
|
||||
return Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/$key");
|
||||
}
|
||||
|
||||
Reference _valueForIndex(int index) {
|
||||
final indirect = _indirect;
|
||||
final elementOffset = indirect + index * _byteWidth;
|
||||
final packedType = _buffer.getUint8(indirect + length * _byteWidth + index);
|
||||
return Reference._(_buffer, elementOffset, BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/[$index]");
|
||||
}
|
||||
|
||||
String _keyForIndex(int index) {
|
||||
final keysVectorOffset = _indirect - _byteWidth * 3;
|
||||
final indirectOffset = keysVectorOffset - _readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
final byteWidth = _readUInt(keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
|
||||
final keyOffset = indirectOffset + index * byteWidth;
|
||||
final keyIndirectOffset = keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth));
|
||||
var length = 0;
|
||||
while (_buffer.getUint8(keyIndirectOffset + length) != 0) {
|
||||
length += 1;
|
||||
}
|
||||
return utf8.decode(_buffer.buffer.asUint8List(keyIndirectOffset, length));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class _VectorIterator with IterableMixin<Reference> implements Iterator<Reference> {
|
||||
final Reference _vector;
|
||||
int index;
|
||||
|
||||
_VectorIterator(this._vector) {
|
||||
index = -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Reference get current => _vector[index];
|
||||
|
||||
@override
|
||||
bool moveNext() {
|
||||
index++;
|
||||
return index < _vector.length;
|
||||
}
|
||||
|
||||
@override
|
||||
Iterator<Reference> get iterator => this;
|
||||
}
|
||||
|
||||
class _MapKeyIterator with IterableMixin<String> implements Iterator<String> {
|
||||
final Reference _map;
|
||||
int index;
|
||||
|
||||
_MapKeyIterator(this._map) {
|
||||
index = -1;
|
||||
}
|
||||
|
||||
@override
|
||||
String get current => _map._keyForIndex(index);
|
||||
|
||||
@override
|
||||
bool moveNext() {
|
||||
index++;
|
||||
return index < _map.length;
|
||||
}
|
||||
|
||||
@override
|
||||
Iterator<String> get iterator => this;
|
||||
}
|
||||
|
||||
class _MapValueIterator with IterableMixin<Reference> implements Iterator<Reference> {
|
||||
final Reference _map;
|
||||
int index;
|
||||
|
||||
_MapValueIterator(this._map) {
|
||||
index = -1;
|
||||
}
|
||||
|
||||
@override
|
||||
Reference get current => _map._valueForIndex(index);
|
||||
|
||||
@override
|
||||
bool moveNext() {
|
||||
index++;
|
||||
return index < _map.length;
|
||||
}
|
||||
|
||||
@override
|
||||
Iterator<Reference> get iterator => this;
|
||||
}
|
||||
166
dart/lib/src/types.dart
Normal file
@@ -0,0 +1,166 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
/// Represents the number of bits a value occupies.
|
||||
enum BitWidth {
|
||||
width8,
|
||||
width16,
|
||||
width32,
|
||||
width64
|
||||
}
|
||||
|
||||
class BitWidthUtil {
|
||||
static int toByteWidth(BitWidth self) {
|
||||
return 1 << self.index;
|
||||
}
|
||||
static BitWidth width(num value) {
|
||||
if (value.toInt() == value) {
|
||||
var v = value.toInt().abs();
|
||||
if (v >> 7 == 0) return BitWidth.width8;
|
||||
if (v >> 15 == 0) return BitWidth.width16;
|
||||
if (v >> 31 == 0) return BitWidth.width32;
|
||||
return BitWidth.width64;
|
||||
}
|
||||
return value == _toF32(value) ? BitWidth.width32 : BitWidth.width64;
|
||||
}
|
||||
static BitWidth uwidth(num value) {
|
||||
if (value.toInt() == value) {
|
||||
var v = value.toInt().abs();
|
||||
if (v >> 8 == 0) return BitWidth.width8;
|
||||
if (v >> 16 == 0) return BitWidth.width16;
|
||||
if (v >> 32 == 0) return BitWidth.width32;
|
||||
return BitWidth.width64;
|
||||
}
|
||||
return value == _toF32(value) ? BitWidth.width32 : BitWidth.width64;
|
||||
}
|
||||
static BitWidth fromByteWidth(int value) {
|
||||
if (value == 1) {
|
||||
return BitWidth.width8;
|
||||
}
|
||||
if (value == 2) {
|
||||
return BitWidth.width16;
|
||||
}
|
||||
if (value == 4) {
|
||||
return BitWidth.width32;
|
||||
}
|
||||
if (value == 8) {
|
||||
return BitWidth.width64;
|
||||
}
|
||||
throw Exception('Unexpected value ${value}');
|
||||
}
|
||||
static int paddingSize(int bufSize, int scalarSize) {
|
||||
return (~bufSize + 1) & (scalarSize - 1);
|
||||
}
|
||||
static double _toF32(double value) {
|
||||
var bdata = ByteData(4);
|
||||
bdata.setFloat32(0, value);
|
||||
return bdata.getFloat32(0);
|
||||
}
|
||||
|
||||
static BitWidth max(BitWidth self, BitWidth other) {
|
||||
if (self.index < other.index) {
|
||||
return other;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents all internal FlexBuffer types.
|
||||
enum ValueType {
|
||||
Null, Int, UInt, Float,
|
||||
Key, String, IndirectInt, IndirectUInt, IndirectFloat,
|
||||
Map, Vector, VectorInt, VectorUInt, VectorFloat, VectorKey,
|
||||
@Deprecated('VectorString is deprecated due to a flaw in the binary format (https://github.com/google/flatbuffers/issues/5627)')
|
||||
VectorString,
|
||||
VectorInt2, VectorUInt2, VectorFloat2,
|
||||
VectorInt3, VectorUInt3, VectorFloat3,
|
||||
VectorInt4, VectorUInt4, VectorFloat4,
|
||||
Blob, Bool, VectorBool
|
||||
}
|
||||
|
||||
class ValueTypeUtils {
|
||||
static int toInt(ValueType self) {
|
||||
if (self == ValueType.VectorBool) return 36;
|
||||
return self.index;
|
||||
}
|
||||
|
||||
static ValueType fromInt(int value) {
|
||||
if (value == 36) return ValueType.VectorBool;
|
||||
return ValueType.values[value];
|
||||
}
|
||||
|
||||
static bool isInline(ValueType self) {
|
||||
return self == ValueType.Bool
|
||||
|| toInt(self) <= toInt(ValueType.Float);
|
||||
}
|
||||
|
||||
static bool isNumber(ValueType self) {
|
||||
return toInt(self) >= toInt(ValueType.Int)
|
||||
&& toInt(self) <= toInt(ValueType.Float);
|
||||
}
|
||||
|
||||
static bool isIndirectNumber(ValueType self) {
|
||||
return toInt(self) >= toInt(ValueType.IndirectInt)
|
||||
&& toInt(self) <= toInt(ValueType.IndirectFloat);
|
||||
}
|
||||
|
||||
static bool isTypedVectorElement(ValueType self) {
|
||||
return self == ValueType.Bool ||
|
||||
(
|
||||
toInt(self) >= toInt(ValueType.Int)
|
||||
&& toInt(self) <= toInt(ValueType.String)
|
||||
);
|
||||
}
|
||||
|
||||
static bool isTypedVector(ValueType self) {
|
||||
return self == ValueType.VectorBool ||
|
||||
(
|
||||
toInt(self) >= toInt(ValueType.VectorInt)
|
||||
&& toInt(self) <= toInt(ValueType.VectorString)
|
||||
);
|
||||
}
|
||||
|
||||
static bool isFixedTypedVector(ValueType self) {
|
||||
return (
|
||||
toInt(self) >= toInt(ValueType.VectorInt2)
|
||||
&& toInt(self) <= toInt(ValueType.VectorFloat4)
|
||||
);
|
||||
}
|
||||
|
||||
static bool isAVector(ValueType self) {
|
||||
return (
|
||||
isTypedVector(self) || isFixedTypedVector(self) || self == ValueType.Vector
|
||||
);
|
||||
}
|
||||
|
||||
static ValueType toTypedVector(ValueType self, int length) {
|
||||
if (length == 0) {
|
||||
return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt));
|
||||
}
|
||||
if (length == 2) {
|
||||
return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt2));
|
||||
}
|
||||
if (length == 3) {
|
||||
return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt3));
|
||||
}
|
||||
if (length == 4) {
|
||||
return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt4));
|
||||
}
|
||||
throw Exception('unexpected length ' + length.toString());
|
||||
}
|
||||
|
||||
static ValueType typedVectorElementType(ValueType self) {
|
||||
return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.VectorInt) + toInt(ValueType.Int));
|
||||
}
|
||||
|
||||
static ValueType fixedTypedVectorElementType(ValueType self) {
|
||||
return ValueTypeUtils.fromInt((toInt(self) - toInt(ValueType.VectorInt2)) % 3 + toInt(ValueType.Int));
|
||||
}
|
||||
|
||||
static int fixedTypedVectorElementSize(ValueType self) {
|
||||
return (toInt(self) - toInt(ValueType.VectorInt2)) ~/ 3 + 2;
|
||||
}
|
||||
|
||||
static int packedType(ValueType self, BitWidth bitWidth) {
|
||||
return bitWidth.index | (toInt(self) << 2);
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,25 @@
|
||||
# Note to pub consumers: this file is used to assist with publishing the
|
||||
# pub package from the flatbuffers repository and is not meant for general use.
|
||||
# As pub does not currently provide a way to exclude files, it is included here.
|
||||
set -e
|
||||
|
||||
command -v pub >/dev/null 2>&1 || { echo >&2 "Require `pub` but it's not installed. Aborting."; exit 1; }
|
||||
command -v dart >/dev/null 2>&1 || { echo >&2 "Require `dart` but it's not installed. Aborting."; exit 1; }
|
||||
|
||||
cp ../samples/monster.fbs example/
|
||||
cp ../tests/monster_test.fbs test/
|
||||
pub publish
|
||||
cp -r ../tests/include_test/*.fbs test/
|
||||
cp -r ../tests/include_test/sub test/
|
||||
|
||||
pushd example
|
||||
../../flatc --dart ./monster.fbs
|
||||
popd
|
||||
|
||||
pushd test
|
||||
../../flatc --dart ./monster_test.fbs
|
||||
popd
|
||||
|
||||
dart pub publish
|
||||
|
||||
rm example/monster.fbs
|
||||
rm test/monster_test.fbs
|
||||
rm test/*.fbs
|
||||
rm -rf test/sub
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: flat_buffers
|
||||
version: 1.11.0
|
||||
version: 2.0.0
|
||||
description: >
|
||||
FlatBuffers reading and writing library for Dart. Use the flatc compiler to
|
||||
generate Dart classes for a FlatBuffers schema, and this library to assist with
|
||||
|
||||
@@ -17,6 +17,7 @@ main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(BuilderTest);
|
||||
defineReflectiveTests(CheckOtherLangaugesData);
|
||||
defineReflectiveTests(GeneratorTest);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -30,8 +31,7 @@ class CheckOtherLangaugesData {
|
||||
List<int> data = await new io.File(path.join(
|
||||
path.dirname(io.Platform.script.path),
|
||||
'monsterdata_test.mon',
|
||||
))
|
||||
.readAsBytes();
|
||||
)).readAsBytes();
|
||||
example.Monster mon = new example.Monster(data);
|
||||
expect(mon.hp, 80);
|
||||
expect(mon.mana, 150);
|
||||
@@ -58,8 +58,74 @@ class CheckOtherLangaugesData {
|
||||
expect(mon.testarrayofstring[1], "test2");
|
||||
|
||||
// this will fail if accessing any field fails.
|
||||
expect(mon.toString(),
|
||||
'Monster{pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color{value: 2}, test3: Test{a: 5, b: 6}}, mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], color: Color{value: 8}, testType: AnyTypeId{value: 1}, test: Monster{pos: null, mana: 150, hp: 100, name: Fred, inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, test: null, test4: null, testarrayofstring: null, testarrayoftables: null, enemy: null, testnestedflatbuffer: null, testempty: null, testbool: false, testhashs32Fnv1: 0, testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, testf2: 3.0, testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, flex: null, test5: null, vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, vectorOfReferrables: null, singleWeakReference: 0, vectorOfWeakReferences: null, vectorOfStrongReferrables: null, coOwningReference: 0, vectorOfCoOwningReferences: null, nonOwningReference: 0, vectorOfNonOwningReferences: null}, test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], testarrayofstring: [test1, test2], testarrayoftables: null, enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, test: null, test4: null, testarrayofstring: null, testarrayoftables: null, enemy: null, testnestedflatbuffer: null, testempty: null, testbool: false, testhashs32Fnv1: 0, testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, testf2: 3.0, testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, flex: null, test5: null, vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, vectorOfReferrables: null, singleWeakReference: 0, vectorOfWeakReferences: null, vectorOfStrongReferrables: null, coOwningReference: 0, vectorOfCoOwningReferences: null, nonOwningReference: 0, vectorOfNonOwningReferences: null}, testnestedflatbuffer: null, testempty: null, testbool: false, testhashs32Fnv1: -579221183, testhashu32Fnv1: 3715746113, testhashs64Fnv1: 7930699090847568257, testhashu64Fnv1: 7930699090847568257, testhashs32Fnv1a: -1904106383, testhashu32Fnv1a: 2390860913, testhashs64Fnv1a: 4898026182817603057, testhashu64Fnv1a: 4898026182817603057, testarrayofbools: [true, false, true], testf: 3.14159, testf2: 3.0, testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, flex: null, test5: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], vectorOfLongs: [1, 100, 10000, 1000000, 100000000], vectorOfDoubles: [-1.7976931348623157e+308, 0.0, 1.7976931348623157e+308], parentNamespaceTest: null, vectorOfReferrables: null, singleWeakReference: 0, vectorOfWeakReferences: null, vectorOfStrongReferrables: null, coOwningReference: 0, vectorOfCoOwningReferences: null, nonOwningReference: 0, vectorOfNonOwningReferences: null}');
|
||||
expect(
|
||||
mon.toString(),
|
||||
'Monster{'
|
||||
'pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color{value: 2}, test3: Test{a: 5, b: 6}}, '
|
||||
'mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], '
|
||||
'color: Color{value: 8}, testType: AnyTypeId{value: 1}, '
|
||||
'test: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
|
||||
'inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, '
|
||||
'test: null, test4: null, testarrayofstring: null, '
|
||||
'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, '
|
||||
'testempty: null, testbool: false, testhashs32Fnv1: 0, '
|
||||
'testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, '
|
||||
'testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, '
|
||||
'testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, '
|
||||
'testf2: 3.0, testf3: 0.0, testarrayofstring2: null, '
|
||||
'testarrayofsortedstruct: null, flex: null, test5: null, '
|
||||
'vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, '
|
||||
'vectorOfReferrables: null, singleWeakReference: 0, '
|
||||
'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, '
|
||||
'coOwningReference: 0, vectorOfCoOwningReferences: null, '
|
||||
'nonOwningReference: 0, vectorOfNonOwningReferences: null, '
|
||||
'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, '
|
||||
'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, '
|
||||
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
|
||||
'testrequirednestedflatbuffer: null}, '
|
||||
'test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], '
|
||||
'testarrayofstring: [test1, test2], testarrayoftables: null, '
|
||||
'enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
|
||||
'inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, '
|
||||
'test: null, test4: null, testarrayofstring: null, '
|
||||
'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, '
|
||||
'testempty: null, testbool: false, testhashs32Fnv1: 0, '
|
||||
'testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, '
|
||||
'testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, '
|
||||
'testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, '
|
||||
'testf2: 3.0, testf3: 0.0, testarrayofstring2: null, '
|
||||
'testarrayofsortedstruct: null, flex: null, test5: null, '
|
||||
'vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, '
|
||||
'vectorOfReferrables: null, singleWeakReference: 0, '
|
||||
'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, '
|
||||
'coOwningReference: 0, vectorOfCoOwningReferences: null, '
|
||||
'nonOwningReference: 0, vectorOfNonOwningReferences: null, '
|
||||
'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, '
|
||||
'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, '
|
||||
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
|
||||
'testrequirednestedflatbuffer: null}, '
|
||||
'testnestedflatbuffer: null, testempty: null, testbool: true, '
|
||||
'testhashs32Fnv1: -579221183, testhashu32Fnv1: 3715746113, '
|
||||
'testhashs64Fnv1: 7930699090847568257, '
|
||||
'testhashu64Fnv1: 7930699090847568257, '
|
||||
'testhashs32Fnv1a: -1904106383, testhashu32Fnv1a: 2390860913, '
|
||||
'testhashs64Fnv1a: 4898026182817603057, '
|
||||
'testhashu64Fnv1a: 4898026182817603057, '
|
||||
'testarrayofbools: [true, false, true], testf: 3.14159, testf2: 3.0, '
|
||||
'testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, '
|
||||
'flex: null, test5: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], '
|
||||
'vectorOfLongs: [1, 100, 10000, 1000000, 100000000], '
|
||||
'vectorOfDoubles: [-1.7976931348623157e+308, 0.0, 1.7976931348623157e+308], '
|
||||
'parentNamespaceTest: null, vectorOfReferrables: null, '
|
||||
'singleWeakReference: 0, vectorOfWeakReferences: null, '
|
||||
'vectorOfStrongReferrables: null, coOwningReference: 0, '
|
||||
'vectorOfCoOwningReferences: null, nonOwningReference: 0, '
|
||||
'vectorOfNonOwningReferences: null, '
|
||||
'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, '
|
||||
'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, '
|
||||
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
|
||||
'testrequirednestedflatbuffer: null}',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +154,6 @@ class BuilderTest {
|
||||
testBuilder.finish(30, 40);
|
||||
final test4 = fbBuilder.endStructVector(2);
|
||||
|
||||
|
||||
monBuilder
|
||||
..begin()
|
||||
..addPos(
|
||||
@@ -167,8 +232,8 @@ class BuilderTest {
|
||||
expect(byteData.getUint8(6), 126); // '~'
|
||||
expect(byteData.getUint8(7), 255); // 'ÿ'
|
||||
// First 4 bytes of the table data are a backwards offset to the vtable.
|
||||
int vTableLoc = tableDataLoc -
|
||||
byteData.getInt32(tableDataLoc, Endian.little);
|
||||
int vTableLoc =
|
||||
tableDataLoc - byteData.getInt32(tableDataLoc, Endian.little);
|
||||
// First 2 bytes of the vtable are the size of the vtable in bytes, which
|
||||
// should be 4.
|
||||
expect(byteData.getUint16(vTableLoc, Endian.little), 4);
|
||||
@@ -200,6 +265,7 @@ class BuilderTest {
|
||||
builder.addInt32(1, 20, 10);
|
||||
int offset = builder.endTable();
|
||||
byteList = builder.finish(offset);
|
||||
expect(builder.size(), byteList.length);
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buffer = new BufferContext.fromBytes(byteList);
|
||||
@@ -231,8 +297,8 @@ class BuilderTest {
|
||||
// First 4 bytes are an offset to the table data.
|
||||
int tableDataLoc = byteData.getUint32(0, Endian.little);
|
||||
// First 4 bytes of the table data are a backwards offset to the vtable.
|
||||
int vTableLoc = tableDataLoc -
|
||||
byteData.getInt32(tableDataLoc, Endian.little);
|
||||
int vTableLoc =
|
||||
tableDataLoc - byteData.getInt32(tableDataLoc, Endian.little);
|
||||
// First 2 bytes of the vtable are the size of the vtable in bytes, which
|
||||
// should be 10.
|
||||
expect(byteData.getUint16(vTableLoc, Endian.little), 10);
|
||||
@@ -242,10 +308,9 @@ class BuilderTest {
|
||||
// Remaining 6 bytes are the offsets within the object where the ints are
|
||||
// located.
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int offset =
|
||||
byteData.getUint16(vTableLoc + 4 + 2 * i, Endian.little);
|
||||
expect(byteData.getInt32(tableDataLoc + offset, Endian.little),
|
||||
10 + 10 * i);
|
||||
int offset = byteData.getUint16(vTableLoc + 4 + 2 * i, Endian.little);
|
||||
expect(
|
||||
byteData.getInt32(tableDataLoc + offset, Endian.little), 10 + 10 * i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -571,3 +636,14 @@ class TestPointReader extends TableReader<TestPointImpl> {
|
||||
return new TestPointImpl(object, offset);
|
||||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class GeneratorTest {
|
||||
void test_constantEnumValues() async {
|
||||
expect(example.Color.values, same(example.Color.values));
|
||||
expect(example.Race.values, same(example.Race.values));
|
||||
expect(example.AnyTypeId.values, same(example.AnyTypeId.values));
|
||||
expect(example.AnyUniqueAliasesTypeId.values, same(example.AnyUniqueAliasesTypeId.values));
|
||||
expect(example.AnyAmbiguousAliasesTypeId.values, same(example.AnyAmbiguousAliasesTypeId.values));
|
||||
}
|
||||
}
|
||||
|
||||
315
dart/test/flex_builder_test.dart
Normal file
@@ -0,0 +1,315 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flat_buffers/flex_buffers.dart' show Builder;
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('build with single value', () {
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addNull();
|
||||
expect(flx.finish(), [0, 0, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addBool(true);
|
||||
expect(flx.finish(), [1, 104, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addBool(false);
|
||||
expect(flx.finish(), [0, 104, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addInt(1);
|
||||
expect(flx.finish(), [1, 4, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addInt(230);
|
||||
expect(flx.finish(), [230, 0, 5, 2]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addInt(1025);
|
||||
expect(flx.finish(), [1, 4, 5, 2]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addInt(-1025);
|
||||
expect(flx.finish(), [255, 251, 5, 2]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addDouble(0.1);
|
||||
expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 15, 8]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addDouble(0.5);
|
||||
expect(flx.finish(), [0, 0, 0, 63, 14, 4]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addString('Maxim');
|
||||
expect(flx.finish(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder();
|
||||
flx.addString('hello 😱');
|
||||
expect(flx.finish(), [10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1]);
|
||||
}
|
||||
});
|
||||
|
||||
test('build vector', (){
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addInt(1)
|
||||
..addInt(2)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [1, 2, 2, 64, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addInt(-1)
|
||||
..addInt(256)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [255, 255, 0, 1, 4, 65, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addInt(-45)
|
||||
..addInt(256000)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addDouble(1.1)
|
||||
..addDouble(-256)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [154, 153, 153, 153, 153, 153, 241, 63, 0, 0, 0, 0, 0, 0, 112, 192, 16, 75, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addInt(1)
|
||||
..addInt(2)
|
||||
..addInt(4)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [1, 2, 4, 3, 76, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addInt(-1)
|
||||
..addInt(256)
|
||||
..addInt(4)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [255, 255, 0, 1, 4, 0, 6, 77, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..startVector()
|
||||
..addInt(61)
|
||||
..end()
|
||||
..addInt(64)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [1, 61, 2, 2, 64, 44, 4, 4, 40, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addString('foo')
|
||||
..addString('bar')
|
||||
..addString('baz')
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [3, 102, 111, 111, 0, 3, 98, 97, 114, 0, 3, 98, 97, 122, 0, 3, 15, 11, 7, 3, 60, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addString('foo')
|
||||
..addString('bar')
|
||||
..addString('baz')
|
||||
..addString('foo')
|
||||
..addString('bar')
|
||||
..addString('baz')
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [3, 102, 111, 111, 0, 3, 98, 97, 114, 0, 3, 98, 97, 122, 0, 6, 15, 11, 7, 18, 14, 10, 6, 60, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addBool(true)
|
||||
..addBool(false)
|
||||
..addBool(true)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [3, 1, 0, 1, 3, 144, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addString('foo')
|
||||
..addInt(1)
|
||||
..addInt(-5)
|
||||
..addDouble(1.3)
|
||||
..addBool(true)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [
|
||||
3, 102, 111, 111, 0, 0, 0, 0,
|
||||
5, 0, 0, 0, 0, 0, 0, 0,
|
||||
15, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
251, 255, 255, 255, 255, 255, 255, 255,
|
||||
205, 204, 204, 204, 204, 204, 244, 63,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
20, 4, 4, 15, 104, 45, 43, 1]);
|
||||
}
|
||||
});
|
||||
|
||||
test('build map', ()
|
||||
{
|
||||
{
|
||||
var flx = Builder()
|
||||
..startMap()
|
||||
..addKey('a')
|
||||
..addInt(12)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startMap()
|
||||
..addKey('a')
|
||||
..addInt(12)
|
||||
..addKey('')
|
||||
..addInt(45)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [97, 0, 0, 2, 2, 5, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]);
|
||||
}
|
||||
{
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..startMap()
|
||||
..addKey('something')
|
||||
..addInt(12)
|
||||
..end()
|
||||
..startMap()
|
||||
..addKey('something')
|
||||
..addInt(45)
|
||||
..end()
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [115, 111, 109, 101, 116, 104, 105, 110, 103, 0,
|
||||
1, 11, 1, 1, 1, 12, 4, 6, 1, 1, 45, 4, 2, 8, 4, 36, 36, 4, 40, 1]);
|
||||
}
|
||||
});
|
||||
|
||||
test('build blob', ()
|
||||
{
|
||||
{
|
||||
var flx = Builder()
|
||||
..addBlob(Uint8List.fromList([1, 2, 3]).buffer)
|
||||
;
|
||||
expect(flx.finish(), [3, 1, 2, 3, 3, 100, 1]);
|
||||
}
|
||||
});
|
||||
|
||||
test('build from object', (){
|
||||
expect(Builder.buildFromObject(Uint8List.fromList([1, 2, 3]).buffer).asUint8List(), [3, 1, 2, 3, 3, 100, 1]);
|
||||
expect(Builder.buildFromObject(null).asUint8List(), [0, 0, 1]);
|
||||
expect(Builder.buildFromObject(true).asUint8List(), [1, 104, 1]);
|
||||
expect(Builder.buildFromObject(false).asUint8List(), [0, 104, 1]);
|
||||
expect(Builder.buildFromObject(25).asUint8List(), [25, 4, 1]);
|
||||
expect(Builder.buildFromObject(-250).asUint8List(), [6, 255, 5, 2]);
|
||||
expect(Builder.buildFromObject(-2.50).asUint8List(), [0, 0, 32, 192, 14, 4]);
|
||||
expect(Builder.buildFromObject('Maxim').asUint8List(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
|
||||
expect(Builder.buildFromObject([1, 3.3, 'max', true, null, false]).asUint8List(), [
|
||||
3, 109, 97, 120, 0, 0, 0, 0,
|
||||
6, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
102, 102, 102, 102, 102, 102, 10, 64,
|
||||
31, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
4, 15, 20, 104, 0, 104, 54, 43, 1
|
||||
]);
|
||||
expect(Builder.buildFromObject([{'something':12}, {'something': 45}]).asUint8List(), [
|
||||
115, 111, 109, 101, 116, 104, 105, 110, 103, 0,
|
||||
1, 11, 1, 1, 1, 12, 4, 6, 1, 1, 45, 4, 2, 8, 4, 36, 36, 4, 40, 1
|
||||
]);
|
||||
});
|
||||
|
||||
test('add double indirectly', (){
|
||||
var flx = Builder()
|
||||
..addDoubleIndirectly(0.1)
|
||||
;
|
||||
expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 8, 35, 1]);
|
||||
});
|
||||
|
||||
test('add double indirectly to vector with cache', (){
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addDoubleIndirectly(0.1, cache: true)
|
||||
..addDoubleIndirectly(0.1, cache: true)
|
||||
..addDoubleIndirectly(0.1, cache: true)
|
||||
..addDoubleIndirectly(0.1, cache: true)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63,
|
||||
4, 9, 10, 11, 12, 35, 35, 35, 35, 8, 40, 1]);
|
||||
});
|
||||
|
||||
test('add int indirectly', (){
|
||||
var flx = Builder()
|
||||
..addIntIndirectly(2345234523452345)
|
||||
;
|
||||
expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0, 8, 27, 1]);
|
||||
});
|
||||
|
||||
test('add int indirectly to vector with cache', (){
|
||||
var flx = Builder()
|
||||
..startVector()
|
||||
..addIntIndirectly(2345234523452345, cache: true)
|
||||
..addIntIndirectly(2345234523452345, cache: true)
|
||||
..addIntIndirectly(2345234523452345, cache: true)
|
||||
..addIntIndirectly(2345234523452345, cache: true)
|
||||
..end()
|
||||
;
|
||||
expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0,
|
||||
4, 9, 10, 11, 12, 27, 27, 27, 27, 8, 40, 1]);
|
||||
});
|
||||
|
||||
test('snapshot', (){
|
||||
var flx = Builder();
|
||||
flx.startVector();
|
||||
flx.addInt(12);
|
||||
expect(flx.snapshot().asUint8List(), [1, 12, 1, 44, 1]);
|
||||
flx.addInt(24);
|
||||
expect(flx.snapshot().asUint8List(), [12, 24, 2, 64, 1]);
|
||||
flx.addInt(45);
|
||||
expect(flx.snapshot().asUint8List(), [12, 24, 45, 3, 76, 1]);
|
||||
});
|
||||
}
|
||||
|
||||
334
dart/test/flex_reader_test.dart
Normal file
@@ -0,0 +1,334 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flat_buffers/flex_buffers.dart' show Reference, Builder;
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('is null', () {
|
||||
expect(Reference.fromBuffer(b([0, 0, 1])).isNull, isTrue);
|
||||
});
|
||||
|
||||
test('bool value', () {
|
||||
expect(Reference.fromBuffer(b([1, 104, 1])).boolValue, isTrue);
|
||||
expect(Reference.fromBuffer(b([0, 104, 1])).boolValue, isFalse);
|
||||
});
|
||||
test('int value', () {
|
||||
expect(Reference.fromBuffer(b([25, 4, 1])).intValue, 25);
|
||||
expect(Reference.fromBuffer(b([231, 4, 1])).intValue, -25);
|
||||
expect(Reference.fromBuffer(b([230, 8, 1])).intValue, 230);
|
||||
expect(Reference.fromBuffer(b([230, 0, 5, 2])).intValue, 230);
|
||||
expect(Reference.fromBuffer(b([1, 4, 5, 2])).intValue, 1025);
|
||||
expect(Reference.fromBuffer(b([255, 251, 5, 2])).intValue, -1025);
|
||||
expect(Reference.fromBuffer(b([1, 4, 9, 2])).intValue, 1025);
|
||||
expect(Reference.fromBuffer(b([255, 255, 255, 127, 6, 4])).intValue,
|
||||
2147483647);
|
||||
expect(
|
||||
Reference.fromBuffer(b([0, 0, 0, 128, 6, 4])).intValue, -2147483648);
|
||||
expect(
|
||||
Reference.fromBuffer(b([255, 255, 255, 255, 0, 0, 0, 0, 7, 8]))
|
||||
.intValue,
|
||||
4294967295);
|
||||
expect(
|
||||
Reference.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 127, 7, 8]))
|
||||
.intValue,
|
||||
9223372036854775807);
|
||||
expect(Reference.fromBuffer(b([0, 0, 0, 0, 0, 0, 0, 128, 7, 8])).intValue,
|
||||
-9223372036854775808);
|
||||
// Dart does not really support UInt64
|
||||
// expect(FlxValue.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 255, 11, 8])).intValue, 18446744073709551615);
|
||||
});
|
||||
test('double value', () {
|
||||
expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).doubleValue, 4.5);
|
||||
expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).doubleValue,
|
||||
closeTo(.1, .001));
|
||||
expect(
|
||||
Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8]))
|
||||
.doubleValue,
|
||||
.1);
|
||||
});
|
||||
test('num value', () {
|
||||
expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).numValue, 4.5);
|
||||
expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).numValue,
|
||||
closeTo(.1, .001));
|
||||
expect(
|
||||
Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8]))
|
||||
.numValue,
|
||||
.1);
|
||||
expect(Reference.fromBuffer(b([255, 251, 5, 2])).numValue, -1025);
|
||||
});
|
||||
test('string value', () {
|
||||
expect(
|
||||
Reference.fromBuffer(b([5, 77, 97, 120, 105, 109, 0, 6, 20, 1]))
|
||||
.stringValue,
|
||||
'Maxim');
|
||||
expect(
|
||||
Reference.fromBuffer(b([
|
||||
10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1
|
||||
])).stringValue,
|
||||
'hello 😱');
|
||||
});
|
||||
test('blob value', () {
|
||||
expect(
|
||||
Reference.fromBuffer(b([3, 1, 2, 3, 3, 100, 1])).blobValue, [1, 2, 3]);
|
||||
});
|
||||
test('bool vector', () {
|
||||
var flx = Reference.fromBuffer(b([3, 1, 0, 1, 3, 144, 1]));
|
||||
expect(flx[0].boolValue, true);
|
||||
expect(flx[1].boolValue, false);
|
||||
expect(flx[2].boolValue, true);
|
||||
});
|
||||
test('number vector', () {
|
||||
testNumbers([3, 1, 2, 3, 3, 44, 1], [1, 2, 3]);
|
||||
testNumbers([3, 255, 2, 3, 3, 44, 1], [-1, 2, 3]);
|
||||
testNumbers([3, 0, 1, 0, 43, 2, 3, 0, 6, 45, 1], [1, 555, 3]);
|
||||
testNumbers(
|
||||
[3, 0, 0, 0, 1, 0, 0, 0, 204, 216, 0, 0, 3, 0, 0, 0, 12, 46, 1],
|
||||
[1, 55500, 3]);
|
||||
testNumbers([
|
||||
3, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
172, 128, 94, 239, 12, 0, 0, 0,
|
||||
3, 0, 0, 0, 0, 0, 0, 0,
|
||||
24, 47, 1
|
||||
], [1, 55555555500, 3
|
||||
]);
|
||||
testNumbers(
|
||||
[3, 0, 0, 0, 0, 0, 192, 63, 0, 0, 32, 64, 0, 0, 96, 64, 12, 54, 1],
|
||||
[1.5, 2.5, 3.5]);
|
||||
testNumbers([
|
||||
3, 0, 0, 0, 0, 0, 0, 0,
|
||||
154, 153, 153, 153, 153, 153, 241, 63,
|
||||
154, 153, 153, 153, 153, 153, 1, 64,
|
||||
102, 102, 102, 102, 102, 102, 10, 64,
|
||||
24, 55, 1
|
||||
], [1.1, 2.2, 3.3
|
||||
]);
|
||||
});
|
||||
test('number vector, fixed type', () {
|
||||
testNumbers([1, 2, 2, 64, 1], [1, 2]);
|
||||
testNumbers([255, 255, 0, 1, 4, 65, 1], [-1, 256]);
|
||||
testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1], [-45, 256000]);
|
||||
testNumbers([
|
||||
211, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 127,
|
||||
16, 67, 1
|
||||
], [
|
||||
-45, 9223372036854775807
|
||||
]);
|
||||
|
||||
testNumbers([1, 2, 2, 68, 1], [1, 2]);
|
||||
testNumbers([1, 0, 0, 1, 4, 69, 1], [1, 256]);
|
||||
testNumbers([45, 0, 0, 0, 0, 232, 3, 0, 8, 70, 1], [45, 256000]);
|
||||
|
||||
testNumbers([205, 204, 140, 63, 0, 0, 0, 192, 8, 74, 1], [1.1, -2]);
|
||||
testNumbers([
|
||||
154, 153, 153, 153, 153, 153, 241, 63,
|
||||
0, 0, 0, 0, 0, 0, 112, 192,
|
||||
16, 75, 1
|
||||
], [
|
||||
1.1, -256
|
||||
]);
|
||||
|
||||
testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 4, 0, 0, 0, 12, 78, 1],
|
||||
[-45, 256000, 4]);
|
||||
|
||||
testNumbers([
|
||||
211, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 127,
|
||||
4, 0, 0, 0, 0, 0, 0, 0,
|
||||
9, 0, 0, 0, 0, 0, 0, 0,
|
||||
32, 91, 1
|
||||
], [
|
||||
-45, 9223372036854775807, 4, 9
|
||||
]);
|
||||
|
||||
testNumbers([
|
||||
45, 0, 0, 0, 0, 0, 0, 0,
|
||||
255, 255, 255, 255, 255, 255, 255, 127,
|
||||
4, 0, 0, 0, 0, 0, 0, 0,
|
||||
9, 0, 0, 0, 0, 0, 0, 0,
|
||||
32, 95, 1
|
||||
], [
|
||||
45, 9223372036854775807, 4, 9
|
||||
]);
|
||||
|
||||
testNumbers([
|
||||
154, 153, 153, 153, 153, 153, 241, 63,
|
||||
0, 0, 0, 0, 0, 0, 112, 64,
|
||||
0, 0, 0, 0, 0, 0, 16, 64,
|
||||
24, 87, 1
|
||||
], [
|
||||
1.1, 256, 4
|
||||
]);
|
||||
|
||||
testNumbers([
|
||||
154, 153, 153, 153, 153, 153, 241, 63,
|
||||
0, 0, 0, 0, 0, 0, 112, 64,
|
||||
0, 0, 0, 0, 0, 0, 16, 64,
|
||||
0, 0, 0, 0, 0, 0, 34, 64,
|
||||
32, 99, 1
|
||||
], [
|
||||
1.1, 256, 4, 9
|
||||
]);
|
||||
});
|
||||
test('string vector', () {
|
||||
testStrings([
|
||||
3, 102, 111, 111, 0,
|
||||
3, 98, 97, 114, 0,
|
||||
3, 98, 97, 122, 0,
|
||||
3, 15, 11, 7,
|
||||
3, 60, 1
|
||||
], [
|
||||
'foo', 'bar', 'baz'
|
||||
]);
|
||||
testStrings([
|
||||
3, 102, 111, 111, 0,
|
||||
3, 98, 97, 114, 0,
|
||||
3, 98, 97, 122, 0,
|
||||
6, 15, 11, 7, 18, 14, 10,
|
||||
6, 60, 1
|
||||
], [
|
||||
'foo', 'bar', 'baz', 'foo', 'bar', 'baz'
|
||||
]);
|
||||
});
|
||||
test('mixed vector', () {
|
||||
var flx = Reference.fromBuffer(b([
|
||||
3, 102, 111, 111, 0, 0, 0, 0,
|
||||
5, 0, 0, 0, 0, 0, 0, 0,
|
||||
15, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
251, 255, 255, 255, 255, 255, 255, 255,
|
||||
205, 204, 204, 204, 204, 204, 244, 63,
|
||||
1, 0, 0, 0, 0, 0, 0, 0,
|
||||
20, 4, 4, 15, 104, 45, 43, 1
|
||||
]));
|
||||
expect(flx.length, 5);
|
||||
expect(flx[0].stringValue, 'foo');
|
||||
expect(flx[1].numValue, 1);
|
||||
expect(flx[2].numValue, -5);
|
||||
expect(flx[3].numValue, 1.3);
|
||||
expect(flx[4].boolValue, true);
|
||||
});
|
||||
|
||||
test('single value map', () {
|
||||
var flx = Reference.fromBuffer(b([97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]));
|
||||
expect(flx.length, 1);
|
||||
expect(flx['a'].numValue, 12);
|
||||
});
|
||||
test('two value map', () {
|
||||
var flx = Reference.fromBuffer(b([0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]));
|
||||
expect(flx.length, 2);
|
||||
expect(flx['a'].numValue, 12);
|
||||
expect(flx[''].numValue, 45);
|
||||
});
|
||||
test('complex map', () {
|
||||
var flx = complexMap();
|
||||
expect(flx.length, 5);
|
||||
expect(flx['age'].numValue, 35);
|
||||
expect(flx['weight'].numValue, 72.5);
|
||||
expect(flx['name'].stringValue, 'Maxim');
|
||||
|
||||
expect(flx['flags'].length, 4);
|
||||
expect(flx['flags'][0].boolValue, true);
|
||||
expect(flx['flags'][1].boolValue, false);
|
||||
expect(flx['flags'][2].boolValue, true);
|
||||
expect(flx['flags'][3].boolValue, true);
|
||||
|
||||
expect(flx['address'].length, 3);
|
||||
expect(flx['address']['city'].stringValue, 'Bla');
|
||||
expect(flx['address']['zip'].stringValue, '12345');
|
||||
expect(flx['address']['countryCode'].stringValue, 'XX');
|
||||
|
||||
expect(() => flx['address']['country'].stringValue,
|
||||
throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [country] is not applicable on: //address of: ValueType.Map')));
|
||||
expect(() => flx['address']['countryCode'][0],
|
||||
throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [0] is not applicable on: //address/countryCode of: ValueType.String')));
|
||||
expect(() => flx[1],
|
||||
throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [1] is not applicable on: / of: ValueType.Map')));
|
||||
expect(() => flx['flags'][4],
|
||||
throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
|
||||
expect(() => flx['flags'][-1],
|
||||
throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [-1] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
|
||||
});
|
||||
test('complex map to json', () {
|
||||
var flx = complexMap();
|
||||
expect(flx.json, '{"address":{"city":"Bla","countryCode":"XX","zip":"12345"},"age":35,"flags":[true,false,true,true],"name":"Maxim","weight":72.5}');
|
||||
});
|
||||
|
||||
test('complex map iterators', () {
|
||||
var flx = complexMap();
|
||||
expect(flx.mapKeyIterable.map((e) => e).toList(), ['address', 'age', 'flags', 'name', 'weight']);
|
||||
expect(flx.mapValueIterable.map((e) => e.json).toList(), [flx['address'].json, flx['age'].json, flx['flags'].json, flx['name'].json, flx['weight'].json]);
|
||||
expect(flx['flags'].vectorIterable.map((e) => e.boolValue).toList(), [true, false, true, true]);
|
||||
});
|
||||
|
||||
test('bug where offest were stored as int instead of uint', (){
|
||||
const data = [99, 104, 97, 110, 110, 101, 108, 115, 95, 105, 110, 0,
|
||||
100, 105, 108, 97, 116, 105, 111, 110, 95, 104, 101, 105, 103, 104, 116, 95, 102, 97, 99, 116, 111, 114, 0,
|
||||
100, 105, 108, 97, 116, 105, 111, 110, 95, 119, 105, 100, 116, 104, 95, 102, 97, 99, 116, 111, 114, 0,
|
||||
102, 117, 115, 101, 100, 95, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 95, 102, 117, 110, 99, 116, 105, 111, 110, 0,
|
||||
112, 97, 100, 95, 118, 97, 108, 117, 101, 115, 0, 112, 97, 100, 100, 105, 110, 103, 0,
|
||||
115, 116, 114, 105, 100, 101, 95, 104, 101, 105, 103, 104, 116, 0,
|
||||
115, 116, 114, 105, 100, 101, 95, 119, 105, 100, 116, 104, 0,
|
||||
8, 130, 119, 97, 76, 51, 41, 34, 21, 8, 1, 8, 64, 1, 1, 1, 1, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 16, 36, 1];
|
||||
var flx = Reference.fromBuffer(b(data));
|
||||
expect(flx.json, '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}');
|
||||
const object = {"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1};
|
||||
var data1 = Builder.buildFromObject(object).asUint8List();
|
||||
expect(data1.length, data.length);
|
||||
var flx1 = Reference.fromBuffer(b(data1));
|
||||
expect(flx1.json, '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}');
|
||||
});
|
||||
}
|
||||
|
||||
ByteBuffer b(List<int> values) {
|
||||
var data = Uint8List.fromList(values);
|
||||
return data.buffer;
|
||||
}
|
||||
|
||||
void testNumbers(List<int> buffer, List<num> numbers) {
|
||||
var flx = Reference.fromBuffer(b(buffer));
|
||||
expect(flx.length, numbers.length);
|
||||
for (var i = 0; i < flx.length; i++) {
|
||||
expect(flx[i].numValue, closeTo(numbers[i], 0.001));
|
||||
}
|
||||
}
|
||||
|
||||
void testStrings(List<int> buffer, List<String> numbers) {
|
||||
var flx = Reference.fromBuffer(b(buffer));
|
||||
expect(flx.length, numbers.length);
|
||||
for (var i = 0; i < flx.length; i++) {
|
||||
expect(flx[i].stringValue, numbers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference complexMap(){
|
||||
// {
|
||||
// "age": 35,
|
||||
// "flags": [True, False, True, True],
|
||||
// "weight": 72.5,
|
||||
// "name": "Maxim",
|
||||
// "address": {
|
||||
// "city": "Bla",
|
||||
// "zip": "12345",
|
||||
// "countryCode": "XX",
|
||||
// }
|
||||
// }
|
||||
return Reference.fromBuffer(b([
|
||||
97, 100, 100, 114, 101, 115, 115, 0,
|
||||
99, 105, 116, 121, 0, 3, 66, 108, 97, 0,
|
||||
99, 111, 117, 110, 116, 114, 121, 67, 111, 100, 101, 0,
|
||||
2, 88, 88, 0,
|
||||
122, 105, 112, 0,
|
||||
5, 49, 50, 51, 52, 53, 0,
|
||||
3, 38, 29, 14, 3, 1, 3, 38, 22, 15, 20, 20, 20,
|
||||
97, 103, 101, 0,
|
||||
102, 108, 97, 103, 115, 0,
|
||||
4, 1, 0, 1, 1,
|
||||
110, 97, 109, 101, 0,
|
||||
5, 77, 97, 120, 105, 109, 0,
|
||||
119, 101, 105, 103, 104, 116, 0,
|
||||
5, 93, 36, 33, 23, 12, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 60, 0, 0, 0, 35, 0, 0, 0, 51, 0, 0, 0, 45,
|
||||
0, 0, 0, 0, 0, 145, 66, 36, 4, 144, 20, 14, 25, 38, 1
|
||||
]));
|
||||
}
|
||||
137
dart/test/flex_types_test.dart
Normal file
@@ -0,0 +1,137 @@
|
||||
import 'package:flat_buffers/src/types.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('is inline', () {
|
||||
expect(ValueTypeUtils.isInline(ValueType.Bool), isTrue);
|
||||
expect(ValueTypeUtils.isInline(ValueType.Int), isTrue);
|
||||
expect(ValueTypeUtils.isInline(ValueType.UInt), isTrue);
|
||||
expect(ValueTypeUtils.isInline(ValueType.Float), isTrue);
|
||||
expect(ValueTypeUtils.isInline(ValueType.Null), isTrue);
|
||||
expect(ValueTypeUtils.isInline(ValueType.String), isFalse);
|
||||
});
|
||||
test('is type vector element', () {
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Bool), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Int), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.UInt), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Float), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Key), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.String), isTrue);
|
||||
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Null), isFalse);
|
||||
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Blob), isFalse);
|
||||
});
|
||||
test('is typed vector', () {
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorInt), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorUInt), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorFloat), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorBool), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorKey), isTrue);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorString), isTrue);
|
||||
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.Vector), isFalse);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.Map), isFalse);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.Bool), isFalse);
|
||||
expect(ValueTypeUtils.isTypedVector(ValueType.VectorInt2), isFalse);
|
||||
});
|
||||
test('is fixed typed vector', () {
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt2), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt3), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt4), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorUInt2), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorUInt3), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorUInt4), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorFloat2), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorFloat3), isTrue);
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorFloat4), isTrue);
|
||||
|
||||
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt), isFalse);
|
||||
});
|
||||
test('to typed vector', () {
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Int,0), equals(ValueType.VectorInt));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.UInt,0), equals(ValueType.VectorUInt));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Bool,0), equals(ValueType.VectorBool));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Float,0), equals(ValueType.VectorFloat));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Key,0), equals(ValueType.VectorKey));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.String,0), equals(ValueType.VectorString));
|
||||
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Int,2), equals(ValueType.VectorInt2));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.UInt,2), equals(ValueType.VectorUInt2));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Float,2), equals(ValueType.VectorFloat2));
|
||||
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Int,3), equals(ValueType.VectorInt3));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.UInt,3), equals(ValueType.VectorUInt3));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Float,3), equals(ValueType.VectorFloat3));
|
||||
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Int,4), equals(ValueType.VectorInt4));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.UInt,4), equals(ValueType.VectorUInt4));
|
||||
expect(ValueTypeUtils.toTypedVector(ValueType.Float,4), equals(ValueType.VectorFloat4));
|
||||
});
|
||||
test('typed vector element type', () {
|
||||
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorInt), equals(ValueType.Int));
|
||||
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorUInt), equals(ValueType.UInt));
|
||||
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorFloat), equals(ValueType.Float));
|
||||
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorString), equals(ValueType.String));
|
||||
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorKey), equals(ValueType.Key));
|
||||
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorBool), equals(ValueType.Bool));
|
||||
});
|
||||
test('fixed typed vector element type', () {
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt2), equals(ValueType.Int));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt3), equals(ValueType.Int));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt4), equals(ValueType.Int));
|
||||
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt2), equals(ValueType.UInt));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt3), equals(ValueType.UInt));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt4), equals(ValueType.UInt));
|
||||
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat2), equals(ValueType.Float));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat3), equals(ValueType.Float));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat4), equals(ValueType.Float));
|
||||
});
|
||||
test('fixed typed vector element size', () {
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt2), equals(2));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt3), equals(3));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt4), equals(4));
|
||||
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt2), equals(2));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt3), equals(3));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt4), equals(4));
|
||||
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat2), equals(2));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat3), equals(3));
|
||||
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat4), equals(4));
|
||||
});
|
||||
test('packed type', () {
|
||||
expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width8), equals(0));
|
||||
expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width16), equals(1));
|
||||
expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width32), equals(2));
|
||||
expect(ValueTypeUtils.packedType(ValueType.Null, BitWidth.width64), equals(3));
|
||||
|
||||
expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width8), equals(4));
|
||||
expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width16), equals(5));
|
||||
expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width32), equals(6));
|
||||
expect(ValueTypeUtils.packedType(ValueType.Int, BitWidth.width64), equals(7));
|
||||
});
|
||||
test('bit width', () {
|
||||
expect(BitWidthUtil.width(0), BitWidth.width8);
|
||||
expect(BitWidthUtil.width(-20), BitWidth.width8);
|
||||
expect(BitWidthUtil.width(127), BitWidth.width8);
|
||||
expect(BitWidthUtil.width(128), BitWidth.width16);
|
||||
expect(BitWidthUtil.width(128123), BitWidth.width32);
|
||||
expect(BitWidthUtil.width(12812324534), BitWidth.width64);
|
||||
expect(BitWidthUtil.width(-127), BitWidth.width8);
|
||||
expect(BitWidthUtil.width(-128), BitWidth.width16);
|
||||
expect(BitWidthUtil.width(-12812324534), BitWidth.width64);
|
||||
expect(BitWidthUtil.width(-0.1), BitWidth.width64);
|
||||
expect(BitWidthUtil.width(0.25), BitWidth.width32);
|
||||
});
|
||||
test('padding size', () {
|
||||
expect(BitWidthUtil.paddingSize(10, 8), 6);
|
||||
expect(BitWidthUtil.paddingSize(10, 4), 2);
|
||||
expect(BitWidthUtil.paddingSize(15, 4), 1);
|
||||
expect(BitWidthUtil.paddingSize(15, 2), 1);
|
||||
expect(BitWidthUtil.paddingSize(15, 1), 0);
|
||||
expect(BitWidthUtil.paddingSize(16, 8), 0);
|
||||
expect(BitWidthUtil.paddingSize(17, 8), 7);
|
||||
});
|
||||
}
|
||||
@@ -6,8 +6,6 @@ library my_game.example2;
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
import 'include_test1_my_game.example2_generated.dart';
|
||||
import 'include_test2_my_game.example2_generated.dart';
|
||||
import './monster_test_my_game_generated.dart' as my_game;
|
||||
import './monster_test_my_game.example_generated.dart' as my_game_example;
|
||||
|
||||
|
||||
@@ -6,11 +6,10 @@ library my_game.example;
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
import 'include_test1_my_game.example_generated.dart';
|
||||
import 'include_test2_my_game.example_generated.dart';
|
||||
import './monster_test_my_game_generated.dart' as my_game;
|
||||
import './monster_test_my_game.example2_generated.dart' as my_game_example2;
|
||||
|
||||
/// Composite components of Monster color.
|
||||
class Color {
|
||||
final int value;
|
||||
const Color._(this.value);
|
||||
@@ -26,9 +25,14 @@ class Color {
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const Color Red = const Color._(1);
|
||||
|
||||
/// \brief color Green
|
||||
/// Green is bit_flag with value (1u << 1)
|
||||
static const Color Green = const Color._(2);
|
||||
|
||||
/// \brief color Blue (1u << 3)
|
||||
static const Color Blue = const Color._(8);
|
||||
static get values => {1: Red,2: Green,8: Blue,};
|
||||
static const Map<int,Color> values = {1: Red,2: Green,8: Blue,};
|
||||
|
||||
static const fb.Reader<Color> reader = const _ColorReader();
|
||||
|
||||
@@ -46,7 +50,48 @@ class _ColorReader extends fb.Reader<Color> {
|
||||
|
||||
@override
|
||||
Color read(fb.BufferContext bc, int offset) =>
|
||||
new Color.fromValue(const fb.Int8Reader().read(bc, offset));
|
||||
new Color.fromValue(const fb.Uint8Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class Race {
|
||||
final int value;
|
||||
const Race._(this.value);
|
||||
|
||||
factory Race.fromValue(int value) {
|
||||
if (value == null) value = 0;
|
||||
if (!values.containsKey(value)) {
|
||||
throw new StateError('Invalid value $value for bit flag enum Race');
|
||||
}
|
||||
return values[value];
|
||||
}
|
||||
|
||||
static const int minValue = -1;
|
||||
static const int maxValue = 2;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const Race None = const Race._(-1);
|
||||
static const Race Human = const Race._(0);
|
||||
static const Race Dwarf = const Race._(1);
|
||||
static const Race Elf = const Race._(2);
|
||||
static const Map<int,Race> values = {-1: None,0: Human,1: Dwarf,2: Elf,};
|
||||
|
||||
static const fb.Reader<Race> reader = const _RaceReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Race{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _RaceReader extends fb.Reader<Race> {
|
||||
const _RaceReader();
|
||||
|
||||
@override
|
||||
int get size => 1;
|
||||
|
||||
@override
|
||||
Race read(fb.BufferContext bc, int offset) =>
|
||||
new Race.fromValue(const fb.Int8Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class AnyTypeId {
|
||||
@@ -69,7 +114,7 @@ class AnyTypeId {
|
||||
static const AnyTypeId Monster = const AnyTypeId._(1);
|
||||
static const AnyTypeId TestSimpleTableWithEnum = const AnyTypeId._(2);
|
||||
static const AnyTypeId MyGame_Example2_Monster = const AnyTypeId._(3);
|
||||
static get values => {0: NONE,1: Monster,2: TestSimpleTableWithEnum,3: MyGame_Example2_Monster,};
|
||||
static const Map<int,AnyTypeId> values = {0: NONE,1: Monster,2: TestSimpleTableWithEnum,3: MyGame_Example2_Monster,};
|
||||
|
||||
static const fb.Reader<AnyTypeId> reader = const _AnyTypeIdReader();
|
||||
|
||||
@@ -108,9 +153,9 @@ class AnyUniqueAliasesTypeId {
|
||||
|
||||
static const AnyUniqueAliasesTypeId NONE = const AnyUniqueAliasesTypeId._(0);
|
||||
static const AnyUniqueAliasesTypeId M = const AnyUniqueAliasesTypeId._(1);
|
||||
static const AnyUniqueAliasesTypeId T = const AnyUniqueAliasesTypeId._(2);
|
||||
static const AnyUniqueAliasesTypeId TS = const AnyUniqueAliasesTypeId._(2);
|
||||
static const AnyUniqueAliasesTypeId M2 = const AnyUniqueAliasesTypeId._(3);
|
||||
static get values => {0: NONE,1: M,2: T,3: M2,};
|
||||
static const Map<int,AnyUniqueAliasesTypeId> values = {0: NONE,1: M,2: TS,3: M2,};
|
||||
|
||||
static const fb.Reader<AnyUniqueAliasesTypeId> reader = const _AnyUniqueAliasesTypeIdReader();
|
||||
|
||||
@@ -151,7 +196,7 @@ class AnyAmbiguousAliasesTypeId {
|
||||
static const AnyAmbiguousAliasesTypeId M1 = const AnyAmbiguousAliasesTypeId._(1);
|
||||
static const AnyAmbiguousAliasesTypeId M2 = const AnyAmbiguousAliasesTypeId._(2);
|
||||
static const AnyAmbiguousAliasesTypeId M3 = const AnyAmbiguousAliasesTypeId._(3);
|
||||
static get values => {0: NONE,1: M1,2: M2,3: M3,};
|
||||
static const Map<int,AnyAmbiguousAliasesTypeId> values = {0: NONE,1: M1,2: M2,3: M3,};
|
||||
|
||||
static const fb.Reader<AnyAmbiguousAliasesTypeId> reader = const _AnyAmbiguousAliasesTypeIdReader();
|
||||
|
||||
@@ -259,7 +304,7 @@ class TestSimpleTableWithEnum {
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 4, 2));
|
||||
Color get color => new Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 4, 2));
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -287,7 +332,7 @@ class TestSimpleTableWithEnumBuilder {
|
||||
}
|
||||
|
||||
int addColor(Color color) {
|
||||
fbBuilder.addInt8(0, color?.value);
|
||||
fbBuilder.addUint8(0, color?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
@@ -311,7 +356,7 @@ class TestSimpleTableWithEnumObjectBuilder extends fb.ObjectBuilder {
|
||||
assert(fbBuilder != null);
|
||||
|
||||
fbBuilder.startTable();
|
||||
fbBuilder.addInt8(0, _color?.value);
|
||||
fbBuilder.addUint8(0, _color?.value);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@@ -335,7 +380,7 @@ class Vec3 {
|
||||
double get y => const fb.Float32Reader().read(_bc, _bcOffset + 4);
|
||||
double get z => const fb.Float32Reader().read(_bc, _bcOffset + 8);
|
||||
double get test1 => const fb.Float64Reader().read(_bc, _bcOffset + 16);
|
||||
Color get test2 => new Color.fromValue(const fb.Int8Reader().read(_bc, _bcOffset + 24));
|
||||
Color get test2 => new Color.fromValue(const fb.Uint8Reader().read(_bc, _bcOffset + 24));
|
||||
Test get test3 => Test.reader.read(_bc, _bcOffset + 26);
|
||||
|
||||
@override
|
||||
@@ -366,7 +411,7 @@ class Vec3Builder {
|
||||
fbBuilder.pad(2);
|
||||
test3();
|
||||
fbBuilder.pad(1);
|
||||
fbBuilder.putInt8(test2?.value);
|
||||
fbBuilder.putUint8(test2?.value);
|
||||
fbBuilder.putFloat64(test1);
|
||||
fbBuilder.pad(4);
|
||||
fbBuilder.putFloat32(z);
|
||||
@@ -409,7 +454,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder {
|
||||
fbBuilder.pad(2);
|
||||
_test3.finish(fbBuilder);
|
||||
fbBuilder.pad(1);
|
||||
fbBuilder.putInt8(_test2?.value);
|
||||
fbBuilder.putUint8(_test2?.value);
|
||||
fbBuilder.putFloat64(_test1);
|
||||
fbBuilder.pad(4);
|
||||
fbBuilder.putFloat32(_z);
|
||||
@@ -499,6 +544,85 @@ class AbilityObjectBuilder extends fb.ObjectBuilder {
|
||||
return fbBuilder.finish(offset, fileIdentifier);
|
||||
}
|
||||
}
|
||||
class StructOfStructs {
|
||||
StructOfStructs._(this._bc, this._bcOffset);
|
||||
|
||||
static const fb.Reader<StructOfStructs> reader = const _StructOfStructsReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
Ability get a => Ability.reader.read(_bc, _bcOffset + 0);
|
||||
Test get b => Test.reader.read(_bc, _bcOffset + 8);
|
||||
Ability get c => Ability.reader.read(_bc, _bcOffset + 12);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'StructOfStructs{a: $a, b: $b, c: $c}';
|
||||
}
|
||||
}
|
||||
|
||||
class _StructOfStructsReader extends fb.StructReader<StructOfStructs> {
|
||||
const _StructOfStructsReader();
|
||||
|
||||
@override
|
||||
int get size => 20;
|
||||
|
||||
@override
|
||||
StructOfStructs createObject(fb.BufferContext bc, int offset) =>
|
||||
new StructOfStructs._(bc, offset);
|
||||
}
|
||||
|
||||
class StructOfStructsBuilder {
|
||||
StructOfStructsBuilder(this.fbBuilder) {
|
||||
assert(fbBuilder != null);
|
||||
}
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
int finish(fb.StructBuilder a, fb.StructBuilder b, fb.StructBuilder c) {
|
||||
c();
|
||||
b();
|
||||
a();
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class StructOfStructsObjectBuilder extends fb.ObjectBuilder {
|
||||
final AbilityObjectBuilder _a;
|
||||
final TestObjectBuilder _b;
|
||||
final AbilityObjectBuilder _c;
|
||||
|
||||
StructOfStructsObjectBuilder({
|
||||
AbilityObjectBuilder a,
|
||||
TestObjectBuilder b,
|
||||
AbilityObjectBuilder c,
|
||||
})
|
||||
: _a = a,
|
||||
_b = b,
|
||||
_c = c;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(
|
||||
fb.Builder fbBuilder) {
|
||||
assert(fbBuilder != null);
|
||||
|
||||
_c.finish(fbBuilder);
|
||||
_b.finish(fbBuilder);
|
||||
_a.finish(fbBuilder);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String fileIdentifier]) {
|
||||
fb.Builder fbBuilder = new fb.Builder();
|
||||
int offset = finish(fbBuilder);
|
||||
return fbBuilder.finish(offset, fileIdentifier);
|
||||
}
|
||||
}
|
||||
class Stat {
|
||||
Stat._(this._bc, this._bcOffset);
|
||||
factory Stat(List<int> bytes) {
|
||||
@@ -672,7 +796,7 @@ class ReferrableObjectBuilder extends fb.ObjectBuilder {
|
||||
return fbBuilder.finish(offset, fileIdentifier);
|
||||
}
|
||||
}
|
||||
/// an example documentation comment: monster object
|
||||
/// an example documentation comment: "monster object"
|
||||
class Monster {
|
||||
Monster._(this._bc, this._bcOffset);
|
||||
factory Monster(List<int> bytes) {
|
||||
@@ -690,7 +814,7 @@ class Monster {
|
||||
int get hp => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 100);
|
||||
String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 10, null);
|
||||
List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null);
|
||||
Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 8));
|
||||
Color get color => new Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 16, 8));
|
||||
AnyTypeId get testType => new AnyTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 18, 0));
|
||||
dynamic get test {
|
||||
switch (testType?.value) {
|
||||
@@ -702,8 +826,8 @@ class Monster {
|
||||
}
|
||||
List<Test> get test4 => const fb.ListReader<Test>(Test.reader).vTableGet(_bc, _bcOffset, 22, null);
|
||||
List<String> get testarrayofstring => const fb.ListReader<String>(const fb.StringReader()).vTableGet(_bc, _bcOffset, 24, null);
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
List<Monster> get testarrayoftables => const fb.ListReader<Monster>(Monster.reader).vTableGet(_bc, _bcOffset, 26, null);
|
||||
Monster get enemy => Monster.reader.vTableGet(_bc, _bcOffset, 28, null);
|
||||
List<int> get testnestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 30, null);
|
||||
@@ -739,26 +863,29 @@ class Monster {
|
||||
AnyUniqueAliasesTypeId get anyUniqueType => new AnyUniqueAliasesTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 90, 0));
|
||||
dynamic get anyUnique {
|
||||
switch (anyUniqueType?.value) {
|
||||
case 1: return M.reader.vTableGet(_bc, _bcOffset, 92, null);
|
||||
case 2: return T.reader.vTableGet(_bc, _bcOffset, 92, null);
|
||||
case 3: return M2.reader.vTableGet(_bc, _bcOffset, 92, null);
|
||||
case 1: return Monster.reader.vTableGet(_bc, _bcOffset, 92, null);
|
||||
case 2: return TestSimpleTableWithEnum.reader.vTableGet(_bc, _bcOffset, 92, null);
|
||||
case 3: return my_game_example2.Monster.reader.vTableGet(_bc, _bcOffset, 92, null);
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
AnyAmbiguousAliasesTypeId get anyAmbiguousType => new AnyAmbiguousAliasesTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 94, 0));
|
||||
dynamic get anyAmbiguous {
|
||||
switch (anyAmbiguousType?.value) {
|
||||
case 1: return M1.reader.vTableGet(_bc, _bcOffset, 96, null);
|
||||
case 2: return M2.reader.vTableGet(_bc, _bcOffset, 96, null);
|
||||
case 3: return M3.reader.vTableGet(_bc, _bcOffset, 96, null);
|
||||
case 1: return Monster.reader.vTableGet(_bc, _bcOffset, 96, null);
|
||||
case 2: return Monster.reader.vTableGet(_bc, _bcOffset, 96, null);
|
||||
case 3: return Monster.reader.vTableGet(_bc, _bcOffset, 96, null);
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
List<Color> get vectorOfEnums => const fb.ListReader<Color>(Color.reader).vTableGet(_bc, _bcOffset, 98, null);
|
||||
Race get signedEnum => new Race.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 100, -1));
|
||||
List<int> get testrequirednestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 102, null);
|
||||
List<Stat> get scalarKeySortedTables => const fb.ListReader<Stat>(Stat.reader).vTableGet(_bc, _bcOffset, 104, null);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, testType: $testType, test: $test, test4: $test4, testarrayofstring: $testarrayofstring, testarrayoftables: $testarrayoftables, enemy: $enemy, testnestedflatbuffer: $testnestedflatbuffer, testempty: $testempty, testbool: $testbool, testhashs32Fnv1: $testhashs32Fnv1, testhashu32Fnv1: $testhashu32Fnv1, testhashs64Fnv1: $testhashs64Fnv1, testhashu64Fnv1: $testhashu64Fnv1, testhashs32Fnv1a: $testhashs32Fnv1a, testhashu32Fnv1a: $testhashu32Fnv1a, testhashs64Fnv1a: $testhashs64Fnv1a, testhashu64Fnv1a: $testhashu64Fnv1a, testarrayofbools: $testarrayofbools, testf: $testf, testf2: $testf2, testf3: $testf3, testarrayofstring2: $testarrayofstring2, testarrayofsortedstruct: $testarrayofsortedstruct, flex: $flex, test5: $test5, vectorOfLongs: $vectorOfLongs, vectorOfDoubles: $vectorOfDoubles, parentNamespaceTest: $parentNamespaceTest, vectorOfReferrables: $vectorOfReferrables, singleWeakReference: $singleWeakReference, vectorOfWeakReferences: $vectorOfWeakReferences, vectorOfStrongReferrables: $vectorOfStrongReferrables, coOwningReference: $coOwningReference, vectorOfCoOwningReferences: $vectorOfCoOwningReferences, nonOwningReference: $nonOwningReference, vectorOfNonOwningReferences: $vectorOfNonOwningReferences, anyUniqueType: $anyUniqueType, anyUnique: $anyUnique, anyAmbiguousType: $anyAmbiguousType, anyAmbiguous: $anyAmbiguous, vectorOfEnums: $vectorOfEnums}';
|
||||
return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, testType: $testType, test: $test, test4: $test4, testarrayofstring: $testarrayofstring, testarrayoftables: $testarrayoftables, enemy: $enemy, testnestedflatbuffer: $testnestedflatbuffer, testempty: $testempty, testbool: $testbool, testhashs32Fnv1: $testhashs32Fnv1, testhashu32Fnv1: $testhashu32Fnv1, testhashs64Fnv1: $testhashs64Fnv1, testhashu64Fnv1: $testhashu64Fnv1, testhashs32Fnv1a: $testhashs32Fnv1a, testhashu32Fnv1a: $testhashu32Fnv1a, testhashs64Fnv1a: $testhashs64Fnv1a, testhashu64Fnv1a: $testhashu64Fnv1a, testarrayofbools: $testarrayofbools, testf: $testf, testf2: $testf2, testf3: $testf3, testarrayofstring2: $testarrayofstring2, testarrayofsortedstruct: $testarrayofsortedstruct, flex: $flex, test5: $test5, vectorOfLongs: $vectorOfLongs, vectorOfDoubles: $vectorOfDoubles, parentNamespaceTest: $parentNamespaceTest, vectorOfReferrables: $vectorOfReferrables, singleWeakReference: $singleWeakReference, vectorOfWeakReferences: $vectorOfWeakReferences, vectorOfStrongReferrables: $vectorOfStrongReferrables, coOwningReference: $coOwningReference, vectorOfCoOwningReferences: $vectorOfCoOwningReferences, nonOwningReference: $nonOwningReference, vectorOfNonOwningReferences: $vectorOfNonOwningReferences, anyUniqueType: $anyUniqueType, anyUnique: $anyUnique, anyAmbiguousType: $anyAmbiguousType, anyAmbiguous: $anyAmbiguous, vectorOfEnums: $vectorOfEnums, signedEnum: $signedEnum, testrequirednestedflatbuffer: $testrequirednestedflatbuffer, scalarKeySortedTables: $scalarKeySortedTables}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -802,7 +929,7 @@ class MonsterBuilder {
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addColor(Color color) {
|
||||
fbBuilder.addInt8(6, color?.value);
|
||||
fbBuilder.addUint8(6, color?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addTestType(AnyTypeId testType) {
|
||||
@@ -969,6 +1096,18 @@ class MonsterBuilder {
|
||||
fbBuilder.addOffset(47, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addSignedEnum(Race signedEnum) {
|
||||
fbBuilder.addInt8(48, signedEnum?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addTestrequirednestedflatbufferOffset(int offset) {
|
||||
fbBuilder.addOffset(49, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addScalarKeySortedTablesOffset(int offset) {
|
||||
fbBuilder.addOffset(50, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
@@ -1023,6 +1162,9 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
final AnyAmbiguousAliasesTypeId _anyAmbiguousType;
|
||||
final dynamic _anyAmbiguous;
|
||||
final List<Color> _vectorOfEnums;
|
||||
final Race _signedEnum;
|
||||
final List<int> _testrequirednestedflatbuffer;
|
||||
final List<StatObjectBuilder> _scalarKeySortedTables;
|
||||
|
||||
MonsterObjectBuilder({
|
||||
Vec3ObjectBuilder pos,
|
||||
@@ -1072,6 +1214,9 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
AnyAmbiguousAliasesTypeId anyAmbiguousType,
|
||||
dynamic anyAmbiguous,
|
||||
List<Color> vectorOfEnums,
|
||||
Race signedEnum,
|
||||
List<int> testrequirednestedflatbuffer,
|
||||
List<StatObjectBuilder> scalarKeySortedTables,
|
||||
})
|
||||
: _pos = pos,
|
||||
_mana = mana,
|
||||
@@ -1119,7 +1264,10 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
_anyUnique = anyUnique,
|
||||
_anyAmbiguousType = anyAmbiguousType,
|
||||
_anyAmbiguous = anyAmbiguous,
|
||||
_vectorOfEnums = vectorOfEnums;
|
||||
_vectorOfEnums = vectorOfEnums,
|
||||
_signedEnum = signedEnum,
|
||||
_testrequirednestedflatbuffer = testrequirednestedflatbuffer,
|
||||
_scalarKeySortedTables = scalarKeySortedTables;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
@@ -1185,7 +1333,13 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
final int anyUniqueOffset = _anyUnique?.getOrCreateOffset(fbBuilder);
|
||||
final int anyAmbiguousOffset = _anyAmbiguous?.getOrCreateOffset(fbBuilder);
|
||||
final int vectorOfEnumsOffset = _vectorOfEnums?.isNotEmpty == true
|
||||
? fbBuilder.writeListInt8(_vectorOfEnums.map((f) => f.value))
|
||||
? fbBuilder.writeListUint8(_vectorOfEnums.map((f) => f.value))
|
||||
: null;
|
||||
final int testrequirednestedflatbufferOffset = _testrequirednestedflatbuffer?.isNotEmpty == true
|
||||
? fbBuilder.writeListUint8(_testrequirednestedflatbuffer)
|
||||
: null;
|
||||
final int scalarKeySortedTablesOffset = _scalarKeySortedTables?.isNotEmpty == true
|
||||
? fbBuilder.writeList(_scalarKeySortedTables.map((b) => b.getOrCreateOffset(fbBuilder)).toList())
|
||||
: null;
|
||||
|
||||
fbBuilder.startTable();
|
||||
@@ -1200,7 +1354,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
if (inventoryOffset != null) {
|
||||
fbBuilder.addOffset(5, inventoryOffset);
|
||||
}
|
||||
fbBuilder.addInt8(6, _color?.value);
|
||||
fbBuilder.addUint8(6, _color?.value);
|
||||
fbBuilder.addUint8(7, _testType?.value);
|
||||
if (testOffset != null) {
|
||||
fbBuilder.addOffset(8, testOffset);
|
||||
@@ -1288,6 +1442,13 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
if (vectorOfEnumsOffset != null) {
|
||||
fbBuilder.addOffset(47, vectorOfEnumsOffset);
|
||||
}
|
||||
fbBuilder.addInt8(48, _signedEnum?.value);
|
||||
if (testrequirednestedflatbufferOffset != null) {
|
||||
fbBuilder.addOffset(49, testrequirednestedflatbufferOffset);
|
||||
}
|
||||
if (scalarKeySortedTablesOffset != null) {
|
||||
fbBuilder.addOffset(50, scalarKeySortedTablesOffset);
|
||||
}
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ library my_game;
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
import 'include_test1_my_game_generated.dart';
|
||||
import 'include_test2_my_game_generated.dart';
|
||||
import './monster_test_my_game.example_generated.dart' as my_game_example;
|
||||
import './monster_test_my_game.example2_generated.dart' as my_game_example2;
|
||||
|
||||
|
||||
@@ -29,6 +29,40 @@ Building should also produce two sample executables, `flatsamplebinary` and
|
||||
*Note that you MUST be in the root of the FlatBuffers distribution when you
|
||||
run 'flattests' or `flatsampletext`, or it will fail to load its files.*
|
||||
|
||||
### Make all warnings into errors
|
||||
|
||||
By default all Flatbuffers `cmake` targets are build with `-Werror` flag.
|
||||
With this flag (or `/WX` for MSVC) C++ compiler will treat all warnings as errors.
|
||||
Additionally `-Wall -pedantic -Wextra` (or `/W4` form MSVC) flags are set.
|
||||
These flags minimize the number of possible defects in code and keep code highly portable.
|
||||
Using these flags is considered good practice but sometimes it can break dependent projects
|
||||
if a compiler is upgraded or a toolset is changed.
|
||||
Usually, newer compiler versions add new compile-time diagnostics that were unavailable before.
|
||||
These new diagnostic warnings could stop the build process if `-Werror` flag is set.
|
||||
|
||||
It is possible to cancel `warnings as errors` flag at `cmake` configuration stage using
|
||||
`FLATBUFFERS_CXX_FLAGS` option. Compilation flags declared in `FLATBUFFERS_CXX_FLAGS` will be
|
||||
appended to the project-level `CMAKE_CXX_FLAGS` variable.
|
||||
Examples:
|
||||
|
||||
- GCC and Clang: `cmake . -D FLATBUFFERS_CXX_FLAGS="-Wno-error"`
|
||||
- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/WX-"`
|
||||
- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/Wv <compiler.version>"`
|
||||
|
||||
|
||||
## Building with VCPKG
|
||||
|
||||
You can download and install flatbuffers using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
|
||||
|
||||
git clone https://github.com/Microsoft/vcpkg.git
|
||||
cd vcpkg
|
||||
./bootstrap-vcpkg.sh
|
||||
./vcpkg integrate install
|
||||
./vcpkg install flatbuffers
|
||||
|
||||
The flatbuffers port in vcpkg is kept up to date by Microsoft team members and community contributors.
|
||||
If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
|
||||
|
||||
## Building for Android
|
||||
|
||||
There is a `flatbuffers/android` directory that contains all you need to build
|
||||
|
||||
@@ -3,7 +3,7 @@ Using the schema compiler {#flatbuffers_guide_using_schema_compiler}
|
||||
|
||||
Usage:
|
||||
|
||||
flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] [ -S ] FILES...
|
||||
flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] FILES...
|
||||
[ -- FILES...]
|
||||
|
||||
The files are read and parsed in order, and can contain either schemas
|
||||
@@ -23,6 +23,8 @@ For any schema input files, one or more generators can be specified:
|
||||
|
||||
- `--java`, `-j` : Generate Java code.
|
||||
|
||||
- `--kotlin`, `-k` : Generate Kotlin code.
|
||||
|
||||
- `--csharp`, `-n` : Generate C# code.
|
||||
|
||||
- `--go`, `-g` : Generate Go code.
|
||||
@@ -45,6 +47,8 @@ For any schema input files, one or more generators can be specified:
|
||||
|
||||
- `--rust`, `-r` : Generate Rust code.
|
||||
|
||||
- `--swift`: Generate Swift code.
|
||||
|
||||
For any data input files:
|
||||
|
||||
- `--binary`, `-b` : If data is contained in this file, generate a
|
||||
@@ -92,7 +96,7 @@ Additional options:
|
||||
statements) use `--no-includes.`
|
||||
|
||||
- `--no-includes` : Don't generate include statements for included schemas the
|
||||
generated file depends on (C++).
|
||||
generated file depends on (C++ / Python).
|
||||
|
||||
- `--gen-mutable` : Generate additional non-const accessors for mutating
|
||||
FlatBuffers in-place.
|
||||
@@ -112,11 +116,16 @@ Additional options:
|
||||
|
||||
- `--gen-generated` : Add @Generated annotation for Java.
|
||||
|
||||
- `--gen-jvmstatic` : Add @JvmStatic annotation for Kotlin methods
|
||||
in companion object for interop from Java to Kotlin.
|
||||
|
||||
- `--gen-all` : Generate not just code for the current schema files, but
|
||||
for all files it includes as well. If the language uses a single file for
|
||||
output (by default the case for C++ and JS), all code will end up in
|
||||
this one file.
|
||||
|
||||
- `--cpp-include` : Adds an #include in generated file
|
||||
|
||||
- `--cpp-ptr-type T` : Set object API pointer type (default std::unique_ptr)
|
||||
|
||||
- `--cpp-str-type T` : Set object API string type (default std::string)
|
||||
@@ -128,20 +137,19 @@ Additional options:
|
||||
std::string from Flatbuffers, but (char* + length). This allows efficient
|
||||
construction of custom string types, including zero-copy construction.
|
||||
|
||||
- `--no-cpp-direct-copy` : Don't generate direct copy methods for C++
|
||||
object-based API.
|
||||
|
||||
- `--cpp-std CPP_STD` : Generate a C++ code using features of selected C++ standard.
|
||||
Supported `CPP_STD` values:
|
||||
* `c++0x` - generate code compatible with old compilers (VS2010),
|
||||
* `c++11` - use C++11 code generator (default),
|
||||
* `c++17` - use C++17 features in generated code (experimental).
|
||||
|
||||
- `--object-prefix` : Customise class prefix for C++ object-based API.
|
||||
|
||||
- `--object-suffix` : Customise class suffix for C++ object-based API.
|
||||
|
||||
- `--no-js-exports` : Removes Node.js style export lines (useful for JS)
|
||||
|
||||
- `--goog-js-export` : Uses goog.exportsSymbol and goog.exportsProperty
|
||||
instead of Node.js style exporting. Needed for compatibility with the
|
||||
Google closure compiler (useful for JS).
|
||||
|
||||
- `--es6-js-export` : Generates ECMAScript v6 style export definitions
|
||||
instead of Node.js style exporting. Useful when integrating flatbuffers
|
||||
with modern Javascript projects.
|
||||
|
||||
- `--go-namespace` : Generate the overrided namespace in Golang.
|
||||
|
||||
- `--go-import` : Generate the overrided import for flatbuffers in Golang.
|
||||
@@ -177,27 +185,38 @@ Additional options:
|
||||
- `--conform-includes PATH` : Include path for the schema given with
|
||||
`--conform PATH`.
|
||||
|
||||
- `--filename-suffix SUFFIX` : The suffix appended to the generated
|
||||
file names. Default is '_generated'.
|
||||
|
||||
- `--filename-ext EXTENSION` : The extension appended to the generated
|
||||
file names. Default is language-specific (e.g. "h" for C++). This
|
||||
should not be used when multiple languages are specified.
|
||||
|
||||
- `--include-prefix PATH` : Prefix this path to any generated include
|
||||
statements.
|
||||
|
||||
- `--keep-prefix` : Keep original prefix of schema include statement.
|
||||
|
||||
- `--no-fb-impor` : Don't include flatbuffers import statement for TypeScript.
|
||||
|
||||
- `--no-ts-reexpor` : Don't re-export imported dependencies for TypeScript.
|
||||
|
||||
- `--short-name` : Use short function names for JS and TypeScript.
|
||||
|
||||
- `--reflect-types` : Add minimal type reflection to code generation.
|
||||
|
||||
- `--reflect-names` : Add minimal type/name reflection.
|
||||
|
||||
- `--root-type T` : Select or override the default root_type.
|
||||
|
||||
- `--require-explicit-ids` : When parsing schemas, require explicit ids (id: x).
|
||||
|
||||
- `--force-defaults` : Emit default values in binary output from JSON.
|
||||
|
||||
- `--force-empty` : When serializing from object API representation, force
|
||||
strings and vectors to empty rather than null.
|
||||
|
||||
- `--force-empty-vectors` : When serializing from object API representation, force
|
||||
vectors to empty rather than null.
|
||||
|
||||
- `--flexbuffers` : Used with "binary" and "json" options, it generates
|
||||
data using schema-less FlexBuffers.
|
||||
|
||||
- `--no-warnings` : Inhibit all warning messages.
|
||||
|
||||
NOTE: short-form options for generators are deprecated, use the long form
|
||||
whenever possible.
|
||||
|
||||
@@ -35,7 +35,7 @@ The test code itself is located in
|
||||
[test.cpp](https://github.com/google/flatbuffers/blob/master/tests/test.cpp).
|
||||
|
||||
This test file is built alongside `flatc`. To review how to build the project,
|
||||
please read the [Building](@ref flatbuffers_guide_building) documenation.
|
||||
please read the [Building](@ref flatbuffers_guide_building) documentation.
|
||||
|
||||
To run the tests, execute `flattests` from the root `flatbuffers/` directory.
|
||||
For example, on [Linux](https://en.wikipedia.org/wiki/Linux), you would simply
|
||||
@@ -114,15 +114,15 @@ To use:
|
||||
MonsterT monsterobj;
|
||||
|
||||
// Deserialize from buffer into object.
|
||||
UnPackTo(&monsterobj, flatbuffer);
|
||||
GetMonster(flatbuffer)->UnPackTo(&monsterobj);
|
||||
|
||||
// Update object directly like a C++ class instance.
|
||||
cout << monsterobj->name; // This is now a std::string!
|
||||
monsterobj->name = "Bob"; // Change the name.
|
||||
cout << monsterobj.name; // This is now a std::string!
|
||||
monsterobj.name = "Bob"; // Change the name.
|
||||
|
||||
// Serialize into new flatbuffer.
|
||||
FlatBufferBuilder fbb;
|
||||
Pack(fbb, &monsterobj);
|
||||
fbb.Finish(Monster::Pack(fbb, &monsterobj));
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The following attributes are specific to the object-based API code generation:
|
||||
@@ -133,11 +133,11 @@ The following attributes are specific to the object-based API code generation:
|
||||
This attribute changes the member declaration to use the type directly
|
||||
rather than wrapped in a unique_ptr.
|
||||
|
||||
- `native_default`: "value" (on a field): For members that are declared
|
||||
- `native_default("value")` (on a field): For members that are declared
|
||||
"native_inline", the value specified with this attribute will be included
|
||||
verbatim in the class constructor initializer list for this member.
|
||||
|
||||
- `native_custom_alloc`:"custom_allocator" (on a table or struct): When using the
|
||||
- `native_custom_alloc("custom_allocator")` (on a table or struct): When using the
|
||||
object-based API all generated NativeTables that are allocated when unpacking
|
||||
your flatbuffer will use "custom allocator". The allocator is also used by
|
||||
any std::vector that appears in a table defined with `native_custom_alloc`.
|
||||
@@ -148,12 +148,15 @@ The following attributes are specific to the object-based API code generation:
|
||||
|
||||
schema:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
table mytable(native_custom_alloc:"custom_allocator") {
|
||||
...
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
with custom_allocator defined before flatbuffers.h is included, as:
|
||||
with custom_allocator defined before `flatbuffers.h` is included, as:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
template <typename T> struct custom_allocator : public std::allocator<T> {
|
||||
|
||||
typedef T *pointer;
|
||||
@@ -175,48 +178,73 @@ The following attributes are specific to the object-based API code generation:
|
||||
template <class U>
|
||||
custom_allocator(const custom_allocator<U>&) throw() {}
|
||||
};
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
- `native_type`' "type" (on a struct): In some cases, a more optimal C++ data
|
||||
- `native_type("type")` (on a struct): In some cases, a more optimal C++ data
|
||||
type exists for a given struct. For example, the following schema:
|
||||
|
||||
struct Vec2 {
|
||||
x: float;
|
||||
y: float;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
struct Vec2 {
|
||||
x: float;
|
||||
y: float;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
generates the following Object-Based API class:
|
||||
|
||||
struct Vec2T : flatbuffers::NativeTable {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
struct Vec2T : flatbuffers::NativeTable {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
However, it can be useful to instead use a user-defined C++ type since it
|
||||
can provide more functionality, eg.
|
||||
|
||||
struct vector2 {
|
||||
float x = 0, y = 0;
|
||||
vector2 operator+(vector2 rhs) const { ... }
|
||||
vector2 operator-(vector2 rhs) const { ... }
|
||||
float length() const { ... }
|
||||
// etc.
|
||||
};
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
struct vector2 {
|
||||
float x = 0, y = 0;
|
||||
vector2 operator+(vector2 rhs) const { ... }
|
||||
vector2 operator-(vector2 rhs) const { ... }
|
||||
float length() const { ... }
|
||||
// etc.
|
||||
};
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The `native_type` attribute will replace the usage of the generated class
|
||||
with the given type. So, continuing with the example, the generated
|
||||
code would use |vector2| in place of |Vec2T| for all generated code.
|
||||
code would use `vector2` in place of `Vec2T` for all generated code of
|
||||
the Object-Based API.
|
||||
|
||||
However, becuase the native_type is unknown to flatbuffers, the user must
|
||||
However, because the `native_type` is unknown to flatbuffers, the user must
|
||||
provide the following functions to aide in the serialization process:
|
||||
|
||||
namespace flatbuffers {
|
||||
FlatbufferStruct Pack(const native_type& obj);
|
||||
native_type UnPack(const FlatbufferStruct& obj);
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
namespace flatbuffers {
|
||||
Vec2 Pack(const vector2& obj);
|
||||
vector2 UnPack(const Vec2& obj);
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Finally, the following top-level attribute
|
||||
- `native_type_pack_name("name")` (on a struct when `native_type` is
|
||||
specified, too): when you want to use the same `native_type` multiple times
|
||||
(e. g. with different precision) you must make the names of the Pack/UnPack
|
||||
functions unique, otherwise you will run into compile errors. This attribute
|
||||
appends a name to the expected Pack/UnPack functions. So when you
|
||||
specify `native_type_pack_name("Vec2")` in the above example you now need to
|
||||
implement these serialization functions instead:
|
||||
|
||||
- `native_include`: "path" (at file level): Because the `native_type` attribute
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
namespace flatbuffers {
|
||||
Vec2 PackVec2(const vector2& obj);
|
||||
vector2 UnPackVec2(const Vec2& obj);
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Finally, the following top-level attributes:
|
||||
|
||||
- `native_include("path")` (at file level): Because the `native_type` attribute
|
||||
can be used to introduce types that are unknown to flatbuffers, it may be
|
||||
necessary to include "external" header files in the generated code. This
|
||||
attribute can be used to directly add an #include directive to the top of
|
||||
@@ -299,7 +327,7 @@ And example of usage, for the time being, can be found in
|
||||
## Mini Reflection
|
||||
|
||||
A more limited form of reflection is available for direct inclusion in
|
||||
generated code, which doesn't any (binary) schema access at all. It was designed
|
||||
generated code, which doesn't do any (binary) schema access at all. It was designed
|
||||
to keep the overhead of reflection as low as possible (on the order of 2-6
|
||||
bytes per field added to your executable), but doesn't contain all the
|
||||
information the (binary) schema contains.
|
||||
@@ -426,6 +454,8 @@ it, this will provide you an easy way to use that data directly.
|
||||
(see the schema documentation for some specifics on the JSON format
|
||||
accepted).
|
||||
|
||||
Schema evolution compatibility for the JSON format follows the same rules as the binary format (JSON formatted data will be forwards/backwards compatible with schemas that evolve in a compatible way).
|
||||
|
||||
There are two ways to use text formats:
|
||||
|
||||
#### Using the compiler as a conversion tool
|
||||
@@ -477,7 +507,7 @@ include paths. If not specified, any include statements try to resolve from
|
||||
the current directory.
|
||||
|
||||
If there were any parsing errors, `Parse` will return `false`, and
|
||||
`Parser::err` contains a human readable error string with a line number
|
||||
`Parser::error_` contains a human readable error string with a line number
|
||||
etc, which you should present to the creator of that file.
|
||||
|
||||
After each JSON file, the `Parser::fbb` member variable is the
|
||||
@@ -546,21 +576,63 @@ locale-independent or locale-narrow functions `strtof_l`, `strtod_l`,
|
||||
These functions use specified locale rather than the global or per-thread
|
||||
locale instead. They are part of POSIX-2008 but not part of the C/C++
|
||||
standard library, therefore, may be missing on some platforms.
|
||||
|
||||
The Flatbuffers library try to detect these functions at configuration and
|
||||
compile time:
|
||||
- `_MSC_VER >= 1900`: check MSVC2012 or higher for MSVC buid
|
||||
- `_XOPEN_SOURCE>=700`: check POSIX-2008 for GCC/Clang build
|
||||
- `check_cxx_symbol_exists(strtof_l stdlib.h)`: CMake check of `strtod_f`
|
||||
- CMake `"CMakeLists.txt"`:
|
||||
- Check existence of `strtol_l` and `strtod_l` in the `<stdlib.h>`.
|
||||
- Compile-time `"/include/base.h"`:
|
||||
- `_MSC_VER >= 1900`: MSVC2012 or higher if build with MSVC.
|
||||
- `_XOPEN_SOURCE>=700`: POSIX-2008 if build with GCC/Clang.
|
||||
|
||||
After detection, the definition `FLATBUFFERS_LOCALE_INDEPENDENT` will be
|
||||
set to `0` or `1`.
|
||||
To override or stop this detection use CMake `-DFLATBUFFERS_LOCALE_INDEPENDENT={0|1}`
|
||||
or predefine `FLATBUFFERS_LOCALE_INDEPENDENT` symbol.
|
||||
|
||||
It is possible to test the compatibility of the Flatbuffers library with
|
||||
a specific locale using the environment variable `FLATBUFFERS_TEST_LOCALE`:
|
||||
To test the compatibility of the Flatbuffers library with
|
||||
a specific locale use the environment variable `FLATBUFFERS_TEST_LOCALE`:
|
||||
```sh
|
||||
>FLATBUFFERS_TEST_LOCALE="" ./flattests
|
||||
>FLATBUFFERS_TEST_LOCALE="ru_RU.CP1251" ./flattests
|
||||
```
|
||||
|
||||
## Support of floating-point numbers
|
||||
The Flatbuffers library assumes that a C++ compiler and a CPU are
|
||||
compatible with the `IEEE-754` floating-point standard.
|
||||
The schema and json parser may fail if `fast-math` or `/fp:fast` mode is active.
|
||||
|
||||
### Support of hexadecimal and special floating-point numbers
|
||||
According to the [grammar](@ref flatbuffers_grammar) `fbs` and `json` files
|
||||
may use hexadecimal and special (`NaN`, `Inf`) floating-point literals.
|
||||
The Flatbuffers uses `strtof` and `strtod` functions to parse floating-point
|
||||
literals. The Flatbuffers library has a code to detect a compiler compatibility
|
||||
with the literals. If necessary conditions are met the preprocessor constant
|
||||
`FLATBUFFERS_HAS_NEW_STRTOD` will be set to `1`.
|
||||
The support of floating-point literals will be limited at compile time
|
||||
if `FLATBUFFERS_HAS_NEW_STRTOD` constant is less than `1`.
|
||||
In this case, schemas with hexadecimal or special literals cannot be used.
|
||||
|
||||
### Comparison of floating-point NaN values
|
||||
The floating-point `NaN` (`not a number`) is special value which
|
||||
representing an undefined or unrepresentable value.
|
||||
`NaN` may be explicitly assigned to variables, typically as a representation
|
||||
for missing values or may be a result of a mathematical operation.
|
||||
The `IEEE-754` defines two kind of `NaNs`:
|
||||
- Quiet NaNs, or `qNaNs`.
|
||||
- Signaling NaNs, or `sNaNs`.
|
||||
|
||||
According to the `IEEE-754`, a comparison with `NaN` always returns
|
||||
an unordered result even when compared with itself. As a result, a whole
|
||||
Flatbuffers object will be not equal to itself if has one or more `NaN`.
|
||||
Flatbuffers scalar fields that have the default value are not actually stored
|
||||
in the serialized data but are generated in code (see [Writing a schema](@ref flatbuffers_guide_writing_schema)).
|
||||
Scalar fields with `NaN` defaults break this behavior.
|
||||
If a schema has a lot of `NaN` defaults the Flatbuffers can override
|
||||
the unordered comparison by the ordered: `(NaN==NaN)->true`.
|
||||
This ordered comparison is enabled when compiling a program with the symbol
|
||||
`FLATBUFFERS_NAN_DEFAULTS` defined.
|
||||
Additional computations added by `FLATBUFFERS_NAN_DEFAULTS` are very cheap
|
||||
if GCC or Clang used. These compilers have a compile-time implementation
|
||||
of `isnan` checking which MSVC does not.
|
||||
|
||||
<br>
|
||||
|
||||
226
docs/source/CsharpUsage.md
Normal file
@@ -0,0 +1,226 @@
|
||||
Use in C# {#flatbuffers_guide_use_c-sharp}
|
||||
==============
|
||||
|
||||
## Before you get started
|
||||
|
||||
Before diving into the FlatBuffers usage in C#, it should be noted that
|
||||
the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
|
||||
general FlatBuffers usage in all of the supported languages (including C#).
|
||||
This page is designed to cover the nuances of FlatBuffers usage,
|
||||
specific to C#.
|
||||
|
||||
You should also have read the [Building](@ref flatbuffers_guide_building)
|
||||
documentation to build `flatc` and should be familiar with
|
||||
[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
|
||||
[Writing a schema](@ref flatbuffers_guide_writing_schema).
|
||||
|
||||
## FlatBuffers C# code location
|
||||
|
||||
The code for the FlatBuffers C# library can be found at
|
||||
`flatbuffers/net/FlatBuffers`. You can browse the library on the
|
||||
[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/net/
|
||||
FlatBuffers).
|
||||
|
||||
## Building the FlatBuffers C# library
|
||||
|
||||
The `FlatBuffers.csproj` project contains multitargeting for .NET Standard 2.1,
|
||||
.NET Standard 2.0, and .NET Framework 4.6 (Unity 2017). Support for .NET
|
||||
Framework 3.5 (Unity 5) is provided by the `FlatBuffers.net35.csproj` project.
|
||||
In most cases (including Unity 2018 and newer), .NET Standard 2.0 is
|
||||
recommended.
|
||||
|
||||
You can build for a specific framework target when using the cross-platform
|
||||
[.NET Core SDK](https://dotnet.microsoft.com/download) by adding the `-f`
|
||||
command line option:
|
||||
|
||||
~~~{.sh}
|
||||
dotnet build -f netstandard2.0 "FlatBuffers.csproj"
|
||||
~~~
|
||||
|
||||
The `FlatBuffers.csproj` project also provides support for defining various
|
||||
conditional compilation symbols (see "Conditional compilation symbols" section
|
||||
below) using the `-p` command line option:
|
||||
|
||||
~~~{.sh}
|
||||
dotnet build -f netstandard2.1 -p:ENABLE_SPAN_T=true -p:UNSAFE_BYTEBUFFER=true "FlatBuffers.csproj"
|
||||
~~~
|
||||
|
||||
## Testing the FlatBuffers C# library
|
||||
|
||||
The code to test the libraries can be found at `flatbuffers/tests`.
|
||||
|
||||
The test code for C# is located in the [FlatBuffers.Test](https://github.com/
|
||||
google/flatbuffers/tree/master/tests/FlatBuffers.Test) subfolder. To run the
|
||||
tests, open `FlatBuffers.Test.csproj` in [Visual Studio](
|
||||
https://www.visualstudio.com), and compile/run the project.
|
||||
|
||||
Optionally, you can run this using [Mono](http://www.mono-project.com/) instead.
|
||||
Once you have installed Mono, you can run the tests from the command line
|
||||
by running the following commands from inside the `FlatBuffers.Test` folder:
|
||||
|
||||
~~~{.sh}
|
||||
mcs *.cs ../MyGame/Example/*.cs ../../net/FlatBuffers/*.cs
|
||||
mono Assert.exe
|
||||
~~~
|
||||
|
||||
## Using the FlatBuffers C# library
|
||||
|
||||
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
|
||||
example of how to use FlatBuffers in C#.*
|
||||
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in C#.
|
||||
|
||||
To use FlatBuffers in your own code, first generate C# classes from your
|
||||
schema with the `--csharp` option to `flatc`.
|
||||
Then you can include both FlatBuffers and the generated code to read
|
||||
or write a FlatBuffer.
|
||||
|
||||
For example, here is how you would read a FlatBuffer binary file in C#:
|
||||
First, import the library and generated code. Then, you read a FlatBuffer binary
|
||||
file into a `byte[]`. You then turn the `byte[]` into a `ByteBuffer`, which you
|
||||
pass to the `GetRootAsMyRootType` function:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
|
||||
using MyGame.Example;
|
||||
using FlatBuffers;
|
||||
|
||||
// This snippet ignores exceptions for brevity.
|
||||
byte[] data = File.ReadAllBytes("monsterdata_test.mon");
|
||||
|
||||
ByteBuffer bb = new ByteBuffer(data);
|
||||
Monster monster = Monster.GetRootAsMonster(bb);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Now you can access the data from the `Monster monster`:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
|
||||
short hp = monster.Hp;
|
||||
Vec3 pos = monster.Pos;
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
C# code naming follows standard C# style with PascalCasing identifiers,
|
||||
e.g. `GetRootAsMyRootType`. Also, values (except vectors and unions) are
|
||||
available as properties instead of parameterless accessor methods.
|
||||
The performance-enhancing methods to which you can pass an already created
|
||||
object are prefixed with `Get`, e.g.:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
|
||||
// property
|
||||
var pos = monster.Pos;
|
||||
|
||||
// method filling a preconstructed object
|
||||
var preconstructedPos = new Vec3();
|
||||
monster.GetPos(preconstructedPos);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
## Storing dictionaries in a FlatBuffer
|
||||
|
||||
FlatBuffers doesn't support dictionaries natively, but there is support to
|
||||
emulate their behavior with vectors and binary search, which means you
|
||||
can have fast lookups directly from a FlatBuffer without having to unpack
|
||||
your data into a `Dictionary` or similar.
|
||||
|
||||
To use it:
|
||||
- Designate one of the fields in a table as the "key" field. You do this
|
||||
by setting the `key` attribute on this field, e.g.
|
||||
`name:string (key)`.
|
||||
You may only have one key field, and it must be of string or scalar type.
|
||||
- Write out tables of this type as usual, collect their offsets in an
|
||||
array.
|
||||
- Instead of calling standard generated method,
|
||||
e.g.: `Monster.createTestarrayoftablesVector`,
|
||||
call `CreateSortedVectorOfMonster` in C#
|
||||
which will first sort all offsets such that the tables they refer to
|
||||
are sorted by the key field, then serialize it.
|
||||
- Now when you're accessing the FlatBuffer, you can use
|
||||
the `ByKey` accessor to access elements of the vector, e.g.:
|
||||
`monster.TestarrayoftablesByKey("Frodo")` in C#,
|
||||
which returns an object of the corresponding table type,
|
||||
or `null` if not found.
|
||||
`ByKey` performs a binary search, so should have a similar
|
||||
speed to `Dictionary`, though may be faster because of better caching.
|
||||
`ByKey` only works if the vector has been sorted, it will
|
||||
likely not find elements if it hasn't been sorted.
|
||||
|
||||
## Text parsing
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
from C#, though you could use the C++ parser through native call
|
||||
interfaces available to each language. Please see the
|
||||
C++ documentation for more on text parsing.
|
||||
|
||||
## Object based API
|
||||
|
||||
FlatBuffers is all about memory efficiency, which is why its base API is written
|
||||
around using as little as possible of it. This does make the API clumsier
|
||||
(requiring pre-order construction of all data, and making mutation harder).
|
||||
|
||||
For times when efficiency is less important a more convenient object based API
|
||||
can be used (through `--gen-object-api`) that is able to unpack & pack a
|
||||
FlatBuffer into objects and standard `System.Collections.Generic` containers,
|
||||
allowing for convenient construction, access and mutation.
|
||||
|
||||
To use:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
|
||||
// Deserialize from buffer into object.
|
||||
MonsterT monsterobj = GetMonster(flatbuffer).UnPack();
|
||||
|
||||
// Update object directly like a C# class instance.
|
||||
Console.WriteLine(monsterobj.Name);
|
||||
monsterobj.Name = "Bob"; // Change the name.
|
||||
|
||||
// Serialize into new flatbuffer.
|
||||
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
|
||||
fbb.Finish(Monster.Pack(fbb, monsterobj).Value);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
### Json Serialization
|
||||
|
||||
An additional feature of the object API is the ability to allow you to
|
||||
serialize & deserialize a JSON text.
|
||||
To use Json Serialization, add `--cs-gen-json-serializer` option to `flatc` and
|
||||
add `Newtonsoft.Json` nuget package to csproj.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
|
||||
// Deserialize MonsterT from json
|
||||
string jsonText = File.ReadAllText(@"Resources/monsterdata_test.json");
|
||||
MonsterT mon = MonsterT.DeserializeFromJson(jsonText);
|
||||
|
||||
// Serialize MonsterT to json
|
||||
string jsonText2 = mon.SerializeToJson();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
* Limitation
|
||||
* `hash` attribute currentry not supported.
|
||||
* NuGet package Dependency
|
||||
* [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json)
|
||||
|
||||
## Conditional compilation symbols
|
||||
|
||||
There are three conditional compilation symbols that have an impact on
|
||||
performance/features of the C# `ByteBuffer` implementation.
|
||||
|
||||
* `UNSAFE_BYTEBUFFER`
|
||||
|
||||
This will use unsafe code to manipulate the underlying byte array. This can
|
||||
yield a reasonable performance increase.
|
||||
|
||||
* `BYTEBUFFER_NO_BOUNDS_CHECK`
|
||||
|
||||
This will disable the bounds check asserts to the byte array. This can yield a
|
||||
small performance gain in normal code.
|
||||
|
||||
* `ENABLE_SPAN_T`
|
||||
|
||||
This will enable reading and writing blocks of memory with a `Span<T>` instead
|
||||
of just `T[]`. You can also enable writing directly to shared memory or other
|
||||
types of memory by providing a custom implementation of `ByteBufferAllocator`.
|
||||
`ENABLE_SPAN_T` also requires `UNSAFE_BYTEBUFFER` to be defined, or .NET
|
||||
Standard 2.1.
|
||||
|
||||
Using `UNSAFE_BYTEBUFFER` and `BYTEBUFFER_NO_BOUNDS_CHECK` together can yield a
|
||||
performance gain of ~15% for some operations, however doing so is potentially
|
||||
dangerous. Do so at your own risk!
|
||||
|
||||
<br>
|
||||
@@ -4,7 +4,7 @@ FlatBuffers {#flatbuffers_index}
|
||||
# Overview {#flatbuffers_overview}
|
||||
|
||||
[FlatBuffers](@ref flatbuffers_overview) is an efficient cross platform
|
||||
serialization library for C++, C#, C, Go, Java, JavaScript, Lobster, Lua, TypeScript, PHP, Python, and Rust.
|
||||
serialization library for C++, C#, C, Go, Java, Kotlin, JavaScript, Lobster, Lua, TypeScript, PHP, Python, Rust and Swift.
|
||||
It was originally created at Google for game development and other
|
||||
performance-critical applications.
|
||||
|
||||
@@ -51,7 +51,7 @@ under the Apache license, v2 (see LICENSE.txt).
|
||||
needed (faster and more memory efficient than other JSON
|
||||
parsers).
|
||||
|
||||
Java and Go code supports object-reuse. C# has efficient struct based
|
||||
Java, Kotlin and Go code supports object-reuse. C# has efficient struct based
|
||||
accessors.
|
||||
|
||||
- **Cross platform code with no dependencies** - C++ code will work
|
||||
@@ -64,8 +64,8 @@ Protocol Buffers is indeed relatively similar to FlatBuffers,
|
||||
with the primary difference being that FlatBuffers does not need a parsing/
|
||||
unpacking step to a secondary representation before you can
|
||||
access data, often coupled with per-object memory allocation. The code
|
||||
is an order of magnitude bigger, too. Protocol Buffers has neither optional
|
||||
text import/export nor schema language features like unions.
|
||||
is an order of magnitude bigger, too. Protocol Buffers has no optional
|
||||
text import/export.
|
||||
|
||||
### But all the cool kids use JSON!
|
||||
|
||||
@@ -108,7 +108,7 @@ sections provide a more in-depth usage guide.
|
||||
present for every object instance.
|
||||
|
||||
- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or
|
||||
Java/C#/Go/Python.. classes) with helper classes to access and construct
|
||||
Java/Kotlin/C#/Go/Python.. classes) with helper classes to access and construct
|
||||
serialized data. This header (say `mydata_generated.h`) only depends on
|
||||
`flatbuffers.h`, which defines the core functionality.
|
||||
|
||||
@@ -130,8 +130,12 @@ sections provide a more in-depth usage guide.
|
||||
- How to [write a schema](@ref flatbuffers_guide_writing_schema).
|
||||
- How to [use the generated C++ code](@ref flatbuffers_guide_use_cpp) in your
|
||||
own programs.
|
||||
- How to [use the generated Java/C# code](@ref flatbuffers_guide_use_java_c-sharp)
|
||||
- How to [use the generated Java code](@ref flatbuffers_guide_use_java)
|
||||
in your own programs.
|
||||
- How to [use the generated C# code](@ref flatbuffers_guide_use_c-sharp)
|
||||
in your own programs.
|
||||
- How to [use the generated Kotlin code](@ref flatbuffers_guide_use_kotlin)
|
||||
in your own programs.
|
||||
- How to [use the generated Go code](@ref flatbuffers_guide_use_go) in your
|
||||
own programs.
|
||||
- How to [use the generated Lua code](@ref flatbuffers_guide_use_lua) in your
|
||||
@@ -146,6 +150,8 @@ sections provide a more in-depth usage guide.
|
||||
own programs.
|
||||
- How to [use the generated Rust code](@ref flatbuffers_guide_use_rust) in your
|
||||
own programs.
|
||||
- How to [use the generated Swift code](@ref flatbuffers_guide_use_swift) in your
|
||||
own programs.
|
||||
- [Support matrix](@ref flatbuffers_support) for platforms/languages/features.
|
||||
- Some [benchmarks](@ref flatbuffers_benchmarks) showing the advantage of
|
||||
using FlatBuffers.
|
||||
@@ -161,6 +167,7 @@ sections provide a more in-depth usage guide.
|
||||
- [GitHub repository](http://github.com/google/flatbuffers)
|
||||
- [Landing page](http://google.github.io/flatbuffers)
|
||||
- [FlatBuffers Google Group](https://groups.google.com/forum/#!forum/flatbuffers)
|
||||
- [Discord](https://discord.gg/6qgKs3R) and [Gitter](https://gitter.im/lobster_programming_language/community) chat.
|
||||
- [FlatBuffers Issues Tracker](http://github.com/google/flatbuffers/issues)
|
||||
- Independent implementations & tools:
|
||||
- [FlatCC](https://github.com/dvidelabs/flatcc) Alternative FlatBuffers
|
||||
@@ -176,3 +183,6 @@ sections provide a more in-depth usage guide.
|
||||
- [FlatBuffers in Android](http://frogermcs.github.io/flatbuffers-in-android-introdution/)
|
||||
- [Parsing JSON to FlatBuffers in Java](http://frogermcs.github.io/json-parsing-with-flatbuffers-in-android/)
|
||||
- [FlatBuffers in Unity](http://exiin.com/blog/flatbuffers-for-unity-sample-code/)
|
||||
- [FlexBuffers C#](https://github.com/mzaks/FlexBuffers-CSharp) and
|
||||
[article](https://medium.com/@icex33/flexbuffers-for-unity3d-4d1ab5c53fbe?)
|
||||
on its use.
|
||||
|
||||
@@ -29,9 +29,7 @@ FlexBuffers is still slower than regular FlatBuffers though, so we recommend to
|
||||
only use it if you need it.
|
||||
|
||||
|
||||
# Usage
|
||||
|
||||
This is for C++, other languages may follow.
|
||||
# Usage in C++
|
||||
|
||||
Include the header `flexbuffers.h`, which in turn depends on `flatbuffers.h`
|
||||
and `util.h`.
|
||||
@@ -98,10 +96,10 @@ allows a single type, and uses a bit less memory.
|
||||
`IndirectFloat` is an interesting feature that allows you to store values
|
||||
by offset rather than inline. Though that doesn't make any visible change
|
||||
to the user, the consequence is that large values (especially doubles or
|
||||
64 bit ints) that occur more than once can be shared. Another use case is
|
||||
inside of vectors, where the largest element makes up the size of all elements
|
||||
(e.g. a single double forces all elements to 64bit), so storing a lot of small
|
||||
integers together with a double is more efficient if the double is indirect.
|
||||
64 bit ints) that occur more than once can be shared (see ReuseValue).
|
||||
Another use case is inside of vectors, where the largest element makes
|
||||
up the size of all elements (e.g. a single double forces all elements to
|
||||
64bit), so storing a lot of small integers together with a double is more efficient if the double is indirect.
|
||||
|
||||
Accessing it:
|
||||
|
||||
@@ -122,6 +120,46 @@ map["unknown"].IsNull(); // true
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
# Usage in Java
|
||||
|
||||
Java implementation follows the C++ one, closely.
|
||||
|
||||
For creating the equivalent of the same JSON `{ vec: [ -100, "Fred", 4.0 ], foo: 100 }`,
|
||||
one could use the following code:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java}
|
||||
FlexBuffersBuilder builder = new FlexBuffersBuilder(ByteBuffer.allocate(512),
|
||||
FlexBuffersBuilder.BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
|
||||
int smap = builder.startMap();
|
||||
int svec = builder.startVector();
|
||||
builder.putInt(-100);
|
||||
builder.putString("Fred");
|
||||
builder.putFloat(4.0);
|
||||
builder.endVector("vec", svec, false, false);
|
||||
builder.putInt("foo", 100);
|
||||
builder.endMap(null, smap);
|
||||
ByteBuffer bb = builder.finish();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Similarly, to read the data, just:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java}
|
||||
FlexBuffers.Map map = FlexBuffers.getRoot(bb).asMap();
|
||||
map.size(); // 2
|
||||
FlexBuffers.Vector vec = map.get("vec").asVector();
|
||||
vec.size(); // 3
|
||||
vec.get(0).asLong(); // -100;
|
||||
vec.get(1).asString(); // "Fred";
|
||||
vec.get(1).asLong(); // 0 (Number parsing failed).
|
||||
vec.get(2).asFloat(); // 4.0
|
||||
vec.get(2).asString().isEmpty(); // true (Wrong Type).
|
||||
vec.get(2).asString(); // "" (This still works though).
|
||||
vec.get(2).toString(); // "4.0" (Or have it converted).
|
||||
map.get("foo").asUInt(); // 100
|
||||
map.get("unknown").isNull(); // true
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
# Binary encoding
|
||||
|
||||
A description of how FlexBuffers are encoded is in the
|
||||
|
||||
@@ -35,9 +35,9 @@ enumval\_decl = ident [ `=` integer\_constant ]
|
||||
|
||||
metadata = [ `(` commasep( ident [ `:` single\_value ] ) `)` ]
|
||||
|
||||
scalar = integer\_constant | float\_constant
|
||||
scalar = boolean\_constant | integer\_constant | float\_constant
|
||||
|
||||
object = { commasep( ident `:` value ) }
|
||||
object = `{` commasep( ident `:` value ) `}`
|
||||
|
||||
single\_value = scalar | string\_constant
|
||||
|
||||
@@ -69,6 +69,6 @@ hex\_float\_constant = `[-+]?0[xX](([.][:xdigit:]+)|([:xdigit:]+[.][:xdigit:]*)|
|
||||
|
||||
special\_float\_constant = `[-+]?(nan|inf|infinity)`
|
||||
|
||||
float\_constant = decimal\_float\_constant | hexadecimal\_float\_constant | special\_float\_constant
|
||||
float\_constant = dec\_float\_constant | hex\_float\_constant | special\_float\_constant
|
||||
|
||||
boolean\_constant = `(true|false)` | (integer\_constant ? `true` : `false`)
|
||||
boolean\_constant = `true` | `false`
|
||||
|
||||
@@ -15,6 +15,12 @@ all commonly used CPUs today. FlatBuffers will also work on big-endian
|
||||
machines, but will be slightly slower because of additional
|
||||
byte-swap intrinsics.
|
||||
|
||||
It is assumed that the following conditions are met, to ensure
|
||||
cross-platform interoperability:
|
||||
- The binary `IEEE-754` format is used for floating-point numbers.
|
||||
- The `two's complemented` representation is used for signed integers.
|
||||
- The endianness is the same for floating-point numbers as for integers.
|
||||
|
||||
On purpose, the format leaves a lot of details about where exactly
|
||||
things live in memory undefined, e.g. fields in a table can have any
|
||||
order, and objects to some extent can be stored in many orders. This is
|
||||
@@ -56,7 +62,7 @@ when needed. Unsigned means they can only point in one direction, which
|
||||
typically is forward (towards a higher memory location). Any backwards
|
||||
offsets will be explicitly marked as such.
|
||||
|
||||
The format starts with an `uoffset_t` to the root object in the buffer.
|
||||
The format starts with an `uoffset_t` to the root table in the buffer.
|
||||
|
||||
We have two kinds of objects, structs and tables.
|
||||
|
||||
@@ -82,7 +88,9 @@ They start with an `soffset_t` to a vtable. This is a signed version of
|
||||
This offset is substracted (not added) from the object start to arrive at
|
||||
the vtable start. This offset is followed by all the
|
||||
fields as aligned scalars (or offsets). Unlike structs, not all fields
|
||||
need to be present. There is no set order and layout.
|
||||
need to be present. There is no set order and layout. A table may contain
|
||||
field offsets that point to the same value if the user explicitly
|
||||
serializes the same offset twice.
|
||||
|
||||
To be able to access fields regardless of these uncertainties, we go
|
||||
through a vtable of offsets. Vtables are shared between any objects that
|
||||
@@ -105,13 +113,21 @@ is 0, that means the field is not present in this object, and the
|
||||
default value is return. Otherwise, the entry is used as offset to the
|
||||
field to be read.
|
||||
|
||||
### Unions
|
||||
|
||||
Unions are encoded as the combination of two fields: an enum representing the
|
||||
union choice and the offset to the actual element. FlatBuffers reserves the
|
||||
enumeration constant `NONE` (encoded as 0) to mean that the union field is not
|
||||
set.
|
||||
|
||||
### Strings and Vectors
|
||||
|
||||
Strings are simply a vector of bytes, and are always
|
||||
null-terminated. Vectors are stored as contiguous aligned scalar
|
||||
elements prefixed by a 32bit element count (not including any
|
||||
null termination). Neither is stored inline in their parent, but are referred to
|
||||
by offset.
|
||||
by offset. A vector may consist of more than one offset pointing to the same
|
||||
value if the user explicitly serializes the same offset twice.
|
||||
|
||||
### Construction
|
||||
|
||||
@@ -340,6 +356,9 @@ Since this is an untyped vector `SL_VECTOR`, it is followed by 3 type
|
||||
bytes (one per element of the vector), which are always following the vector,
|
||||
and are always a uint8_t even if the vector is made up of bigger scalars.
|
||||
|
||||
A vector may include more than one offset pointing to the same value if the
|
||||
user explicitly serializes the same offset twice.
|
||||
|
||||
### Types
|
||||
|
||||
A type byte is made up of 2 components (see flexbuffers.h for exact values):
|
||||
|
||||
@@ -16,43 +16,31 @@ documentation to build `flatc` and should be familiar with
|
||||
|
||||
## FlatBuffers JavaScript library code location
|
||||
|
||||
The code for the FlatBuffers JavaScript library can be found at
|
||||
`flatbuffers/js`. You can browse the library code on the [FlatBuffers
|
||||
GitHub page](https://github.com/google/flatbuffers/tree/master/js).
|
||||
The generated code for the FlatBuffers JavaScript library can be found at
|
||||
https://www.npmjs.com/package/flatbuffers. To use it from sources:
|
||||
|
||||
## Testing the FlatBuffers JavaScript library
|
||||
|
||||
The code to test the JavaScript library can be found at `flatbuffers/tests`.
|
||||
The test code itself is located in [JavaScriptTest.js](https://github.com/
|
||||
google/flatbuffers/blob/master/tests/JavaScriptTest.js).
|
||||
|
||||
To run the tests, use the [JavaScriptTest.sh](https://github.com/google/
|
||||
flatbuffers/blob/master/tests/JavaScriptTest.sh) shell script.
|
||||
|
||||
*Note: The JavaScript test file requires [Node.js](https://nodejs.org/en/).*
|
||||
1. Run `npm run compile` from the main folder to generate JS files from TS.
|
||||
1. In your project, install it as a normal dependency, using the flatbuffers
|
||||
folder as the source.
|
||||
|
||||
## Using the FlatBuffers JavaScript libary
|
||||
|
||||
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
|
||||
example of how to use FlatBuffers in JavaScript.*
|
||||
example of how to use FlatBuffers.*
|
||||
|
||||
FlatBuffers supports both reading and writing FlatBuffers in JavaScript.
|
||||
Due to the complexity related with large amounts of JS flavors and module types,
|
||||
native JS support has been replaced in 2.0 by transpilation from TypeScript.
|
||||
|
||||
To use FlatBuffers in your own code, first generate JavaScript classes from your
|
||||
schema with the `--js` option to `flatc`. Then you can include both FlatBuffers
|
||||
and the generated code to read or write a FlatBuffer.
|
||||
Please look at [TypeScript usage](@ref flatbuffers_guide_use_typescript) and
|
||||
transpile your sources to desired JS flavor. The minimal steps to get up and
|
||||
running with JS are:
|
||||
|
||||
For example, here is how you would read a FlatBuffer binary file in Javascript:
|
||||
First, include the library and generated code. Then read the file into an
|
||||
`Uint8Array`. Make a `flatbuffers.ByteBuffer` out of the `Uint8Array`, and pass
|
||||
the ByteBuffer to the `getRootAsMonster` function.
|
||||
|
||||
*Note: Both JavaScript module loaders (e.g. Node.js) and browser-based
|
||||
HTML/JavaScript code segments are shown below in the following snippet:*
|
||||
1. Generate TS files from `*.fbs` by using the `--ts` option.
|
||||
1. Transpile resulting TS files to desired JS flavor using `tsc` (see
|
||||
https://www.typescriptlang.org/download for installation instructions).
|
||||
|
||||
~~~{.js}
|
||||
// Note: These require functions are specific to JavaScript module loaders
|
||||
// (namely, Node.js). See below for a browser-based example.
|
||||
// Note: These require functions are an example - use your desired module flavor.
|
||||
var fs = require('fs');
|
||||
|
||||
var flatbuffers = require('../flatbuffers').flatbuffers;
|
||||
@@ -65,7 +53,7 @@ HTML/JavaScript code segments are shown below in the following snippet:*
|
||||
|
||||
//--------------------------------------------------------------------------//
|
||||
|
||||
// Note: This code is specific to browser-based HTML/JavaScript. See above
|
||||
// Note: This code is an example of browser-based HTML/JavaScript. See above
|
||||
// for the code using JavaScript module loaders (e.g. Node.js).
|
||||
<script src="../js/flatbuffers.js"></script>
|
||||
<script src="monster_generated.js"></script>
|
||||
|
||||
@@ -1,41 +1,30 @@
|
||||
Use in Java/C# {#flatbuffers_guide_use_java_c-sharp}
|
||||
Use in Java {#flatbuffers_guide_use_java}
|
||||
==============
|
||||
|
||||
## Before you get started
|
||||
|
||||
Before diving into the FlatBuffers usage in Java or C#, it should be noted that
|
||||
Before diving into the FlatBuffers usage in Java, it should be noted that
|
||||
the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
|
||||
general FlatBuffers usage in all of the supported languages (including both Java
|
||||
and C#). This page is designed to cover the nuances of FlatBuffers usage,
|
||||
specific to Java and C#.
|
||||
general FlatBuffers usage in all of the supported languages (including Java).
|
||||
This page is designed to cover the nuances of FlatBuffers usage,
|
||||
specific to Java.
|
||||
|
||||
You should also have read the [Building](@ref flatbuffers_guide_building)
|
||||
documentation to build `flatc` and should be familiar with
|
||||
[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
|
||||
[Writing a schema](@ref flatbuffers_guide_writing_schema).
|
||||
|
||||
## FlatBuffers Java and C-sharp code location
|
||||
|
||||
#### Java
|
||||
## FlatBuffers Java code location
|
||||
|
||||
The code for the FlatBuffers Java library can be found at
|
||||
`flatbuffers/java/com/google/flatbuffers`. You can browse the library on the
|
||||
[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
|
||||
java/com/google/flatbuffers).
|
||||
|
||||
#### C-sharp
|
||||
|
||||
The code for the FlatBuffers C# library can be found at
|
||||
`flatbuffers/net/FlatBuffers`. You can browse the library on the
|
||||
[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/net/
|
||||
FlatBuffers).
|
||||
|
||||
## Testing the FlatBuffers Java and C-sharp libraries
|
||||
## Testing the FlatBuffers Java libraries
|
||||
|
||||
The code to test the libraries can be found at `flatbuffers/tests`.
|
||||
|
||||
#### Java
|
||||
|
||||
The test code for Java is located in [JavaTest.java](https://github.com/google
|
||||
/flatbuffers/blob/master/tests/JavaTest.java).
|
||||
|
||||
@@ -47,31 +36,15 @@ system.
|
||||
*Note: These scripts require that [Java](https://www.oracle.com/java/index.html)
|
||||
is installed.*
|
||||
|
||||
#### C-sharp
|
||||
|
||||
The test code for C# is located in the [FlatBuffers.Test](https://github.com/
|
||||
google/flatbuffers/tree/master/tests/FlatBuffers.Test) subfolder. To run the
|
||||
tests, open `FlatBuffers.Test.csproj` in [Visual Studio](
|
||||
https://www.visualstudio.com), and compile/run the project.
|
||||
|
||||
Optionally, you can run this using [Mono](http://www.mono-project.com/) instead.
|
||||
Once you have installed `Mono`, you can run the tests from the command line
|
||||
by running the following commands from inside the `FlatBuffers.Test` folder:
|
||||
|
||||
~~~{.sh}
|
||||
mcs *.cs ../MyGame/Example/*.cs ../../net/FlatBuffers/*.cs
|
||||
mono Assert.exe
|
||||
~~~
|
||||
|
||||
## Using the FlatBuffers Java (and C#) library
|
||||
## Using the FlatBuffers Java library
|
||||
|
||||
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
|
||||
example of how to use FlatBuffers in Java or C#.*
|
||||
example of how to use FlatBuffers in Java.*
|
||||
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in Java and C#.
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in Java.
|
||||
|
||||
To use FlatBuffers in your own code, first generate Java classes from your
|
||||
schema with the `--java` option to `flatc`. (Or for C# with `--csharp`).
|
||||
schema with the `--java` option to `flatc`.
|
||||
Then you can include both FlatBuffers and the generated code to read
|
||||
or write a FlatBuffer.
|
||||
|
||||
@@ -80,11 +53,6 @@ First, import the library and generated code. Then, you read a FlatBuffer binary
|
||||
file into a `byte[]`. You then turn the `byte[]` into a `ByteBuffer`, which you
|
||||
pass to the `getRootAsMyRootType` function:
|
||||
|
||||
*Note: The code here is written from the perspective of Java. Code for both
|
||||
languages is both generated and used in nearly the exact same way, with only
|
||||
minor differences. These differences are
|
||||
[explained in a section below](#differences_in_c-sharp).*
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java}
|
||||
import MyGame.Example.*;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
@@ -107,30 +75,6 @@ Now you can access the data from the `Monster monster`:
|
||||
Vec3 pos = monster.pos();
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
<a name="differences_in_c-sharp">
|
||||
#### Differences in C-sharp
|
||||
</a>
|
||||
|
||||
C# code works almost identically to Java, with only a few minor differences.
|
||||
You can see an example of C# code in
|
||||
`tests/FlatBuffers.Test/FlatBuffersExampleTests.cs` or
|
||||
`samples/SampleBinary.cs`.
|
||||
|
||||
First of all, naming follows standard C# style with `PascalCasing` identifiers,
|
||||
e.g. `GetRootAsMyRootType`. Also, values (except vectors and unions) are
|
||||
available as properties instead of parameterless accessor methods as in Java.
|
||||
The performance-enhancing methods to which you can pass an already created
|
||||
object are prefixed with `Get`, e.g.:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
|
||||
// property
|
||||
var pos = monster.Pos;
|
||||
|
||||
// method filling a preconstructed object
|
||||
var preconstructedPos = new Vec3();
|
||||
monster.GetPos(preconstructedPos);
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
## Storing dictionaries in a FlatBuffer
|
||||
|
||||
FlatBuffers doesn't support dictionaries natively, but there is support to
|
||||
@@ -147,14 +91,12 @@ To use it:
|
||||
array.
|
||||
- Instead of calling standard generated method,
|
||||
e.g.: `Monster.createTestarrayoftablesVector`,
|
||||
call `CreateSortedVectorOfMonster` in C# or
|
||||
`createSortedVectorOfTables` (from the `FlatBufferBuilder` object) in Java,
|
||||
call `createSortedVectorOfTables` (from the `FlatBufferBuilder` object).
|
||||
which will first sort all offsets such that the tables they refer to
|
||||
are sorted by the key field, then serialize it.
|
||||
- Now when you're accessing the FlatBuffer, you can use
|
||||
the `ByKey` accessor to access elements of the vector, e.g.:
|
||||
`monster.testarrayoftablesByKey("Frodo")` in Java or
|
||||
`monster.TestarrayoftablesByKey("Frodo")` in C#,
|
||||
`monster.testarrayoftablesByKey("Frodo")`.
|
||||
which returns an object of the corresponding table type,
|
||||
or `null` if not found.
|
||||
`ByKey` performs a binary search, so should have a similar
|
||||
@@ -165,7 +107,7 @@ To use it:
|
||||
## Text parsing
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
from Java or C#, though you could use the C++ parser through native call
|
||||
from Java, though you could use the C++ parser through native call
|
||||
interfaces available to each language. Please see the
|
||||
C++ documentation for more on text parsing.
|
||||
|
||||
84
docs/source/KotlinUsage.md
Normal file
@@ -0,0 +1,84 @@
|
||||
Use in Kotlin {#flatbuffers_guide_use_kotlin}
|
||||
==============
|
||||
|
||||
## Before you get started
|
||||
|
||||
Before diving into the FlatBuffers usage in Kotlin, it should be noted that
|
||||
the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
|
||||
general FlatBuffers usage in all of the supported languages (including K).
|
||||
|
||||
This page is designed to cover the nuances of FlatBuffers usage, specific to Kotlin.
|
||||
|
||||
You should also have read the [Building](@ref flatbuffers_guide_building)
|
||||
documentation to build `flatc` and should be familiar with
|
||||
[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
|
||||
[Writing a schema](@ref flatbuffers_guide_writing_schema).
|
||||
|
||||
## Kotlin and FlatBuffers Java code location
|
||||
|
||||
Code generated for Kotlin currently uses the flatbuffers java runtime library. That means that Kotlin generated code can only have Java virtual machine as target architecture (which includes Android). Kotlin Native and Kotlin.js are currently not supported.
|
||||
|
||||
The code for the FlatBuffers Java library can be found at
|
||||
`flatbuffers/java/com/google/flatbuffers`. You can browse the library on the
|
||||
[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
|
||||
java/com/google/flatbuffers).
|
||||
|
||||
## Testing FlatBuffers Kotlin
|
||||
|
||||
The test code for Java is located in [KotlinTest.java](https://github.com/google
|
||||
/flatbuffers/blob/master/tests/KotlinTest.kt).
|
||||
|
||||
To run the tests, use [KotlinTest.sh](https://github.com/google/
|
||||
flatbuffers/blob/master/tests/KotlinTest.sh) shell script.
|
||||
|
||||
*Note: These scripts require that [Kotlin](https://kotlinlang.org/) is installed.*
|
||||
|
||||
## Using the FlatBuffers Kotlin library
|
||||
|
||||
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
|
||||
example of how to use FlatBuffers in Kotlin.*
|
||||
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in Kotlin.
|
||||
|
||||
To use FlatBuffers in your own code, first generate Java classes from your
|
||||
schema with the `--kotlin` option to `flatc`.
|
||||
Then you can include both FlatBuffers and the generated code to read
|
||||
or write a FlatBuffer.
|
||||
|
||||
For example, here is how you would read a FlatBuffer binary file in Kotlin:
|
||||
First, import the library and generated code. Then, you read a FlatBuffer binary
|
||||
file into a `ByteArray`. You then turn the `ByteArray` into a `ByteBuffer`, which you
|
||||
pass to the `getRootAsMyRootType` function:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
|
||||
import MyGame.Example.*
|
||||
import com.google.flatbuffers.FlatBufferBuilder
|
||||
|
||||
// This snippet ignores exceptions for brevity.
|
||||
val data = RandomAccessFile(File("monsterdata_test.mon"), "r").use {
|
||||
val temp = ByteArray(it.length().toInt())
|
||||
it.readFully(temp)
|
||||
temp
|
||||
}
|
||||
|
||||
val bb = ByteBuffer.wrap(data)
|
||||
val monster = Monster.getRootAsMonster(bb)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Now you can access the data from the `Monster monster`:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.kt}
|
||||
val hp = monster.hp
|
||||
val pos = monster.pos!!;
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
## Differences between Kotlin and Java code
|
||||
|
||||
Kotlin generated code was designed to be as close as possible to the java counterpart, as for now, we only support kotlin on java virtual machine. So the differences in implementation and usage are basically the ones introduced by the Kotlin language itself. You can find more in-depth information [here](https://kotlinlang.org/docs/reference/comparison-to-java.html).
|
||||
|
||||
The most obvious ones are:
|
||||
|
||||
* Fields as accessed as Kotlin [properties](https://kotlinlang.org/docs/reference/properties.html)
|
||||
* Static methods are accessed in [companion object](https://kotlinlang.org/docs/reference/classes.html#companion-objects)
|
||||
@@ -29,8 +29,8 @@ flatbuffers/blob/master/tests/luatest.lua).
|
||||
To run the tests, use the [LuaTest.sh](https://github.com/google/flatbuffers/
|
||||
blob/master/tests/LuaTest.sh) shell script.
|
||||
|
||||
*Note: This script requires [Lua 5.3](https://www.lua.org/) to be
|
||||
installed.*
|
||||
*Note: This script requires [Lua 5.3](https://www.lua.org/) and
|
||||
[LuaJIT](http://luajit.org/) to be installed.*
|
||||
|
||||
## Using the FlatBuffers Lua library
|
||||
|
||||
|
||||
@@ -39,16 +39,16 @@ first:
|
||||
|
||||
### Tables
|
||||
|
||||
Tables are the main way of defining objects in FlatBuffers, and consist
|
||||
of a name (here `Monster`) and a list of fields. Each field has a name,
|
||||
a type, and optionally a default value (if omitted, it defaults to `0` /
|
||||
`NULL`).
|
||||
Tables are the main way of defining objects in FlatBuffers, and consist of a
|
||||
name (here `Monster`) and a list of fields. Each field has a name, a type, and
|
||||
optionally a default value. If the default value is not specified in the schema,
|
||||
it will be `0` for scalar types, or `null` for other types. Some languages
|
||||
support setting a scalar's default to `null`. This makes the scalar optional.
|
||||
|
||||
Each field is optional: It does not have to appear in the wire
|
||||
representation, and you can choose to omit fields for each individual
|
||||
object. As a result, you have the flexibility to add fields without fear of
|
||||
bloating your data. This design is also FlatBuffer's mechanism for forward
|
||||
and backwards compatibility. Note that:
|
||||
Fields do not have to appear in the wire representation, and you can choose
|
||||
to omit fields when constructing an object. You have the flexibility to add
|
||||
fields without fear of bloating your data. This design is also FlatBuffer's
|
||||
mechanism for forward and backwards compatibility. Note that:
|
||||
|
||||
- You can add new fields in the schema ONLY at the end of a table
|
||||
definition. Older data will still
|
||||
@@ -114,24 +114,56 @@ of same-size data where a `reinterpret_cast` would give you a desirable result,
|
||||
e.g. you could change a `uint` to an `int` if no values in current data use the
|
||||
high bit yet.
|
||||
|
||||
### (Default) Values
|
||||
### Arrays
|
||||
|
||||
Values are a sequence of digits. Values may be optionally followed by a decimal
|
||||
point (`.`) and more digits, for float constants, or optionally prefixed by
|
||||
a `-`. Floats may also be in scientific notation; optionally ending with an `e`
|
||||
or `E`, followed by a `+` or `-` and more digits.
|
||||
Arrays are a convenience short-hand for a fixed-length collection of elements.
|
||||
Arrays can be used to replace the following schema:
|
||||
|
||||
struct Vec3 {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
}
|
||||
|
||||
with the following schema:
|
||||
|
||||
struct Vec3 {
|
||||
v:[float:3];
|
||||
}
|
||||
|
||||
Both representations are binary equivalent.
|
||||
|
||||
Arrays are currently only supported in a `struct`.
|
||||
|
||||
### Default, Optional and Required Values
|
||||
|
||||
There are three, mutually exclusive, reactions to the non-presence of a table's
|
||||
field in the binary data:
|
||||
|
||||
1. Default valued fields will return the default value (as defined in the schema).
|
||||
2. Optional valued fields will return some form of `null` depending on the
|
||||
local language. (In a sense, `null` is the default value).
|
||||
3. Required fields will cause an error. Flatbuffer verifiers would
|
||||
consider the whole buffer invalid. See the `required` tag below.
|
||||
|
||||
When writing a schema, values are a sequence of digits. Values may be optionally
|
||||
followed by a decimal point (`.`) and more digits, for float constants, or
|
||||
optionally prefixed by a `-`. Floats may also be in scientific notation;
|
||||
optionally ending with an `e` or `E`, followed by a `+` or `-` and more digits.
|
||||
Values can also be the keyword `null`.
|
||||
|
||||
Only scalar values can have defaults, non-scalar (string/vector/table) fields
|
||||
default to `NULL` when not present.
|
||||
default to `null` when not present.
|
||||
|
||||
You generally do not want to change default values after they're initially
|
||||
defined. Fields that have the default value are not actually stored in the
|
||||
serialized data (see also Gotchas below) but are generated in code,
|
||||
so when you change the default, you'd
|
||||
now get a different value than from code generated from an older version of
|
||||
the schema. There are situations, however, where this may be
|
||||
desirable, especially if you can ensure a simultaneous rebuild of
|
||||
all code.
|
||||
serialized data (see also Gotchas below). Values explicitly written by code
|
||||
generated by the old schema old version, if they happen to be the default, will
|
||||
be read as a different value by code generated with the new schema. This is
|
||||
slightly less bad when converting an optional scalar into a default valued
|
||||
scalar since non-presence would not be overloaded with a previous default value.
|
||||
There are situations, however, where this may be desirable, especially if you
|
||||
can ensure a simultaneous rebuild of all code.
|
||||
|
||||
### Enums
|
||||
|
||||
@@ -179,9 +211,11 @@ If you have a need to distinguish between different FlatBuffers in a more
|
||||
open-ended way, for example for use as files, see the file identification
|
||||
feature below.
|
||||
|
||||
There is an experimental support only in C++ for a vector of unions
|
||||
(and types). In the example IDL file above, use [Any] to add a
|
||||
vector of Any to Monster table.
|
||||
There is an experimental support only in C++ for a vector of unions (and
|
||||
types). In the example IDL file above, use [Any] to add a vector of Any to
|
||||
Monster table. There is also experimental support for other types besides
|
||||
tables in unions, in particular structs and strings. There's no direct support
|
||||
for scalars in unions, but they can be wrapped in a struct at no space cost.
|
||||
|
||||
### Namespaces
|
||||
|
||||
@@ -304,18 +338,16 @@ Current understood attributes:
|
||||
deprecate a field that was previous required, old code may fail to validate
|
||||
new data (when using the optional verifier).
|
||||
- `required` (on a non-scalar table field): this field must always be set.
|
||||
By default, all fields are optional, i.e. may be left out. This is
|
||||
By default, fields do not need to be present in the binary. This is
|
||||
desirable, as it helps with forwards/backwards compatibility, and
|
||||
flexibility of data structures. It is also a burden on the reading code,
|
||||
since for non-scalar fields it requires you to check against NULL and
|
||||
take appropriate action. By specifying this field, you force code that
|
||||
constructs FlatBuffers to ensure this field is initialized, so the reading
|
||||
code may access it directly, without checking for NULL. If the constructing
|
||||
code does not initialize this field, they will get an assert, and also
|
||||
the verifier will fail on buffers that have missing required fields. Note
|
||||
that if you add this attribute to an existing field, this will only be
|
||||
valid if existing data always contains this field / existing code always
|
||||
writes this field.
|
||||
flexibility of data structures. By specifying this attribute, you make non-
|
||||
presence in an error for both reader and writer. The reading code may access
|
||||
the field directly, without checking for null. If the constructing code does
|
||||
not initialize this field, they will get an assert, and also the verifier
|
||||
will fail on buffers that have missing required fields. Both adding and
|
||||
removing this attribute may be forwards/backwards incompatible as readers
|
||||
will be unable read old or new data, respectively, unless the data happens to
|
||||
always have the field set.
|
||||
- `force_align: size` (on a struct): force the alignment of this struct
|
||||
to be something higher than what it is naturally aligned to. Causes
|
||||
these structs to be aligned to that amount inside a buffer, IF that
|
||||
@@ -324,6 +356,9 @@ Current understood attributes:
|
||||
Note: currently not guaranteed to have an effect when used with
|
||||
`--object-api`, since that may allocate objects at alignments less than
|
||||
what you specify with `force_align`.
|
||||
- `force_align: size` (on a vector): force the alignment of this vector to be
|
||||
something different than what the element size would normally dictate.
|
||||
Note: Now only work for generated C++ code.
|
||||
- `bit_flags` (on an unsigned enum): the values of this field indicate bits,
|
||||
meaning that any unsigned value N specified in the schema will end up
|
||||
representing 1<<N, or if you don't specify values at all, you'll get
|
||||
@@ -418,14 +453,19 @@ numerical literals:
|
||||
For example: `[0x123, +0x45, -0x67]` are equal to `[291, 69, -103]` decimals.
|
||||
- The format of float-point numbers is fully compatible with C/C++ format.
|
||||
If a modern C++ compiler is used the parser accepts hexadecimal and special
|
||||
float-point literals as well:
|
||||
floating-point literals as well:
|
||||
`[-1.0, 2., .3e0, 3.e4, 0x21.34p-5, -inf, nan]`.
|
||||
The exponent suffix of hexadecimal float-point number is mandatory.
|
||||
|
||||
Extended float-point support was tested with:
|
||||
The following conventions for floating-point numbers are used:
|
||||
- The exponent suffix of hexadecimal floating-point number is mandatory.
|
||||
- Parsed `NaN` converted to unsigned IEEE-754 `quiet-NaN` value.
|
||||
|
||||
Extended floating-point support was tested with:
|
||||
- x64 Windows: `MSVC2015` and higher.
|
||||
- x64 Linux: `LLVM 6.0`, `GCC 4.9` and higher.
|
||||
|
||||
For details, see [Use in C++](@ref flatbuffers_guide_use_cpp) section.
|
||||
|
||||
- For compatibility with a JSON lint tool all numeric literals of scalar
|
||||
fields can be wrapped to quoted string:
|
||||
`"1", "2.0", "0x48A", "0x0C.0Ep-1", "-inf", "true"`.
|
||||
@@ -457,7 +497,7 @@ of related data structures is a union. Unions do have a cost however,
|
||||
so an alternative to a union is to have a single table that has
|
||||
all the fields of all the data structures you are trying to
|
||||
represent, if they are relatively similar / share many fields.
|
||||
Again, this is efficient because optional fields are cheap.
|
||||
Again, this is efficient because non-present fields are cheap.
|
||||
|
||||
FlatBuffers supports the full range of integer sizes, so try to pick
|
||||
the smallest size needed, rather than defaulting to int/long.
|
||||
@@ -537,7 +577,7 @@ with the new schema now cannot read nor write `a` anymore (any existing code
|
||||
that tries to do so will result in compile errors), but can still read
|
||||
old data (they will ignore the field).
|
||||
|
||||
table { c:int a:int; b:int; }
|
||||
table { c:int; a:int; b:int; }
|
||||
|
||||
This is NOT ok, as this makes the schemas incompatible. Old code reading newer
|
||||
data will interpret `c` as if it was `a`, and new code reading old data
|
||||
@@ -582,22 +622,25 @@ Most serialization formats (e.g. JSON or Protocol Buffers) make it very
|
||||
explicit in the format whether a field is present in an object or not,
|
||||
allowing you to use this as "extra" information.
|
||||
|
||||
In FlatBuffers, this also holds for everything except scalar values.
|
||||
FlatBuffers will not write fields that are equal to their default value,
|
||||
sometimes resulting in significant space savings. However, this also means we
|
||||
cannot disambiguate the meaning of non-presence as "written default value" or
|
||||
"not written at all". This only applies to scalar fields since only they support
|
||||
default values. Unless otherwise specified, their default is 0.
|
||||
|
||||
FlatBuffers by default will not write fields that are equal to the default
|
||||
value (for scalars), sometimes resulting in a significant space savings.
|
||||
|
||||
However, this also means testing whether a field is "present" is somewhat
|
||||
meaningless, since it does not tell you if the field was actually written by
|
||||
calling `add_field` style calls, unless you're only interested in this
|
||||
information for non-default values.
|
||||
If you care about the presence of scalars, most languages support "optional
|
||||
scalars." You can set `null` as the default value in the schema. `null` is a
|
||||
value that's outside of all types, so we will always write if `add_field` is
|
||||
called. The generated field accessor should use the local language's canonical
|
||||
optional type.
|
||||
|
||||
Some `FlatBufferBuilder` implementations have an option called `force_defaults`
|
||||
that circumvents this behavior, and writes fields even if they are equal to
|
||||
the default. You can then use `IsFieldPresent` to query this.
|
||||
that circumvents this "not writing defaults" behavior you can then use
|
||||
`IsFieldPresent` to query presence.
|
||||
|
||||
Another option that works in all languages is to wrap a scalar field in a
|
||||
struct. This way it will return null if it is not present. The cool thing
|
||||
is that structs don't take up any more space than the scalar they represent.
|
||||
struct. This way it will return null if it is not present. This will be slightly
|
||||
less ergonomic but structs don't take up any more space than the scalar they
|
||||
represent.
|
||||
|
||||
[Interface Definition Language]: https://en.wikipedia.org/wiki/Interface_description_language
|
||||
|
||||
@@ -18,29 +18,40 @@ In general:
|
||||
|
||||
NOTE: this table is a start, it needs to be extended.
|
||||
|
||||
Feature | C++ | Java | C# | Go | Python | JS | TS | C | PHP | Dart | Lobster | Rust
|
||||
------------------------------ | ------ | ------ | ------ | ------ | ------ | --------- | --------- | ------ | --- | ------- | ------- | ----
|
||||
Codegen for all basic features | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | WiP | Yes | Yes | Yes
|
||||
JSON parsing | Yes | No | No | No | No | No | No | Yes | No | No | Yes | No
|
||||
Simple mutation | Yes | Yes | Yes | Yes | No | No | No | No | No | No | No | No
|
||||
Reflection | Yes | No | No | No | No | No | No | Basic | No | No | No | No
|
||||
Buffer verifier | Yes | No | No | No | No | No | No | Yes | No | No | No | No
|
||||
Testing: basic | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | Yes | Yes | Yes
|
||||
Testing: fuzz | Yes | No | No | Yes | Yes | No | No | No | ? | No | No | Yes
|
||||
Performance: | Superb | Great | Great | Great | Ok | ? | ? | Superb | ? | ? | Great | Superb
|
||||
Platform: Windows | VS2010 | Yes | Yes | ? | ? | ? | Yes | VS2010 | ? | Yes | Yes | Yes
|
||||
Platform: Linux | GCC282 | Yes | ? | Yes | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes
|
||||
Platform: OS X | Xcode4 | ? | ? | ? | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes
|
||||
Platform: Android | NDK10d | Yes | ? | ? | ? | ? | ? | ? | ? | Flutter | Yes | ?
|
||||
Platform: iOS | ? | ? | ? | ? | ? | ? | ? | ? | ? | Flutter | Yes | ?
|
||||
Engine: Unity | ? | ? | Yes | ? | ? | ? | ? | ? | ? | ? | No | ?
|
||||
Primary authors (github) | aard* | aard* | ev*/js*| rw | rw | evanw/ev* | kr* | mik* | ch* | dnfield | aard* | rw
|
||||
Feature | C++ | Java | C# | Go | Python | JS | TS | C | PHP | Dart | Lobster | Rust | Swift
|
||||
------------------------------ | ------ | ----- | -------- | ----- | ------ | ----- | --- | ------ | --- | ------- | ------- | ------ | ------
|
||||
Codegen for all basic features | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | WiP | Yes | Yes | Yes | Yes
|
||||
JSON parsing | Yes | No | No | No | No | No | No | Yes | No | No | Yes | No | No
|
||||
Simple mutation | Yes | Yes | Yes | Yes | No | No | No | No | No | No | No | No | Yes
|
||||
Reflection | Yes | No | No | No | No | No | No | Basic | No | No | No | No | No
|
||||
Buffer verifier | Yes | No | No | No | No | No | No | Yes | No | No | No | No | No
|
||||
Native Object API | Yes | No | Yes | Yes | Yes | Yes | Yes | No | No | No | No | No | No
|
||||
Optional Scalars | Yes | Yes | Yes | No | No | Yes | Yes | Yes | No | No | Yes | Yes | Yes
|
||||
Flexbuffers | Yes | Yes | ? | ? | ? | ? | ? | ? | ? | ? | ? | Yes | ?
|
||||
Testing: basic | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | Yes | Yes | Yes | Yes
|
||||
Testing: fuzz | Yes | No | No | Yes | Yes | No | No | No | ? | No | No | Yes | No
|
||||
Performance: | Superb | Great | Great | Great | Ok | ? | ? | Superb | ? | ? | Great | Superb | Great
|
||||
Platform: Windows | VS2010 | Yes | Yes | ? | ? | ? | Yes | VS2010 | ? | Yes | Yes | Yes | No
|
||||
Platform: Linux | GCC282 | Yes | ? | Yes | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes | Yes
|
||||
Platform: OS X | Xcode4 | ? | ? | ? | Yes | ? | Yes | Yes | ? | Yes | Yes | Yes | Yes
|
||||
Platform: Android | NDK10d | Yes | ? | ? | ? | ? | ? | ? | ? | Flutter | Yes | ? | No
|
||||
Platform: iOS | ? | ? | ? | ? | ? | ? | ? | ? | ? | Flutter | Yes | ? | Yes
|
||||
Engine: Unity | ? | ? | Yes | ? | ? | ? | ? | ? | ? | ? | No | ? | No
|
||||
Primary authors (github) | aard | aard | ev/js/df | rw | rw | ew/ev | kr | mik | ch | df | aard | rw/cn | mi/mz
|
||||
|
||||
* aard = aardappel (previously: gwvo)
|
||||
* ev = evolutional
|
||||
* js = jonsimantov
|
||||
* mik = mikkelfj
|
||||
* ch = chobie
|
||||
* kr = krojew
|
||||
Above | Github username
|
||||
----- | -----------------------------
|
||||
aard | aardappel (previously: gwvo)
|
||||
ch | chobie
|
||||
cn | caspern
|
||||
df | dnfield
|
||||
ev | evolutional
|
||||
ew | evanw
|
||||
js | jonsimantov
|
||||
kr | krojew
|
||||
mi | mustiikhalil
|
||||
mik | mikkelfj
|
||||
mz | mzaks
|
||||
rw | rw
|
||||
|
||||
<br>
|
||||
|
||||
94
docs/source/SwiftUsage.md
Normal file
@@ -0,0 +1,94 @@
|
||||
Use in Swift {#flatbuffers_guide_use_swift}
|
||||
=========
|
||||
|
||||
## Before you get started
|
||||
|
||||
Before diving into the FlatBuffers usage in Swift, it should be noted that
|
||||
the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide
|
||||
to general FlatBuffers usage in all of the supported languages (including Swift).
|
||||
This page is designed to cover the nuances of FlatBuffers usage, specific to
|
||||
Swift.
|
||||
|
||||
You should also have read the [Building](@ref flatbuffers_guide_building)
|
||||
documentation to build `flatc` and should be familiar with
|
||||
[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
|
||||
[Writing a schema](@ref flatbuffers_guide_writing_schema).
|
||||
|
||||
## FlatBuffers Swift library code location
|
||||
|
||||
The code for the FlatBuffers Swift library can be found at
|
||||
`flatbuffers/swift`. You can browse the library code on the [FlatBuffers
|
||||
GitHub page](https://github.com/google/flatbuffers/tree/master/swift).
|
||||
|
||||
## Testing the FlatBuffers Swift library
|
||||
|
||||
The code to test the Swift library can be found at `flatbuffers/Flatbuffers.Test.Swift`.
|
||||
The test code itself is located in [Flatbuffers.Test.Swift](https://github.com/google/flatbuffers/blob/master/tests/FlatBuffers.Test.Swift).
|
||||
|
||||
To run the tests, use the [SwiftTest.sh](https://github.com/google/flatbuffers/blob/master/tests/FlatBuffers.Test.Swift/SwiftTest.sh) shell script.
|
||||
|
||||
*Note: The shell script requires [Swift](https://swift.org) to
|
||||
be installed.*
|
||||
|
||||
## Using the FlatBuffers Swift library
|
||||
|
||||
*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
|
||||
example of how to use FlatBuffers in Swift.*
|
||||
|
||||
FlatBuffers supports reading and writing binary FlatBuffers in Swift.
|
||||
|
||||
To use FlatBuffers in your own code, first generate Swift structs from your
|
||||
schema with the `--swift` option to `flatc`. Then include FlatBuffers using `SPM` in
|
||||
by adding the path to `FlatBuffers/swift` into it. The generated code should also be
|
||||
added to xcode or the path of the package you will be using. Note: sometimes xcode cant
|
||||
and wont see the generated files, so it's better that you copy them to xcode.
|
||||
|
||||
For example, here is how you would read a FlatBuffer binary file in Swift: First,
|
||||
include the library and copy thegenerated code. Then read a FlatBuffer binary file or
|
||||
a data object from the server, which you can pass into the `GetRootAsMonster` function.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
|
||||
import FlatBuffers
|
||||
|
||||
typealias Monster1 = MyGame.Sample.Monster
|
||||
typealias Vec3 = MyGame.Sample.Vec3
|
||||
|
||||
let path = FileManager.default.currentDirectoryPath
|
||||
let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon")
|
||||
guard let data = try? Data(contentsOf: url) else { return }
|
||||
|
||||
let monster = Monster.getRootAsMonster(bb: ByteBuffer(data: data))
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Now you can access values like this:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
|
||||
let hp = monster.hp
|
||||
let pos = monster.pos // uses native swift structs
|
||||
let pos = monster.mutablePos // uses flatbuffers structs
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
In some cases it's necessary to modify values in an existing FlatBuffer in place (without creating a copy). For this reason, scalar fields of a Flatbuffer table or struct can be mutated.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
|
||||
let monster = Monster.getRootAsMonster(bb: ByteBuffer(data: data))
|
||||
|
||||
if !monster.mutate(hp: 10) {
|
||||
fatalError("couldn't mutate")
|
||||
}
|
||||
// mutate a struct field using flatbuffers struct
|
||||
// DONT use monster.pos to mutate since swift copy on write
|
||||
// will not mutate the value in the buffer
|
||||
let vec = monster.mutablePos.mutate(z: 4)
|
||||
|
||||
// This mutation will fail because the mana field is not available in
|
||||
// the buffer. It should be set when creating the buffer.
|
||||
if !monster.mutate(mana: 20) {
|
||||
fatalError("couldn't mutate")
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The term `mutate` is used instead of `set` to indicate that this is a special use case. All mutate functions return a boolean value which is false if the field we're trying to mutate is not available in the buffer.
|
||||
|
||||
<br>
|
||||
@@ -17,7 +17,7 @@ documentation to build `flatc` and should be familiar with
|
||||
## FlatBuffers TypeScript library code location
|
||||
|
||||
The code for the FlatBuffers TypeScript library can be found at
|
||||
`flatbuffers/js` with typings available at `@types/flatbuffers`.
|
||||
https://www.npmjs.com/package/flatbuffers.
|
||||
|
||||
## Testing the FlatBuffers TypeScript library
|
||||
|
||||
@@ -43,7 +43,7 @@ First, include the library and generated code. Then read the file into an
|
||||
the ByteBuffer to the `getRootAsMonster` function.
|
||||
|
||||
~~~{.ts}
|
||||
// note: import flabuffers with your desired import method
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
|
||||
import { MyGame } from './monster_generated';
|
||||
|
||||
@@ -60,6 +60,36 @@ Now you can access values like this:
|
||||
let pos = monster.pos();
|
||||
~~~
|
||||
|
||||
## Object based API
|
||||
|
||||
FlatBuffers is all about memory efficiency, which is why its base API is written
|
||||
around using as little as possible of it. This does make the API clumsier
|
||||
(requiring pre-order construction of all data, and making mutation harder).
|
||||
|
||||
For times when efficiency is less important a more convenient object based API
|
||||
can be used (through `--gen-object-api`) that is able to unpack & pack a
|
||||
FlatBuffer into objects and standard TS types.
|
||||
|
||||
To use:
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.ts}
|
||||
// Autogenerated class from table Monster.
|
||||
let monsterobj = new MonsterT();
|
||||
|
||||
// Deserialize from buffer into object.
|
||||
Monster.getRootAsMonster(flatbuffer).unpackTo(monsterobj);
|
||||
// or
|
||||
let monsterobj = Monster.getRootAsMonster(flatbuffer).unpack();
|
||||
|
||||
// Update object directly like a regular TS class instance.
|
||||
console.log(monsterobj.name);
|
||||
monsterobj.name = "Bob";
|
||||
|
||||
// Serialize into new flatbuffer.
|
||||
let fbb = new flatbuffers.Builder(1);
|
||||
Monster.finishMonsterBuffer(fbb, monsterobj.pack(fbb));
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
## Text parsing FlatBuffers in TypeScript
|
||||
|
||||
There currently is no support for parsing text (Schema's and JSON) directly
|
||||
|
||||
@@ -753,7 +753,9 @@ INPUT = "FlatBuffers.md" \
|
||||
"CUsage.md" \
|
||||
"DartUsage.md" \
|
||||
"GoUsage.md" \
|
||||
"JavaCsharpUsage.md" \
|
||||
"JavaUsage.md" \
|
||||
"CsharpUsage.md" \
|
||||
"SwiftUsage.md" \
|
||||
"JavaScriptUsage.md" \
|
||||
"TypeScriptUsage.md" \
|
||||
"PHPUsage.md" \
|
||||
@@ -778,7 +780,7 @@ INPUT = "FlatBuffers.md" \
|
||||
"../../php/FlatbufferBuilder.php" \
|
||||
"../../net/FlatBuffers/FlatBufferBuilder.cs" \
|
||||
"../../include/flatbuffers/flatbuffers.h" \
|
||||
"../../go/builder.go"
|
||||
"../../go/builder.go" \
|
||||
"../../rust/flatbuffers/src/builder.rs"
|
||||
|
||||
# This tag can be used to specify the character encoding of the source files
|
||||
|
||||
@@ -29,8 +29,10 @@
|
||||
title="Use in C"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_go"
|
||||
title="Use in Go"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_java_c-sharp"
|
||||
title="Use in Java/C#"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_java"
|
||||
title="Use in Java"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_c-sharp"
|
||||
title="Use in C#"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_javascript"
|
||||
title="Use in JavaScript"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_typescript"
|
||||
@@ -47,8 +49,10 @@
|
||||
title="Use in Lobster"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_rust"
|
||||
title="Use in Rust"/>
|
||||
<tab type="user" url="@ref flatbuffers_guide_use_swift"
|
||||
title="Use in Swift"/>
|
||||
<tab type="user" url="@ref flexbuffers"
|
||||
title="Schema-less version"/>
|
||||
title="FlexBuffers (Schema-less version)"/>
|
||||
<tab type="usergroup" url="" title="gRPC">
|
||||
<tab type="user" url="@ref flatbuffers_grpc_guide_use_cpp"
|
||||
title="Use in C++"/>
|
||||
|
||||