summaryrefslogtreecommitdiff
path: root/docs/programmer_reference/stl_primitive_rw.html
blob: be44c1bee4377d83947b75fd7b1c25e82e4a93e8 (plain)
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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Working with primitive types</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
    <link rel="up" href="stl.html" title="Chapter 7. Standard Template Library API" />
    <link rel="prev" href="stl_mt_usage.html" title="Using dbstl in multithreaded applications" />
    <link rel="next" href="stl_complex_rw.html" title="Store and Retrieve data or objects of complex types" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 12.1.6.1</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Working with primitive types </th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="stl_mt_usage.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 7. Standard Template Library API</th>
          <td width="20%" align="right"> <a accesskey="n" href="stl_complex_rw.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="stl_primitive_rw"></a>Working with primitive types </h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <dl>
          <dt>
            <span class="sect2">
              <a href="stl_primitive_rw.html#idp849056">Storing strings</a>
            </span>
          </dt>
        </dl>
      </div>
      <p> 
        To store simple primitive types such as
        <code class="literal">int</code>, <code class="literal">long</code>,
        <code class="literal">double</code>, and so forth, an additional
        type parameter for the container class templates is needed.
        For example, to store an <code class="literal">int</code> in a
        <code class="classname">db_vector</code>, use this container
        class:
    </p>
      <pre class="programlisting">db_vector&lt;int, ElementHolder&lt;int&gt; &gt;;</pre>
      <p> 
        To map integers to doubles, use this: 
    </p>
      <pre class="programlisting">db_map&lt;int, double, ElementHolder&lt;double&gt; &gt;;</pre>
      <p> 
        To store a <code class="literal">char*</code> string with
        <code class="literal">long</code> keys, use this: 
    </p>
      <pre class="programlisting">db_map&lt;long, char*, ElementHolder&lt;char*&gt; &gt;;</pre>
      <p> 
        Use this for <code class="literal">const char*</code> strings:
    </p>
      <pre class="programlisting">db_map&lt;long, const char*, ElementHolder&lt;const char*&gt; &gt;;</pre>
      <p> 
        To map one const string to another, use this type:
    </p>
      <pre class="programlisting">db_map&lt;const char*, const char*, ElementHolder&lt;const char*&gt; &gt;;</pre>
      <p> 
        The
        <code class="methodname">StlAdvancedFeaturesExample::primitive()</code>
        method demonstrates more of these examples. 
    </p>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp849056"></a>Storing strings</h3>
            </div>
          </div>
        </div>
        <p>
            For <code class="literal">char*</code> and
            <code class="literal">wchar_t*</code> strings,
            <code class="methodname">_DB_STL_StoreElement()</code> must
            be called following partial or total modifications before
            iterator movement,
            <code class="literal">container::operator[]</code> or
            <code class="literal">iterator::operator*/-&gt;</code> calls.
            Without the
            <code class="methodname">_DB_STL_StoreElement()</code> call,
            the modified change will be lost. If storing an new value
            like this:
        </p>
        <pre class="programlisting">*iterator = new_char_star_string;</pre>
        <p>
            the call to
            <code class="methodname">_DB_STL_StoreElement()</code> is not
            needed.
        </p>
        <p> 
            Note that passing a NULL pointer to a container of
            <code class="literal">char*</code> type or passing a
            <code class="classname">std::string</code> with no contents at
            all will insert an empty string of zero length into the
            database.
        </p>
        <p> 
            The string returned from a container will not live
            beyond the next iterator movement call,
            <code class="literal">container::operator[]</code> or
            <code class="literal">iterator::operator*/-&gt;</code> call.
        </p>
        <p>
            A <span class="bold"><strong>db_map::value_type::second_type</strong></span> or
            <span class="bold"><strong>db_map::datatype_wrap</strong></span>
            should be used to hold a reference to a
            <code class="literal">container::operator[]</code> return value.
            Then the reference should be used for repeated references
            to that value. The *iterator is of type
            <code class="literal">ElementHolder&lt;char *&gt;</code>, which
            can be automatically converted to a <code class="literal">char*</code> pointer 
            using its type conversion
            operator. Wherever an auto conversion is done by the
            compiler, the conversion operator of
            <code class="literal">ElementHolder&lt;T&gt;</code> is called.
            This avoids almost all explicit conversions, except for
            two use cases: 
        </p>
        <div class="orderedlist">
          <ol type="1">
            <li>
              <p> 
                    The *iterator is used as a "..." parameter like
                    this: 
                </p>
              <pre class="programlisting">printf("this is the special case %s", *iterator);</pre>
              <p> 
                    This compiles but causes errors. Instead, an
                    explicit cast should be used:
                </p>
              <pre class="programlisting">printf("this is the special case %s", (char *)*iterator);</pre>
            </li>
            <li>
              <p>
                    For some old compilers, such as gcc3.4.6, the
                    *iterator cannot be used with the ternary
                    <code class="literal">?</code> operator, like this: 
                </p>
              <pre class="programlisting">expr ? *iterator : var</pre>
              <p> 
                    Even when <span class="bold"><strong>var</strong></span>
                    is the same type as the iterator's
                    <code class="literal">value_type</code>, the compiler
                    fails to perform an auto conversion. 
                </p>
            </li>
          </ol>
        </div>
        <p>
            When using <code class="classname">std::string</code> or
            <code class="classname">std::wstring</code> as the data type
            for dbstl containers — that is,
            <code class="classname">db_vector&lt;string&gt;</code>, and
            <code class="classname">db_map&lt;string, wstring&gt;</code>
            — the string's content rather than the string object
            itself is stored in order to maintain persistence.
        </p>
        <p> 
            You can find example code demonstrating string storage
            in the
            <code class="methodname">StlAdvancedFeaturesExample::char_star_string_storage()</code>
            and
            <code class="methodname">StlAdvancedFeaturesExample::storing_std_strings()</code>
            methods. 
        </p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="stl_mt_usage.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="stl.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="stl_complex_rw.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Using dbstl in multithreaded
        applications </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Store and Retrieve data or
        objects of complex types </td>
        </tr>
      </table>
    </div>
  </body>
</html>