File: node129.html

package info (click to toggle)
babel 0.10.2-1
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 43,932 kB
  • ctags: 29,707
  • sloc: java: 74,695; ansic: 73,142; cpp: 40,649; sh: 18,411; f90: 10,062; fortran: 6,727; python: 6,406; makefile: 3,866; xml: 118; perl: 48
file content (276 lines) | stat: -rw-r--r-- 9,565 bytes parent folder | download | duplicates (2)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<!--Converted with LaTeX2HTML 2002-2-1 (1.70)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>Accessing SIDL Arrays From C++</TITLE>
<META NAME="description" CONTENT="Accessing SIDL Arrays From C++">
<META NAME="keywords" CONTENT="users_guide">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">

<META NAME="Generator" CONTENT="LaTeX2HTML v2002-2-1">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

<LINK REL="STYLESHEET" HREF="users_guide.css">

<LINK REL="next" HREF="node130.html">
<LINK REL="previous" HREF="node128.html">
<LINK REL="up" HREF="node120.html">
<LINK REL="next" HREF="node130.html">
</HEAD>

<BODY >

<DIV CLASS="navigation"><!--Navigation Panel-->
<A NAME="tex2html2639"
  HREF="node130.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html2633"
  HREF="node120.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html2627"
  HREF="node128.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html2635"
  HREF="node14.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
<A NAME="tex2html2637"
  HREF="node317.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html2640"
  HREF="node130.html">C++ Specific Babel Command</A>
<B> Up:</B> <A NAME="tex2html2634"
  HREF="node120.html">C++ Bindings</A>
<B> Previous:</B> <A NAME="tex2html2628"
  HREF="node128.html">Implementing SIDL Classes in</A>
 &nbsp; <B>  <A NAME="tex2html2636"
  HREF="node14.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html2638"
  HREF="node317.html">Index</A></B> 
<BR>
<BR></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION03290000000000000000"></A>
<A NAME="6667"></A>
<BR>
Accessing SIDL Arrays From C++
</H1>
Although it is  feasible to expose the underlying C array API to create, 
destroy and access array elements and meta-data, the C++ bindings
provide a <TT>sidl::array&lt;T&gt;</TT> template mechanism that is more in keeping with C++ idioms. 

<P>
For SIDL built-in types, template specializations of <TT>sidl::array&lt;T&gt;</TT>
are defined in <TT>sidl_cxx.hh</TT>. 
For SIDL interface and classes, the array template is
again specialized in the corresponding stub header. 
The reason for the extensive use of template specialization is an effort 
to hide the detail that the array  implementation is really templated on three terms: 
the type of the C struct that represents the array internally, 
the internal representation of each item in the array, 
and the C++ representation of each item in the array. 
(See <TT>array_mixin</TT> in <TT>sidl_cxx.hh</TT> for grungy implementation details.) 

<P>
An example is given below. 

<P>
<BR>
<PRE  CLASS="verbatim">int32_t len = 10; // array length=10
int32_t dim = 1;  // one dimensional
int32_t lower[1] = {0}; // zero offset
int32_t upper[1] = {len-1};
int32_t prime = nextPrime(0);

// create a SIDL array of primes.
sidl::array&lt;int32_t&gt; a = sidl::array&lt;int32_t&gt;::createRow(dim, lower, upper);
for( int i=0; i&lt;len; ++i ) { 
   prime = nextPrime( prime );
   a.set(i, v);
}
</PRE></td></tr></table></blockquote>
<P>
Of course, the example above is only one way to create an array. 
The list of member functions for all C++ array classes is: 

<P>
<BR>
<PRE  CLASS="verbatim">// constructors
array ( array_ior_t * src ); // internal
array () ;                   // empty

// destructor
~array() ;

// creation 
static array&lt;x&gt;
createRow( int32_t dimen, const int32_t lower[], 
           const int32_t upper[]);
static array&lt;x&gt;
createCol( int32_t dimen, const int32_t lower[], 
           const int32_t upper[]);
static array&lt;x&gt;
create1d( int32_t len);
static array&lt;x&gt;
create2dCol( int32_t m, int32_t n);
static array&lt;x&gt;
create2dRow( int32_t m, int32_t n);
array&lt;x&gt;
slice( int32_t dimen, const int32_t numElem[], 
       const int32_t *srcStart = 0,
       const int32_t *srcStride = 0,
       const int32_t *newStart = 0);

void borrow( item_ior_t * first_element, int32_t dimen,
        const int32_t lower[], const int32_t upper[], 
        const int32_t stride[]);

void addRef();
void deleteRef();

// get/set
item_cxx_wrapper_t get(int32_t i);
item_cxx_wrapper_t get(int32_t i1, int32_t i2);
item_cxx_wrapper_t get(int32_t i1, int32_t i2, int32_t i3);
item_cxx_wrapper_t get(int32_t i1, int32_t i2, int32_t i3, int32_t i4);
item_cxx_wrapper_t get(const int32_t *indices);

void set(int32_t i, item_cxx_wrapper_t elem);
void set(int32_t i1, int32_t i2, item_cxx_wrapper_t elem);
void set(int32_t i1, int32_t i2, int32_t i3,
	 item_cxx_wrapper_t elem);
void set(int32_t i1, int32_t i2, int32_t i3, int32_t i4,
	 item_cxx_wrapper_t elem);
void set(const int32_t *indices, item_cxx_wrapper_t elem);

// other accessors
int32_t dimen() const;

int32_t lower( int32_t dim ) const;

int32_t upper( int32_t dim ) const;

int32_t stride( int32_t dim ) const;

bool _is_nil() const;

bool _not_nil() const;

// get a const pointer to the actual array ior 
const array_ior_t* _get_ior() const { return d_array; }

// get a non-const pointer to the actual array ior
array_ior_t* _get_ior() { return d_array;}
</PRE></td></tr></table></blockquote>
<P>
where 

<P>

<UL>
<LI><TT>array_ior_t</TT> is the type of the C struct that represents the array internally, 
</LI>
<LI><TT>item_ior_t</TT> is the internal representation of each item in the array, 
</LI>
<LI><TT>item_cxx_wrapper_t</TT> is the C++ representation of each item in the array 
</LI>
</UL>

<P>
Please note that all SIDL array constructors are static methods
returning a newly allocated array. Normally, you assign the return
value to a variable.

<P>
The C++ mapping for r-arrays is essentially identical to the mapping
for C (see Section&nbsp;<A HREF="node114.html#s:C_arrays">6.4</A>). The only difference is that the
C++ client header provides an overloaded version of each method
containing an r-array taking normal SIDL arrays instead of raw
data. For example, the <TT>solve</TT> method from
Section&nbsp;<A HREF="node60.html#ss:r-arrays">5.4</A> produces the following code in the
client-side header file.

<P>
<BR>
<PRE  CLASS="verbatim">    void solve (/*in*/    double* A,
                /*inout*/ double* x,
                /*in*/    double* b,
                /*in*/    int32_t m,
                /*in*/    int32_t n) throw ();

    void solve (/*in*/    ::sidl::array&lt;double&gt; A,
                /*inout*/ ::sidl::array&lt;double&gt;&amp; x,
                /*in*/    ::sidl::array&lt;double&gt; b) throw();
</PRE></td></tr></table></blockquote>
<P>
Please note that multi-dimensional arrays, such as <TT>A</TT> in this
case, are stored in column-major ordering. Babel provides macros to
access r-array data correctly. In this example, you can use
<TT>RarrayElem2(A, i, j, m)</TT> to access the element in row <TT>i</TT>
and column <TT>j</TT>. There are similar macros for arrays of dimension
1 through 7 in <TT>sidlArray.h</TT>.

<P>
If you were implementing <TT>solve</TT> in C++, the Babel generated
implementation file for it would look like this:

<P>
<BR>
<PRE  CLASS="verbatim">void num::Linsol_impl::solve (/*in*/    double* A,
                              /*inout*/ double* x,
                              /*in*/    double* b,
                              /*in*/    int32_t m,
                              /*in*/    int32_t n ) throw () 
{
  // DO-NOT-DELETE splicer.begin(num.Linsol.solve)
  // insert implementation here
  // DO-NOT-DELETE splicer.end(num.Linsol.solve)
}
</PRE></td></tr></table></blockquote>
<P>
To access memory by stride one make the row index your inner loop and
the column index your outer loop.

<P>

<DIV CLASS="navigation"><HR>
<!--Navigation Panel-->
<A NAME="tex2html2639"
  HREF="node130.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html2633"
  HREF="node120.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html2627"
  HREF="node128.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html2635"
  HREF="node14.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
<A NAME="tex2html2637"
  HREF="node317.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html2640"
  HREF="node130.html">C++ Specific Babel Command</A>
<B> Up:</B> <A NAME="tex2html2634"
  HREF="node120.html">C++ Bindings</A>
<B> Previous:</B> <A NAME="tex2html2628"
  HREF="node128.html">Implementing SIDL Classes in</A>
 &nbsp; <B>  <A NAME="tex2html2636"
  HREF="node14.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html2638"
  HREF="node317.html">Index</A></B> </DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<br><br>babel-0.10.2<br>users_guide Last Modified 2005-03-23<br><br><a href="http://www.llnl.gov/CASC/components">http://www.llnl.gov/CASC/components</a><br><a href="mailto:components@llnl.gov">components@llnl.gov</a>
</ADDRESS>
</BODY>
</HTML>