aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/src/dialyzer_race_data_server.erl
blob: 36004918097172132a7684f92ed931cdada2bc57 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
%% -*- erlang-indent-level: 2 -*-
%%-----------------------------------------------------------------------
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2006-2016. 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.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%%%-------------------------------------------------------------------
%%% File    : dialyzer_race_data_server.erl
%%% Author  : Tobias Lindahl <[email protected]>
%%% Description : 
%%%
%%% Created : 18 Sep 2015 by Luca Favatella <[email protected]>
%%%-------------------------------------------------------------------
-module(dialyzer_race_data_server).

-export([new/0,
         duplicate/1,
         stop/1,
         call/2,
         cast/2]).

-include("dialyzer.hrl").

%%----------------------------------------------------------------------

-record(state, {race_code     = dict:new() :: dict:dict(),
                public_tables = []         :: [label()],
                named_tables  = []         :: [string()],
                beh_api_calls = []         :: [{mfa(), mfa()}]}).

%%----------------------------------------------------------------------

-spec new() -> pid().

new() ->
  spawn_link(fun() -> loop(#state{}) end).

-spec duplicate(pid()) -> pid().

duplicate(Server) ->
  call(dup, Server).

-spec stop(pid()) -> ok.

stop(Server) ->
  cast(stop, Server).

-spec call(atom(), pid()) -> term().

call(Query, Server) ->
  Ref = make_ref(),
  Server ! {call, self(), Ref, Query},
  receive
    {Ref, Reply} -> Reply
  end.

-spec cast(atom() | {atom(), term()}, pid()) -> ok.

cast(Message, Server) ->
  Server ! {cast, Message},
  ok.

%%----------------------------------------------------------------------

loop(State) ->
  receive
    {call, From, Ref, Query} ->
      Reply = handle_call(Query, State),
      From ! {Ref, Reply},
      loop(State);
    {cast, stop} ->
      ok;
    {cast, Message} ->
      NewState = handle_cast(Message, State),
      loop(NewState)
  end.

handle_cast(race_code_new, State) ->
  State#state{race_code = dict:new()};
handle_cast({Tag, Data}, State) ->
  case Tag of
    renew_race_info -> renew_race_info_handler(Data, State);
    renew_race_code -> renew_race_code_handler(Data, State);
    renew_race_public_tables -> renew_race_public_tables_handler(Data, State);
    put_race_code -> State#state{race_code = Data};
    put_public_tables -> State#state{public_tables = Data};
    put_named_tables -> State#state{named_tables = Data};
    put_behaviour_api_calls -> State#state{beh_api_calls = Data}
  end.

handle_call(Query,
            #state{race_code = RaceCode,
                   public_tables = PublicTables,
                   named_tables = NamedTables,
                   beh_api_calls = BehApiCalls}
            = State) ->
  case Query of
    dup -> spawn_link(fun() -> loop(State) end);
    get_race_code -> RaceCode;
    get_public_tables -> PublicTables;
    get_named_tables -> NamedTables;
    get_behaviour_api_calls -> BehApiCalls
  end.

%%----------------------------------------------------------------------

renew_race_info_handler({RaceCode, PublicTables, NamedTables},
                        #state{} = State) ->
  State#state{race_code = RaceCode,
              public_tables = PublicTables,
              named_tables = NamedTables}.

renew_race_code_handler({Fun, FunArgs, Code},
                        #state{race_code = RaceCode} = State) ->
  State#state{race_code = dict:store(Fun, [FunArgs, Code], RaceCode)}.

renew_race_public_tables_handler(VarLabel,
                                 #state{public_tables = PT} = State) ->
  State#state{public_tables = ordsets:add_element(VarLabel, PT)}.