mirror of
https://github.com/berkeleydb/libdb.git
synced 2024-11-16 09:06:25 +00:00
185 lines
8.2 KiB
HTML
185 lines
8.2 KiB
HTML
<?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 11.2.5.3</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#idp1288424">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<int, ElementHolder<int> >;</pre>
|
||
<p>
|
||
To map integers to doubles, use this:
|
||
</p>
|
||
<pre class="programlisting">db_map<int, double, ElementHolder<double> >;</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<long, char*, ElementHolder<char*> >;</pre>
|
||
<p>
|
||
Use this for <code class="literal">const char*</code> strings:
|
||
</p>
|
||
<pre class="programlisting">db_map<long, const char*, ElementHolder<const char*> >;</pre>
|
||
<p>
|
||
To map one const string to another, use this type:
|
||
</p>
|
||
<pre class="programlisting">db_map<const char*, const char*, ElementHolder<const char*> >;</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="idp1288424"></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*/-></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*/-></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<char *></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<T></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<string></code>,
|
||
and <code class="classname">db_map<string, wstring></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>
|