annotate Gears/introduction/interface.md @ 93:04cff9568106

backup 2021-10-05
author autobackup
date Tue, 05 Oct 2021 00:10:04 +0900
parents b6c284fd5ae4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 # GearsOSのinterface
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 - GearsOSにはモジュール化の仕組みとしてInterface構文が存在します
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 - JavaやgolangのInterface, Haskellの型クラスに相当する構文です
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 ## Interfaceで定義するもの
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
7 - golangのInterfaceでは、関数の集合、及び関数の引数と返り値がinterfaceとして定義されていました
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
8 - GearsOSのItnerfaceでも同様に、使用するデータ構造と、関数を記述します
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
9 - 使用するデータ(DataGear)はPerlスクリプト側の改良により、`__code`の中に定義しておけば問題ないようになった
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
10
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
11 ## 書き方(~2019/02/03)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
12
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
13 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
14 typedef struct InterfaceName<Type, Impl>{
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
15 //入出力で使用するデータ構造を記述する
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
16 union Data* stack;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
17 union Data* data;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
18 union Data* data1;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
19
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
20 // APIとして使用するCodeGearを列挙する
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
21 //ここで利用する引数|出力は必ず上で列挙する必要がある
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
22 __code whenEmpty(...);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
23 __code clear(Impl* stack,__code next(...));
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
24 __code get2(Impl* stack, __code next(Type* data, Type* data1, ...));
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
25
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
26 //継続先のCodeGear
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
27 __code next(...);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
28 } InterfaceName;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
29 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
30 - Type
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
31 - 自分自身の型
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
32 - Impl
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
33 - 自分自身の実装の型
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
34 - Stackの場合はSingleLinkedStackなど...
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
35
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
36 ## 書き方(2019/02/03~)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
37
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
38 - Perl側を改良したので引数を別で定義する必要がなくなった
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
39
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
40 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
41 typedef struct InterfaceName<Type, Impl>{
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
42 // APIとして使用するCodeGearを列挙する
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
43 __code whenEmpty(...);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
44 __code clear(Impl* stack,__code next(...));
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
45 __code get2(Impl* stack, __code next(Type* data, Type* data1, ...));
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
46
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
47 //継続先のCodeGear
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
48 __code next(...);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
49 } InterfaceName;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
50 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
51 ## 仕様QA
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
52 `__code`の記述が始まる前に引数を宣言していた場合、宣言された引数のみが使われる。
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
53
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
54 (`__code`の中の引数はキャプチャされない)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
55
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
56 ## Interfaceのファイルを利用するスクリプト
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
57 - interfaceのファイルは次のPerlスクリプトで利用され、CbCレベルでは実は参照されません
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
58 - `trans_impl.pl`
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
59 - cbcファイルの雛形を生成する際に、実装しているファイルから動的に呼び出されます
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
60 - ` generate_stub.pl`
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
61 - ` generate_context.pl`
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
62 - 入出力の引数チェックで行われます
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
63 - `generate_context`では、 `context.h`を生成する際に読み込まれます
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
64
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
65 ## CbCレベルでのInterface
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
66 - CbCレベルでは、 stub 及び, `context.h` の `union Data` の定義内で定義されています
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
67 - その為 `context.h` を `include`することで、これらの型を利用できます
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
68
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
69 ## Interfaceの具体例
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
70 - [Stack](/Gears/introduction/interface/Stack)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
71
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
72 ## Interfaceの実装の具体例
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
73 - [SingleLinkedStack](/Gears/introduction/interface/impl/SingleLinkedStack)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
74
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
75 ## Q&A
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
76
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
77 ### Q.「Interefaceに書いたDataGearはどうやって実装側から使うの?」
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
78
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
79 **A.「引数で与える」**
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
80
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
81 ### Q.「引数で使ってないけどContextにDataGearを登録したい」
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
82
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
83 **A.「専用の構文があります」**
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
84 - `// data_gear "WantStruct.h"` と書いておくと `generate_context.pl`時に解決される
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
85
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
86 #### 例
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
87
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
88 - 例えば `"DataGearExample`をcontextに加えたい場合
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
89 - まず最初に対象のヘッダファイルをどこかに作っておく
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
90 - `struct DataGearExample`を使いたいCbCファイルに以下の記述を書いておく
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
91
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
92 ```c
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
93 #include "hoge.h"
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
94 //data_gear "DataGearExample.h"
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
95 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
96
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
97 - `#interface "hoge.h"`構文もあるが、 `generate_stub`の時点でこの文が消えるので、コメントで追加している
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
98
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
99 ### Q.Intefaceに値持たせたいんだけど
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
100
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
101 **A. 値は実装側に持たせるのでInterfaceには書いちゃだめ!!**
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
102
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
103 - interfaceはあくまでもCodeGearの入出力だけのDataしかない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
104 - 細かなDataはすべて実装に持たせる
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
105 - 同じfieldを持つ実装が複数存在する