blob: efb698b1e37cb82721a71a635a994cb0d58e2e40 [file] [log] [blame]
James Kuszmaul8e62b022022-03-22 09:33:25 -07001/*
2 * Copyright 2021 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import Foundation
18
19/// Enum is a protocol that all flatbuffers enums should conform to
20/// Since it allows us to get the actual `ByteSize` and `Value` from
21/// a swift enum.
22public protocol Enum {
23 /// associatedtype that the type of the enum should conform to
24 associatedtype T: Scalar & Verifiable
25 /// Size of the current associatedtype in the enum
26 static var byteSize: Int { get }
27 /// The current value the enum hosts
28 var value: T { get }
29}
30
31extension Enum where Self: Verifiable {
32
33 /// Verifies that the current value is which the bounds of the buffer, and if
34 /// the current `Value` is aligned properly
35 /// - Parameters:
36 /// - verifier: Verifier that hosts the buffer
37 /// - position: Current position within the buffer
38 /// - type: The type of the object to be verified
39 /// - Throws: Errors coming from `inBuffer` function
40 public static func verify<T>(
41 _ verifier: inout Verifier,
42 at position: Int,
43 of type: T.Type) throws where T: Verifiable
44 {
45 try verifier.inBuffer(position: position, of: type.self)
46 }
47
48}
49
50/// UnionEnum is a Protocol that allows us to create Union type of enums
51/// and their value initializers. Since an `init` was required by
52/// the verifier
53public protocol UnionEnum: Enum {
54 init?(value: T) throws
55}