File: create_your_first_buffer.tutorial

package info (click to toggle)
flatbuffers 2.0.8%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,308 kB
  • sloc: cpp: 44,808; python: 6,544; cs: 4,852; java: 4,389; ansic: 1,615; php: 1,455; xml: 973; javascript: 938; sh: 806; makefile: 35
file content (72 lines) | stat: -rw-r--r-- 3,888 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@Tutorial(time: 5) {
  @Intro(title: "After having our code generated") {
    After generating the code from the previous section, we will know start creating our monster object.
    We will create a monster object called orc.
    }
    
    @Section(title: "Building your first buffer") {
      @ContentAndMedia {}
      @Steps {
        @Step {
          Starting with a new file, we will create our very first Flatbuffer.
          @Code(name: "ViewController.swift", file: "swift_code_1.swift")
        }
        @Step {
          First, we need to import ``FlatBuffers``
          @Code(name: "ViewController.swift", file: "swift_code_2.swift")
        }
        @Step {
          We need to create an instance of the `FlatBufferBuilder`, which will contain the buffer as it grows. 
          You can pass an initial size of the buffer (here 1024 bytes), which will grow automatically if needed.
          @Code(name: "ViewController.swift", file: "swift_code_3.swift")
        }
        @Step {
          After creating the builder, we can start serializing our data. Before we make our orc Monster, 
          let's create some Weapons: a Sword and an Axe. However we will start by naming our weapons as `Sword` and `Axe`
          @Code(name: "ViewController.swift", file: "swift_code_4.swift")
        }
        @Step {
          After naming the weapons, we will create two weapon objects with the damage that the weapon is going to deal. 
          That's done by calling the `start` Method on each table you will be creating, in this case its called `startWeapon` 
          and finished by calling `end`.
          @Code(name: "ViewController.swift", file: "swift_code_5.swift")
        }
        @Step {
          We will take our (Sword and Axe) serialized data and serialize their offsets as a vector of tables into our `ByteBuffer`. 
          So we can reference them later on from our Monster Object
          @Code(name: "ViewController.swift", file: "swift_code_6.swift")
        }
        @Step {
          We will add our Monster name as a string value just like we did with the weapons.
          @Code(name: "ViewController.swift", file: "swift_code_7.swift")
        }
        
        @Step {
          We will create a path that our monster should be using while roaming in its den. To create a vector of paths we would us
          `createVector(ofStructs: [])` which will take a Native `Swift` struct that has been padded to fit the `FlatBuffers` standards.
          
          There are usually two ways of creating vectors in `FlatBuffers` which you can see in commented out code. 
          And thus there are multiple convenience methods that will cover all the bases 
          when trying to create a vector so that you dont have to create it with `start` and `end`
          @Code(name: "ViewController.swift", file: "swift_code_8.swift")
        }
        
        @Step {
          Now to serialize our data into our `Monster` object. Which again there are two ways of doing, by calling the `create` method or
          by serializing the objects yourself. What we added to our Monster were the `Equipped Type` and the `Equipped` union itself, which
          allows the Monster to have the `Axe` as his equipped weapon.
          
          Important: Unlike structs, you should not nest tables or other objects, 
          which is why we created all the `strings/vectors/tables` that this monster refers to before start.
          If you try to create any of them between start and end, you will get an `assert`.
          @Code(name: "ViewController.swift", file: "swift_code_9.swift")
        }
        
        @Step {
          Finally you can just finalize the buffer by calling `builder.finish` and get the Byte array from the buffer.
          @Code(name: "ViewController.swift", file: "swift_code_10.swift")
        }

      }
    }
  }