aboutsummaryrefslogtreecommitdiffstats
path: root/lib/wx/src/wx_object.erl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/wx/src/wx_object.erl')
-rw-r--r--lib/wx/src/wx_object.erl73
1 files changed, 52 insertions, 21 deletions
diff --git a/lib/wx/src/wx_object.erl b/lib/wx/src/wx_object.erl
index 40170b6eb1..cc19ff9770 100644
--- a/lib/wx/src/wx_object.erl
+++ b/lib/wx/src/wx_object.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2008-2016. All Rights Reserved.
+%% Copyright Ericsson AB 2008-2017. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
@@ -39,19 +39,31 @@
%% {wxObject, State} | {wxObject, State, Timeout} |
%% ignore | {stop, Reason}
%%
+%% Asynchronous window event handling: <br/>
+%% handle_event(#wx{}, State) should return <br/>
+%% {noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}
+%%
+%% The user module can export the following callback functions:
+%%
%% handle_call(Msg, {From, Tag}, State) should return <br/>
%% {reply, Reply, State} | {reply, Reply, State, Timeout} |
%% {noreply, State} | {noreply, State, Timeout} |
%% {stop, Reason, Reply, State}
%%
-%% Asynchronous window event handling: <br/>
-%% handle_event(#wx{}, State) should return <br/>
-%% {noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}
+%% handle_cast(Msg, State) should return <br/>
+%% {noreply, State} | {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%%
+%% If the above are not exported but called, the wx_object process will crash.
+%% The user module can also export:
%%
%% Info is message e.g. {'EXIT', P, R}, {nodedown, N}, ... <br/>
%% handle_info(Info, State) should return , ... <br/>
%% {noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}
-%%
+%%
+%% If a message is sent to the wx_object process when handle_info is not
+%% exported, the message will be dropped and ignored.
+%%
%% When stop is returned in one of the functions above with Reason =
%% normal | shutdown | Term, terminate(State) is called. It lets the
%% user module clean up, it is always called when server terminates or
@@ -135,6 +147,8 @@
{'noreply', NewState :: term()} |
{'noreply', NewState :: term(), timeout() | 'hibernate'} |
{'stop', Reason :: term(), NewState :: term()}.
+-callback handle_sync_event(Request :: #wx{}, Ref :: #wx_ref{}, State :: term()) ->
+ ok.
-callback terminate(Reason :: ('normal' | 'shutdown' | {'shutdown', term()} |
term()),
State :: term()) ->
@@ -143,6 +157,9 @@
Extra :: term()) ->
{'ok', NewState :: term()} | {'error', Reason :: term()}.
+-optional_callbacks(
+ [handle_call/3, handle_cast/2, handle_info/2,
+ handle_sync_event/3, terminate/2, code_change/3]).
%% System exports
-export([system_continue/3,
@@ -426,6 +443,7 @@ dispatch(Msg = #wx{}, Mod, State) ->
Mod:handle_event(Msg, State);
dispatch(Info, Mod, State) ->
Mod:handle_info(Info, State).
+
%% @hidden
handle_msg({'$gen_call', From, Msg}, Parent, Name, State, Mod) ->
case catch Mod:handle_call(Msg, From, State) of
@@ -447,8 +465,12 @@ handle_msg({'$gen_call', From, Msg}, Parent, Name, State, Mod) ->
Other -> handle_common_reply(Other, Name, Msg, Mod, State, [])
end;
handle_msg(Msg, Parent, Name, State, Mod) ->
- Reply = (catch dispatch(Msg, Mod, State)),
- handle_no_reply(Reply, Parent, Name, Msg, Mod, State, []).
+ case catch dispatch(Msg, Mod, State) of
+ {'EXIT', {undef, [{Mod, handle_info, [_,_], _}|_]}} ->
+ handle_no_reply({noreply, State}, Parent, Name, Msg, Mod, State, []);
+ Reply ->
+ handle_no_reply(Reply, Parent, Name, Msg, Mod, State, [])
+ end.
%% @hidden
handle_msg({'$gen_call', From, Msg}, Parent, Name, State, Mod, Debug) ->
@@ -528,8 +550,8 @@ system_terminate(Reason, _Parent, Debug, [Name, State, Mod, _Time]) ->
%% @hidden
system_code_change([Name, State, Mod, Time], _Module, OldVsn, Extra) ->
case catch Mod:code_change(OldVsn, State, Extra) of
- {ok, NewState} -> {ok, [Name, NewState, Mod, Time]};
- Else -> Else
+ {ok, NewState} -> {ok, [Name, NewState, Mod, Time]};
+ Else -> Else
end.
%%-----------------------------------------------------------------
@@ -539,28 +561,28 @@ system_code_change([Name, State, Mod, Time], _Module, OldVsn, Extra) ->
print_event(Dev, {in, Msg}, Name) ->
case Msg of
{'$gen_call', {From, _Tag}, Call} ->
- io:format(Dev, "*DBG* ~p got call ~p from ~w~n",
+ io:format(Dev, "*DBG* ~tp got call ~tp from ~w~n",
[Name, Call, From]);
{'$gen_cast', Cast} ->
- io:format(Dev, "*DBG* ~p got cast ~p~n",
+ io:format(Dev, "*DBG* ~tp got cast ~tp~n",
[Name, Cast]);
_ ->
- io:format(Dev, "*DBG* ~p got ~p~n", [Name, Msg])
+ io:format(Dev, "*DBG* ~tp got ~tp~n", [Name, Msg])
end;
print_event(Dev, {out, Msg, To, State}, Name) ->
- io:format(Dev, "*DBG* ~p sent ~p to ~w, new state ~w~n",
+ io:format(Dev, "*DBG* ~tp sent ~tp to ~w, new state ~tp~n",
[Name, Msg, To, State]);
print_event(Dev, {noreply, State}, Name) ->
- io:format(Dev, "*DBG* ~p new state ~w~n", [Name, State]);
+ io:format(Dev, "*DBG* ~tp new state ~tp~n", [Name, State]);
print_event(Dev, Event, Name) ->
- io:format(Dev, "*DBG* ~p dbg ~p~n", [Name, Event]).
+ io:format(Dev, "*DBG* ~tp dbg ~tp~n", [Name, Event]).
%%% ---------------------------------------------------
%%% Terminate the server.
%%% ---------------------------------------------------
%% @hidden
terminate(Reason, Name, Msg, Mod, State, Debug) ->
- case catch Mod:terminate(Reason, State) of
+ case try_terminate(Mod, Reason, State) of
{'EXIT', R} ->
error_info(R, Name, Msg, State, Debug),
exit(R);
@@ -577,6 +599,15 @@ terminate(Reason, Name, Msg, Mod, State, Debug) ->
exit(Reason)
end
end.
+
+try_terminate(Mod, Reason, State) ->
+ case erlang:function_exported(Mod, terminate, 2) of
+ true ->
+ catch Mod:terminate(Reason, State);
+ _ ->
+ ok
+ end.
+
%% @hidden
error_info(_Reason, application_controller, _Msg, _State, _Debug) ->
ok;
@@ -598,10 +629,10 @@ error_info(Reason, Name, Msg, State, Debug) ->
_ ->
Reason
end,
- format("** wx object server ~p terminating \n"
- "** Last message in was ~p~n"
- "** When Server state == ~p~n"
- "** Reason for termination == ~n** ~p~n",
+ format("** wx object server ~tp terminating \n"
+ "** Last message in was ~tp~n"
+ "** When Server state == ~tp~n"
+ "** Reason for termination == ~n** ~tp~n",
[Name, Msg, State, Reason1]),
sys:print_log(Debug),
ok.
@@ -626,7 +657,7 @@ debug_options(Name, Opts) ->
dbg_opts(Name, Opts) ->
case catch sys:debug_options(Opts) of
{'EXIT',_} ->
- format("~p: ignoring erroneous debug options - ~p~n",
+ format("~tp: ignoring erroneous debug options - ~tp~n",
[Name, Opts]),
[];
Dbg ->