File: classwx_object_ref_data.html

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (197 lines) | stat: -rw-r--r-- 16,897 bytes parent folder | download | duplicates (2)
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
<!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/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: wxObjectRefData Class Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
 <tbody>
 <tr>
  <td id="projectlogo">
    <a href="http://www.wxwidgets.org/" target="_new">
      <img alt="wxWidgets" src="logo.png"/>
    </a>
  </td>
  <td style="padding-left: 0.5em; text-align: right;">
   <span id="projectnumber">Version: 3.0.2</span>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Categories</span></a></li>
      <li class="current"><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>Class&#160;List</span></a></li>
      <li><a href="classes.html"><span>Class&#160;Index</span></a></li>
      <li><a href="hierarchy.html"><span>Class&#160;Hierarchy</span></a></li>
      <li><a href="functions.html"><span>Class&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">wxObjectRefData Class Reference<div class="ingroups"><a class="el" href="group__group__class__rtti.html">Runtime Type Information (RTTI)</a></div></div>  </div>
</div><!--header-->
<div class="contents">

<p><code>#include &lt;wx/object.h&gt;</code></p>
<div id="dynsection-0" onclick="return toggleVisibility(this)" class="dynheader closed" style="cursor:pointer;">
  <img id="dynsection-0-trigger" src="closed.png" alt="+"/> Inheritance diagram for wxObjectRefData:</div>
<div id="dynsection-0-summary" class="dynsummary" style="display:block;">
</div>
<div id="dynsection-0-content" class="dyncontent" style="display:none;">
<div class="center"><img src="classwx_object_ref_data__inherit__graph.png" border="0" usemap="#wx_object_ref_data_inherit__map" alt="Inheritance graph"/></div>
<map name="wx_object_ref_data_inherit__map" id="wx_object_ref_data_inherit__map">
<area shape="rect" id="node3" href="classwx_variant_data.html" title="The wxVariantData class is used to implement a new type for wxVariant." alt="" coords="212,83,316,111"/><area shape="rect" id="node5" href="classwx_variant_data_currency.html" title="This class represents a thin wrapper for Microsoft Windows CURRENCY type." alt="" coords="5,161,160,189"/><area shape="rect" id="node7" href="classwx_variant_data_error_code.html" title="This class represents a thin wrapper for Microsoft Windows SCODE type (which is the same as HRESULT)..." alt="" coords="184,161,344,189"/><area shape="rect" id="node9" href="classwx_variant_data_safe_array.html" title="This class represents a thin wrapper for Microsoft Windows SAFEARRAY type." alt="" coords="368,161,528,189"/></map>
<center><span class="legend">[<a href="graph_legend.html">legend</a>]</span></center></div>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This class is just a typedef to <a class="el" href="classwx_ref_counter.html" title="This class is used to manage reference-counting providing a simple interface and a counter...">wxRefCounter</a> and is used by <a class="el" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a>. </p>
<p>Derive classes from this to store your own data in wxObject-derived classes. When retrieving information from a <a class="el" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a>'s reference data, you will need to cast to your own derived class.</p>
<p>Below is an example illustrating how to store reference counted data in a class derived from <a class="el" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a> including copy-on-write semantics.</p>
<h1><a class="anchor" id="objectrefdata_example"></a>
Example</h1>
<div class="fragment"><div class="line"><span class="comment">// include file</span></div>
<div class="line"><span class="comment">// ------------</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">class </span>MyCar : <span class="keyword">public</span> <a class="code" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    MyCar() { }</div>
<div class="line">    MyCar( <span class="keywordtype">int</span> price );</div>
<div class="line"></div>
<div class="line">    <span class="keywordtype">bool</span> IsOk()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> m_refData != NULL; }</div>
<div class="line"></div>
<div class="line">    <span class="keywordtype">bool</span> <a class="code" href="interface_2wx_2string_8h.html#adb9de78b97ce2369da971e3d1604a899" title="Comparison operator for string types.">operator == </a>( <span class="keyword">const</span> MyCar&amp; car ) <span class="keyword">const</span>;</div>
<div class="line">    <span class="keywordtype">bool</span> <a class="code" href="interface_2wx_2string_8h.html#a8f5342a458e5be812e8e52954fe0d12c" title="Comparison operator for string types.">operator != </a>(<span class="keyword">const</span> MyCar&amp; car)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> !(*<span class="keyword">this</span> == car); }</div>
<div class="line"></div>
<div class="line">    <span class="keywordtype">void</span> SetPrice( <span class="keywordtype">int</span> price );</div>
<div class="line">    <span class="keywordtype">int</span> GetPrice() <span class="keyword">const</span>;</div>
<div class="line"></div>
<div class="line"><span class="keyword">protected</span>:</div>
<div class="line">    <span class="keyword">virtual</span> <a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a> *CreateRefData() <span class="keyword">const</span>;</div>
<div class="line">    <span class="keyword">virtual</span> <a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a> *CloneRefData(<span class="keyword">const</span> <a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a> *data) <span class="keyword">const</span>;</div>
<div class="line"></div>
<div class="line">    <a class="code" href="group__group__funcmacro__rtti.html#ga9c252d076397bec5ac241272a7a60747" title="Used inside a class declaration to make the class known to wxWidgets RTTI system and also declare tha...">wxDECLARE_DYNAMIC_CLASS</a>(MyCar)</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"></div>
<div class="line"><span class="comment">// implementation</span></div>
<div class="line"><span class="comment">// --------------</span></div>
<div class="line"></div>
<div class="line"><span class="comment">// the reference data class is typically a private class only visible in the</span></div>
<div class="line"><span class="comment">// implementation source file of the refcounted class.</span></div>
<div class="line"><span class="keyword">class </span>MyCarRefData : <span class="keyword">public</span> <a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a></div>
<div class="line">{</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    MyCarRefData()</div>
<div class="line">    {</div>
<div class="line">        m_price = 0;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    MyCarRefData( <span class="keyword">const</span> MyCarRefData&amp; data )</div>
<div class="line">        : <a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a>()</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">// copy refcounted data; this is usually a time- and memory-consuming operation</span></div>
<div class="line">        <span class="comment">// and is only done when two (or more) MyCar instances need to unshare a</span></div>
<div class="line">        <span class="comment">// common instance of MyCarRefData</span></div>
<div class="line">        m_price = data.m_price;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordtype">bool</span> <a class="code" href="interface_2wx_2string_8h.html#adb9de78b97ce2369da971e3d1604a899" title="Comparison operator for string types.">operator == </a>(<span class="keyword">const</span> MyCarRefData&amp; data)<span class="keyword"> const</span></div>
<div class="line"><span class="keyword">    </span>{</div>
<div class="line">        <span class="keywordflow">return</span> m_price == data.m_price;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line">    <span class="comment">// in real world, reference counting is usually used only when</span></div>
<div class="line">    <span class="comment">// the wxObjectRefData-derived class holds data very memory-consuming;</span></div>
<div class="line">    <span class="comment">// in this example the various MyCar instances may share a MyCarRefData</span></div>
<div class="line">    <span class="comment">// instance which however only takes 4 bytes for this integer!</span></div>
<div class="line">    <span class="keywordtype">int</span> m_price;</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define M_CARDATA ((MyCarRefData *)m_refData)</span></div>
<div class="line"><span class="preprocessor"></span><a class="code" href="group__group__funcmacro__rtti.html#ga4cbf0c21eb5986eb489dc67450eab1d1" title="Used in a C++ implementation file to complete the declaration of a class that has run-time type infor...">wxIMPLEMENT_DYNAMIC_CLASS</a>(MyCar, <a class="code" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a>);</div>
<div class="line"></div>
<div class="line">MyCar::MyCar( <span class="keywordtype">int</span> price )</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// here we init the MyCar internal data:</span></div>
<div class="line">    m_refData = <span class="keyword">new</span> MyCarRefData();</div>
<div class="line">    M_CARDATA-&gt;m_price = price;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a> *MyCar::CreateRefData()<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{</div>
<div class="line">    <span class="keywordflow">return</span> <span class="keyword">new</span> MyCarRefData;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a> *MyCar::CloneRefData(<span class="keyword">const</span> <a class="code" href="classwx_object_ref_data.html" title="This class is just a typedef to wxRefCounter and is used by wxObject.">wxObjectRefData</a> *data)<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{</div>
<div class="line">    <span class="keywordflow">return</span> <span class="keyword">new</span> MyCarRefData(*(MyCarRefData *)data);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">bool</span> <a class="code" href="interface_2wx_2string_8h.html#adb9de78b97ce2369da971e3d1604a899" title="Comparison operator for string types.">MyCar::operator == </a>( <span class="keyword">const</span> MyCar&amp; car )<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{</div>
<div class="line">    <span class="keywordflow">if</span> (m_refData == car.m_refData)</div>
<div class="line">        <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line">    <span class="keywordflow">if</span> (!m_refData || !car.m_refData)</div>
<div class="line">        <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// here we use the MyCarRefData::operator==() function.</span></div>
<div class="line">    <span class="comment">// Note however that this comparison may be very slow if the</span></div>
<div class="line">    <span class="comment">// reference data contains a lot of data to be compared.</span></div>
<div class="line">    <span class="keywordflow">return</span> ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> MyCar::SetPrice( <span class="keywordtype">int</span> price )</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// since this function modifies one of the MyCar internal property,</span></div>
<div class="line">    <span class="comment">// we need to be sure that the other MyCar instances which share the</span></div>
<div class="line">    <span class="comment">// same MyCarRefData instance are not affected by this call.</span></div>
<div class="line">    <span class="comment">// I.e. it&#39;s very important to call UnShare() in all setters of</span></div>
<div class="line">    <span class="comment">// refcounted classes!</span></div>
<div class="line">    UnShare();</div>
<div class="line"></div>
<div class="line">    M_CARDATA-&gt;m_price = price;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> MyCar::GetPrice()<span class="keyword"> const</span></div>
<div class="line"><span class="keyword"></span>{</div>
<div class="line">    <a class="code" href="group__group__funcmacro__debug.html#ga4822a2ea9fdd0bc98caa3ff42587743e" title="Checks that the condition is true, returns with the given return value if not (stops execution in deb...">wxCHECK_MSG</a>( IsOk(), -1, <span class="stringliteral">&quot;invalid car&quot;</span> );</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> M_CARDATA-&gt;m_price;</div>
<div class="line">}</div>
</div><!-- fragment --><h2></h2>
<div><span class="lib">Library:</span>&#160;&#160;<span class="lib_text"><a class="el" href="page_libs.html#page_libs_wxbase">wxBase</a></span></div><div><span class="category">Category:</span>&#160;&#160;<span class="category_text"><a class="el" href="group__group__class__rtti.html">Runtime Type Information (RTTI)</a></span></div><dl class="section see"><dt>See Also</dt><dd><a class="el" href="classwx_object.html" title="This is the root class of many of the wxWidgets classes.">wxObject</a>, <a class="el" href="classwx_object_data_ptr_3_01_t_01_4.html" title="This is an helper template class primarily written to avoid memory leaks because of missing calls to ...">wxObjectDataPtr&lt;T&gt;</a>, <a class="el" href="overview_refcount.html">Reference Counting</a> </dd></dl>
</div></div><!-- contents -->

<address class="footer">
	<small>
		Generated on Thu Nov 27 2014 13:46:51 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
	</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>