1 import Foundation 2 3 /// FlatbufferObject structures all the Flatbuffers objects 4 public protocol FlatBufferObject { 5 var __buffer: ByteBuffer! { get } 6 init(_ bb: ByteBuffer, o: Int32) 7 } 8 9 /// Readable is structures all the Flatbuffers structs 10 /// 11 /// Readable is a procotol that each Flatbuffer struct should confirm to since 12 /// FlatBufferBuilder would require a Type to both create(struct:) and createVector(structs:) functions 13 public protocol Readable: FlatBufferObject { 14 static var size: Int { get } 15 static var alignment: Int { get } 16 } 17 18 public protocol Enum { 19 associatedtype T: Scalar 20 static var byteSize: Int { get } 21 var value: T { get } 22 } 23 24 /// Mutable is a protocol that allows us to mutate Scalar values within the buffer 25 public protocol Mutable { 26 /// makes Flatbuffer accessed within the Protocol 27 var bb: ByteBuffer { get } 28 /// makes position of the table/struct accessed within the Protocol 29 var postion: Int32 { get } 30 } 31 32 extension Mutable { 33 34 /// Mutates the memory in the buffer, this is only called from the access function of table and structs 35 /// - Parameters: 36 /// - value: New value to be inserted to the buffer 37 /// - index: index of the Element mutate<T: Scalar>null38 func mutate<T: Scalar>(value: T, o: Int32) -> Bool { 39 guard o != 0 else { return false } 40 bb.write(value: value, index: Int(o), direct: true) 41 return true 42 } 43 } 44 45 extension Mutable where Self == Table { 46 47 /// Mutates a value by calling mutate with respect to the position in the table 48 /// - Parameters: 49 /// - value: New value to be inserted to the buffer 50 /// - index: index of the Element mutate<T: Scalar>null51 public func mutate<T: Scalar>(_ value: T, index: Int32) -> Bool { 52 guard index != 0 else { return false } 53 return mutate(value: value, o: index + postion) 54 } 55 56 /// Directly mutates the element by calling mutate 57 /// 58 /// Mutates the Element at index ignoring the current position by calling mutate 59 /// - Parameters: 60 /// - value: New value to be inserted to the buffer 61 /// - index: index of the Element directMutate<T: Scalar>null62 public func directMutate<T: Scalar>(_ value: T, index: Int32) -> Bool { 63 return mutate(value: value, o: index) 64 } 65 } 66 67 extension Mutable where Self == Struct { 68 69 /// Mutates a value by calling mutate with respect to the position in the struct 70 /// - Parameters: 71 /// - value: New value to be inserted to the buffer 72 /// - index: index of the Element mutate<T: Scalar>null73 public func mutate<T: Scalar>(_ value: T, index: Int32) -> Bool { 74 return mutate(value: value, o: index + postion) 75 } 76 77 /// Directly mutates the element by calling mutate 78 /// 79 /// Mutates the Element at index ignoring the current position by calling mutate 80 /// - Parameters: 81 /// - value: New value to be inserted to the buffer 82 /// - index: index of the Element directMutate<T: Scalar>null83 public func directMutate<T: Scalar>(_ value: T, index: Int32) -> Bool { 84 return mutate(value: value, o: index) 85 } 86 } 87 extension Struct: Mutable {} 88 extension Table: Mutable {} 89