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>Chapter 25. DbstlDbt</title> 7 <link rel="stylesheet" href="apiReference.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 C++ Standard Template Library API Reference" /> 10 <link rel="up" href="index.html" title="Berkeley DB C++ Standard Template Library API Reference" /> 11 <link rel="prev" href="stlElementRef_DB_STL_value.html" title="_DB_STL_value" /> 12 <link rel="next" href="stlDbstlDbtdstr_DbstlDbt.html" title="~DbstlDbt" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Chapter 25. 19 DbstlDbt </th> 20 </tr> 21 <tr> 22 <td width="20%" align="left"><a accesskey="p" href="stlElementRef_DB_STL_value.html">Prev</a> </td> 23 <th width="60%" align="center"> </th> 24 <td width="20%" align="right"> <a accesskey="n" href="stlDbstlDbtdstr_DbstlDbt.html">Next</a></td> 25 </tr> 26 </table> 27 <hr /> 28 </div> 29 <div class="chapter" lang="en" xml:lang="en"> 30 <div class="titlepage"> 31 <div> 32 <div> 33 <h2 class="title"><a id="DbstlDbt"></a>Chapter 25. 34 DbstlDbt </h2> 35 </div> 36 </div> 37 </div> 38 <p>You can persist all bytes in a chunk of contiguous memory by constructing an <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 39 object A(use malloc to allocate the required number of bytes for A.data and copy the bytes to be stored into A.data, set other fields as necessary) and store A into a container, e.g. </p> 40 <p>db_vector<DbstlDbt>, this stores the bytes rather than the object A into the underlying database. The <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 41 class can help you avoid memory leaks, so it is strongly recommended that you use <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 42 rather than Dbt class.</p> 43 <p><a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 44 derives from Dbt class, and it does an deep copy on copy construction and assignment --by calling malloc to allocate its own memory and then copying the bytes to it; Conversely the destructor will free the memory on destruction if the data pointer is non-NULL. The destructor assumes the memory is allocated via malloc, hence why you are required to call malloc to allocate memory in order to use <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 45.</p> 46 <p><a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 47 simply inherits all methods from Dbt with no extra new methods except the constructors/destructor and assignment operator, so it is easy to use.</p> 48 <p>In practice you rarely need to use <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 49 or Dbt because dbstl enables you to store any complex objects or primitive data. Only when you need to store raw bytes, e.g. a bitmap, do you need to use <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 50.</p> 51 <p>Hence, <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 52 is the right class to use to store any object into Berkeley DB via dbstl without memory leaks.</p> 53 <p>Don't free the memory referenced by <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 54 objects, it will be freed when the <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 55 object is destructed.</p> 56 <p>Please refer to the two examples using <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 57 in TestAssoc::test_arbitrary_object_storage and TestAssoc::test_char_star_string_storage member functions, which illustrate how to correctly use <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt">DbstlDbt</a> 58 in order to store raw bytes.</p> 59 <p>This class handles the task of allocating and de-allocating memory internally. Although it can be used to store data which cannot be handled by the <a class="link" href="DbstlElemTraits.html" title="Chapter 26. DbstlElemTraits">DbstlElemTraits</a> 60 class, in practice, it is usually more convenient to register callbacks in the <a class="link" href="DbstlElemTraits.html" title="Chapter 26. DbstlElemTraits">DbstlElemTraits</a> 61 class for the type you are storing/retrieving using dbstl. </p> 62 <h4><a id="id1570745"></a> Public Members </h4> 63 <div class="informaltable"> 64 <table border="1" width="80%"> 65 <colgroup> 66 <col /> 67 <col /> 68 </colgroup> 69 <thead> 70 <tr> 71 <th>Member</th> 72 <th>Description</th> 73 </tr> 74 </thead> 75 <tbody> 76 <tr> 77 <td> 78<a class="xref" href="DbstlDbt.html#stlDbstlDbtDbstlDbt" title="DbstlDbt"> 79DbstlDbt 80</a> 81</td> 82 <td> 83 <p>Construct an object with an existing chunk of memory of size1 bytes, refered by data1,. </p> </td> 84 </tr> 85 <tr> 86 <td> 87<a class="xref" href="stlDbstlDbtdstr_DbstlDbt.html" title="~DbstlDbt"> 88~DbstlDbt 89</a> 90</td> 91 <td> 92 <p>The memory will be free'ed by the destructor. </p> </td> 93 </tr> 94 <tr> 95 <td> 96<a class="xref" href="stlDbstlDbtoperator_assign.html" title="operator="> 97operator= 98</a> 99</td> 100 <td> 101 <p>The memory will be reallocated if neccessary. </p> </td> 102 </tr> 103 </tbody> 104 </table> 105 </div> 106 <h4><a id="id1570961"></a> 107Group</h4> 108 <p> 109<a class="xref" href="dbstl_helper_classes.html" title="Chapter 21. Dbstl Helper Classes"> 110 Dbstl Helper Classes </a> 111</p> 112 <p> 113</p> 114 <div class="sect1" lang="en" xml:lang="en"> 115 <div class="titlepage"> 116 <div> 117 <div> 118 <h2 class="title" style="clear: both"><a id="stlDbstlDbtDbstlDbt"></a> 119DbstlDbt 120</h2> 121 </div> 122 </div> 123 </div> 124 <div class="sect2" lang="en" xml:lang="en"> 125 <div class="titlepage"> 126 <div> 127 <div> 128 <h3 class="title"><a id="func_details"></a> 129Function Details</h3> 130 </div> 131 </div> 132 </div> 133 <pre class="programlisting"> 134DbstlDbt(void *data1, 135 u_int32_t size1) 136 </pre> 137 <div class="sect3" lang="en" xml:lang="en"> 138 <div class="titlepage"></div> 139 <p>Construct an object with an existing chunk of memory of size1 bytes, refered by data1,. </p> 140 </div> 141 <pre class="programlisting"> 142DbstlDbt() 143 </pre> 144 <div class="sect3" lang="en" xml:lang="en"> 145 <div class="titlepage"></div> 146 </div> 147 <pre class="programlisting"> 148DbstlDbt(const DbstlDbt &d) 149 </pre> 150 <div class="sect3" lang="en" xml:lang="en"> 151 <div class="titlepage"></div> 152 <p>This copy constructor does a deep copy. </p> 153 </div> 154 </div> 155 <div class="sect2" lang="en" xml:lang="en"> 156 <div class="titlepage"> 157 <div> 158 <div> 159 <h3 class="title"><a id="id1570948"></a> 160Class</h3> 161 </div> 162 </div> 163 </div> 164 <p> 165 <a class="link" href="DbstlDbt.html" title="Chapter 25. DbstlDbt"> 166 DbstlDbt</a> 167 </p> 168 </div> 169 </div> 170 </div> 171 <div class="navfooter"> 172 <hr /> 173 <table width="100%" summary="Navigation footer"> 174 <tr> 175 <td width="40%" align="left"><a accesskey="p" href="stlElementRef_DB_STL_value.html">Prev</a> </td> 176 <td width="20%" align="center"> </td> 177 <td width="40%" align="right"> <a accesskey="n" href="stlDbstlDbtdstr_DbstlDbt.html">Next</a></td> 178 </tr> 179 <tr> 180 <td width="40%" align="left" valign="top"> 181_DB_STL_value 182 </td> 183 <td width="20%" align="center"> 184 <a accesskey="h" href="index.html">Home</a> 185 </td> 186 <td width="40%" align="right" valign="top"> 187~DbstlDbt 188</td> 189 </tr> 190 </table> 191 </div> 192 </body> 193</html> 194