for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
const auto &enum_def = **it;
- if (!enum_def.generated) {
- GenEnum(enum_def);
- }
+ if (!enum_def.generated) { GenEnum(enum_def); }
}
for (auto it = parser_.structs_.vec.begin();
code_.SetValue("PROTOCOL",
struct_def.fixed ? "Readable" : "FlatBufferObject");
code_.SetValue("OBJECTTYPE", struct_def.fixed ? "Struct" : "Table");
- code_ += "public struct {{STRUCTNAME}}: {{PROTOCOL}} {\n";
+ code_ += "public struct {{STRUCTNAME}}: {{PROTOCOL}}\\";
+ if (!struct_def.fixed && parser_.opts.generate_object_based_api)
+ code_ += ", ObjectAPI\\";
+ code_ += " {\n";
Indent();
code_ += ValidateFunc();
code_ += "public var __buffer: ByteBuffer! { return {{ACCESS}}.bb }";
++it) {
code_ += *it;
}
- code_ += "return {{STRUCTNAME}}.end{{SHORT_STRUCTNAME}}(&fbb, start: __start)";
+ code_ +=
+ "return {{STRUCTNAME}}.end{{SHORT_STRUCTNAME}}(&fbb, start: __start)";
Outdent();
code_ += "}";
}
base_constructor);
}
code_ += "";
- BuildObjectAPIConstructor(buffer_constructor,
- "_ _t: inout " + NameWrappedInNameSpace(struct_def));
+ BuildObjectAPIConstructor(
+ buffer_constructor,
+ "_ _t: inout " + NameWrappedInNameSpace(struct_def));
BuildObjectAPIConstructor(base_constructor);
+ if (!struct_def.fixed)
+ code_ +=
+ "func serialize() -> ByteBuffer { return serialize(type: "
+ "{{STRUCTNAME}}.self) }\n";
Outdent();
code_ += "}";
}
GenerateStructArgs(*field.value.type.struct_def, &code, "", "",
"$0", true);
code = code.substr(0, code.size() - 2);
- code_ += "let __" + name + " = obj." + name + ".map { " + NameWrappedInNameSpace(*field.value.type.struct_def) + ".create" +
- Name(*field.value.type.struct_def) + "(" + code + ") }";
+ code_ += "let __" + name + " = obj." + name + ".map { " +
+ NameWrappedInNameSpace(*field.value.type.struct_def) +
+ ".create" + Name(*field.value.type.struct_def) + "(" +
+ code + ") }";
} else {
code_ += "let __" + name + " = " + type +
".pack(&builder, obj: &obj." + name + ")";
code_ += "for i in obj." + name + " {";
Indent();
code_ += "guard let _o = i else { continue }";
- code_ += "__" + name + "__.append(" + NameWrappedInNameSpace(*field.value.type.struct_def) + ".create" +
- Name(*field.value.type.struct_def) + "(" + code + "))";
+ code_ += "__" + name + "__.append(" +
+ NameWrappedInNameSpace(*field.value.type.struct_def) +
+ ".create" + Name(*field.value.type.struct_def) + "(" + code +
+ "))";
Outdent();
code_ += "}";
code_ += "let __" + name + " = builder.createVector(structs: __" +
return WrapInNameSpace(struct_.defined_namespace,
ObjectAPIName(Name(struct_)));
}
- return WrapInNameSpace(struct_.defined_namespace,
- Name(struct_));
+ return WrapInNameSpace(struct_.defined_namespace, Name(struct_));
}
case BASE_TYPE_UNION:
default: return "FlatBufferObject";
void Outdent() { code_.DecrementIdentLevel(); }
std::string NameWrappedInNameSpace(const EnumDef &enum_def) const {
- return WrapInNameSpace(enum_def.defined_namespace,
- Name(enum_def));
+ return WrapInNameSpace(enum_def.defined_namespace, Name(enum_def));
}
std::string NameWrappedInNameSpace(const StructDef &struct_def) const {
- return WrapInNameSpace(struct_def.defined_namespace,
- Name(struct_def));
+ return WrapInNameSpace(struct_def.defined_namespace, Name(struct_def));
}
std::string GenTypeBasic(const Type &type, bool can_override) const {
};
// clang-format on
if (can_override) {
- if (type.enum_def)
- return NameWrappedInNameSpace(*type.enum_def);
+ if (type.enum_def) return NameWrappedInNameSpace(*type.enum_def);
if (type.base_type == BASE_TYPE_BOOL) return "Bool";
}
return swift_type[static_cast<int>(type.base_type)];
std::string Name(const Definition &def) const {
return EscapeKeyword(MakeCamel(def.name, false));
}
-
};
} // namespace swift
bool GenerateSwift(const Parser &parser, const std::string &path,
Pod::Spec.new do |s|
s.name = 'FlatBuffers'
- s.version = '0.7.0'
+ s.version = '0.7.1'
s.summary = 'FlatBuffers: Memory Efficient Serialization Library'
s.description = "FlatBuffers is a cross platform serialization library architected for
init(_ bb: ByteBuffer, o: Int32)
}
-public protocol NativeTable {}
-
public protocol ObjectAPI {
associatedtype T
static func pack(_ builder: inout FlatBufferBuilder, obj: inout T) -> Offset<UOffset>
--- /dev/null
+import Foundation
+
+public protocol NativeTable {}
+
+extension NativeTable {
+
+ /// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly th
+ /// - Parameter type: Type of the Flatbuffer object
+ /// - Returns: returns the encoded sized ByteBuffer
+ public func serialize<T: ObjectAPI>(type: T.Type) -> ByteBuffer where T.T == Self {
+ var builder = FlatBufferBuilder(initialSize: 1024)
+ return serialize(builder: &builder, type: type.self)
+ }
+
+ /// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly.
+ ///
+ /// - Parameters:
+ /// - builder: A FlatBufferBuilder
+ /// - type: Type of the Flatbuffer object
+ /// - Returns: returns the encoded sized ByteBuffer
+ /// - Note: The `serialize(builder:type)` can be considered as a function that allows you to create smaller builder instead of the default `1024`.
+ /// It can be considered less expensive in terms of memory allocation
+ public func serialize<T: ObjectAPI>(builder: inout FlatBufferBuilder, type: T.Type) -> ByteBuffer where T.T == Self {
+ var s = self
+ let root = type.pack(&builder, obj: &s)
+ builder.finish(offset: root)
+ return builder.sizedBuffer
+ }
+}
func readMonster(fb: ByteBuffer) {
var monster = Monster.getRootAsMonster(bb: fb)
readFlatbufferMonster(monster: &monster)
- var unpacked: MyGame_Example_MonsterT? = monster.unpack()
+ let unpacked: MyGame_Example_MonsterT? = monster.unpack()
readObjectApi(monster: unpacked!)
- var builder = FlatBufferBuilder()
- let root = Monster.pack(&builder, obj: &unpacked)
- builder.finish(offset: root)
- var newMonster = Monster.getRootAsMonster(bb: builder.sizedBuffer)
+ guard let buffer = unpacked?.serialize() else { fatalError("Couldnt generate bytebuffer") }
+ var newMonster = Monster.getRootAsMonster(bb: buffer)
readFlatbufferMonster(monster: &newMonster)
}
}
-public struct MyGame_InParentNamespace: FlatBufferObject {
+public struct MyGame_InParentNamespace: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
init() {
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_InParentNamespace.self) }
+
}
-public struct MyGame_Example2_Monster: FlatBufferObject {
+public struct MyGame_Example2_Monster: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
init() {
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_Example2_Monster.self) }
+
}
-public struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferObject {
+public struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
color = .green
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_TestSimpleTableWithEnum.self) }
+
}
-public struct MyGame_Example_Stat: FlatBufferObject {
+public struct MyGame_Example_Stat: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
count = 0
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_Stat.self) }
+
}
-public struct MyGame_Example_Referrable: FlatBufferObject {
+public struct MyGame_Example_Referrable: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
id = 0
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_Referrable.self) }
+
}
/// an example documentation comment: "monster object"
-public struct MyGame_Example_Monster: FlatBufferObject {
+public struct MyGame_Example_Monster: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
signedEnum = .none_
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_Monster.self) }
+
}
-public struct MyGame_Example_TypeAliases: FlatBufferObject {
+public struct MyGame_Example_TypeAliases: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
vf64 = []
}
+ func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_TypeAliases.self) }
+
}
}
-public struct Attacker: FlatBufferObject {
+public struct Attacker: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
swordAttackDamage = 0
}
+ func serialize() -> ByteBuffer { return serialize(type: Attacker.self) }
+
}
-public struct Movie: FlatBufferObject {
+public struct Movie: FlatBufferObject, ObjectAPI {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
characters = []
}
+ func serialize() -> ByteBuffer { return serialize(type: Movie.self) }
+
}