| 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>Working with primitive 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_mt_usage.html" title="Using dbstl in multithreaded applications" /> |
| 12 | <link rel="next" href="stl_complex_rw.html" title="Store and Retrieve data or objects of complex types" /> |
| 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">Working with primitive types </th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="stl_mt_usage.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_complex_rw.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_primitive_rw"></a>Working with primitive types </h2> |
| 36 | </div> |
| 37 | </div> |
| 38 | </div> |
| 39 | <div class="toc"> |
| 40 | <dl> |
| 41 | <dt> |
| 42 | <span class="sect2"> |
| 43 | <a href="stl_primitive_rw.html#idp51467888">Storing strings</a> |
| 44 | </span> |
| 45 | </dt> |
| 46 | </dl> |
| 47 | </div> |
| 48 | <p> |
| 49 | To store simple primitive types such as <code class="literal">int</code>, |
| 50 | <code class="literal">long</code>, <code class="literal">double</code>, and so forth, an |
| 51 | additional type parameter for the container class |
| 52 | templates is needed. For example, to store an <code class="literal">int</code> |
| 53 | in a <code class="classname">db_vector</code>, use this container class: |
| 54 | </p> |
| 55 | <pre class="programlisting">db_vector<int, ElementHolder<int> >;</pre> |
| 56 | <p> |
| 57 | To map integers to doubles, use this: |
| 58 | </p> |
| 59 | <pre class="programlisting">db_map<int, double, ElementHolder<double> >;</pre> |
| 60 | <p> |
| 61 | To store a <code class="literal">char*</code> string with <code class="literal">long</code> keys, |
| 62 | use this: |
| 63 | </p> |
| 64 | <pre class="programlisting">db_map<long, char*, ElementHolder<char*> >;</pre> |
| 65 | <p> |
| 66 | Use this for <code class="literal">const char*</code> strings: |
| 67 | </p> |
| 68 | <pre class="programlisting">db_map<long, const char*, ElementHolder<const char*> >;</pre> |
| 69 | <p> |
| 70 | To map one const string to another, use this type: |
| 71 | </p> |
| 72 | <pre class="programlisting">db_map<const char*, const char*, ElementHolder<const char*> >;</pre> |
| 73 | <p> |
| 74 | The <code class="methodname">StlAdvancedFeaturesExample::primitive()</code> method demonstrates more of these examples. |
| 75 | </p> |
| 76 | <div class="sect2" lang="en" xml:lang="en"> |
| 77 | <div class="titlepage"> |
| 78 | <div> |
| 79 | <div> |
| 80 | <h3 class="title"><a id="idp51467888"></a>Storing strings</h3> |
| 81 | </div> |
| 82 | </div> |
| 83 | </div> |
| 84 | <p> |
| 85 | For <code class="literal">char*</code> and <code class="literal">wchar_t*</code> strings, |
| 86 | <code class="methodname">_DB_STL_StoreElement()</code> must be called |
| 87 | following partial or total modifications before iterator movement, |
| 88 | <code class="literal">container::operator[]</code> or |
| 89 | <code class="literal">iterator::operator*/-></code> calls. Without the |
| 90 | <code class="methodname">_DB_STL_StoreElement()</code> call, the modified |
| 91 | change will be lost. If storing an new value like this: |
| 92 | </p> |
| 93 | <pre class="programlisting">*iterator = new_char_star_string;</pre> |
| 94 | <p> |
| 95 | the call to <code class="methodname">_DB_STL_StoreElement()</code> is not needed. |
| 96 | </p> |
| 97 | <p> |
| 98 | Note that passing a NULL pointer to a container of |
| 99 | <code class="literal">char*</code> type or passing a |
| 100 | <code class="classname">std::string</code> with no contents at all will insert |
| 101 | an empty string of zero length into the database. |
| 102 | </p> |
| 103 | <p> |
| 104 | The string returned from a container will not live beyond the next |
| 105 | iterator movement call, <code class="literal">container::operator[]</code> or |
| 106 | <code class="literal">iterator::operator*/-></code> call. |
| 107 | </p> |
| 108 | <p> |
| 109 | |
| 110 | A <span class="bold"><strong>db_map::value_type::second_type</strong></span> or |
| 111 | <span class="bold"><strong>db_map::datatype_wrap</strong></span> should be used |
| 112 | to hold a reference to a <code class="literal">container::operator[]</code> |
| 113 | return value. Then the reference should be used for repeated |
| 114 | references to that value. The *iterator is of type |
| 115 | <code class="literal">ElementHolder<char *></code>, which can be automatically converted to a |
| 116 | <code class="literal">char *</code> pointer using its type conversion operator. |
| 117 | Wherever an auto conversion is done by the compiler, the conversion |
| 118 | operator of <code class="literal">ElementHolder<T></code> is called. This |
| 119 | avoids almost all explicit conversions, except for two use cases: |
| 120 | |
| 121 | </p> |
| 122 | <div class="orderedlist"> |
| 123 | <ol type="1"> |
| 124 | <li> |
| 125 | <p> |
| 126 | The *iterator is used as a "..." parameter like this: |
| 127 | </p> |
| 128 | <pre class="programlisting">printf("this is the special case %s", *iterator);</pre> |
| 129 | <p> |
| 130 | This compiles but causes errors. Instead, an explicit cast |
| 131 | should be used: |
| 132 | </p> |
| 133 | <pre class="programlisting">printf("this is the special case %s", (char *)*iterator);</pre> |
| 134 | </li> |
| 135 | <li> |
| 136 | <p> |
| 137 | For some old compilers, such as gcc3.4.6, the *iterator cannot be |
| 138 | used with the ternary <code class="literal">?</code> operator, like |
| 139 | this: |
| 140 | </p> |
| 141 | <pre class="programlisting">expr ? *iterator : var</pre> |
| 142 | <p> |
| 143 | Even when <span class="bold"><strong>var</strong></span> is the same |
| 144 | type as the iterator's <code class="literal">value_type</code>, the |
| 145 | compiler fails to perform an auto conversion. |
| 146 | </p> |
| 147 | </li> |
| 148 | </ol> |
| 149 | </div> |
| 150 | <p> |
| 151 | When using <code class="classname">std::string</code> or |
| 152 | <code class="classname">std::wstring</code> as the data type for dbstl |
| 153 | containers — that is, <code class="classname">db_vector<string></code>, |
| 154 | and <code class="classname">db_map<string, wstring></code> — the |
| 155 | string's content rather than the string object itself is stored in order |
| 156 | to maintain persistence. |
| 157 | </p> |
| 158 | <p> |
| 159 | You can find example code demonstrating string storage in the |
| 160 | <code class="methodname">StlAdvancedFeaturesExample::char_star_string_storage()</code> and |
| 161 | <code class="methodname">StlAdvancedFeaturesExample::storing_std_strings()</code> methods. |
| 162 | </p> |
| 163 | </div> |
| 164 | </div> |
| 165 | <div class="navfooter"> |
| 166 | <hr /> |
| 167 | <table width="100%" summary="Navigation footer"> |
| 168 | <tr> |
| 169 | <td width="40%" align="left"><a accesskey="p" href="stl_mt_usage.html">Prev</a> </td> |
| 170 | <td width="20%" align="center"> |
| 171 | <a accesskey="u" href="stl.html">Up</a> |
| 172 | </td> |
| 173 | <td width="40%" align="right"> <a accesskey="n" href="stl_complex_rw.html">Next</a></td> |
| 174 | </tr> |
| 175 | <tr> |
| 176 | <td width="40%" align="left" valign="top">Using dbstl in multithreaded applications </td> |
| 177 | <td width="20%" align="center"> |
| 178 | <a accesskey="h" href="index.html">Home</a> |
| 179 | </td> |
| 180 | <td width="40%" align="right" valign="top"> Store and Retrieve data or objects of complex types </td> |
| 181 | </tr> |
| 182 | </table> |
| 183 | </div> |
| 184 | </body> |
| 185 | </html> |