1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2012-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21-module(asn1ct_table).
22
23%% Table abstraction module for ASN.1 compiler
24
25-export([new/1]).
26-export([new_reuse/1]).
27-export([exists/1]).
28-export([size/1]).
29-export([insert/2]).
30-export([lookup/2]).
31-export([match/2]).
32-export([to_list/1]).
33-export([delete/1]).
34
35
36%% Always create a new table.
37new(Table) ->
38    undefined = get(Table),			%Assertion.
39    TableId = ets:new(Table, []),
40    put(Table, TableId).
41
42%% Only create it if it doesn't exist yet.
43new_reuse(Table) ->
44    not exists(Table) andalso new(Table).
45
46exists(Table) -> get(Table) =/= undefined.
47
48size(Table) -> ets:info(get(Table), size).
49
50insert(Table, Tuple) -> ets:insert(get(Table), Tuple).
51
52lookup(Table, Key) -> ets:lookup(get(Table), Key).
53
54match(Table, MatchSpec) -> ets:match(get(Table), MatchSpec).
55
56to_list(Table) -> ets:tab2list(get(Table)).
57
58%% Deleting tables is no longer strictly necessary since each compilation
59%% runs in separate process, but it will reduce memory consumption
60%% especially when many compilations are run in parallel.
61
62delete(Tables) when is_list(Tables) ->
63    [delete(T) || T <- Tables],
64    true;
65delete(Table) when is_atom(Table) ->
66    case erase(Table) of
67        undefined ->
68            true;
69        TableId ->
70            ets:delete(TableId)
71    end.
72