Debian Codegraph @

docs/programmer_reference/stl_complex_rw.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Store and Retrieve data or objects of complex types</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="stl.html" title="Chapter 7. Standard Template Library API" />
11 <link rel="prev" href="stl_primitive_rw.html" title="Working with primitive types" />
12 <link rel="next" href="stl_persistence.html" title="Dbstl persistence" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Store and Retrieve data or objects of complex types </th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="stl_primitive_rw.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 7. Standard Template Library API</th>
26 <td width="20%" align="right"> <a accesskey="n" href="stl_persistence.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title" style="clear: both"><a id="stl_complex_rw"></a>Store and Retrieve data or objects of complex types </h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <dl>
41 <dt>
42 <span class="sect2">
43 <a href="stl_complex_rw.html#idp51458752">Storing varying length objects</a>
44 </span>
45 </dt>
46 <dt>
47 <span class="sect2">
48 <a href="stl_complex_rw.html#idp51477944">Storing arbitrary sequences</a>
49 </span>
50 </dt>
51 <dt>
52 <span class="sect2">
53 <a href="stl_complex_rw.html#idp51524696">Notes</a>
54 </span>
55 </dt>
56 </dl>
57 </div>
58 <div class="sect2" lang="en" xml:lang="en">
59 <div class="titlepage">
60 <div>
61 <div>
62 <h3 class="title"><a id="idp51458752"></a>Storing varying length objects</h3>
63 </div>
64 </div>
65 </div>
66 <p>
67 A structure like this:
68 </p>
69 <pre class="programlisting">class SMSMsg
70 {
71 public:
72 size_t mysize;
73 time_t when;
74 size_t szmsg;
75 int to;
76 char msg[1];
77 }; </pre>
78 <p>
79 with a varying length string in <code class="literal">msg</code> cannot simply be
80 stored in a <code class="literal">db_vector&lt;SMSMsg&gt;</code> without some
81 configuration on your part. This is because, by default, dbstl uses the
82 <span class="bold"><strong>sizeof()</strong></span> operator to get the size of
83 an object and then <code class="function">memcpy()</code> to copy the object. This
84 process is not suitable for this use-case as it will fail to capture the
85 variable length string contained in <code class="literal">msg</code>.
86 </p>
87 <p>
88 There are currently two ways to store these kind of objects:
89 </p>
90 <div class="orderedlist">
91 <ol type="1">
92 <li>
93 <p>
94 Register callback functions with dbstl that are used to measure an object's size,
95 and then marshal/unmarshal the object.
96 </p>
97 </li>
98 <li>
99 <p>
100 Use a <code class="classname">DbstlDbt</code> wrapper object.
101 </p>
102 </li>
103 </ol>
104 </div>
105 <div class="sect3" lang="en" xml:lang="en">
106 <div class="titlepage">
107 <div>
108 <div>
109 <h4 class="title"><a id="idp51457480"></a>Storing by marshaling objects</h4>
110 </div>
111 </div>
112 </div>
113 <p>
114 One way to store an object that contains variable-sized fields is
115 to marshall all of the object's data into a single contiguous area
116 in memory, and then store the contents of that buffer. This means that upon retrieval, the
117 contents of the buffer must be unmarshalled. To do these things,
118 you must register three callback functions:
119 </p>
120 <div class="itemizedlist">
121 <ul type="disc">
122 <li>
123 <p>
124 <code class="function">typedef void (*ElemRstoreFunct)(T&amp; dest, const void *srcdata);</code>
125 </p>
126 <p>
127 This callback is used to unmarshal an object, updating <span class="bold"><strong>dest</strong></span>
128 using data found in <span class="bold"><strong>srcdata</strong></span>. The data in
129 <span class="bold"><strong>srcdata </strong></span> contains the chunk of memory into which the
130 object was originally marshalled. The default unmarshalling function simply performs a cast
131 (for example, <code class="literal">dest = *((T*)srcdata)</code>), which assumes the
132 <span class="bold"><strong>srcdata</strong></span> simply points to the memory layout of the object.
133 </p>
134 </li>
135 <li>
136 <p>
137 <code class="function">typedef size_t (*ElemSizeFunct)(const T&amp; elem);</code>
138 </p>
139 <p>
140 This callback returns the size in bytes needed to store the
141 <span class="bold"><strong>elem</strong></span> object. By default this function
142 simply uses <span class="bold"><strong>sizeof(elem)</strong></span> to determine
143 the size of <span class="bold"><strong>elem</strong></span>.
144 </p>
145 </li>
146 <li>
147 <p>
148 <code class="function">typedef void (*ElemCopyFunct)(void *dest, const T&amp;elem);</code>
149 </p>
150 <p>
151 This callback is used to arrange all data contained by <span class="bold"><strong>elem</strong></span>
152 into the chunk of memory to which <span class="bold"><strong>dest</strong></span> refers. The size of
153 <span class="bold"><strong>dest</strong></span> is set by the <code class="function">ElemSizeFunct</code>
154 function, discussed above. The default marshalling function simply uses
155 <code class="function">memcpy()</code> to copy <span class="bold"><strong>elem</strong></span> to
156 <span class="bold"><strong>dest</strong></span>.
157 </p>
158 </li>
159 </ul>
160 </div>
161 <p>
162 The <code class="function">DbstlElemTraits&lt;SMSMsg&gt;::instance()-&gt;set_size_function()</code>,
163 <code class="function">set_copy_function()</code> and <code class="function">set_restore_function()</code> methods
164 are used to register these callback functions. If a callback is not registered, its
165 default function is used.
166 </p>
167 <p>
168 By providing non-default implementations of the callbacks described here, you can store objects
169 of varying length and/or objects which do not reside in a continuous memory chunk — for
170 example, objects containing a pointer which refers another object, or a string, and so forth.
171 As a result, containers/iterators can manage variable length objects in the same as they would
172 manage objects that reside in continuous chunks of memory and are of identical size.
173 </p>
174 </div>
175 <div class="sect3" lang="en" xml:lang="en">
176 <div class="titlepage">
177 <div>
178 <div>
179 <h4 class="title"><a id="idp51445440"></a>Using a <code class="classname">DbstlDbt</code> wrapper object</h4>
180 </div>
181 </div>
182 </div>
183 <p>
184 To use a <code class="classname">DbstlDbt</code> wrapper object to store objects of variable length, a
185 <code class="literal">db_vector&lt;DbstlDbt&gt;</code> container is used to store complex objects in a
186 <code class="classname">db_vector</code>. <code class="classname">DbstlDbt</code> derives from DB C++ API's
187 <code class="classname">Dbt</code>class, but can manage its referenced memory properly and release it
188 upon destruction. The memory referenced by <code class="classname">DbstlDbt</code> objects is required
189 to be allocated using the <code class="function">malloc()</code>/<code class="function">realloc()</code> functions
190 from the standard C library.
191 </p>
192 <p>
193 Note that the use of <code class="classname">DbstlDbt</code> wrapper class is not ideal. It exists only
194 to allow raw bytes of no specific type to be stored in a container.
195 </p>
196 <p>
197 To store an <code class="classname">SMSMsg</code> object into a <code class="literal">db_vector&lt;DbstlDbt&gt;</code>
198 container using a <code class="classname">DbstlDbt</code> object:
199 </p>
200 <div class="orderedlist">
201 <ol type="1">
202 <li>
203 Wrap the <code class="classname">SMSMSg</code> object into a <code class="classname">DbstlDbt</code> object,
204 then marshal the SMSMsg object properly into the memory chunk referenced by
205 <code class="methodname">DbstlDbt::data</code>.
206 </li>
207 <li>
208 Store the <code class="classname">DbstlDbt</code> object into a <code class="literal">db_vector&lt;DbstlDbt&gt;</code>
209 container. The bytes in the memory chunk referenced by the <code class="classname">DbstlDbt</code> object's
210 <span class="bold"><strong>data</strong></span> member are stored in the
211 <code class="literal">db_vector&lt;DbstlDbt&gt;</code> container.
212 </li>
213 <li>
214 Reading from the container returns a <code class="classname">DbstlDbt</code> object whose
215 <span class="bold"><strong>data</strong></span> field points to the <code class="classname">SMSMsg</code> object
216 located in a continuous chunk of memory. The application needs to perform its own unmarshalling.
217 </li>
218 <li>
219 The memory referenced by <code class="literal">DbstlDbt::data</code> is freed automatically,
220 and so the application should not attempt to free the memory.
221 </li>
222 </ol>
223 </div>
224 <p>
225 <code class="classname">ElementHolder</code> should not be used to store objects of a class because it
226 doesn't support access to object members using <span class="bold"><strong>(*iter).member</strong></span>
227 or <span class="bold"><strong>iter-&gt;member</strong></span> expressions. In this case, the default
228 <code class="literal">ElementRef&lt;ddt&gt;</code> is used automatically.
229 </p>
230 <p>
231 <code class="classname">ElementRef</code> inherits from <code class="classname">ddt</code>, which allows
232 <span class="bold"><strong>*iter</strong></span> to return the object stored in the container.
233 (Technically it is an <code class="classname">ElementRef&lt;ddt&gt; object</code>, whose "base class"
234 part is the object you stored). There are a few data members and member functions in
235 <code class="classname">ElementRef</code>, which all start with <code class="literal">_DB_STL_</code>. To avoid
236 potential name clashes, applications should not use names prefixing <code class="literal">_DB_STL_</code>
237 in classes whose instances may be stored into dbstl containers.
238 </p>
239 <p>
240 Example code demonstrating this feature can be found in the
241 <code class="methodname">StlAdvancedFeaturesExample::arbitrary_object_storage</code> method.
242 </p>
243 </div>
244 </div>
245 <div class="sect2" lang="en" xml:lang="en">
246 <div class="titlepage">
247 <div>
248 <div>
249 <h3 class="title"><a id="idp51477944"></a>Storing arbitrary sequences</h3>
250 </div>
251 </div>
252 </div>
253 <p>
254 A sequence is a group of related objects, such as an array, a string, and so forth.
255 You can store sequences of any structure using dbstl, so long as you implement and register the
256 proper callback functions. By using these callbacks, each object in the sequence can be a
257 complex object with data members that are all not stored in a continuous memory chunk.
258 </p>
259 <p>
260 Note that when using these callbacks, when you retrieve a stored sequence from the database, the
261 entire sequence will reside in a single continuous block of memory with the same layout as that
262 constructed by your sequence copy function.
263 </p>
264 <p>
265 For example, given a type RGB:
266 </p>
267 <pre class="programlisting">
268 struct RGB{char r, g, b, bright;}; </pre>
269 <p>
270 and an array of RGB objects, the following steps describe how to store an array into one
271 key/data pair of a <code class="classname">db_map</code> container.
272 </p>
273 <div class="orderedlist">
274 <ol type="1">
275 <li>
276 Use a <code class="classname">db_map&lt;int, RGB *, ElementHolder&lt;RGB *&gt; &gt;</code> container.
277 </li>
278 <li>
279 <p>
280 Define two functions. The first returns the number of objects in a sequence, the second that
281 copies objects from a sequence to a defined destination in memory:
282 </p>
283 <pre class="programlisting">typedef size_t (*SequenceLenFunct)(const RGB*); </pre>
284 <p>
285 and
286 </p>
287 <pre class="programlisting"> typedef void (*SequenceCopyFunct)(RGB*dest, const RGB*src); </pre>
288 </li>
289 <li>
290 Call DbstlElemTraits&lt;RGB&gt;::set_sequence_len_function()/set_sequence_copy_function()
291 to register them as callbacks.
292 </li>
293 </ol>
294 </div>
295 <div class="sect3" lang="en" xml:lang="en">
296 <div class="titlepage">
297 <div>
298 <div>
299 <h4 class="title"><a id="idp51487704"></a>The <code class="function">SequenceLenFunct</code> function</h4>
300 </div>
301 </div>
302 </div>
303 <pre class="programlisting">typedef size_t (*SequenceLenFunct)(const RGB*); </pre>
304 <p>
305 A <code class="function">SequenceLenFunct</code> function returns the number of objects in a sequence. It
306 is called when inserting into or reading from the database, so there must be enough information
307 in the sequence itself to enable the <code class="function">SequenceLenFunct</code> function to tell how many
308 objects the sequence contains. The <code class="literal">char*</code> and <code class="literal">wchar_t*</code>
309 strings use a <code class="literal">'\0'</code> special character to do this. For example, RGB(0, 0, 0, 0)
310 could be used to denote the end of the sequence. Note that for your implementation of this
311 callback, you are not required to use a
312 trailing object with a special value like <code class="literal">'\0'</code> or
313 <code class="literal">RGB(0, 0, 0, 0)</code> to denote the end of the sequence. You are free to use
314 what mechanism you want in your
315 <code class="function">SequenceLenFunct</code> function implementation to figure out the length of the sequence.
316 </p>
317 </div>
318 <div class="sect3" lang="en" xml:lang="en">
319 <div class="titlepage">
320 <div>
321 <div>
322 <h4 class="title"><a id="idp51519720"></a>The <code class="function">SequenceCopyFunct</code> function</h4>
323 </div>
324 </div>
325 </div>
326 <pre class="programlisting"> typedef void (*SequenceCopyFunct)(RGB*dest, const RGB*src); </pre>
327 <p>
328 <code class="function">SequenceCopyFunct</code> copies objects from the sequence
329 <span class="bold"><strong>src</strong></span> into memory chunk <span class="bold"><strong>dest</strong></span>.
330 If the objects in the sequence do not reside in a continuous memory chunk, this function must
331 marshal each object in the sequence into the <span class="bold"><strong>dest</strong></span> memory chunk.
332 </p>
333 <p>
334 The sequence objects will reside in the continuous memory chunk referred to by <span class="bold"><strong>dest</strong></span>, which has been sized by <code class="classname">SequenceLenFunct</code>
335 and <code class="classname">ElemSizeFunct</code> if available (which is when objects in the sequence are
336 of varying lengths). <code class="classname">ElemSizeFunct</code> function is not needed in this example
337 because <span class="bold"><strong>RGB</strong></span> is a simple fixed length type, the
338 <code class="literal">sizeof()</code> operator is sufficient to return the size of the sequence.
339 </p>
340 </div>
341 </div>
342 <div class="sect2" lang="en" xml:lang="en">
343 <div class="titlepage">
344 <div>
345 <div>
346 <h3 class="title"><a id="idp51524696"></a>Notes</h3>
347 </div>
348 </div>
349 </div>
350 <div class="itemizedlist">
351 <ul type="disc">
352 <li>
353 <p>
354 The get and set functions of this class are not protected by any mutexes. When using
355 multiple threads to access the function pointers, the callback functions must be
356 registered to the singleton of this class before any retrieval of the callback function
357 pointers. Isolation may also be required among multiple threads. The best way is to
358 register all callback function pointers in a single thread before making use of the any
359 containers.
360 </p>
361 </li>
362 <li>
363 <p>
364 If objects in a sequence are not of identical sizes, or are not located in a consecutive
365 chunk of memory, you also need to implement and register the
366 <code class="function">DbstlElemTraits&lt;&gt;::ElemSizeFunct</code> callback function to measure
367 the size of each object. When this function is registered, it is also used when
368 allocating memory space.
369 </p>
370 <p>
371 There is example code demonstrating the use this feature in the
372 <code class="methodname">StlAdvancedFeaturesExample::arbitray_sequence_storage()</code> method.
373 </p>
374 </li>
375 <li>
376 <p>
377 A consequence of this dbstl feature is that you can not store a pointer value directly
378 because dbstl will think it is a sequence head pointer. Instead, you need to convert the
379 pointer into a <code class="literal">long</code> and then store it into a <code class="literal">long</code>
380 container. And please note that pointer values are probably meaningless if the stored
381 value is to be used across different application run times.
382 </p>
383 </li>
384 </ul>
385 </div>
386 </div>
387 </div>
388 <div class="navfooter">
389 <hr />
390 <table width="100%" summary="Navigation footer">
391 <tr>
392 <td width="40%" align="left"><a accesskey="p" href="stl_primitive_rw.html">Prev</a> </td>
393 <td width="20%" align="center">
394 <a accesskey="u" href="stl.html">Up</a>
395 </td>
396 <td width="40%" align="right"> <a accesskey="n" href="stl_persistence.html">Next</a></td>
397 </tr>
398 <tr>
399 <td width="40%" align="left" valign="top">Working with primitive types  </td>
400 <td width="20%" align="center">
401 <a accesskey="h" href="index.html">Home</a>
402 </td>
403 <td width="40%" align="right" valign="top"> Dbstl persistence</td>
404 </tr>
405 </table>
406 </div>
407 </body>
408 </html>