Merge commit '0f6aab9da6fe982218a01f4a5b896e65fcced437' as 'third_party/flatbuffers'
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
||||
# Changelog
|
||||
|
||||
## 23.5.26
|
||||
|
||||
- omit type annotationes for local variables (#7067, #7069, #7070)
|
||||
- remove BSD 3-clause license (#7073)
|
||||
- correctly parse lists of enums (#7157)
|
||||
- align naming conventions for generated code (#7187)
|
||||
- add `putBool` to fix errors when serializing structs with booleans (#7359)
|
||||
- fix handling of +/-inf defaults in codegen (#7588)
|
||||
- fix import issues in generated code (#7621)
|
||||
- Fix incorrect storage of floats as ints in some cases (#7703)
|
||||
- add final modifiers to the library implementation (#7943)
|
||||
|
||||
## 2.0.5
|
||||
|
||||
- switch to null safety (#6696)
|
||||
- add Object APIs (pack/unpack) (#6682, #6723, #6846)
|
||||
- add custom builder buffer allocator support (#6711)
|
||||
- add `Builder.size()` - finished buffer size (#6403)
|
||||
- make `writeString()` argument non-nullable (#6737)
|
||||
- make tables fixed size (expect the number of fields when creating) (#6735)
|
||||
- make table deduplication optional (param `deduplicateTables`) (#6734)
|
||||
- change `Builder.reset()` to reuse an existing buffer (#6661)
|
||||
- change table building to assert() instead of exceptions (#6754)
|
||||
- optimize `writeString()` for ASCII (param `asciiOptimization`) (#6736)
|
||||
- change `StringReader` to make ASCII optimization optional (param `asciiOptimization`) (#6758)
|
||||
- change `[byte]` and `[ubyte]` representation to `dart:typed_data` `Int8List` and `Uint8List` (#6839)
|
||||
- rename `lowFinish()` to `buffer` getter (#6712)
|
||||
- fix `Builder._writeString()` - always write trailing zero byte (#6390)
|
||||
- fix `Builder.reset()` - clear vTables (#6386)
|
||||
- make sure added padding is zeroed, same as in C++ (#6716)
|
||||
- many performance improvements (#6755)
|
||||
|
||||
## 1.9.2
|
||||
|
||||
- Ensure `_writeString` adds enough padding to null terminate strings.
|
||||
|
||||
## 1.9.1
|
||||
|
||||
- Changed constant identifiers to be compatible with Dart 2.x
|
||||
- No longer supports Dart 1.x
|
||||
|
||||
## 1.9.0
|
||||
|
||||
- Initial release, supports Dart 1.x and many dev versions of Dart 2.x
|
||||
Vendored
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2014 Google Inc.
|
||||
|
||||
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.
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
# FlatBuffers for Dart
|
||||
|
||||
This package is used to read and write [FlatBuffers](https://google.github.io/flatbuffers/).
|
||||
|
||||
Most consumers will want to use the [`flatc` - FlatBuffer compiler](https://github.com/google/flatbuffers) binary for your platform.
|
||||
You can download the flatc version matching your dart package version from [GitHub releases](https://github.com/google/flatbuffers/releases).
|
||||
|
||||
The FlatBuffer compiler `flatc` reads a FlatBuffers IDL schema and generates Dart code.
|
||||
The generated classes can be used to read or write binary data/files that are interoperable with
|
||||
other languages and platforms supported by FlatBuffers, as illustrated in the `example.dart` in the
|
||||
examples folder.
|
||||
|
||||
For more details and documentation, head over to the official site and read the
|
||||
[Tutorial](https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html) and how to
|
||||
[use FlatBuffers in Dart](https://google.github.io/flatbuffers/flatbuffers_guide_use_dart.html).
|
||||
@@ -0,0 +1 @@
|
||||
include: package:lints/recommended.yaml
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright 2018 Dan Field. 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 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
import './monster_my_game.sample_generated.dart' as my_game;
|
||||
|
||||
// Example how to use FlatBuffers to create and read binary buffers.
|
||||
|
||||
void main() {
|
||||
builderTest();
|
||||
objectBuilderTest();
|
||||
}
|
||||
|
||||
void builderTest() {
|
||||
final builder = fb.Builder(initialSize: 1024);
|
||||
final int? weaponOneName = builder.writeString("Sword");
|
||||
final int weaponOneDamage = 3;
|
||||
|
||||
final int? weaponTwoName = builder.writeString("Axe");
|
||||
final int weaponTwoDamage = 5;
|
||||
|
||||
final swordBuilder = my_game.WeaponBuilder(builder)
|
||||
..begin()
|
||||
..addNameOffset(weaponOneName)
|
||||
..addDamage(weaponOneDamage);
|
||||
final int sword = swordBuilder.finish();
|
||||
|
||||
final axeBuilder = my_game.WeaponBuilder(builder)
|
||||
..begin()
|
||||
..addNameOffset(weaponTwoName)
|
||||
..addDamage(weaponTwoDamage);
|
||||
final int axe = axeBuilder.finish();
|
||||
|
||||
// Serialize a name for our monster, called "Orc".
|
||||
final int? name = builder.writeString('Orc');
|
||||
|
||||
// Create a list representing the inventory of the Orc. Each number
|
||||
// could correspond to an item that can be claimed after he is slain.
|
||||
final List<int> treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
final inventory = builder.writeListUint8(treasure);
|
||||
final weapons = builder.writeList([sword, axe]);
|
||||
|
||||
// Struct builders are very easy to reuse.
|
||||
final vec3Builder = my_game.Vec3Builder(builder);
|
||||
|
||||
vec3Builder.finish(4.0, 5.0, 6.0);
|
||||
vec3Builder.finish(1.0, 2.0, 3.0);
|
||||
// Set his hit points to 300 and his mana to 150.
|
||||
final int hp = 300;
|
||||
final int mana = 150;
|
||||
|
||||
final monster = my_game.MonsterBuilder(builder)
|
||||
..begin()
|
||||
..addNameOffset(name)
|
||||
..addInventoryOffset(inventory)
|
||||
..addWeaponsOffset(weapons)
|
||||
..addEquippedType(my_game.EquipmentTypeId.Weapon)
|
||||
..addEquippedOffset(axe)
|
||||
..addHp(hp)
|
||||
..addMana(mana)
|
||||
..addPos(vec3Builder.finish(1.0, 2.0, 3.0))
|
||||
..addColor(my_game.Color.Red);
|
||||
|
||||
final int monsteroff = monster.finish();
|
||||
builder.finish(monsteroff);
|
||||
if (verify(builder.buffer)) {
|
||||
print(
|
||||
"The FlatBuffer was successfully created with a builder and verified!");
|
||||
}
|
||||
}
|
||||
|
||||
void objectBuilderTest() {
|
||||
// Create the builder here so we can use it for both weapons and equipped
|
||||
// the actual data will only be written to the buffer once.
|
||||
var axe = my_game.WeaponObjectBuilder(name: 'Axe', damage: 5);
|
||||
|
||||
var monsterBuilder = my_game.MonsterObjectBuilder(
|
||||
pos: my_game.Vec3ObjectBuilder(x: 1.0, y: 2.0, z: 3.0),
|
||||
mana: 150,
|
||||
hp: 300,
|
||||
name: 'Orc',
|
||||
inventory: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
color: my_game.Color.Red,
|
||||
weapons: [my_game.WeaponObjectBuilder(name: 'Sword', damage: 3), axe],
|
||||
equippedType: my_game.EquipmentTypeId.Weapon,
|
||||
equipped: axe,
|
||||
);
|
||||
|
||||
var buffer = monsterBuilder.toBytes();
|
||||
|
||||
// We now have a FlatBuffer we can store on disk or send over a network.
|
||||
|
||||
// ** file/network code goes here :) **
|
||||
|
||||
// Instead, we're going to access it right away (as if we just received it).
|
||||
if (verify(buffer)) {
|
||||
print(
|
||||
"The FlatBuffer was successfully created with an object builder and verified!");
|
||||
}
|
||||
}
|
||||
|
||||
bool verify(List<int> buffer) {
|
||||
// Get access to the root:
|
||||
var monster = my_game.Monster(buffer);
|
||||
|
||||
// Get and test some scalar types from the FlatBuffer.
|
||||
assert(monster.hp == 80);
|
||||
assert(monster.mana == 150); // default
|
||||
assert(monster.name == "MyMonster");
|
||||
|
||||
// Get and test a field of the FlatBuffer's `struct`.
|
||||
var pos = monster.pos!;
|
||||
assert(pos.z == 3.0);
|
||||
|
||||
// Get a test an element from the `inventory` FlatBuffer's `vector`.
|
||||
var inv = monster.inventory!;
|
||||
assert(inv.length == 10);
|
||||
assert(inv[9] == 9);
|
||||
|
||||
// Get and test the `weapons` FlatBuffers's `vector`.
|
||||
var expectedWeaponNames = ["Sword", "Axe"];
|
||||
var expectedWeaponDamages = [3, 5];
|
||||
var weps = monster.weapons!;
|
||||
for (int i = 0; i < weps.length; i++) {
|
||||
assert(weps[i].name == expectedWeaponNames[i]);
|
||||
assert(weps[i].damage == expectedWeaponDamages[i]);
|
||||
}
|
||||
|
||||
// Get and test the `Equipment` union (`equipped` field).
|
||||
assert(monster.equippedType!.value == my_game.EquipmentTypeId.Weapon.value);
|
||||
assert(monster.equippedType == my_game.EquipmentTypeId.Weapon);
|
||||
|
||||
assert(monster.equipped is my_game.Weapon);
|
||||
var equipped = monster.equipped as my_game.Weapon;
|
||||
assert(equipped.name == "Axe");
|
||||
assert(equipped.damage == 5);
|
||||
|
||||
print(monster);
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,423 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
library my_game.sample;
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
|
||||
class Color {
|
||||
final int value;
|
||||
const Color._(this.value);
|
||||
|
||||
factory Color.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum Color');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Color? _createOrNull(int? value) =>
|
||||
value == null ? null : Color.fromValue(value);
|
||||
|
||||
static const int minValue = 0;
|
||||
static const int maxValue = 2;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const Color Red = Color._(0);
|
||||
static const Color Green = Color._(1);
|
||||
static const Color Blue = Color._(2);
|
||||
static const Map<int, Color> values = {
|
||||
0: Red,
|
||||
1: Green,
|
||||
2: Blue};
|
||||
|
||||
static const fb.Reader<Color> reader = _ColorReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Color{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _ColorReader extends fb.Reader<Color> {
|
||||
const _ColorReader();
|
||||
|
||||
@override
|
||||
int get size => 1;
|
||||
|
||||
@override
|
||||
Color read(fb.BufferContext bc, int offset) =>
|
||||
Color.fromValue(const fb.Int8Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class EquipmentTypeId {
|
||||
final int value;
|
||||
const EquipmentTypeId._(this.value);
|
||||
|
||||
factory EquipmentTypeId.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum EquipmentTypeId');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static EquipmentTypeId? _createOrNull(int? value) =>
|
||||
value == null ? null : EquipmentTypeId.fromValue(value);
|
||||
|
||||
static const int minValue = 0;
|
||||
static const int maxValue = 1;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const EquipmentTypeId NONE = EquipmentTypeId._(0);
|
||||
static const EquipmentTypeId Weapon = EquipmentTypeId._(1);
|
||||
static const Map<int, EquipmentTypeId> values = {
|
||||
0: NONE,
|
||||
1: Weapon};
|
||||
|
||||
static const fb.Reader<EquipmentTypeId> reader = _EquipmentTypeIdReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'EquipmentTypeId{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _EquipmentTypeIdReader extends fb.Reader<EquipmentTypeId> {
|
||||
const _EquipmentTypeIdReader();
|
||||
|
||||
@override
|
||||
int get size => 1;
|
||||
|
||||
@override
|
||||
EquipmentTypeId read(fb.BufferContext bc, int offset) =>
|
||||
EquipmentTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class Vec3 {
|
||||
Vec3._(this._bc, this._bcOffset);
|
||||
|
||||
static const fb.Reader<Vec3> reader = _Vec3Reader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
double get x => const fb.Float32Reader().read(_bc, _bcOffset + 0);
|
||||
double get y => const fb.Float32Reader().read(_bc, _bcOffset + 4);
|
||||
double get z => const fb.Float32Reader().read(_bc, _bcOffset + 8);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Vec3{x: $x, y: $y, z: $z}';
|
||||
}
|
||||
}
|
||||
|
||||
class _Vec3Reader extends fb.StructReader<Vec3> {
|
||||
const _Vec3Reader();
|
||||
|
||||
@override
|
||||
int get size => 12;
|
||||
|
||||
@override
|
||||
Vec3 createObject(fb.BufferContext bc, int offset) =>
|
||||
Vec3._(bc, offset);
|
||||
}
|
||||
|
||||
class Vec3Builder {
|
||||
Vec3Builder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
int finish(double x, double y, double z) {
|
||||
fbBuilder.putFloat32(z);
|
||||
fbBuilder.putFloat32(y);
|
||||
fbBuilder.putFloat32(x);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Vec3ObjectBuilder extends fb.ObjectBuilder {
|
||||
final double _x;
|
||||
final double _y;
|
||||
final double _z;
|
||||
|
||||
Vec3ObjectBuilder({
|
||||
required double x,
|
||||
required double y,
|
||||
required double z,
|
||||
})
|
||||
: _x = x,
|
||||
_y = y,
|
||||
_z = z;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.putFloat32(_z);
|
||||
fbBuilder.putFloat32(_y);
|
||||
fbBuilder.putFloat32(_x);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
class Monster {
|
||||
Monster._(this._bc, this._bcOffset);
|
||||
factory Monster(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<Monster> reader = _MonsterReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
Vec3? get pos => Vec3.reader.vTableGetNullable(_bc, _bcOffset, 4);
|
||||
int get mana => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 150);
|
||||
int get hp => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 100);
|
||||
String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 10);
|
||||
List<int>? get inventory => const fb.Uint8ListReader().vTableGetNullable(_bc, _bcOffset, 14);
|
||||
Color get color => Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 2));
|
||||
List<Weapon>? get weapons => const fb.ListReader<Weapon>(Weapon.reader).vTableGetNullable(_bc, _bcOffset, 18);
|
||||
EquipmentTypeId? get equippedType => EquipmentTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 20));
|
||||
dynamic get equipped {
|
||||
switch (equippedType?.value) {
|
||||
case 1: return Weapon.reader.vTableGetNullable(_bc, _bcOffset, 22);
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
List<Vec3>? get path => const fb.ListReader<Vec3>(Vec3.reader).vTableGetNullable(_bc, _bcOffset, 24);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, weapons: $weapons, equippedType: $equippedType, equipped: $equipped, path: $path}';
|
||||
}
|
||||
}
|
||||
|
||||
class _MonsterReader extends fb.TableReader<Monster> {
|
||||
const _MonsterReader();
|
||||
|
||||
@override
|
||||
Monster createObject(fb.BufferContext bc, int offset) =>
|
||||
Monster._(bc, offset);
|
||||
}
|
||||
|
||||
class MonsterBuilder {
|
||||
MonsterBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(10);
|
||||
}
|
||||
|
||||
int addPos(int offset) {
|
||||
fbBuilder.addStruct(0, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addMana(int? mana) {
|
||||
fbBuilder.addInt16(1, mana);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addHp(int? hp) {
|
||||
fbBuilder.addInt16(2, hp);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addNameOffset(int? offset) {
|
||||
fbBuilder.addOffset(3, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addInventoryOffset(int? offset) {
|
||||
fbBuilder.addOffset(5, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addColor(Color? color) {
|
||||
fbBuilder.addInt8(6, color?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addWeaponsOffset(int? offset) {
|
||||
fbBuilder.addOffset(7, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addEquippedType(EquipmentTypeId? equippedType) {
|
||||
fbBuilder.addUint8(8, equippedType?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addEquippedOffset(int? offset) {
|
||||
fbBuilder.addOffset(9, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addPathOffset(int? offset) {
|
||||
fbBuilder.addOffset(10, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
final Vec3ObjectBuilder? _pos;
|
||||
final int? _mana;
|
||||
final int? _hp;
|
||||
final String? _name;
|
||||
final List<int>? _inventory;
|
||||
final Color? _color;
|
||||
final List<WeaponObjectBuilder>? _weapons;
|
||||
final EquipmentTypeId? _equippedType;
|
||||
final dynamic _equipped;
|
||||
final List<Vec3ObjectBuilder>? _path;
|
||||
|
||||
MonsterObjectBuilder({
|
||||
Vec3ObjectBuilder? pos,
|
||||
int? mana,
|
||||
int? hp,
|
||||
String? name,
|
||||
List<int>? inventory,
|
||||
Color? color,
|
||||
List<WeaponObjectBuilder>? weapons,
|
||||
EquipmentTypeId? equippedType,
|
||||
dynamic equipped,
|
||||
List<Vec3ObjectBuilder>? path,
|
||||
})
|
||||
: _pos = pos,
|
||||
_mana = mana,
|
||||
_hp = hp,
|
||||
_name = name,
|
||||
_inventory = inventory,
|
||||
_color = color,
|
||||
_weapons = weapons,
|
||||
_equippedType = equippedType,
|
||||
_equipped = equipped,
|
||||
_path = path;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
final int? nameOffset = _name == null ? null
|
||||
: fbBuilder.writeString(_name!);
|
||||
final int? inventoryOffset = _inventory == null ? null
|
||||
: fbBuilder.writeListUint8(_inventory!);
|
||||
final int? weaponsOffset = _weapons == null ? null
|
||||
: fbBuilder.writeList(_weapons!.map((b) => b.getOrCreateOffset(fbBuilder)).toList());
|
||||
final int? equippedOffset = _equipped?.getOrCreateOffset(fbBuilder);
|
||||
final int? pathOffset = _path == null ? null
|
||||
: fbBuilder.writeListOfStructs(_path!);
|
||||
fbBuilder.startTable(10);
|
||||
if (_pos != null) {
|
||||
fbBuilder.addStruct(0, _pos!.finish(fbBuilder));
|
||||
}
|
||||
fbBuilder.addInt16(1, _mana);
|
||||
fbBuilder.addInt16(2, _hp);
|
||||
fbBuilder.addOffset(3, nameOffset);
|
||||
fbBuilder.addOffset(5, inventoryOffset);
|
||||
fbBuilder.addInt8(6, _color?.value);
|
||||
fbBuilder.addOffset(7, weaponsOffset);
|
||||
fbBuilder.addUint8(8, _equippedType?.value);
|
||||
fbBuilder.addOffset(9, equippedOffset);
|
||||
fbBuilder.addOffset(10, pathOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
class Weapon {
|
||||
Weapon._(this._bc, this._bcOffset);
|
||||
factory Weapon(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<Weapon> reader = _WeaponReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 4);
|
||||
int get damage => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 0);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Weapon{name: $name, damage: $damage}';
|
||||
}
|
||||
}
|
||||
|
||||
class _WeaponReader extends fb.TableReader<Weapon> {
|
||||
const _WeaponReader();
|
||||
|
||||
@override
|
||||
Weapon createObject(fb.BufferContext bc, int offset) =>
|
||||
Weapon._(bc, offset);
|
||||
}
|
||||
|
||||
class WeaponBuilder {
|
||||
WeaponBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(2);
|
||||
}
|
||||
|
||||
int addNameOffset(int? offset) {
|
||||
fbBuilder.addOffset(0, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addDamage(int? damage) {
|
||||
fbBuilder.addInt16(1, damage);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class WeaponObjectBuilder extends fb.ObjectBuilder {
|
||||
final String? _name;
|
||||
final int? _damage;
|
||||
|
||||
WeaponObjectBuilder({
|
||||
String? name,
|
||||
int? damage,
|
||||
})
|
||||
: _name = name,
|
||||
_damage = damage;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
final int? nameOffset = _name == null ? null
|
||||
: fbBuilder.writeString(_name!);
|
||||
fbBuilder.startTable(2);
|
||||
fbBuilder.addOffset(0, nameOffset);
|
||||
fbBuilder.addInt16(1, _damage);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
+1493
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
||||
export 'src/builder.dart';
|
||||
export 'src/reference.dart';
|
||||
+676
@@ -0,0 +1,676 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'types.dart';
|
||||
|
||||
/// The main builder class for creation of a FlexBuffer.
|
||||
class Builder {
|
||||
final ByteData _buffer;
|
||||
List<_StackValue> _stack = [];
|
||||
List<_StackPointer> _stackPointers = [];
|
||||
int _offset = 0;
|
||||
bool _finished = false;
|
||||
final Map<String, _StackValue> _stringCache = {};
|
||||
final Map<String, _StackValue> _keyCache = {};
|
||||
final Map<_KeysHash, _StackValue> _keyVectorCache = {};
|
||||
final Map<int, _StackValue> _indirectIntCache = {};
|
||||
final 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 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);
|
||||
|
||||
/// 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);
|
||||
_StackValue? 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.');
|
||||
}
|
||||
|
||||
late int 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 {
|
||||
late Object _value;
|
||||
int? _offset;
|
||||
final ValueType _type;
|
||||
final BitWidth _width;
|
||||
|
||||
_StackValue.withNull()
|
||||
: _type = ValueType.Null,
|
||||
_width = BitWidth.width8;
|
||||
|
||||
_StackValue.withInt(int value)
|
||||
: _type = ValueType.Int,
|
||||
_width = BitWidthUtil.width(value),
|
||||
_value = value;
|
||||
|
||||
_StackValue.withBool(bool value)
|
||||
: _type = ValueType.Bool,
|
||||
_width = BitWidth.width8,
|
||||
_value = value;
|
||||
|
||||
_StackValue.withDouble(double value)
|
||||
: _type = ValueType.Float,
|
||||
_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;
|
||||
final offset = _offset!;
|
||||
for (var i = 0; i < 4; i++) {
|
||||
final width = 1 << i;
|
||||
final bitWidth = BitWidthUtil.uwidth(size +
|
||||
BitWidthUtil.paddingSize(size, width) +
|
||||
index * width -
|
||||
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 as double, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
} else {
|
||||
final result = ByteData(8);
|
||||
result.setFloat64(0, _value as double, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
}
|
||||
} else {
|
||||
switch (width) {
|
||||
case BitWidth.width8:
|
||||
final result = ByteData(1);
|
||||
result.setInt8(0, _value as int);
|
||||
return result.buffer.asUint8List();
|
||||
case BitWidth.width16:
|
||||
final result = ByteData(2);
|
||||
result.setInt16(0, _value as int, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
case BitWidth.width32:
|
||||
final result = ByteData(4);
|
||||
result.setInt32(0, _value as int, Endian.little);
|
||||
return result.buffer.asUint8List();
|
||||
case BitWidth.width64:
|
||||
final result = ByteData(8);
|
||||
result.setInt64(0, _value as int, 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 as bool ? 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;
|
||||
}
|
||||
}
|
||||
+481
@@ -0,0 +1,481 @@
|
||||
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;
|
||||
final int _byteWidth;
|
||||
final ValueType _valueType;
|
||||
int? _length;
|
||||
|
||||
Reference._(
|
||||
this._buffer, this._offset, this._parentWidth, int packedType, this._path,
|
||||
[int? byteWidth, ValueType? valueType])
|
||||
: _byteWidth = byteWidth ?? 1 << (packedType & 3),
|
||||
_valueType = 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;
|
||||
int packedType = 0;
|
||||
int? byteWidth;
|
||||
ValueType? valueType;
|
||||
if (ValueTypeUtils.isTypedVector(_valueType)) {
|
||||
byteWidth = 1;
|
||||
valueType = ValueTypeUtils.typedVectorElementType(_valueType);
|
||||
} else if (ValueTypeUtils.isFixedTypedVector(_valueType)) {
|
||||
byteWidth = 1;
|
||||
valueType = ValueTypeUtils.fixedTypedVectorElementType(_valueType);
|
||||
} else {
|
||||
packedType = _buffer.getUint8(_indirect + length * _byteWidth + index);
|
||||
}
|
||||
return Reference._(
|
||||
_buffer,
|
||||
elementOffset,
|
||||
BitWidthUtil.fromByteWidth(_byteWidth),
|
||||
packedType,
|
||||
"$_path[$index]",
|
||||
byteWidth,
|
||||
valueType);
|
||||
}
|
||||
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 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) {
|
||||
// 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 sizeByteWidth = _byteWidth;
|
||||
var size = _readUInt(indirect - sizeByteWidth,
|
||||
BitWidthUtil.fromByteWidth(sizeByteWidth));
|
||||
while (_buffer.getInt8(indirect + size) != 0) {
|
||||
sizeByteWidth <<= 1;
|
||||
size = _readUInt(indirect - sizeByteWidth,
|
||||
BitWidthUtil.fromByteWidth(sizeByteWidth));
|
||||
}
|
||||
_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 indirectOffset, int byteWidth) {
|
||||
final keyOffset = indirectOffset + 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 = -1;
|
||||
|
||||
_VectorIterator(this._vector);
|
||||
|
||||
@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 = -1;
|
||||
|
||||
_MapKeyIterator(this._map);
|
||||
|
||||
@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 = -1;
|
||||
|
||||
_MapValueIterator(this._map);
|
||||
|
||||
@override
|
||||
Reference get current => _map._valueForIndex(index);
|
||||
|
||||
@override
|
||||
bool moveNext() {
|
||||
index++;
|
||||
return index < _map.length;
|
||||
}
|
||||
|
||||
@override
|
||||
Iterator<Reference> get iterator => this;
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
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 is int) {
|
||||
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 as double)
|
||||
? 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 as double)
|
||||
? 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);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# Copyright 2018 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.
|
||||
#
|
||||
# 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 dart >/dev/null 2>&1 || { echo >&2 "Require `dart` but it's not installed. Aborting."; exit 1; }
|
||||
|
||||
pushd ../tests
|
||||
./DartTest.sh
|
||||
popd
|
||||
|
||||
pushd ../samples
|
||||
./dart_sample.sh
|
||||
popd
|
||||
|
||||
dart pub publish
|
||||
|
||||
rm example/monster.fbs
|
||||
rm test/*.fbs
|
||||
rm -rf test/sub
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
name: flat_buffers
|
||||
version: 24.3.25
|
||||
description: FlatBuffers reading and writing library for Dart. Based on original work by Konstantin Scheglov and Paul Berry of the Dart SDK team.
|
||||
homepage: https://github.com/google/flatbuffers
|
||||
documentation: https://google.github.io/flatbuffers/index.html
|
||||
|
||||
environment:
|
||||
sdk: '>=2.12.0 <4.0.0'
|
||||
|
||||
dev_dependencies:
|
||||
test: ^1.17.7
|
||||
test_reflective_loader: ^0.2.0
|
||||
path: ^1.8.0
|
||||
lints: ^1.0.1
|
||||
@@ -0,0 +1,10 @@
|
||||
// Test for #7355
|
||||
table Foo {
|
||||
my_foo : foo_properties;
|
||||
}
|
||||
|
||||
struct foo_properties
|
||||
{
|
||||
a : bool;
|
||||
b : bool;
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
|
||||
class Foo {
|
||||
Foo._(this._bc, this._bcOffset);
|
||||
factory Foo(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<Foo> reader = _FooReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
FooProperties? get myFoo => FooProperties.reader.vTableGetNullable(_bc, _bcOffset, 4);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Foo{myFoo: ${myFoo}}';
|
||||
}
|
||||
|
||||
FooT unpack() => FooT(
|
||||
myFoo: myFoo?.unpack());
|
||||
|
||||
static int pack(fb.Builder fbBuilder, FooT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class FooT implements fb.Packable {
|
||||
FooPropertiesT? myFoo;
|
||||
|
||||
FooT({
|
||||
this.myFoo});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(1);
|
||||
if (myFoo != null) {
|
||||
fbBuilder.addStruct(0, myFoo!.pack(fbBuilder));
|
||||
}
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'FooT{myFoo: ${myFoo}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _FooReader extends fb.TableReader<Foo> {
|
||||
const _FooReader();
|
||||
|
||||
@override
|
||||
Foo createObject(fb.BufferContext bc, int offset) =>
|
||||
Foo._(bc, offset);
|
||||
}
|
||||
|
||||
class FooBuilder {
|
||||
FooBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(1);
|
||||
}
|
||||
|
||||
int addMyFoo(int offset) {
|
||||
fbBuilder.addStruct(0, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class FooObjectBuilder extends fb.ObjectBuilder {
|
||||
final FooPropertiesObjectBuilder? _myFoo;
|
||||
|
||||
FooObjectBuilder({
|
||||
FooPropertiesObjectBuilder? myFoo,
|
||||
})
|
||||
: _myFoo = myFoo;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(1);
|
||||
if (_myFoo != null) {
|
||||
fbBuilder.addStruct(0, _myFoo!.finish(fbBuilder));
|
||||
}
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
class FooProperties {
|
||||
FooProperties._(this._bc, this._bcOffset);
|
||||
|
||||
static const fb.Reader<FooProperties> reader = _FooPropertiesReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
bool get a => const fb.BoolReader().read(_bc, _bcOffset + 0);
|
||||
bool get b => const fb.BoolReader().read(_bc, _bcOffset + 1);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'FooProperties{a: ${a}, b: ${b}}';
|
||||
}
|
||||
|
||||
FooPropertiesT unpack() => FooPropertiesT(
|
||||
a: a,
|
||||
b: b);
|
||||
|
||||
static int pack(fb.Builder fbBuilder, FooPropertiesT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class FooPropertiesT implements fb.Packable {
|
||||
bool a;
|
||||
bool b;
|
||||
|
||||
FooPropertiesT({
|
||||
required this.a,
|
||||
required this.b});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
fbBuilder.putBool(b);
|
||||
fbBuilder.putBool(a);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'FooPropertiesT{a: ${a}, b: ${b}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _FooPropertiesReader extends fb.StructReader<FooProperties> {
|
||||
const _FooPropertiesReader();
|
||||
|
||||
@override
|
||||
int get size => 2;
|
||||
|
||||
@override
|
||||
FooProperties createObject(fb.BufferContext bc, int offset) =>
|
||||
FooProperties._(bc, offset);
|
||||
}
|
||||
|
||||
class FooPropertiesBuilder {
|
||||
FooPropertiesBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
int finish(bool a, bool b) {
|
||||
fbBuilder.putBool(b);
|
||||
fbBuilder.putBool(a);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FooPropertiesObjectBuilder extends fb.ObjectBuilder {
|
||||
final bool _a;
|
||||
final bool _b;
|
||||
|
||||
FooPropertiesObjectBuilder({
|
||||
required bool a,
|
||||
required bool b,
|
||||
})
|
||||
: _a = a,
|
||||
_b = b;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.putBool(_b);
|
||||
fbBuilder.putBool(_a);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,939 @@
|
||||
import 'dart:typed_data';
|
||||
import 'dart:io' as io;
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'package:flat_buffers/flat_buffers.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import './monster_test_my_game.example_generated.dart' as example;
|
||||
import './monster_test_my_game.example2_generated.dart' as example2;
|
||||
import './list_of_enums_generated.dart' as example3;
|
||||
import './bool_structs_generated.dart' as example4;
|
||||
|
||||
main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(BuilderTest);
|
||||
defineReflectiveTests(ObjectAPITest);
|
||||
defineReflectiveTests(CheckOtherLangaugesData);
|
||||
defineReflectiveTests(GeneratorTest);
|
||||
defineReflectiveTests(ListOfEnumsTest);
|
||||
});
|
||||
}
|
||||
|
||||
int indexToField(int index) {
|
||||
return (1 + 1 + index) * 2;
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class CheckOtherLangaugesData {
|
||||
test_cppData() async {
|
||||
List<int> data = await io.File(path.join(
|
||||
path.context.current,
|
||||
'test',
|
||||
'monsterdata_test.mon',
|
||||
)).readAsBytes();
|
||||
example.Monster mon = example.Monster(data);
|
||||
expect(mon.hp, 80);
|
||||
expect(mon.mana, 150);
|
||||
expect(mon.name, 'MyMonster');
|
||||
expect(mon.pos!.x, 1.0);
|
||||
expect(mon.pos!.y, 2.0);
|
||||
expect(mon.pos!.z, 3.0);
|
||||
expect(mon.pos!.test1, 3.0);
|
||||
expect(mon.pos!.test2.value, 2.0);
|
||||
expect(mon.pos!.test3.a, 5);
|
||||
expect(mon.pos!.test3.b, 6);
|
||||
expect(mon.testType!.value, example.AnyTypeId.Monster.value);
|
||||
expect(mon.test is example.Monster, true);
|
||||
final monster2 = mon.test as example.Monster;
|
||||
expect(monster2.name, "Fred");
|
||||
|
||||
expect(mon.inventory!.length, 5);
|
||||
expect(mon.inventory!.reduce((cur, next) => cur + next), 10);
|
||||
final test4 = mon.test4!;
|
||||
expect(test4.length, 2);
|
||||
expect(test4[0].a + test4[0].b + test4[1].a + test4[1].b, 100);
|
||||
expect(mon.testarrayofstring!.length, 2);
|
||||
expect(mon.testarrayofstring![0], "test1");
|
||||
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: null, '
|
||||
'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: null, anyUnique: null, anyAmbiguousType: null, '
|
||||
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
|
||||
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null, '
|
||||
'nativeInline: null, '
|
||||
'longEnumNonEnumDefault: LongEnum{value: 0}, '
|
||||
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
|
||||
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
|
||||
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
|
||||
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}, '
|
||||
'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: null, '
|
||||
'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: null, anyUnique: null, anyAmbiguousType: null, '
|
||||
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
|
||||
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null, '
|
||||
'nativeInline: null, '
|
||||
'longEnumNonEnumDefault: LongEnum{value: 0}, '
|
||||
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
|
||||
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
|
||||
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
|
||||
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}, '
|
||||
'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: ['
|
||||
'Ability{id: 0, distance: 45}, Ability{id: 1, distance: 21}, '
|
||||
'Ability{id: 5, distance: 12}], '
|
||||
'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: null, anyUnique: null, '
|
||||
'anyAmbiguousType: null, '
|
||||
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
|
||||
'testrequirednestedflatbuffer: null, scalarKeySortedTables: [Stat{id: '
|
||||
'miss, val: 0, count: 0}, Stat{id: hit, val: 10, count: 1}], '
|
||||
'nativeInline: Test{a: 1, b: 2}, '
|
||||
'longEnumNonEnumDefault: LongEnum{value: 0}, '
|
||||
'longEnumNormalDefault: LongEnum{value: 2}, nanDefault: NaN, '
|
||||
'infDefault: Infinity, positiveInfDefault: Infinity, infinityDefault: '
|
||||
'Infinity, positiveInfinityDefault: Infinity, negativeInfDefault: '
|
||||
'-Infinity, negativeInfinityDefault: -Infinity, doubleInfDefault: Infinity}');
|
||||
}
|
||||
}
|
||||
|
||||
/// Test a custom, fixed-memory allocator (no actual allocations performed)
|
||||
class CustomAllocator extends Allocator {
|
||||
final _memory = ByteData(10 * 1024);
|
||||
int _used = 0;
|
||||
|
||||
Uint8List buffer(int size) => _memory.buffer.asUint8List(_used - size, size);
|
||||
|
||||
@override
|
||||
ByteData allocate(int size) {
|
||||
if (size > _memory.lengthInBytes) {
|
||||
throw UnsupportedError('Trying to allocate too much');
|
||||
}
|
||||
_used = size;
|
||||
return ByteData.sublistView(_memory, 0, size);
|
||||
}
|
||||
|
||||
@override
|
||||
void deallocate(ByteData _) {}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class BuilderTest {
|
||||
void test_monsterBuilder([Builder? builder]) {
|
||||
final fbBuilder = builder ?? Builder();
|
||||
final str = fbBuilder.writeString('MyMonster');
|
||||
|
||||
fbBuilder.writeString('test1');
|
||||
fbBuilder.writeString('test2', asciiOptimization: true);
|
||||
final testArrayOfString = fbBuilder.endStructVector(2);
|
||||
|
||||
final fred = fbBuilder.writeString('Fred');
|
||||
|
||||
final List<int> treasure = [0, 1, 2, 3, 4];
|
||||
final inventory = fbBuilder.writeListUint8(treasure);
|
||||
|
||||
final monBuilder = example.MonsterBuilder(fbBuilder)
|
||||
..begin()
|
||||
..addNameOffset(fred);
|
||||
final mon2 = monBuilder.finish();
|
||||
|
||||
final testBuilder = example.TestBuilder(fbBuilder);
|
||||
testBuilder.finish(10, 20);
|
||||
testBuilder.finish(30, 40);
|
||||
final test4 = fbBuilder.endStructVector(2);
|
||||
|
||||
monBuilder
|
||||
..begin()
|
||||
..addPos(
|
||||
example.Vec3Builder(fbBuilder).finish(
|
||||
1.0,
|
||||
2.0,
|
||||
3.0,
|
||||
3.0,
|
||||
example.Color.Green,
|
||||
() => testBuilder.finish(5, 6),
|
||||
),
|
||||
)
|
||||
..addHp(80)
|
||||
..addNameOffset(str)
|
||||
..addInventoryOffset(inventory)
|
||||
..addTestType(example.AnyTypeId.Monster)
|
||||
..addTestOffset(mon2)
|
||||
..addTest4Offset(test4)
|
||||
..addTestarrayofstringOffset(testArrayOfString);
|
||||
final mon = monBuilder.finish();
|
||||
fbBuilder.finish(mon);
|
||||
}
|
||||
|
||||
void test_error_addInt32_withoutStartTable([Builder? builder]) {
|
||||
builder ??= Builder();
|
||||
expect(() {
|
||||
builder!.addInt32(0, 0);
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
}
|
||||
|
||||
void test_error_addOffset_withoutStartTable() {
|
||||
Builder builder = Builder();
|
||||
expect(() {
|
||||
builder.addOffset(0, 0);
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
}
|
||||
|
||||
void test_error_endTable_withoutStartTable() {
|
||||
Builder builder = Builder();
|
||||
expect(() {
|
||||
builder.endTable();
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
}
|
||||
|
||||
void test_error_startTable_duringTable() {
|
||||
Builder builder = Builder();
|
||||
builder.startTable(0);
|
||||
expect(() {
|
||||
builder.startTable(0);
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
}
|
||||
|
||||
void test_error_writeString_duringTable() {
|
||||
Builder builder = Builder();
|
||||
builder.startTable(1);
|
||||
expect(() {
|
||||
builder.writeString('12345');
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
}
|
||||
|
||||
void test_file_identifier() {
|
||||
Uint8List byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
builder.startTable(0);
|
||||
int offset = builder.endTable();
|
||||
builder.finish(offset, 'Az~ÿ');
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// Convert byteList to a ByteData so that we can read data from it.
|
||||
ByteData byteData = byteList.buffer.asByteData(byteList.offsetInBytes);
|
||||
// First 4 bytes are an offset to the table data.
|
||||
int tableDataLoc = byteData.getUint32(0, Endian.little);
|
||||
// Next 4 bytes are the file identifier.
|
||||
expect(byteData.getUint8(4), 65); // 'a'
|
||||
expect(byteData.getUint8(5), 122); // 'z'
|
||||
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);
|
||||
// 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);
|
||||
// Next 2 bytes are the size of the object in bytes (including the vtable
|
||||
// pointer), which should be 4.
|
||||
expect(byteData.getUint16(vTableLoc + 2, Endian.little), 4);
|
||||
}
|
||||
|
||||
void test_low() {
|
||||
final allocator = CustomAllocator();
|
||||
final builder = Builder(initialSize: 0, allocator: allocator);
|
||||
|
||||
builder.putUint8(1);
|
||||
expect(allocator.buffer(builder.size()), [1]);
|
||||
|
||||
builder.putUint32(2);
|
||||
expect(allocator.buffer(builder.size()), [2, 0, 0, 0, 0, 0, 0, 1]);
|
||||
|
||||
builder.putUint8(3);
|
||||
expect(
|
||||
allocator.buffer(builder.size()), [0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
|
||||
|
||||
builder.putUint8(4);
|
||||
expect(
|
||||
allocator.buffer(builder.size()), [0, 0, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
|
||||
|
||||
builder.putUint8(5);
|
||||
expect(
|
||||
allocator.buffer(builder.size()), [0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
|
||||
|
||||
builder.putUint32(6);
|
||||
expect(allocator.buffer(builder.size()),
|
||||
[6, 0, 0, 0, 0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
|
||||
}
|
||||
|
||||
void test_table_default() {
|
||||
List<int> byteList;
|
||||
{
|
||||
final builder = Builder(initialSize: 0, allocator: CustomAllocator());
|
||||
builder.startTable(2);
|
||||
builder.addInt32(0, 10, 10);
|
||||
builder.addInt32(1, 20, 10);
|
||||
int offset = builder.endTable();
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
expect(builder.size(), byteList.length);
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buffer = BufferContext.fromBytes(byteList);
|
||||
int objectOffset = buffer.derefObject(0);
|
||||
// was not written, so uses the new default value
|
||||
expect(
|
||||
const Int32Reader()
|
||||
.vTableGet(buffer, objectOffset, indexToField(0), 15),
|
||||
15);
|
||||
// has the written value
|
||||
expect(
|
||||
const Int32Reader()
|
||||
.vTableGet(buffer, objectOffset, indexToField(1), 15),
|
||||
20);
|
||||
}
|
||||
|
||||
void test_table_format([Builder? builder]) {
|
||||
Uint8List byteList;
|
||||
{
|
||||
builder ??= Builder(initialSize: 0);
|
||||
builder.startTable(3);
|
||||
builder.addInt32(0, 10);
|
||||
builder.addInt32(1, 20);
|
||||
builder.addInt32(2, 30);
|
||||
builder.finish(builder.endTable());
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// Convert byteList to a ByteData so that we can read data from it.
|
||||
ByteData byteData = byteList.buffer.asByteData(byteList.offsetInBytes);
|
||||
// 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);
|
||||
// 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);
|
||||
// Next 2 bytes are the size of the object in bytes (including the vtable
|
||||
// pointer), which should be 16.
|
||||
expect(byteData.getUint16(vTableLoc + 2, Endian.little), 16);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
void test_table_string() {
|
||||
String latinString = 'test';
|
||||
String unicodeString = 'Проба пера';
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int? latinStringOffset =
|
||||
builder.writeString(latinString, asciiOptimization: true);
|
||||
int? unicodeStringOffset =
|
||||
builder.writeString(unicodeString, asciiOptimization: true);
|
||||
builder.startTable(2);
|
||||
builder.addOffset(0, latinStringOffset);
|
||||
builder.addOffset(1, unicodeStringOffset);
|
||||
int offset = builder.endTable();
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
int objectOffset = buf.derefObject(0);
|
||||
expect(
|
||||
const StringReader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(0)),
|
||||
latinString);
|
||||
expect(
|
||||
const StringReader(asciiOptimization: true)
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(1)),
|
||||
unicodeString);
|
||||
}
|
||||
|
||||
void test_table_types([Builder? builder]) {
|
||||
List<int> byteList;
|
||||
{
|
||||
builder ??= Builder(initialSize: 0);
|
||||
int? stringOffset = builder.writeString('12345');
|
||||
builder.startTable(7);
|
||||
builder.addBool(0, true);
|
||||
builder.addInt8(1, 10);
|
||||
builder.addInt32(2, 20);
|
||||
builder.addOffset(3, stringOffset);
|
||||
builder.addInt32(4, 40);
|
||||
builder.addUint32(5, 0x9ABCDEF0);
|
||||
builder.addUint8(6, 0x9A);
|
||||
int offset = builder.endTable();
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
int objectOffset = buf.derefObject(0);
|
||||
expect(
|
||||
const BoolReader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(0)),
|
||||
true);
|
||||
expect(
|
||||
const Int8Reader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(1)),
|
||||
10);
|
||||
expect(
|
||||
const Int32Reader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(2)),
|
||||
20);
|
||||
expect(
|
||||
const StringReader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(3)),
|
||||
'12345');
|
||||
expect(
|
||||
const Int32Reader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(4)),
|
||||
40);
|
||||
expect(
|
||||
const Uint32Reader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(5)),
|
||||
0x9ABCDEF0);
|
||||
expect(
|
||||
const Uint8Reader()
|
||||
.vTableGetNullable(buf, objectOffset, indexToField(6)),
|
||||
0x9A);
|
||||
}
|
||||
|
||||
void test_writeList_of_Uint32() {
|
||||
List<int> values = <int>[10, 100, 12345, 0x9abcdef0];
|
||||
// write
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListUint32(values);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<int> items = const Uint32ListReader().read(buf, 0);
|
||||
expect(items, hasLength(4));
|
||||
expect(items, orderedEquals(values));
|
||||
}
|
||||
|
||||
void test_writeList_ofBool() {
|
||||
void verifyListBooleans(int len, List<int> trueBits) {
|
||||
// write
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
List<bool> values = List<bool>.filled(len, false);
|
||||
for (int bit in trueBits) {
|
||||
values[bit] = true;
|
||||
}
|
||||
int offset = builder.writeListBool(values);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<bool> items = const BoolListReader().read(buf, 0);
|
||||
expect(items, hasLength(len));
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
expect(items[i], trueBits.contains(i), reason: 'bit $i of $len');
|
||||
}
|
||||
}
|
||||
|
||||
verifyListBooleans(0, <int>[]);
|
||||
verifyListBooleans(1, <int>[]);
|
||||
verifyListBooleans(1, <int>[0]);
|
||||
verifyListBooleans(31, <int>[0, 1]);
|
||||
verifyListBooleans(31, <int>[1, 2, 24, 25, 30]);
|
||||
verifyListBooleans(31, <int>[0, 30]);
|
||||
verifyListBooleans(32, <int>[1, 2, 24, 25, 31]);
|
||||
verifyListBooleans(33, <int>[1, 2, 24, 25, 32]);
|
||||
verifyListBooleans(33, <int>[1, 2, 24, 25, 31, 32]);
|
||||
verifyListBooleans(63, <int>[]);
|
||||
verifyListBooleans(63, <int>[0, 1, 2, 61, 62]);
|
||||
verifyListBooleans(63, List<int>.generate(63, (i) => i));
|
||||
verifyListBooleans(64, <int>[]);
|
||||
verifyListBooleans(64, <int>[0, 1, 2, 61, 62, 63]);
|
||||
verifyListBooleans(64, <int>[1, 2, 62]);
|
||||
verifyListBooleans(64, <int>[0, 1, 2, 63]);
|
||||
verifyListBooleans(64, List<int>.generate(64, (i) => i));
|
||||
verifyListBooleans(100, <int>[0, 3, 30, 60, 90, 99]);
|
||||
}
|
||||
|
||||
void test_writeList_ofInt32() {
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListInt32(<int>[1, 2, 3, 4, 5]);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<int> items = const ListReader<int>(Int32Reader()).read(buf, 0);
|
||||
expect(items, hasLength(5));
|
||||
expect(items, orderedEquals(<int>[1, 2, 3, 4, 5]));
|
||||
}
|
||||
|
||||
void test_writeList_ofFloat64() {
|
||||
List<double> values = <double>[-1.234567, 3.4E+9, -5.6E-13, 7.8, 12.13];
|
||||
// write
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListFloat64(values);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<double> items = const Float64ListReader().read(buf, 0);
|
||||
|
||||
expect(items, hasLength(values.length));
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
expect(values[i], closeTo(items[i], .001));
|
||||
}
|
||||
}
|
||||
|
||||
void test_writeList_ofFloat32() {
|
||||
List<double> values = [1.0, 2.23, -3.213, 7.8, 12.13];
|
||||
// write
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListFloat32(values);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<double> items = const Float32ListReader().read(buf, 0);
|
||||
expect(items, hasLength(5));
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
expect(values[i], closeTo(items[i], .001));
|
||||
}
|
||||
}
|
||||
|
||||
void test_writeList_ofObjects([Builder? builder]) {
|
||||
List<int> byteList;
|
||||
{
|
||||
builder ??= Builder(initialSize: 0);
|
||||
// write the object #1
|
||||
int object1;
|
||||
{
|
||||
builder.startTable(2);
|
||||
builder.addInt32(0, 10);
|
||||
builder.addInt32(1, 20);
|
||||
object1 = builder.endTable();
|
||||
}
|
||||
// write the object #1
|
||||
int object2;
|
||||
{
|
||||
builder.startTable(2);
|
||||
builder.addInt32(0, 100);
|
||||
builder.addInt32(1, 200);
|
||||
object2 = builder.endTable();
|
||||
}
|
||||
// write the list
|
||||
int offset = builder.writeList([object1, object2]);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<TestPointImpl> items =
|
||||
const ListReader<TestPointImpl>(TestPointReader()).read(buf, 0);
|
||||
expect(items, hasLength(2));
|
||||
expect(items[0].x, 10);
|
||||
expect(items[0].y, 20);
|
||||
expect(items[1].x, 100);
|
||||
expect(items[1].y, 200);
|
||||
}
|
||||
|
||||
void test_writeList_ofStrings_asRoot() {
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int? str1 = builder.writeString('12345');
|
||||
int? str2 = builder.writeString('ABC');
|
||||
int offset = builder.writeList([str1, str2]);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<String> items = const ListReader<String>(StringReader()).read(buf, 0);
|
||||
expect(items, hasLength(2));
|
||||
expect(items, contains('12345'));
|
||||
expect(items, contains('ABC'));
|
||||
}
|
||||
|
||||
void test_writeList_ofStrings_inObject([Builder? builder]) {
|
||||
List<int> byteList;
|
||||
{
|
||||
builder ??= Builder(initialSize: 0);
|
||||
int listOffset = builder.writeList(
|
||||
[builder.writeString('12345'), builder.writeString('ABC')]);
|
||||
builder.startTable(1);
|
||||
builder.addOffset(0, listOffset);
|
||||
int offset = builder.endTable();
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
StringListWrapperImpl reader = StringListWrapperReader().read(buf, 0);
|
||||
List<String>? items = reader.items;
|
||||
expect(items, hasLength(2));
|
||||
expect(items, contains('12345'));
|
||||
expect(items, contains('ABC'));
|
||||
}
|
||||
|
||||
void test_writeList_ofUint32() {
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListUint32(<int>[1, 2, 0x9ABCDEF0]);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<int> items = const Uint32ListReader().read(buf, 0);
|
||||
expect(items, hasLength(3));
|
||||
expect(items, orderedEquals(<int>[1, 2, 0x9ABCDEF0]));
|
||||
}
|
||||
|
||||
void test_writeList_ofUint16() {
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListUint16(<int>[1, 2, 60000]);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
List<int> items = const Uint16ListReader().read(buf, 0);
|
||||
expect(items, hasLength(3));
|
||||
expect(items, orderedEquals(<int>[1, 2, 60000]));
|
||||
}
|
||||
|
||||
void test_writeList_ofUint8() {
|
||||
List<int> byteList;
|
||||
{
|
||||
Builder builder = Builder(initialSize: 0);
|
||||
int offset = builder.writeListUint8(<int>[1, 2, 3, 4, 0x9A, 0xFA]);
|
||||
builder.finish(offset);
|
||||
byteList = builder.buffer;
|
||||
}
|
||||
// read and verify
|
||||
BufferContext buf = BufferContext.fromBytes(byteList);
|
||||
const buffOffset = 8; // 32-bit offset to the list, + 32-bit length
|
||||
for (final lazy in [true, false]) {
|
||||
List<int> items = Uint8ListReader(lazy: lazy).read(buf, 0);
|
||||
expect(items, hasLength(6));
|
||||
expect(items, orderedEquals(<int>[1, 2, 3, 4, 0x9A, 0xFA]));
|
||||
|
||||
// overwrite the buffer to verify the laziness
|
||||
buf.buffer.setUint8(buffOffset + 1, 99);
|
||||
expect(items, orderedEquals(<int>[1, lazy ? 99 : 2, 3, 4, 0x9A, 0xFA]));
|
||||
|
||||
// restore the previous value for the next loop
|
||||
buf.buffer.setUint8(buffOffset + 1, 2);
|
||||
}
|
||||
}
|
||||
|
||||
void test_reset() {
|
||||
// We'll run a selection of tests , reusing the builder between them.
|
||||
final testCases = <void Function(Builder?)>[
|
||||
test_monsterBuilder,
|
||||
test_error_addInt32_withoutStartTable,
|
||||
test_table_format,
|
||||
test_table_types,
|
||||
test_writeList_ofObjects,
|
||||
test_writeList_ofStrings_inObject
|
||||
];
|
||||
|
||||
// Execute all test cases in all permutations of their order.
|
||||
// To do that, we generate permutations of test case indexes.
|
||||
final testCasesPermutations =
|
||||
_permutationsOf(List.generate(testCases.length, (index) => index));
|
||||
expect(testCasesPermutations.length, _factorial(testCases.length));
|
||||
|
||||
for (var indexes in testCasesPermutations) {
|
||||
// print the order so failures are reproducible
|
||||
printOnFailure('Running reset() test cases in order: $indexes');
|
||||
|
||||
Builder? builder;
|
||||
for (var index in indexes) {
|
||||
if (builder == null) {
|
||||
// Initial size small enough so at least one test case increases it.
|
||||
// On the other hand, it's large enough so that some test cases don't.
|
||||
builder = Builder(initialSize: 32);
|
||||
} else {
|
||||
builder.reset();
|
||||
}
|
||||
testCases[index](builder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate permutations of the given list
|
||||
List<List<T>> _permutationsOf<T>(List<T> source) {
|
||||
final result = <List<T>>[];
|
||||
|
||||
void permutate(List<T> items, int startAt) {
|
||||
for (var i = startAt; i < items.length; i++) {
|
||||
List<T> permutation = items.toList(growable: false);
|
||||
permutation[i] = items[startAt];
|
||||
permutation[startAt] = items[i];
|
||||
|
||||
// add the current list upon reaching the end
|
||||
if (startAt == items.length - 1) {
|
||||
result.add(items);
|
||||
} else {
|
||||
permutate(permutation, startAt + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
permutate(source, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
// a very simple implementation of n!
|
||||
int _factorial(int n) {
|
||||
var result = 1;
|
||||
for (var i = 2; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class ObjectAPITest {
|
||||
void test_tableStat() {
|
||||
final object1 = example.StatT(count: 3, id: "foo", val: 4);
|
||||
final fbb = Builder();
|
||||
fbb.finish(object1.pack(fbb));
|
||||
final object2 = example.Stat(fbb.buffer).unpack();
|
||||
expect(object2.count, object1.count);
|
||||
expect(object2.id, object1.id);
|
||||
expect(object2.val, object1.val);
|
||||
expect(object2.toString(), object1.toString());
|
||||
}
|
||||
|
||||
void test_tableMonster() {
|
||||
final monster = example.MonsterT()
|
||||
..pos = example.Vec3T(
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3,
|
||||
test1: 4.0,
|
||||
test2: example.Color.Red,
|
||||
test3: example.TestT(a: 1, b: 2))
|
||||
..mana = 2
|
||||
..name = 'Monstrous'
|
||||
..inventory = [24, 42]
|
||||
..color = example.Color.Green
|
||||
// TODO be smarter for unions and automatically set the `type` field?
|
||||
..testType = example.AnyTypeId.MyGame_Example2_Monster
|
||||
..test = example2.MonsterT()
|
||||
..test4 = [example.TestT(a: 3, b: 4), example.TestT(a: 5, b: 6)]
|
||||
..testarrayofstring = ["foo", "bar"]
|
||||
..testarrayoftables = [example.MonsterT(name: 'Oof')]
|
||||
..enemy = example.MonsterT(name: 'Enemy')
|
||||
..testarrayofbools = [false, true, false]
|
||||
..testf = 42.24
|
||||
..testarrayofsortedstruct = [
|
||||
example.AbilityT(id: 1, distance: 5),
|
||||
example.AbilityT(id: 3, distance: 7)
|
||||
]
|
||||
..vectorOfLongs = [5, 6, 7]
|
||||
..vectorOfDoubles = [8.9, 9.0, 10.1, 11.2]
|
||||
..anyAmbiguousType = example.AnyAmbiguousAliasesTypeId.M2
|
||||
..anyAmbiguous = null
|
||||
..vectorOfEnums = [example.Color.Blue, example.Color.Green]
|
||||
..signedEnum = example.Race.None;
|
||||
|
||||
final fbBuilder = Builder();
|
||||
final offset = monster.pack(fbBuilder);
|
||||
expect(offset, isNonZero);
|
||||
fbBuilder.finish(offset);
|
||||
final data = fbBuilder.buffer;
|
||||
|
||||
// TODO currently broken because of struct builder issue, see #6688
|
||||
// final monster2 = example.Monster(data); // Monster (reader)
|
||||
// expect(
|
||||
// // map Monster => MonsterT, Vec3 => Vec3T, ...
|
||||
// monster2.toString().replaceAllMapped(
|
||||
// RegExp('([a-zA-z0-9]+){'), (match) => match.group(1) + 'T{'),
|
||||
// monster.toString());
|
||||
//
|
||||
// final monster3 = monster2.unpack(); // MonsterT
|
||||
// expect(monster3.toString(), monster.toString());
|
||||
}
|
||||
|
||||
void test_Lists() {
|
||||
// Ensure unpack() reads lists eagerly by reusing the same builder and
|
||||
// overwriting data. Why: because standard reader reads lists lazily...
|
||||
final fbb = Builder();
|
||||
|
||||
final object1 = example.TypeAliasesT(v8: [1, 2, 3], vf64: [5, 6]);
|
||||
fbb.finish(object1.pack(fbb));
|
||||
final object1Read = example.TypeAliases(fbb.buffer).unpack();
|
||||
|
||||
// overwrite the original buffer by writing to the same builder
|
||||
fbb.reset();
|
||||
final object2 = example.TypeAliasesT(v8: [7, 8, 9], vf64: [10, 11]);
|
||||
fbb.finish(object2.pack(fbb));
|
||||
final object2Read = example.TypeAliases(fbb.buffer).unpack();
|
||||
|
||||
// this is fine even with lazy lists:
|
||||
expect(object2.toString(), object2Read.toString());
|
||||
|
||||
// this fails with lazy lists:
|
||||
expect(object1.toString(), object1Read.toString());
|
||||
|
||||
// empty list must be serialized as such (were stored NULL before v2.0)
|
||||
fbb.reset();
|
||||
final object3 = example.TypeAliasesT(v8: [], vf64: null);
|
||||
fbb.finish(object3.pack(fbb));
|
||||
final object3Read = example.TypeAliases(fbb.buffer).unpack();
|
||||
expect(object3.toString(), object3Read.toString());
|
||||
}
|
||||
}
|
||||
|
||||
class StringListWrapperImpl {
|
||||
final BufferContext bp;
|
||||
final int offset;
|
||||
|
||||
StringListWrapperImpl(this.bp, this.offset);
|
||||
|
||||
List<String>? get items => const ListReader<String>(StringReader())
|
||||
.vTableGetNullable(bp, offset, indexToField(0));
|
||||
}
|
||||
|
||||
class StringListWrapperReader extends TableReader<StringListWrapperImpl> {
|
||||
const StringListWrapperReader();
|
||||
|
||||
@override
|
||||
StringListWrapperImpl createObject(BufferContext object, int offset) {
|
||||
return StringListWrapperImpl(object, offset);
|
||||
}
|
||||
}
|
||||
|
||||
class TestPointImpl {
|
||||
final BufferContext bp;
|
||||
final int offset;
|
||||
|
||||
TestPointImpl(this.bp, this.offset);
|
||||
|
||||
int get x => const Int32Reader().vTableGet(bp, offset, indexToField(0), 0);
|
||||
|
||||
int get y => const Int32Reader().vTableGet(bp, offset, indexToField(1), 0);
|
||||
}
|
||||
|
||||
class TestPointReader extends TableReader<TestPointImpl> {
|
||||
const TestPointReader();
|
||||
|
||||
@override
|
||||
TestPointImpl createObject(BufferContext object, int offset) {
|
||||
return 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));
|
||||
}
|
||||
}
|
||||
|
||||
// See #6869
|
||||
@reflectiveTest
|
||||
class ListOfEnumsTest {
|
||||
void test_listOfEnums() async {
|
||||
var mytable = example3.MyTableObjectBuilder(options: [
|
||||
example3.OptionsEnum.A,
|
||||
example3.OptionsEnum.B,
|
||||
example3.OptionsEnum.C
|
||||
]);
|
||||
var bytes = mytable.toBytes();
|
||||
var mytable_read = example3.MyTable(bytes);
|
||||
expect(mytable_read.options![0].value, example3.OptionsEnum.A.value);
|
||||
expect(mytable_read.options![1].value, example3.OptionsEnum.B.value);
|
||||
expect(mytable_read.options![2].value, example3.OptionsEnum.C.value);
|
||||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class BoolInStructTest {
|
||||
void test_boolInStruct() async {
|
||||
var mystruct = example4.FooObjectBuilder(
|
||||
myFoo: example4.FooPropertiesObjectBuilder(a: true, b: false));
|
||||
var bytes = mystruct.toBytes();
|
||||
var mystruct_read = example4.Foo(bytes);
|
||||
expect(mystruct_read.myFoo!.a, true);
|
||||
expect(mystruct_read.myFoo!.b, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,597 @@
|
||||
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 builder = Builder()..addDouble(1.0);
|
||||
expect(builder.finish(), [0, 0, 128, 63, 14, 4]);
|
||||
}
|
||||
{
|
||||
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]);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,991 @@
|
||||
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, 128, 63, 14, 4])).doubleValue, 1.0);
|
||||
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((dynamic e) =>
|
||||
e is ArgumentError &&
|
||||
e.message ==
|
||||
'Key: [country] is not applicable on: //address of: ValueType.Map')));
|
||||
expect(
|
||||
() => flx['address']['countryCode'][0],
|
||||
throwsA(predicate((dynamic e) =>
|
||||
e is ArgumentError &&
|
||||
e.message ==
|
||||
'Key: [0] is not applicable on: //address/countryCode of: ValueType.String')));
|
||||
expect(
|
||||
() => flx[1],
|
||||
throwsA(predicate((dynamic e) =>
|
||||
e is ArgumentError &&
|
||||
e.message ==
|
||||
'Key: [1] is not applicable on: / of: ValueType.Map')));
|
||||
expect(
|
||||
() => flx['flags'][4],
|
||||
throwsA(predicate((dynamic e) =>
|
||||
e is ArgumentError &&
|
||||
e.message ==
|
||||
'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
|
||||
expect(
|
||||
() => flx['flags'][-1],
|
||||
throwsA(predicate((dynamic 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
|
||||
]));
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
|
||||
import './include_test2_my_game.other_name_space_generated.dart' as my_game_other_name_space;
|
||||
|
||||
class TableA {
|
||||
TableA._(this._bc, this._bcOffset);
|
||||
factory TableA(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<TableA> reader = _TableAReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
my_game_other_name_space.TableB? get b => my_game_other_name_space.TableB.reader.vTableGetNullable(_bc, _bcOffset, 4);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'TableA{b: ${b}}';
|
||||
}
|
||||
|
||||
TableAT unpack() => TableAT(
|
||||
b: b?.unpack());
|
||||
|
||||
static int pack(fb.Builder fbBuilder, TableAT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class TableAT implements fb.Packable {
|
||||
my_game_other_name_space.TableBT? b;
|
||||
|
||||
TableAT({
|
||||
this.b});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
final int? bOffset = b?.pack(fbBuilder);
|
||||
fbBuilder.startTable(1);
|
||||
fbBuilder.addOffset(0, bOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'TableAT{b: ${b}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _TableAReader extends fb.TableReader<TableA> {
|
||||
const _TableAReader();
|
||||
|
||||
@override
|
||||
TableA createObject(fb.BufferContext bc, int offset) =>
|
||||
TableA._(bc, offset);
|
||||
}
|
||||
|
||||
class TableABuilder {
|
||||
TableABuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(1);
|
||||
}
|
||||
|
||||
int addBOffset(int? offset) {
|
||||
fbBuilder.addOffset(0, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class TableAObjectBuilder extends fb.ObjectBuilder {
|
||||
final my_game_other_name_space.TableBObjectBuilder? _b;
|
||||
|
||||
TableAObjectBuilder({
|
||||
my_game_other_name_space.TableBObjectBuilder? b,
|
||||
})
|
||||
: _b = b;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
final int? bOffset = _b?.getOrCreateOffset(fbBuilder);
|
||||
fbBuilder.startTable(1);
|
||||
fbBuilder.addOffset(0, bOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
library my_game.other_name_space;
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
|
||||
import './include_test1_generated.dart';
|
||||
|
||||
class FromInclude {
|
||||
final int value;
|
||||
const FromInclude._(this.value);
|
||||
|
||||
factory FromInclude.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum FromInclude');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static FromInclude? _createOrNull(int? value) =>
|
||||
value == null ? null : FromInclude.fromValue(value);
|
||||
|
||||
static const int minValue = 0;
|
||||
static const int maxValue = 0;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const FromInclude IncludeVal = FromInclude._(0);
|
||||
static const Map<int, FromInclude> values = {
|
||||
0: IncludeVal};
|
||||
|
||||
static const fb.Reader<FromInclude> reader = _FromIncludeReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'FromInclude{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _FromIncludeReader extends fb.Reader<FromInclude> {
|
||||
const _FromIncludeReader();
|
||||
|
||||
@override
|
||||
int get size => 8;
|
||||
|
||||
@override
|
||||
FromInclude read(fb.BufferContext bc, int offset) =>
|
||||
FromInclude.fromValue(const fb.Int64Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class Unused {
|
||||
Unused._(this._bc, this._bcOffset);
|
||||
|
||||
static const fb.Reader<Unused> reader = _UnusedReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
int get a => const fb.Int32Reader().read(_bc, _bcOffset + 0);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Unused{a: ${a}}';
|
||||
}
|
||||
|
||||
UnusedT unpack() => UnusedT(
|
||||
a: a);
|
||||
|
||||
static int pack(fb.Builder fbBuilder, UnusedT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class UnusedT implements fb.Packable {
|
||||
int a;
|
||||
|
||||
UnusedT({
|
||||
required this.a});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
fbBuilder.putInt32(a);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'UnusedT{a: ${a}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _UnusedReader extends fb.StructReader<Unused> {
|
||||
const _UnusedReader();
|
||||
|
||||
@override
|
||||
int get size => 4;
|
||||
|
||||
@override
|
||||
Unused createObject(fb.BufferContext bc, int offset) =>
|
||||
Unused._(bc, offset);
|
||||
}
|
||||
|
||||
class UnusedBuilder {
|
||||
UnusedBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
int finish(int a) {
|
||||
fbBuilder.putInt32(a);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class UnusedObjectBuilder extends fb.ObjectBuilder {
|
||||
final int _a;
|
||||
|
||||
UnusedObjectBuilder({
|
||||
required int a,
|
||||
})
|
||||
: _a = a;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.putInt32(_a);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
class TableB {
|
||||
TableB._(this._bc, this._bcOffset);
|
||||
factory TableB(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<TableB> reader = _TableBReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
TableA? get a => TableA.reader.vTableGetNullable(_bc, _bcOffset, 4);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'TableB{a: ${a}}';
|
||||
}
|
||||
|
||||
TableBT unpack() => TableBT(
|
||||
a: a?.unpack());
|
||||
|
||||
static int pack(fb.Builder fbBuilder, TableBT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class TableBT implements fb.Packable {
|
||||
TableAT? a;
|
||||
|
||||
TableBT({
|
||||
this.a});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
final int? aOffset = a?.pack(fbBuilder);
|
||||
fbBuilder.startTable(1);
|
||||
fbBuilder.addOffset(0, aOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'TableBT{a: ${a}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _TableBReader extends fb.TableReader<TableB> {
|
||||
const _TableBReader();
|
||||
|
||||
@override
|
||||
TableB createObject(fb.BufferContext bc, int offset) =>
|
||||
TableB._(bc, offset);
|
||||
}
|
||||
|
||||
class TableBBuilder {
|
||||
TableBBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(1);
|
||||
}
|
||||
|
||||
int addAOffset(int? offset) {
|
||||
fbBuilder.addOffset(0, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class TableBObjectBuilder extends fb.ObjectBuilder {
|
||||
final TableAObjectBuilder? _a;
|
||||
|
||||
TableBObjectBuilder({
|
||||
TableAObjectBuilder? a,
|
||||
})
|
||||
: _a = a;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
final int? aOffset = _a?.getOrCreateOffset(fbBuilder);
|
||||
fbBuilder.startTable(1);
|
||||
fbBuilder.addOffset(0, aOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
library keyword_test;
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
|
||||
class Abc {
|
||||
final int value;
|
||||
const Abc._(this.value);
|
||||
|
||||
factory Abc.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum Abc');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Abc? _createOrNull(int? value) =>
|
||||
value == null ? null : Abc.fromValue(value);
|
||||
|
||||
static const int minValue = 0;
|
||||
static const int maxValue = 2;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const Abc $void = Abc._(0);
|
||||
static const Abc where = Abc._(1);
|
||||
static const Abc stackalloc = Abc._(2);
|
||||
static const Map<int, Abc> values = {
|
||||
0: $void,
|
||||
1: where,
|
||||
2: stackalloc};
|
||||
|
||||
static const fb.Reader<Abc> reader = _AbcReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Abc{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _AbcReader extends fb.Reader<Abc> {
|
||||
const _AbcReader();
|
||||
|
||||
@override
|
||||
int get size => 4;
|
||||
|
||||
@override
|
||||
Abc read(fb.BufferContext bc, int offset) =>
|
||||
Abc.fromValue(const fb.Int32Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class Public {
|
||||
final int value;
|
||||
const Public._(this.value);
|
||||
|
||||
factory Public.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum Public');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Public? _createOrNull(int? value) =>
|
||||
value == null ? null : Public.fromValue(value);
|
||||
|
||||
static const int minValue = 0;
|
||||
static const int maxValue = 0;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const Public NONE = Public._(0);
|
||||
static const Map<int, Public> values = {
|
||||
0: NONE};
|
||||
|
||||
static const fb.Reader<Public> reader = _PublicReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Public{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _PublicReader extends fb.Reader<Public> {
|
||||
const _PublicReader();
|
||||
|
||||
@override
|
||||
int get size => 4;
|
||||
|
||||
@override
|
||||
Public read(fb.BufferContext bc, int offset) =>
|
||||
Public.fromValue(const fb.Int32Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class KeywordsInUnionTypeId {
|
||||
final int value;
|
||||
const KeywordsInUnionTypeId._(this.value);
|
||||
|
||||
factory KeywordsInUnionTypeId.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum KeywordsInUnionTypeId');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static KeywordsInUnionTypeId? _createOrNull(int? value) =>
|
||||
value == null ? null : KeywordsInUnionTypeId.fromValue(value);
|
||||
|
||||
static const int minValue = 0;
|
||||
static const int maxValue = 2;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const KeywordsInUnionTypeId NONE = KeywordsInUnionTypeId._(0);
|
||||
static const KeywordsInUnionTypeId $static = KeywordsInUnionTypeId._(1);
|
||||
static const KeywordsInUnionTypeId internal = KeywordsInUnionTypeId._(2);
|
||||
static const Map<int, KeywordsInUnionTypeId> values = {
|
||||
0: NONE,
|
||||
1: $static,
|
||||
2: internal};
|
||||
|
||||
static const fb.Reader<KeywordsInUnionTypeId> reader = _KeywordsInUnionTypeIdReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'KeywordsInUnionTypeId{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _KeywordsInUnionTypeIdReader extends fb.Reader<KeywordsInUnionTypeId> {
|
||||
const _KeywordsInUnionTypeIdReader();
|
||||
|
||||
@override
|
||||
int get size => 1;
|
||||
|
||||
@override
|
||||
KeywordsInUnionTypeId read(fb.BufferContext bc, int offset) =>
|
||||
KeywordsInUnionTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class KeywordsInTable {
|
||||
KeywordsInTable._(this._bc, this._bcOffset);
|
||||
factory KeywordsInTable(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<KeywordsInTable> reader = _KeywordsInTableReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
Abc get $is => Abc.fromValue(const fb.Int32Reader().vTableGet(_bc, _bcOffset, 4, 0));
|
||||
Public get private => Public.fromValue(const fb.Int32Reader().vTableGet(_bc, _bcOffset, 6, 0));
|
||||
int get type => const fb.Int32Reader().vTableGet(_bc, _bcOffset, 8, 0);
|
||||
bool get $default => const fb.BoolReader().vTableGet(_bc, _bcOffset, 10, false);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'KeywordsInTable{\$is: ${$is}, private: ${private}, type: ${type}, \$default: ${$default}}';
|
||||
}
|
||||
|
||||
KeywordsInTableT unpack() => KeywordsInTableT(
|
||||
$is: $is,
|
||||
private: private,
|
||||
type: type,
|
||||
$default: $default);
|
||||
|
||||
static int pack(fb.Builder fbBuilder, KeywordsInTableT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class KeywordsInTableT implements fb.Packable {
|
||||
Abc $is;
|
||||
Public private;
|
||||
int type;
|
||||
bool $default;
|
||||
|
||||
KeywordsInTableT({
|
||||
this.$is = Abc.$void,
|
||||
this.private = Public.NONE,
|
||||
this.type = 0,
|
||||
this.$default = false});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(4);
|
||||
fbBuilder.addInt32(0, $is.value);
|
||||
fbBuilder.addInt32(1, private.value);
|
||||
fbBuilder.addInt32(2, type);
|
||||
fbBuilder.addBool(3, $default);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'KeywordsInTableT{\$is: ${$is}, private: ${private}, type: ${type}, \$default: ${$default}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _KeywordsInTableReader extends fb.TableReader<KeywordsInTable> {
|
||||
const _KeywordsInTableReader();
|
||||
|
||||
@override
|
||||
KeywordsInTable createObject(fb.BufferContext bc, int offset) =>
|
||||
KeywordsInTable._(bc, offset);
|
||||
}
|
||||
|
||||
class KeywordsInTableBuilder {
|
||||
KeywordsInTableBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(4);
|
||||
}
|
||||
|
||||
int addIs(Abc? $is) {
|
||||
fbBuilder.addInt32(0, $is?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addPrivate(Public? private) {
|
||||
fbBuilder.addInt32(1, private?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addType(int? type) {
|
||||
fbBuilder.addInt32(2, type);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addDefault(bool? $default) {
|
||||
fbBuilder.addBool(3, $default);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class KeywordsInTableObjectBuilder extends fb.ObjectBuilder {
|
||||
final Abc? _$is;
|
||||
final Public? _private;
|
||||
final int? _type;
|
||||
final bool? _$default;
|
||||
|
||||
KeywordsInTableObjectBuilder({
|
||||
Abc? $is,
|
||||
Public? private,
|
||||
int? type,
|
||||
bool? $default,
|
||||
})
|
||||
: _$is = $is,
|
||||
_private = private,
|
||||
_type = type,
|
||||
_$default = $default;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(4);
|
||||
fbBuilder.addInt32(0, _$is?.value);
|
||||
fbBuilder.addInt32(1, _private?.value);
|
||||
fbBuilder.addInt32(2, _type);
|
||||
fbBuilder.addBool(3, _$default);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
class Table2 {
|
||||
Table2._(this._bc, this._bcOffset);
|
||||
factory Table2(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<Table2> reader = _Table2Reader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
KeywordsInUnionTypeId? get typeType => KeywordsInUnionTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 4));
|
||||
dynamic get type {
|
||||
switch (typeType?.value) {
|
||||
case 1: return KeywordsInTable.reader.vTableGetNullable(_bc, _bcOffset, 6);
|
||||
case 2: return KeywordsInTable.reader.vTableGetNullable(_bc, _bcOffset, 6);
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Table2{typeType: ${typeType}, type: ${type}}';
|
||||
}
|
||||
|
||||
Table2T unpack() => Table2T(
|
||||
typeType: typeType,
|
||||
type: type);
|
||||
|
||||
static int pack(fb.Builder fbBuilder, Table2T? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class Table2T implements fb.Packable {
|
||||
KeywordsInUnionTypeId? typeType;
|
||||
dynamic type;
|
||||
|
||||
Table2T({
|
||||
this.typeType,
|
||||
this.type});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
final int? typeOffset = type?.pack(fbBuilder);
|
||||
fbBuilder.startTable(2);
|
||||
fbBuilder.addUint8(0, typeType?.value);
|
||||
fbBuilder.addOffset(1, typeOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Table2T{typeType: ${typeType}, type: ${type}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _Table2Reader extends fb.TableReader<Table2> {
|
||||
const _Table2Reader();
|
||||
|
||||
@override
|
||||
Table2 createObject(fb.BufferContext bc, int offset) =>
|
||||
Table2._(bc, offset);
|
||||
}
|
||||
|
||||
class Table2Builder {
|
||||
Table2Builder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(2);
|
||||
}
|
||||
|
||||
int addTypeType(KeywordsInUnionTypeId? typeType) {
|
||||
fbBuilder.addUint8(0, typeType?.value);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
int addTypeOffset(int? offset) {
|
||||
fbBuilder.addOffset(1, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class Table2ObjectBuilder extends fb.ObjectBuilder {
|
||||
final KeywordsInUnionTypeId? _typeType;
|
||||
final dynamic _type;
|
||||
|
||||
Table2ObjectBuilder({
|
||||
KeywordsInUnionTypeId? typeType,
|
||||
dynamic type,
|
||||
})
|
||||
: _typeType = typeType,
|
||||
_type = type;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
final int? typeOffset = _type?.getOrCreateOffset(fbBuilder);
|
||||
fbBuilder.startTable(2);
|
||||
fbBuilder.addUint8(0, _typeType?.value);
|
||||
fbBuilder.addOffset(1, typeOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
enum OptionsEnum : uint32
|
||||
{
|
||||
A = 1,
|
||||
B = 2,
|
||||
C = 3
|
||||
}
|
||||
|
||||
table MyTable {
|
||||
options : [OptionsEnum];
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
|
||||
class OptionsEnum {
|
||||
final int value;
|
||||
const OptionsEnum._(this.value);
|
||||
|
||||
factory OptionsEnum.fromValue(int value) {
|
||||
final result = values[value];
|
||||
if (result == null) {
|
||||
throw StateError('Invalid value $value for bit flag enum OptionsEnum');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static OptionsEnum? _createOrNull(int? value) =>
|
||||
value == null ? null : OptionsEnum.fromValue(value);
|
||||
|
||||
static const int minValue = 1;
|
||||
static const int maxValue = 3;
|
||||
static bool containsValue(int value) => values.containsKey(value);
|
||||
|
||||
static const OptionsEnum A = OptionsEnum._(1);
|
||||
static const OptionsEnum B = OptionsEnum._(2);
|
||||
static const OptionsEnum C = OptionsEnum._(3);
|
||||
static const Map<int, OptionsEnum> values = {
|
||||
1: A,
|
||||
2: B,
|
||||
3: C};
|
||||
|
||||
static const fb.Reader<OptionsEnum> reader = _OptionsEnumReader();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'OptionsEnum{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
class _OptionsEnumReader extends fb.Reader<OptionsEnum> {
|
||||
const _OptionsEnumReader();
|
||||
|
||||
@override
|
||||
int get size => 4;
|
||||
|
||||
@override
|
||||
OptionsEnum read(fb.BufferContext bc, int offset) =>
|
||||
OptionsEnum.fromValue(const fb.Uint32Reader().read(bc, offset));
|
||||
}
|
||||
|
||||
class MyTable {
|
||||
MyTable._(this._bc, this._bcOffset);
|
||||
factory MyTable(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<MyTable> reader = _MyTableReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
List<OptionsEnum>? get options => const fb.ListReader<OptionsEnum>(OptionsEnum.reader).vTableGetNullable(_bc, _bcOffset, 4);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'MyTable{options: ${options}}';
|
||||
}
|
||||
|
||||
MyTableT unpack() => MyTableT(
|
||||
options: const fb.ListReader<OptionsEnum>(OptionsEnum.reader, lazy: false).vTableGetNullable(_bc, _bcOffset, 4));
|
||||
|
||||
static int pack(fb.Builder fbBuilder, MyTableT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class MyTableT implements fb.Packable {
|
||||
List<OptionsEnum>? options;
|
||||
|
||||
MyTableT({
|
||||
this.options});
|
||||
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
final int? optionsOffset = options == null ? null
|
||||
: fbBuilder.writeListUint32(options!.map((f) => f.value).toList());
|
||||
fbBuilder.startTable(1);
|
||||
fbBuilder.addOffset(0, optionsOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'MyTableT{options: ${options}}';
|
||||
}
|
||||
}
|
||||
|
||||
class _MyTableReader extends fb.TableReader<MyTable> {
|
||||
const _MyTableReader();
|
||||
|
||||
@override
|
||||
MyTable createObject(fb.BufferContext bc, int offset) =>
|
||||
MyTable._(bc, offset);
|
||||
}
|
||||
|
||||
class MyTableBuilder {
|
||||
MyTableBuilder(this.fbBuilder);
|
||||
|
||||
final fb.Builder fbBuilder;
|
||||
|
||||
void begin() {
|
||||
fbBuilder.startTable(1);
|
||||
}
|
||||
|
||||
int addOptionsOffset(int? offset) {
|
||||
fbBuilder.addOffset(0, offset);
|
||||
return fbBuilder.offset;
|
||||
}
|
||||
|
||||
int finish() {
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
}
|
||||
|
||||
class MyTableObjectBuilder extends fb.ObjectBuilder {
|
||||
final List<OptionsEnum>? _options;
|
||||
|
||||
MyTableObjectBuilder({
|
||||
List<OptionsEnum>? options,
|
||||
})
|
||||
: _options = options;
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
final int? optionsOffset = _options == null ? null
|
||||
: fbBuilder.writeListUint32(_options!.map((f) => f.value).toList());
|
||||
fbBuilder.startTable(1);
|
||||
fbBuilder.addOffset(0, optionsOffset);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
// test schema file
|
||||
|
||||
include "include_test1.fbs";
|
||||
|
||||
namespace MyGame;
|
||||
|
||||
table InParentNamespace {}
|
||||
|
||||
namespace MyGame.Example2;
|
||||
|
||||
table Monster {} // Test having same name as below, but in different namespace.
|
||||
|
||||
namespace MyGame.Example;
|
||||
|
||||
attribute "priority";
|
||||
|
||||
/// Composite components of Monster color.
|
||||
enum Color:ubyte (bit_flags) {
|
||||
Red = 0, // color Red = (1u << 0)
|
||||
/// \brief color Green
|
||||
/// Green is bit_flag with value (1u << 1)
|
||||
Green,
|
||||
/// \brief color Blue (1u << 3)
|
||||
Blue = 3,
|
||||
}
|
||||
|
||||
enum Race:byte {
|
||||
None = -1,
|
||||
Human = 0,
|
||||
Dwarf,
|
||||
Elf,
|
||||
}
|
||||
|
||||
enum LongEnum:ulong (bit_flags) {
|
||||
LongOne = 1,
|
||||
LongTwo = 2,
|
||||
// Because this is a bitflag, 40 will be out of range of a 32-bit integer,
|
||||
// allowing us to exercise any logic special to big numbers.
|
||||
LongBig = 40,
|
||||
}
|
||||
|
||||
union Any { Monster, TestSimpleTableWithEnum, MyGame.Example2.Monster }
|
||||
|
||||
union AnyUniqueAliases { M: Monster, TS: TestSimpleTableWithEnum, M2: MyGame.Example2.Monster }
|
||||
union AnyAmbiguousAliases { M1: Monster, M2: Monster, M3: Monster }
|
||||
|
||||
struct Test { a:short; b:byte; }
|
||||
|
||||
table TestSimpleTableWithEnum (csharp_partial, private) {
|
||||
color: Color = Green;
|
||||
}
|
||||
|
||||
struct Vec3 (force_align: 8) {
|
||||
x:float;
|
||||
y:float;
|
||||
z:float;
|
||||
test1:double;
|
||||
test2:Color;
|
||||
test3:Test;
|
||||
}
|
||||
|
||||
struct Ability {
|
||||
id:uint(key);
|
||||
distance:uint;
|
||||
}
|
||||
|
||||
struct StructOfStructs {
|
||||
a: Ability;
|
||||
b: Test;
|
||||
c: Ability;
|
||||
}
|
||||
|
||||
struct StructOfStructsOfStructs {
|
||||
a: StructOfStructs;
|
||||
}
|
||||
|
||||
table Stat {
|
||||
id:string;
|
||||
val:long;
|
||||
count:ushort (key);
|
||||
}
|
||||
|
||||
table Referrable {
|
||||
id:ulong(key, hash:"fnv1a_64");
|
||||
}
|
||||
|
||||
/// an example documentation comment: "monster object"
|
||||
table Monster {
|
||||
pos:Vec3 (id: 0);
|
||||
hp:short = 100 (id: 2);
|
||||
mana:short = 150 (id: 1);
|
||||
name:string (id: 3, key);
|
||||
color:Color = Blue (id: 6);
|
||||
inventory:[ubyte] (id: 5);
|
||||
friendly:bool = false (deprecated, priority: 1, id: 4);
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
testarrayoftables:[Monster] (id: 11);
|
||||
testarrayofstring:[string] (id: 10);
|
||||
testarrayofstring2:[string] (id: 28);
|
||||
testarrayofbools:[bool] (id: 24);
|
||||
testarrayofsortedstruct:[Ability] (id: 29);
|
||||
enemy:MyGame.Example.Monster (id:12); // Test referring by full namespace.
|
||||
test:Any (id: 8);
|
||||
test4:[Test] (id: 9);
|
||||
test5:[Test] (id: 31);
|
||||
testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster");
|
||||
testempty:Stat (id:14);
|
||||
testbool:bool (id:15);
|
||||
testhashs32_fnv1:int (id:16, hash:"fnv1_32");
|
||||
testhashu32_fnv1:uint (id:17, hash:"fnv1_32");
|
||||
testhashs64_fnv1:long (id:18, hash:"fnv1_64");
|
||||
testhashu64_fnv1:ulong (id:19, hash:"fnv1_64");
|
||||
testhashs32_fnv1a:int (id:20, hash:"fnv1a_32");
|
||||
testhashu32_fnv1a:uint (id:21, hash:"fnv1a_32", cpp_type:"Stat");
|
||||
testhashs64_fnv1a:long (id:22, hash:"fnv1a_64");
|
||||
testhashu64_fnv1a:ulong (id:23, hash:"fnv1a_64");
|
||||
testf:float = 3.14159 (id:25);
|
||||
testf2:float = 3 (id:26);
|
||||
testf3:float (id:27);
|
||||
flex:[ubyte] (id:30, flexbuffer);
|
||||
vector_of_longs:[long] (id:32);
|
||||
vector_of_doubles:[double] (id:33);
|
||||
parent_namespace_test:InParentNamespace (id:34);
|
||||
vector_of_referrables:[Referrable](id:35);
|
||||
single_weak_reference:ulong(id:36, hash:"fnv1a_64", cpp_type:"ReferrableT");
|
||||
vector_of_weak_references:[ulong](id:37, hash:"fnv1a_64", cpp_type:"ReferrableT");
|
||||
vector_of_strong_referrables:[Referrable](id:38, cpp_ptr_type:"default_ptr_type"); //was shared_ptr
|
||||
co_owning_reference:ulong(id:39, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked"); //was shared_ptr as well
|
||||
vector_of_co_owning_references:[ulong](id:40, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"default_ptr_type", cpp_ptr_type_get:".get()"); //was shared_ptr
|
||||
non_owning_reference:ulong(id:41, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked", cpp_ptr_type_get:""); //was weak_ptr
|
||||
vector_of_non_owning_references:[ulong](id:42, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked", cpp_ptr_type_get:""); //was weak_ptr
|
||||
any_unique:AnyUniqueAliases(id:44);
|
||||
any_ambiguous:AnyAmbiguousAliases (id:46);
|
||||
vector_of_enums:[Color] (id:47);
|
||||
signed_enum:Race = None (id:48);
|
||||
testrequirednestedflatbuffer:[ubyte] (id:49, nested_flatbuffer: "Monster");
|
||||
scalar_key_sorted_tables:[Stat] (id: 50);
|
||||
native_inline:Test (id: 51, native_inline);
|
||||
// The default value of this enum will be a numeric zero, which isn't a valid
|
||||
// enum value.
|
||||
long_enum_non_enum_default:LongEnum (id: 52);
|
||||
long_enum_normal_default:LongEnum = LongOne (id: 53);
|
||||
// Test that default values nan and +/-inf work.
|
||||
nan_default:float = nan (id: 54);
|
||||
inf_default:float = inf (id: 55);
|
||||
positive_inf_default:float = +inf (id: 56);
|
||||
infinity_default:float = infinity (id: 57);
|
||||
positive_infinity_default:float = +infinity (id: 58);
|
||||
negative_inf_default:float = -inf (id: 59);
|
||||
negative_infinity_default:float = -infinity (id: 60);
|
||||
double_inf_default:double = inf (id: 61);
|
||||
}
|
||||
|
||||
table TypeAliases {
|
||||
i8:int8;
|
||||
u8:uint8;
|
||||
i16:int16;
|
||||
u16:uint16;
|
||||
i32:int32;
|
||||
u32:uint32;
|
||||
i64:int64;
|
||||
u64:uint64;
|
||||
f32:float32;
|
||||
f64:float64;
|
||||
v8:[int8];
|
||||
vf64:[float64];
|
||||
}
|
||||
|
||||
rpc_service MonsterStorage {
|
||||
Store(Monster):Stat (streaming: "none");
|
||||
Retrieve(Stat):Monster (streaming: "server", idempotent);
|
||||
GetMaxHitPoint(Monster):Stat (streaming: "client");
|
||||
GetMinMaxHitPoints(Monster):Stat (streaming: "bidi");
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
|
||||
file_identifier "MONS";
|
||||
file_extension "mon";
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
library my_game.example2;
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
import './monster_test_my_game_generated.dart' as my_game;
|
||||
import './monster_test_my_game.example_generated.dart' as my_game_example;
|
||||
|
||||
import './include_test1_generated.dart';
|
||||
|
||||
class Monster {
|
||||
Monster._(this._bc, this._bcOffset);
|
||||
factory Monster(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<Monster> reader = _MonsterReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Monster{}';
|
||||
}
|
||||
|
||||
MonsterT unpack() => MonsterT();
|
||||
|
||||
static int pack(fb.Builder fbBuilder, MonsterT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class MonsterT implements fb.Packable {
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(0);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'MonsterT{}';
|
||||
}
|
||||
}
|
||||
|
||||
class _MonsterReader extends fb.TableReader<Monster> {
|
||||
const _MonsterReader();
|
||||
|
||||
@override
|
||||
Monster createObject(fb.BufferContext bc, int offset) =>
|
||||
Monster._(bc, offset);
|
||||
}
|
||||
|
||||
class MonsterObjectBuilder extends fb.ObjectBuilder {
|
||||
|
||||
MonsterObjectBuilder();
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(0);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
+2440
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,79 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
|
||||
|
||||
library my_game;
|
||||
|
||||
import 'dart:typed_data' show Uint8List;
|
||||
import 'package:flat_buffers/flat_buffers.dart' as fb;
|
||||
|
||||
import './monster_test_my_game.example_generated.dart' as my_game_example;
|
||||
import './monster_test_my_game.example2_generated.dart' as my_game_example2;
|
||||
|
||||
import './include_test1_generated.dart';
|
||||
|
||||
class InParentNamespace {
|
||||
InParentNamespace._(this._bc, this._bcOffset);
|
||||
factory InParentNamespace(List<int> bytes) {
|
||||
final rootRef = fb.BufferContext.fromBytes(bytes);
|
||||
return reader.read(rootRef, 0);
|
||||
}
|
||||
|
||||
static const fb.Reader<InParentNamespace> reader = _InParentNamespaceReader();
|
||||
|
||||
final fb.BufferContext _bc;
|
||||
final int _bcOffset;
|
||||
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'InParentNamespace{}';
|
||||
}
|
||||
|
||||
InParentNamespaceT unpack() => InParentNamespaceT();
|
||||
|
||||
static int pack(fb.Builder fbBuilder, InParentNamespaceT? object) {
|
||||
if (object == null) return 0;
|
||||
return object.pack(fbBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
class InParentNamespaceT implements fb.Packable {
|
||||
@override
|
||||
int pack(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(0);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'InParentNamespaceT{}';
|
||||
}
|
||||
}
|
||||
|
||||
class _InParentNamespaceReader extends fb.TableReader<InParentNamespace> {
|
||||
const _InParentNamespaceReader();
|
||||
|
||||
@override
|
||||
InParentNamespace createObject(fb.BufferContext bc, int offset) =>
|
||||
InParentNamespace._(bc, offset);
|
||||
}
|
||||
|
||||
class InParentNamespaceObjectBuilder extends fb.ObjectBuilder {
|
||||
|
||||
InParentNamespaceObjectBuilder();
|
||||
|
||||
/// Finish building, and store into the [fbBuilder].
|
||||
@override
|
||||
int finish(fb.Builder fbBuilder) {
|
||||
fbBuilder.startTable(0);
|
||||
return fbBuilder.endTable();
|
||||
}
|
||||
|
||||
/// Convenience method to serialize to byte list.
|
||||
@override
|
||||
Uint8List toBytes([String? fileIdentifier]) {
|
||||
final fbBuilder = fb.Builder(deduplicateTables: false);
|
||||
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
|
||||
return fbBuilder.buffer;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user