From 2977fbc6b658b0d664f7d3b36ecf8ca9e897aaa3 Mon Sep 17 00:00:00 2001 From: Raimo Niskanen Date: Tue, 19 Apr 2016 09:28:38 +0200 Subject: Use ?NAME macro in examples --- system/doc/design_principles/statem.xml | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'system/doc') diff --git a/system/doc/design_principles/statem.xml b/system/doc/design_principles/statem.xml index 26e4840640..72aaafd780 100644 --- a/system/doc/design_principles/statem.xml +++ b/system/doc/design_principles/statem.xml @@ -216,6 +216,7 @@ handle_event(EventType, EventContent, State, Data) -> -export([locked/3,open/3]). start_link(Code) -> - gen_statem:start_link({local,code_lock}, ?MODULE, Code, []). + gen_statem:start_link({local,?NAME}, ?MODULE, Code, []). button(Digit) -> - gen_statem:cast(code_lock, {button,Digit}). + gen_statem:cast(?NAME, {button,Digit}). init(Code) -> @@ -278,7 +279,7 @@ code_change(_Vsn, State, Data, _Extra) ->

- gen_statem:start_link({local,code_lock}, ?MODULE, Code, []). + gen_statem:start_link({local,?NAME}, ?MODULE, Code, []). ]]>

start_link calls the function @@ -290,9 +291,9 @@ start_link(Code) ->

- The first argument, {local,code_lock}, specifies + The first argument, {local,?NAME}, specifies the name. In this case, the gen_statem is locally - registered as code_lock. + registered as code_lock through the macro ?NAME.

If the name is omitted, the gen_statem is not registered. @@ -394,11 +395,12 @@ init(Code) ->

- gen_statem:cast(code_lock, {button,Digit}). + gen_statem:cast(?NAME, {button,Digit}). ]]>

- code_lock is the name of the gen_statem and must - agree with the name used to start it. + The first argument is the name of the gen_statem and must + agree with the name used to start it so therefore we use the + same macro ?NAME as when starting. {button,Digit} is the actual event content.

@@ -495,7 +497,7 @@ open(timeout, _, Data) -> ... code_length() -> - gen_statem:call(code_lock, code_length). + gen_statem:call(?NAME, code_length). ... locked(...) -> ... ; @@ -627,7 +629,7 @@ terminate(_Reason, State, _Data) -> ... stop() -> - gen_statem:stop(code_lock). + gen_statem:stop(?NAME). ]]>

This makes the gen_statem call the terminate/3 @@ -882,18 +884,19 @@ open(cast, {button,_}, Data) ->

spawn( fun () -> - true = register(code_lock, self()), + true = register(?NAME, self()), do_lock(), locked(Code, Code) end). button(Digit) -> - code_lock ! {button,Digit}. + ?NAME ! {button,Digit}. locked(Code, [Digit|Remaining]) -> receive @@ -1049,6 +1052,7 @@ enter(Tag, State, Data) -> -export([locked/3,open/3]). start_link(Code) -> - gen_statem:start_link({local,code_lock}, ?MODULE, Code, []). + gen_statem:start_link({local,?NAME}, ?MODULE, Code, []). stop() -> - gen_statem:stop(code_lock). + gen_statem:stop(?NAME). button(Digit) -> - gen_statem:cast(code_lock, {button,Digit}). + gen_statem:cast(?NAME, {button,Digit}). code_length() -> - gen_statem:call(code_lock, code_length). + gen_statem:call(?NAME, code_length). init(Code) -> Data = #{code => Code}, -- cgit v1.2.3