~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/ic/doc/src/ch_erl_plain.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20090215164252-dxpjjuq108nz4noa
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
 
3
 
 
4
<chapter>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>1998</year>
 
8
      <year>2008</year>
 
9
      <holder>Ericsson AB, All Rights Reserved</holder>
 
10
    </copyright>
 
11
    <legalnotice>
 
12
  The contents of this file are subject to the Erlang Public License,
 
13
  Version 1.1, (the "License"); you may not use this file except in
 
14
  compliance with the License. You should have received a copy of the
 
15
  Erlang Public License along with this software. If not, it can be
 
16
  retrieved online at http://www.erlang.org/.
 
17
 
 
18
  Software distributed under the License is distributed on an "AS IS"
 
19
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
20
  the License for the specific language governing rights and limitations
 
21
  under the License.
 
22
 
 
23
  The Initial Developer of the Original Code is Ericsson AB.
 
24
    </legalnotice>
 
25
 
 
26
    <title>Using the Plain Erlang Back-end</title>
 
27
    <prepared></prepared>
 
28
    <docno></docno>
 
29
    <date>98-05-06</date>
 
30
    <rev>B</rev>
 
31
  </header>
 
32
 
 
33
  <section>
 
34
    <title>Introduction</title>
 
35
    <p>The mapping of OMG IDL to the Erlang programming language when
 
36
      Plain Erlang 
 
37
      is the back-end of choice is similar to the one used in pure Erlang IDL
 
38
      mapping. The only difference is on the generated code and the extended
 
39
      use of pragmas for code generation: IDL functions are translated
 
40
      to Erlang
 
41
      module function calls.</p>
 
42
  </section>
 
43
 
 
44
  <section>
 
45
    <title>Compiling the Code</title>
 
46
    <p>In the Erlang shell type :</p>
 
47
    <p>ic:gen(<c><![CDATA[<filename>, [{be, erl_plain}])]]></c>.</p>
 
48
  </section>
 
49
 
 
50
  <section>
 
51
    <title>Writing the Implementation File</title>
 
52
    <p>For each IDL interface <c><![CDATA[<interface name>]]></c> defined in the IDL file:</p>
 
53
    <list type="bulleted">
 
54
      <item>Create the corresponding Erlang file that will hold the
 
55
       Erlang implementation of the IDL definitions.  </item>
 
56
      <item>Call the implementation file after the scope of the IDL interface, 
 
57
       followed by the suffix <c>_impl</c>.</item>
 
58
      <item>Export the implementation functions.</item>
 
59
    </list>
 
60
    <p>For each function defined in the IDL interface :</p>
 
61
    <list type="bulleted">
 
62
      <item>Implement an Erlang function that uses as arguments in the same
 
63
       order, as the input arguments described in the IDL file, and returns 
 
64
       the value described in the interface.</item>
 
65
      <item>When using the function, follow the mapping described in chapter 2.</item>
 
66
    </list>
 
67
  </section>
 
68
 
 
69
  <section>
 
70
    <title>An Example</title>
 
71
    <p>      <marker id="plain_idl"></marker>
 
72
 
 
73
      In this example, a file "random.idl" is generates code for the plain Erlang
 
74
      back-end :</p>
 
75
    <list type="bulleted">
 
76
      <item>
 
77
        <p>Main file : "plain.idl"</p>
 
78
        <code type="none">
 
79
\011   
 
80
module rmod {
 
81
 
 
82
  interface random {
 
83
 
 
84
    double produce();
 
85
 
 
86
    oneway void init(in long seed1, in long seed2, in long seed3);
 
87
 
 
88
  };
 
89
 
 
90
};
 
91
        </code>
 
92
      </item>
 
93
    </list>
 
94
    <p>Compile the file :</p>
 
95
    <code type="none">
 
96
      Erlang (BEAM) emulator version 4.9
 
97
      
 
98
      Eshell V4.9  (abort with ^G)
 
99
      1> ic:gen(random,[{be, erl_plain}]).
 
100
      Erlang IDL compiler version 2.5.1
 
101
      ok
 
102
      2> 
 
103
    </code>
 
104
    <p></p>
 
105
    <p>When the file "random.idl" is compiled it produces five files: two for 
 
106
      the top scope, two for the interface scope, and one for the module
 
107
      scope. The header files for top scope and interface
 
108
      are empty and not shown here. In this case only the file for the interface 
 
109
      <c>rmod_random.erl</c> is important :. 
 
110
            <marker id="generated files"></marker>
 
111
</p>
 
112
    <list type="bulleted">
 
113
      <item>
 
114
        <p>Erlang file for interface : "rmod_random.erl"</p>
 
115
        <code type="none">
 
116
 
 
117
-module(rmod_random).
 
118
 
 
119
 
 
120
 
 
121
%% Interface functions
 
122
-export([produce/0, init/3]).
 
123
 
 
124
%%------------------------------------------------------------
 
125
%% Operation: produce
 
126
%% 
 
127
%%   Returns: RetVal
 
128
%%
 
129
produce() ->
 
130
    rmod_random_impl:produce().
 
131
 
 
132
%%------------------------------------------------------------
 
133
%% Operation: init
 
134
%% 
 
135
%%   Returns: RetVal
 
136
%%
 
137
init(Seed1, Seed2, Seed3) ->
 
138
    rmod_random_impl:init(Seed1, Seed2, Seed3).
 
139
        </code>
 
140
      </item>
 
141
    </list>
 
142
    <p>The implementation file should be called <c>rmod_random_impl.erl</c> 
 
143
      and could look like this:</p>
 
144
    <code type="none">
 
145
      -module('rmod_random_impl').
 
146
      
 
147
      -export([produce/0,init/3]).
 
148
      
 
149
      
 
150
      produce()  ->
 
151
        random:uniform().
 
152
       
 
153
      
 
154
      init(S1,S2,S3)  ->
 
155
        random:seed(S1,S2,S3).
 
156
    </code>
 
157
    <p>Compiling the code : </p>
 
158
    <code type="none">
 
159
2> make:all().
 
160
Recompile: rmod_random
 
161
Recompile: oe_random
 
162
Recompile: rmod_random_impl
 
163
up_to_date
 
164
    </code>
 
165
    <p></p>
 
166
    <p>Running the example : </p>
 
167
    <code type="none">
 
168
3>  rmod_random:init(1,2,3).
 
169
ok
 
170
4>  rmod_random:produce().  
 
171
1.97963e-4
 
172
5> 
 
173
    </code>
 
174
  </section>
 
175
</chapter>
 
176