EOS 学習メモ:custom dispatcher 編 ①steemCreated with Sketch.

in eos •  6 years ago  (edited)

dg1uxga7sv.jpg

EOS 学習メモ:EOSIO_DISPATCH 編 の続き。

今回から数回は custom dispatcher 編として、dispatcher のカスタマイズパターンについて、なぜカスタマイズする必要があるのかを意識しながら学習していく。なお、チュートリアルとしてはこれが最後の項目になる。

まず初回は、そもそも EOSIO_DISPATCH が生成する dispatcher がどうなっているのかをより具体的に把握する。

ということで、EOSIO_DISPATCH macro に頼らず、それと同じ処理をする dispatcher を実装した contract を書くと、こんな感じ になる。dispatcher だけ抜き出すと以下。

 extern "C" {
   void apply(uint64_t receiver, uint64_t code, uint64_t action) {
     if (code == receiver) {
       switch (action) {
       case name("a").value:
         execute_action(name(receiver), name(code), &test::a);
         break;
       case name("b").value:
         execute_action(name(receiver), name(code), &test::b);
         break;
       case name("c").value:
         execute_action(name(receiver), name(code), &test::c);
         break;
       }
     }
   }
 };



特にわかりにくい処理はないかなと思う。actionswitch に渡してあげて、対応する action handler を呼び出しているだけ。なお、action handler の呼び出しには execute_action() が使われており、その実装は こんな感じ になっているので、第 1 引数と第 2 引数には apply action handler の引数である receivercode をそのまま渡し、第 3 引数には呼び出したい action handler を渡してあげればよい模様。

今回はここまで。

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Translator screwed up the wordings. But, keep it up.

Posted using Partiko Android

Thanks for using eSteem!
Your post has been voted as a part of eSteem encouragement program. Keep up the good work! Install Android, iOS Mobile app or Windows, Mac, Linux Surfer app, if you haven't already!
Learn more: https://esteem.app
Join our discord: https://discord.gg/8eHupPq