﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-&lt;table border="0" cellspacing="0" cellpadding="0" style="margin-  left:0px;display:inline;height:30px;"&gt;&lt;tr&gt;&lt;td style="font-weight:bold; font-size:16px; line-  height:30px;"&gt;一年十二月&amp;nbsp谁主春秋&lt;/td&gt;&lt;td style="font-size:14px; line-height:30px;"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;-随笔分类-C/C++</title><link>http://www.cppblog.com/qinqing1984/category/10247.html</link><description>关注：操作系统、网络、数据库和安全</description><language>zh-cn</language><lastBuildDate>Wed, 31 Jul 2019 07:55:46 GMT</lastBuildDate><pubDate>Wed, 31 Jul 2019 07:55:46 GMT</pubDate><ttl>60</ttl><item><title>使用CString GetBuffer自适应获取计算机名称</title><link>http://www.cppblog.com/qinqing1984/archive/2019/07/31/216616.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Wed, 31 Jul 2019 04:51:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2019/07/31/216616.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/216616.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2019/07/31/216616.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/216616.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/216616.html</trackback:ping><description><![CDATA[&nbsp; &nbsp;为了减少程序中的硬编码，灵活按需管理字符串空间，使用了ATL中的CString类，代码如下<br />
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080; ">&nbsp;1</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CString bstrComPathName;<br />
<span style="color: #008080; ">&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WCHAR&nbsp;componentPathName[1];<br />
<span style="color: #008080; ">&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwNameLen&nbsp;=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<span style="color: #008080; ">&nbsp;4</span>&nbsp;<br />
<span style="color: #008080; ">&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(!GetComputerNameEx(<span style="color: #ff00ff;">ComputerNamePhysicalDnsFullyQualified</span>,&nbsp;componentPathName,&nbsp;&amp;dwNameLen))<br />
<span style="color: #008080; ">&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />
<span style="color: #008080; ">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwErr&nbsp;=&nbsp;GetLastError();<br />
<span style="color: #008080; ">&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>(<span style="color: #ff0000;">ERROR_MORE_DATA</span>==dwErr)<br />
<span style="color: #008080; ">&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<span style="color: #008080; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(!GetComputerNameEx(ComputerNamePhysicalDnsFullyQualified,&nbsp;bstrComPathName.<span style="color: #993366;"><strong>GetBuffer</strong></span>(dwNameLen),&nbsp;&amp;dwNameLen))<br />
<span style="color: #008080; ">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />
<span style="color: #008080; ">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zlog_error(g_zc,&nbsp;"GetComputerNameEx with&nbsp;ComputerNamePhysicalDnsFullyQualified&nbsp;fail:&nbsp;%d",&nbsp;GetLastError());<br />
<span style="color: #008080; ">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;-1;<br />
<span style="color: #008080; ">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<span style="color: #008080; ">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<span style="color: #008080; ">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br />
<span style="color: #008080; ">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
<span style="color: #008080; ">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zlog_error(g_zc,&nbsp;"GetComputerNameEx with&nbsp;ComputerNamePhysicalDnsFullyQualified&nbsp;for&nbsp;fail:&nbsp;%d",&nbsp;dwErr);<br />
<span style="color: #008080; ">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;-1;<br />
<span style="color: #008080; ">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<span style="color: #008080; ">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<span style="color: #008080; ">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bstrComPathName.<span style="color: #993366;"><strong>ReleaseBuffer</strong></span>();&nbsp;</div>
&nbsp; &nbsp; 需要注意的是，GetBuffer方法虽提供方便了直接修改CString对象的内部缓冲区，但违背了面向对象设计的原则（由公开方法修改内部数据），因此不保证对象的完整性，在操作完成后一定要调用ReleaseBuffer<img src ="http://www.cppblog.com/qinqing1984/aggbug/216616.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2019-07-31 12:51 <a href="http://www.cppblog.com/qinqing1984/archive/2019/07/31/216616.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一种使用函数指针实现状态机的方法</title><link>http://www.cppblog.com/qinqing1984/archive/2016/05/05/213418.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Thu, 05 May 2016 01:46:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2016/05/05/213418.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/213418.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2016/05/05/213418.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/213418.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/213418.html</trackback:ping><description><![CDATA[<strong style="font-size: 12pt;">结构定义</strong><br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">struct</span>&nbsp;state_machine&nbsp;{<br /><span style="color: #008080">&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">int</span>&nbsp;state;<br /><span style="color: #008080">&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img alt="" src="http://www.cppblog.com/Images/dot.gif" /><br /><span style="color: #008080">&nbsp;4</span>&nbsp;};<br /><span style="color: #008080">&nbsp;5</span>&nbsp;<br /><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #0000ff">enum</span>&nbsp;{&nbsp;<br /><span style="color: #008080">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s1,<br /><span style="color: #008080">&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s2,<br /><span style="color: #008080">&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img alt="" src="http://www.cppblog.com/Images/dot.gif" /><br /><span style="color: #008080">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sn<br /><span style="color: #008080">11</span>&nbsp;};</div></div></div>&nbsp;&nbsp;&nbsp;假设s1为初始状态，状态变迁为s1-&gt;s2-&gt;...-&gt;sn。<br /><br /><strong style="font-size: 12pt">常规实现</strong><span style="white-space: pre">&nbsp;</span><br />&nbsp;&nbsp;&nbsp;状态机处理函数state_machine_handle通常在一个循环内或被事件驱动框架调用，输入data会随时序变化，从而引起状态的变迁，伪代码框架如下。<br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;handle_s1(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_53_128_Open_Image" onclick="this.style.display='none'; Codehighlighter1_53_128_Open_Text.style.display='none'; Codehighlighter1_53_128_Closed_Image.style.display='inline'; Codehighlighter1_53_128_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_53_128_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_53_128_Closed_Text.style.display='none'; Codehighlighter1_53_128_Open_Image.style.display='inline'; Codehighlighter1_53_128_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_53_128_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_53_128_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">do&nbsp;something&nbsp;about&nbsp;state&nbsp;1</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(is_satisfy_s2(data))<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sm</span><span style="color: #000000">-&gt;</span><span style="color: #000000">state&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s2;<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;handle_s2(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_184_259_Open_Image" onclick="this.style.display='none'; Codehighlighter1_184_259_Open_Text.style.display='none'; Codehighlighter1_184_259_Closed_Image.style.display='inline'; Codehighlighter1_184_259_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_184_259_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_184_259_Closed_Text.style.display='none'; Codehighlighter1_184_259_Open_Image.style.display='inline'; Codehighlighter1_184_259_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_184_259_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_184_259_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">do&nbsp;something&nbsp;about&nbsp;state&nbsp;2</span><span style="color: #008000"><br /></span><span style="color: #008080">11</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(is_satisfy_s3(data))<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sm</span><span style="color: #000000">-&gt;</span><span style="color: #000000">state&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s3;<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;handle_sn_1(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img id="Codehighlighter1_317_394_Open_Image" onclick="this.style.display='none'; Codehighlighter1_317_394_Open_Text.style.display='none'; Codehighlighter1_317_394_Closed_Image.style.display='inline'; Codehighlighter1_317_394_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_317_394_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_317_394_Closed_Text.style.display='none'; Codehighlighter1_317_394_Open_Image.style.display='inline'; Codehighlighter1_317_394_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_317_394_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_317_394_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">do&nbsp;something&nbsp;about&nbsp;state&nbsp;n-1</span><span style="color: #008000"><br /></span><span style="color: #008080">18</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(is_satisfy_sn(data))<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sm</span><span style="color: #000000">-&gt;</span><span style="color: #000000">state&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;sn;<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;state_machine_handle(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_461_626_Open_Image" onclick="this.style.display='none'; Codehighlighter1_461_626_Open_Text.style.display='none'; Codehighlighter1_461_626_Closed_Image.style.display='inline'; Codehighlighter1_461_626_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_461_626_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_461_626_Closed_Text.style.display='none'; Codehighlighter1_461_626_Open_Image.style.display='inline'; Codehighlighter1_461_626_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_461_626_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_461_626_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img id="Codehighlighter1_481_624_Open_Image" onclick="this.style.display='none'; Codehighlighter1_481_624_Open_Text.style.display='none'; Codehighlighter1_481_624_Closed_Image.style.display='inline'; Codehighlighter1_481_624_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_481_624_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_481_624_Closed_Text.style.display='none'; Codehighlighter1_481_624_Open_Image.style.display='inline'; Codehighlighter1_481_624_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">switch</span><span style="color: #000000">(sm</span><span style="color: #000000">-&gt;</span><span style="color: #000000">state)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_481_624_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_481_624_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;s1:<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_s1(sm,data);<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;s2:<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_s2(sm,data);<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img alt="" src="http://www.cppblog.com/Images/dot.gif" /><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;sn:<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_sn(sm,data);<br /></span><span style="color: #008080">35</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">36</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">37</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div></div></div>&nbsp;&nbsp;&nbsp;sm-&gt;state初始化为s1。<br /><br />
<div><strong style="font-size: 12pt">改进实现</strong></div>
<div>&nbsp;&nbsp;&nbsp;为了免去丑陋的switch case分支结构，在state_machine内用成员函数指针handler替代了state，改进后的框架如下。</div>
<div align="center"></div>
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine;<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />typedef&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">state_handler)(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine</span><span style="color: #000000">*</span><span style="color: #000000">，</span><span style="color: #0000ff">void</span><span style="color: #000000">*</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img id="Codehighlighter1_104_135_Open_Image" onclick="this.style.display='none'; Codehighlighter1_104_135_Open_Text.style.display='none'; Codehighlighter1_104_135_Closed_Image.style.display='inline'; Codehighlighter1_104_135_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_104_135_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_104_135_Closed_Text.style.display='none'; Codehighlighter1_104_135_Open_Image.style.display='inline'; Codehighlighter1_104_135_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_104_135_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_104_135_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;state_handler&nbsp;</span><span style="color: red">handler</span>;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<img alt="" src="http://www.cppblog.com/Images/dot.gif" /><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;handle_s1(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img id="Codehighlighter1_192_276_Open_Image" onclick="this.style.display='none'; Codehighlighter1_192_276_Open_Text.style.display='none'; Codehighlighter1_192_276_Closed_Image.style.display='inline'; Codehighlighter1_192_276_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_192_276_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_192_276_Closed_Text.style.display='none'; Codehighlighter1_192_276_Open_Image.style.display='inline'; Codehighlighter1_192_276_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_192_276_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_192_276_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">do&nbsp;something&nbsp;about&nbsp;state&nbsp;1</span><span style="color: #008000"><br /></span><span style="color: #008080">12</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(is_satisfy_s2(data))<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">&nbsp;&nbsp;&nbsp;sm</span><span style="color: #ff0000">-&gt;</span><span style="color: #ff0000">handler&nbsp;</span><span style="color: #000000">=</span><span style="color: #ff0000">&nbsp;handle_s2</span>;<span style="color: #000000"><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;handle_s2(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img id="Codehighlighter1_332_416_Open_Image" onclick="this.style.display='none'; Codehighlighter1_332_416_Open_Text.style.display='none'; Codehighlighter1_332_416_Closed_Image.style.display='inline'; Codehighlighter1_332_416_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_332_416_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_332_416_Closed_Text.style.display='none'; Codehighlighter1_332_416_Open_Image.style.display='inline'; Codehighlighter1_332_416_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_332_416_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_332_416_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">do&nbsp;something&nbsp;about&nbsp;state&nbsp;2</span><span style="color: #008000"><br /></span><span style="color: #008080">19</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(is_satisfy_s3(data))<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">&nbsp;&nbsp;&nbsp;sm</span><span style="color: #ff0000">-&gt;</span><span style="color: #ff0000">handler&nbsp;</span><span style="color: #000000">=</span><span style="color: #ff0000">&nbsp;handle_s3;</span><br /><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;handle_sn_1(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img id="Codehighlighter1_474_560_Open_Image" onclick="this.style.display='none'; Codehighlighter1_474_560_Open_Text.style.display='none'; Codehighlighter1_474_560_Closed_Image.style.display='inline'; Codehighlighter1_474_560_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_474_560_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_474_560_Closed_Text.style.display='none'; Codehighlighter1_474_560_Open_Image.style.display='inline'; Codehighlighter1_474_560_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_474_560_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_474_560_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">do&nbsp;something&nbsp;about&nbsp;state&nbsp;n-1</span><span style="color: #008000"><br /></span><span style="color: #008080">26</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(is_satisfy_sn(data))<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">&nbsp;&nbsp;&nbsp;sm</span><span style="color: #ff0000">-&gt;</span><span style="color: #ff0000">handler&nbsp;</span><span style="color: #000000">=</span><span style="color: #ff0000">&nbsp;handle_sn</span>;<br /><span style="color: #008080">28</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;state_machine_handle(</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;state_machine&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">sm,&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">data)<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img id="Codehighlighter1_627_653_Open_Image" onclick="this.style.display='none'; Codehighlighter1_627_653_Open_Text.style.display='none'; Codehighlighter1_627_653_Closed_Image.style.display='inline'; Codehighlighter1_627_653_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_627_653_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_627_653_Closed_Text.style.display='none'; Codehighlighter1_627_653_Open_Image.style.display='inline'; Codehighlighter1_627_653_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_627_653_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_627_653_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: red">sm</span><span style="color: red">-&gt;</span><span style="color: red">handler</span><span style="color: #000000">(sm,&nbsp;data);<br /></span><span style="color: #008080">33</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div></div></div>
<div>&nbsp;&nbsp;&nbsp;sm-&gt;handler初始化为handle_s1，该方法在性能上应略优于常规方法，而且逻辑更清晰自然，非常适合于网络流的处理，在nginx中分析http和email协议时，得到了广泛应用。</div><img src ="http://www.cppblog.com/qinqing1984/aggbug/213418.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2016-05-05 09:46 <a href="http://www.cppblog.com/qinqing1984/archive/2016/05/05/213418.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>重载运算符之应用: 支持C式结构的一些运算</title><link>http://www.cppblog.com/qinqing1984/archive/2013/10/28/203954.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Mon, 28 Oct 2013 15:37:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2013/10/28/203954.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/203954.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2013/10/28/203954.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/203954.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/203954.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;本文以系统的struct timeval为例，使其支持+、-、+=、-=4种算术运算和&lt;，&lt;=，&gt;，&gt;=，==、!= 6种关系运算，这样一来就免去了显式调用函数的麻烦，使代码简洁优雅，实现如下 Code highlighting produced by Actipro CodeHighlighter (freeware)htt...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2013/10/28/203954.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/203954.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2013-10-28 23:37 <a href="http://www.cppblog.com/qinqing1984/archive/2013/10/28/203954.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于策略模式的定制new和delete</title><link>http://www.cppblog.com/qinqing1984/archive/2012/09/27/192173.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Thu, 27 Sep 2012 09:37:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/09/27/192173.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/192173.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/09/27/192173.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/192173.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/192173.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 引言&nbsp;&nbsp;&nbsp;在面向对象类的设计中，有时为了强化效能，特别是当构造大量小对象时，为了改善内存碎片，就需要自己实现对象的内存管理，以替换系统缺省的分配和释放行为，即全局的new和delete。按照c++标准，在定制类专属的new和delete时，为了减免客户代码使用时的麻烦和问题，需要考虑同时定制简单(normal new)、定位(placement new)和无异常(...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2012/09/27/192173.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/192173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-09-27 17:37 <a href="http://www.cppblog.com/qinqing1984/archive/2012/09/27/192173.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一种简单的跨平台信号量</title><link>http://www.cppblog.com/qinqing1984/archive/2012/07/20/184247.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Fri, 20 Jul 2012 02:52:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/07/20/184247.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/184247.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/07/20/184247.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/184247.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/184247.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp; &nbsp;信号量是一种用于并发环境同步手段的原语，分为无名信号量和有名信号量两种，前者只能用于线程间同步，而后者还可用于进程间同步。它包括创建、等待、挂出、取值和销毁5种基本操作。与互斥锁不同的是：&nbsp;&nbsp;&nbsp;&#9679;&nbsp;信号量拥有一个计数值，表示可用的资源数量，仅当该值为0或1时，则相当于互斥锁。&nbsp;&nbsp;&nbsp;&...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2012/07/20/184247.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/184247.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-07-20 10:52 <a href="http://www.cppblog.com/qinqing1984/archive/2012/07/20/184247.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一种简单的跨平台互斥锁</title><link>http://www.cppblog.com/qinqing1984/archive/2012/06/23/179822.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Fri, 22 Jun 2012 16:08:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/06/23/179822.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/179822.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/06/23/179822.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/179822.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/179822.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp; &nbsp;互斥锁，用来保证任一时刻只有单个线程或进程拥有对共享资源的互斥访问权，在这里将posix thread中的互斥体、win32中的互斥体和临界区，统称为互斥锁，其特点如下：&nbsp;&nbsp;&nbsp;&#9679; 范围：线程锁和进程锁，前者仅用于同一进程内多线程间，而后者用于进程间，显然，它也能用于同一进程内多线程间，但效率较低。posix的互斥体既可以是线程锁，...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2012/06/23/179822.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/179822.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-06-23 00:08 <a href="http://www.cppblog.com/qinqing1984/archive/2012/06/23/179822.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一种简单的跨平台用户态自旋锁</title><link>http://www.cppblog.com/qinqing1984/archive/2012/06/13/178691.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Wed, 13 Jun 2012 13:02:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/06/13/178691.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/178691.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/06/13/178691.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/178691.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/178691.html</trackback:ping><description><![CDATA[&nbsp; &nbsp;自旋锁作为一种并发同步的手段，特别适用于竞争少和锁时间短的情况，在驱动及内核代码中经常被用到，本文讲述一种适合用户态程序的自旋锁，支持Windows和Linux（GCC&gt;=4.1.2）平台，并提供了C语言的接口和实现。<br />
<br />
<span style="font-size: 12pt"><strong>接口<br /></strong></span><span>&nbsp;&nbsp;&nbsp;spin_trylock如果获取成功返回1，否则返回0；spin_is_lock如果已加锁，返回1，否则返回0。
</span>
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;<br />
</span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_16_64_Open_Image" onclick="this.style.display='none'; Codehighlighter1_16_64_Open_Text.style.display='none'; Codehighlighter1_16_64_Closed_Image.style.display='inline'; Codehighlighter1_16_64_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_16_64_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_16_64_Closed_Text.style.display='none'; Codehighlighter1_16_64_Open_Image.style.display='inline'; Codehighlighter1_16_64_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_16_64_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_16_64_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">&nbsp;&nbsp;flag_;<br />
</span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;spin_;<br />
</span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">spin_lock_t;<br />
</span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spin_init(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">,</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;flag);<br />
</span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spin_lock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">);<br />
</span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;spin_trylock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">);<br />
</span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spin_unlock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">);<br />
</span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;spin_is_lock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">);</span></div>
</div>
</div>
<align="left"><strong style="font-size: 12pt"><br />
实现</strong><div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">#ifdef&nbsp;_MSC_VER<br />
</span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">windows.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#elif</span><span style="color: #000000">&nbsp;defined(__GNUC__)</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#if</span><span style="color: #000000">&nbsp;__GNUC__&lt;4&nbsp;||&nbsp;(__GNUC__==4&nbsp;&amp;&amp;&nbsp;__GNUC_MINOR__&lt;1)</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#error</span><span style="color: #000000">&nbsp;GCC&nbsp;version&nbsp;must&nbsp;be&nbsp;greater&nbsp;or&nbsp;equal&nbsp;than&nbsp;4.1.2</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">sched.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#else</span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#error</span><span style="color: #000000">&nbsp;Currently&nbsp;only&nbsp;windows&nbsp;and&nbsp;linux&nbsp;os&nbsp;are&nbsp;supported</span><span style="color: #000000"><br />
</span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spin_init(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">,</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;flag)<br />
</span><span style="color: #008080">13</span><span style="color: #000000"><img id="Codehighlighter1_310_629_Open_Image" onclick="this.style.display='none'; Codehighlighter1_310_629_Open_Text.style.display='none'; Codehighlighter1_310_629_Closed_Image.style.display='inline'; Codehighlighter1_310_629_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_310_629_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_310_629_Closed_Text.style.display='none'; Codehighlighter1_310_629_Open_Image.style.display='inline'; Codehighlighter1_310_629_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_310_629_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_310_629_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />#ifdef&nbsp;_MSC_VER<br />
</span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;InterlockedExchange((</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">flag_,</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;InterlockedExchange((</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,flag</span><span style="color: #000000">?</span><span style="color: #000000">(</span><span style="color: #0000ff">long</span><span style="color: #000000">)flag:(</span><span style="color: #0000ff">long</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">flag_);<br />
</span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#elif</span><span style="color: #000000">&nbsp;defined(__GNUC__)</span><span style="color: #000000"><br />
</span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;__sync_and_and_fetch((</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">flag_,</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;__sync_lock_test_and_set((</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,flag</span><span style="color: #000000">?</span><span style="color: #000000">(</span><span style="color: #0000ff">long</span><span style="color: #000000">)flag:(</span><span style="color: #0000ff">long</span><span style="color: #000000">)</span><span style="color: #000000">&amp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">flag_);<br />
</span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spin_lock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">)<br />
</span><span style="color: #008080">24</span><span style="color: #000000"><img id="Codehighlighter1_666_866_Open_Image" onclick="this.style.display='none'; Codehighlighter1_666_866_Open_Text.style.display='none'; Codehighlighter1_666_866_Closed_Image.style.display='inline'; Codehighlighter1_666_866_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_666_866_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_666_866_Closed_Text.style.display='none'; Codehighlighter1_666_866_Open_Image.style.display='inline'; Codehighlighter1_666_866_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_666_866_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_666_866_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />#ifdef&nbsp;_MSC_VER<br />
</span><span style="color: #008080">26</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(;</span><span style="color: #000000">0</span><span style="color: #000000">!=</span><span style="color: #000000">InterlockedExchange((</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">1</span><span style="color: #000000">);)<br />
</span><span style="color: #008080">27</span><span style="color: #000000"><img id="Codehighlighter1_748_763_Open_Image" onclick="this.style.display='none'; Codehighlighter1_748_763_Open_Text.style.display='none'; Codehighlighter1_748_763_Closed_Image.style.display='inline'; Codehighlighter1_748_763_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_748_763_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_748_763_Closed_Text.style.display='none'; Codehighlighter1_748_763_Open_Image.style.display='inline'; Codehighlighter1_748_763_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_748_763_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_748_763_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">28</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleep(</span><span style="color: #000000">1</span><span style="color: #000000">);<br />
</span><span style="color: #008080">29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#elif</span><span style="color: #000000">&nbsp;defined(__GNUC__)</span><span style="color: #000000"><br />
</span><span style="color: #008080">31</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(;</span><span style="color: #000000">0</span><span style="color: #000000">!=</span><span style="color: #000000">__sync_fetch_and_or(</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">1</span><span style="color: #000000">);)<br />
</span><span style="color: #008080">32</span><span style="color: #000000"><img id="Codehighlighter1_837_857_Open_Image" onclick="this.style.display='none'; Codehighlighter1_837_857_Open_Text.style.display='none'; Codehighlighter1_837_857_Closed_Image.style.display='inline'; Codehighlighter1_837_857_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_837_857_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_837_857_Closed_Text.style.display='none'; Codehighlighter1_837_857_Open_Image.style.display='inline'; Codehighlighter1_837_857_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_837_857_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_837_857_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">33</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sched_yield();<br />
</span><span style="color: #008080">34</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">35</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">36</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">37</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">38</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;spin_trylock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">)<br />
</span><span style="color: #008080">39</span><span style="color: #000000"><img id="Codehighlighter1_905_1060_Open_Image" onclick="this.style.display='none'; Codehighlighter1_905_1060_Open_Text.style.display='none'; Codehighlighter1_905_1060_Closed_Image.style.display='inline'; Codehighlighter1_905_1060_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_905_1060_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_905_1060_Closed_Text.style.display='none'; Codehighlighter1_905_1060_Open_Image.style.display='inline'; Codehighlighter1_905_1060_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_905_1060_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_905_1060_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">40</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />#ifdef&nbsp;_MSC_VER<br />
</span><span style="color: #008080">41</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">!</span><span style="color: #000000">InterlockedExchange((</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">1</span><span style="color: #000000">);<br />
</span><span style="color: #008080">42</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#elif</span><span style="color: #000000">&nbsp;defined(__GNUC__)</span><span style="color: #000000"><br />
</span><span style="color: #008080">43</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">!</span><span style="color: #000000">__sync_fetch_and_or(</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">1</span><span style="color: #000000">);<br />
</span><span style="color: #008080">44</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">45</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">46</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">47</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spin_unlock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">)<br />
</span><span style="color: #008080">48</span><span style="color: #000000"><img id="Codehighlighter1_1099_1239_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1099_1239_Open_Text.style.display='none'; Codehighlighter1_1099_1239_Closed_Image.style.display='inline'; Codehighlighter1_1099_1239_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1099_1239_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1099_1239_Closed_Text.style.display='none'; Codehighlighter1_1099_1239_Open_Image.style.display='inline'; Codehighlighter1_1099_1239_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1099_1239_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1099_1239_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">49</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />#ifdef&nbsp;_MSC_VER<br />
</span><span style="color: #008080">50</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;InterlockedExchange((</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">51</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#elif</span><span style="color: #000000">&nbsp;defined(__GNUC__)</span><span style="color: #000000"><br />
</span><span style="color: #008080">52</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;__sync_and_and_fetch(</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">53</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">54</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">55</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">56</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;spin_is_lock(spin_lock_t</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">lock</span><span style="color: #000000">)<br />
</span><span style="color: #008080">57</span><span style="color: #000000"><img id="Codehighlighter1_1278_1435_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1278_1435_Open_Text.style.display='none'; Codehighlighter1_1278_1435_Closed_Image.style.display='inline'; Codehighlighter1_1278_1435_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1278_1435_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1278_1435_Closed_Text.style.display='none'; Codehighlighter1_1278_1435_Open_Image.style.display='inline'; Codehighlighter1_1278_1435_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1278_1435_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1278_1435_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">58</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />#ifdef&nbsp;_MSC_VER<br />
</span><span style="color: #008080">59</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;InterlockedExchangeAdd((</span><span style="color: #0000ff">volatile</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">long</span><span style="color: #000000">*</span><span style="color: #000000">)</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">60</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#elif</span><span style="color: #000000">&nbsp;defined(__GNUC__)</span><span style="color: #000000"><br />
</span><span style="color: #008080">61</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;__sync_add_and_fetch(</span><span style="color: #0000ff">lock</span><span style="color: #000000">-&gt;</span><span style="color: #000000">spin_,</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">62</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">#endif</span><span style="color: #000000"><br />
</span><span style="color: #008080">63</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div>
</div>
</div>
</align="left"><img src ="http://www.cppblog.com/qinqing1984/aggbug/178691.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-06-13 21:02 <a href="http://www.cppblog.com/qinqing1984/archive/2012/06/13/178691.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GCC原子操作类模板</title><link>http://www.cppblog.com/qinqing1984/archive/2012/06/08/103124.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Thu, 07 Jun 2012 16:19:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/06/08/103124.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/103124.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/06/08/103124.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/103124.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/103124.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 主类模板&nbsp; &nbsp;gcc从4.1.2版本开始提供了__sync_*系列的内置API，用于加减和逻辑运算，可以对1，2，4，8字节长度的数值或指针类型进行原子操作，为方便使用，笔者对这些API作了简单的封装。Code highlighting produced by Actipro CodeHighlighter (freeware)http://...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2012/06/08/103124.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/103124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-06-08 00:19 <a href="http://www.cppblog.com/qinqing1984/archive/2012/06/08/103124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模板元编程（3）：类型选择</title><link>http://www.cppblog.com/qinqing1984/archive/2012/06/06/177777.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Wed, 06 Jun 2012 05:49:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/06/06/177777.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/177777.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/06/06/177777.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/177777.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/177777.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;&nbsp; 类型选择是一种编译时的类型计算技术，也就是根据条件判断来匹配对应的类型，功能形如运行时的if else和switch case控制结构。在这里仿真运行时的条件语句，实现了类型选择，包括if单分支、if多分支和switch case三种结构，关于其原理及细节就不多讲了，直接看如下代码&nbsp;&nbsp;（1）if单分支 Code ...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2012/06/06/177777.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/177777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-06-06 13:49 <a href="http://www.cppblog.com/qinqing1984/archive/2012/06/06/177777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模板元编程（2）：计算最值</title><link>http://www.cppblog.com/qinqing1984/archive/2012/05/29/176562.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Mon, 28 May 2012 17:03:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2012/05/29/176562.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/176562.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2012/05/29/176562.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/176562.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/176562.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;本文就Loki编译期技术中的类型列表Typelist作了一些扩展，增加了以下几个方法：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8226; 获取最大和最小长度，即求取Typelist中长度最大和最小的值&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; &#8226; 获取最大和最小类型，即求取Typelist中长度最大和最小的类型<br /><br /><hr /><strong style="font-size: 12pt">实现<br />
</strong>&nbsp;&nbsp;&nbsp;位于Loki::TL命名空间，利用递归计算最值结果，使用宏生成主类模板和特化类模板，其中后缀为DEFN(N为正整数)形式的宏中N表示特化类模板所带的模板参数数量，使用DEF1宏定义对应的特化类模板的原因在于：当Typelist中存在非NullType类型时，保证结果的正确性。当N为2时参数取值：name为Max则b为true；name为Min则b为false。<br />&nbsp;&nbsp;&nbsp;<strong>主类模板</strong><br />&nbsp;&nbsp;&nbsp;用于定义MaxSize、MinSize和MaxType、MinType主类模板，使用宏LOKI_TYPELIST_METHOD_DEF生成。&nbsp; 
<div align="center"></div>
<div align="center"></div>
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">1</span><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_METHOD_DEF(name)\</span><span style="color: #000000"><br /></span><span style="color: #008080">2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;TList</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name;\<br /></span><span style="color: #008080">4</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_METHOD_DEF(MaxSize)<br /></span><span style="color: #008080">6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_METHOD_DEF(MinSize)<br /></span><span style="color: #008080">7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_METHOD_DEF(MaxType)<br /></span><span style="color: #008080">8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_METHOD_DEF(MinType)</span></div></div></div><br />&nbsp;&nbsp;&nbsp;<strong>最大(小)长度</strong><br />&nbsp;&nbsp;&nbsp;对应类主模板分别为MaxSize和MinSize，每种有3个特化模板，使用宏LOKI_TYPELIST_SIZE_SPEC_DEFN生成（N为0、1、2）。<br />
<div align="center"></div>
<div align="center"></div>
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF0(name)\</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template</span><span style="color: #000000">&lt;&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name##Size</span><span style="color: #000000">&lt;</span><span style="color: #000000">NullType</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img id="Codehighlighter1_92_121_Open_Image" onclick="this.style.display='none'; Codehighlighter1_92_121_Open_Text.style.display='none'; Codehighlighter1_92_121_Closed_Image.style.display='inline'; Codehighlighter1_92_121_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_92_121_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_92_121_Closed_Text.style.display='none'; Codehighlighter1_92_121_Open_Image.style.display='inline'; Codehighlighter1_92_121_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_92_121_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_92_121_Open_Text"><span style="color: #000000">{\<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_103_115_Open_Image" onclick="this.style.display='none'; Codehighlighter1_103_115_Open_Text.style.display='none'; Codehighlighter1_103_115_Closed_Image.style.display='inline'; Codehighlighter1_103_115_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_103_115_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_103_115_Closed_Text.style.display='none'; Codehighlighter1_103_115_Open_Image.style.display='inline'; Codehighlighter1_103_115_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">enum</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_103_115_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_103_115_Open_Text"><span style="color: #000000">{&nbsp;value&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF1(name)\</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;T</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name##Size</span><span style="color: #000000">&lt;</span><span style="color: #000000">Typelist</span><span style="color: #000000">&lt;</span><span style="color: #000000">T,NullType</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img id="Codehighlighter1_240_277_Open_Image" onclick="this.style.display='none'; Codehighlighter1_240_277_Open_Text.style.display='none'; Codehighlighter1_240_277_Closed_Image.style.display='inline'; Codehighlighter1_240_277_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_240_277_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_240_277_Closed_Text.style.display='none'; Codehighlighter1_240_277_Open_Image.style.display='inline'; Codehighlighter1_240_277_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_240_277_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_240_277_Open_Text"><span style="color: #000000">{\<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img id="Codehighlighter1_251_271_Open_Image" onclick="this.style.display='none'; Codehighlighter1_251_271_Open_Text.style.display='none'; Codehighlighter1_251_271_Closed_Image.style.display='inline'; Codehighlighter1_251_271_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_251_271_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_251_271_Closed_Text.style.display='none'; Codehighlighter1_251_271_Open_Image.style.display='inline'; Codehighlighter1_251_271_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">enum</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_251_271_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_251_271_Open_Text"><span style="color: #000000">{&nbsp;value&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF2(name,b)\</span><span style="color: #000000"><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;T,</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;U</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name##Size</span><span style="color: #000000">&lt;</span><span style="color: #000000">Typelist</span><span style="color: #000000">&lt;</span><span style="color: #000000">T,U</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img id="Codehighlighter1_399_525_Open_Image" onclick="this.style.display='none'; Codehighlighter1_399_525_Open_Text.style.display='none'; Codehighlighter1_399_525_Closed_Image.style.display='inline'; Codehighlighter1_399_525_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_399_525_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_399_525_Closed_Text.style.display='none'; Codehighlighter1_399_525_Open_Image.style.display='inline'; Codehighlighter1_399_525_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_399_525_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_399_525_Open_Text"><span style="color: #000000">{\<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_410_439_Open_Image" onclick="this.style.display='none'; Codehighlighter1_410_439_Open_Text.style.display='none'; Codehighlighter1_410_439_Closed_Image.style.display='inline'; Codehighlighter1_410_439_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_410_439_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_410_439_Closed_Text.style.display='none'; Codehighlighter1_410_439_Open_Image.style.display='inline'; Codehighlighter1_410_439_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">enum</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_410_439_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_410_439_Open_Text"><span style="color: #000000">{&nbsp;tmp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name##Size</span><span style="color: #000000">&lt;</span><span style="color: #000000">U</span><span style="color: #000000">&gt;</span><span style="color: #000000">::value&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img id="Codehighlighter1_451_519_Open_Image" onclick="this.style.display='none'; Codehighlighter1_451_519_Open_Text.style.display='none'; Codehighlighter1_451_519_Closed_Image.style.display='inline'; Codehighlighter1_451_519_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_451_519_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_451_519_Closed_Text.style.display='none'; Codehighlighter1_451_519_Open_Image.style.display='inline'; Codehighlighter1_451_519_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">enum</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_451_519_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_451_519_Open_Text"><span style="color: #000000">{&nbsp;value&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(b&nbsp;</span><span style="color: #000000">?</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;tmp&nbsp;:&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;tmp)&nbsp;</span><span style="color: #000000">?</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)&nbsp;:&nbsp;tmp&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF0(Max)<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF0(Min)<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF1(Max)<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF1(Min)<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF2(Max,</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF2(Min,</span><span style="color: #0000ff">false</span><span style="color: #000000">)<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#undef</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF0</span><span style="color: #000000"><br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#undef</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF1</span><span style="color: #000000"><br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#undef</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_SIZE_SPEC_DEF2</span></div></div></div><br />&nbsp;&nbsp;&nbsp;<strong>最大(小)类型</strong><br />&nbsp;&nbsp;&nbsp;对应类主模板分别为MaxType和MinType，每种有3个特化模板，使用宏LOKI_TYPELIST_TYPE_SPEC_DEFN生成（N为0、1、2）。&nbsp;&nbsp;&nbsp;
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF0(name)\</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template</span><span style="color: #000000">&lt;&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name##Type</span><span style="color: #000000">&lt;</span><span style="color: #000000">NullType</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img id="Codehighlighter1_92_124_Open_Image" onclick="this.style.display='none'; Codehighlighter1_92_124_Open_Text.style.display='none'; Codehighlighter1_92_124_Closed_Image.style.display='inline'; Codehighlighter1_92_124_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_92_124_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_92_124_Closed_Text.style.display='none'; Codehighlighter1_92_124_Open_Image.style.display='inline'; Codehighlighter1_92_124_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_92_124_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_92_124_Open_Text"><span style="color: #000000">{\<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;NullType&nbsp;type;\<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF1(name)\</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;T</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name##Type</span><span style="color: #000000">&lt;</span><span style="color: #000000">Typelist</span><span style="color: #000000">&lt;</span><span style="color: #000000">T,NullType</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img id="Codehighlighter1_243_268_Open_Image" onclick="this.style.display='none'; Codehighlighter1_243_268_Open_Text.style.display='none'; Codehighlighter1_243_268_Closed_Image.style.display='inline'; Codehighlighter1_243_268_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_243_268_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_243_268_Closed_Text.style.display='none'; Codehighlighter1_243_268_Open_Image.style.display='inline'; Codehighlighter1_243_268_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_243_268_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_243_268_Open_Text"><span style="color: #000000">{\<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;T&nbsp;type;\<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF2(name,b)\</span><span style="color: #000000"><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;template</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;T,</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;U</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;name##Type</span><span style="color: #000000">&lt;</span><span style="color: #000000">Typelist</span><span style="color: #000000">&lt;</span><span style="color: #000000">T,U</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">\<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img id="Codehighlighter1_390_537_Open_Image" onclick="this.style.display='none'; Codehighlighter1_390_537_Open_Text.style.display='none'; Codehighlighter1_390_537_Closed_Image.style.display='inline'; Codehighlighter1_390_537_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_390_537_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_390_537_Closed_Text.style.display='none'; Codehighlighter1_390_537_Open_Image.style.display='inline'; Codehighlighter1_390_537_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_390_537_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_390_537_Open_Text"><span style="color: #000000">{\<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;typename&nbsp;name##Type</span><span style="color: #000000">&lt;</span><span style="color: #000000">U</span><span style="color: #000000">&gt;</span><span style="color: #000000">::type&nbsp;R;\<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;typename&nbsp;Select</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;b&nbsp;</span><span style="color: #000000">?</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)</span><span style="color: #000000">&gt;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(R))&nbsp;:&nbsp;(</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(T)</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(R)),T,R</span><span style="color: #000000">&gt;</span><span style="color: #000000">::Result&nbsp;type;\<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">;\<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF0(Max)<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF0(Min)<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF1(Max)<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF1(Min)<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF2(Max,</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF2(Min,</span><span style="color: #0000ff">false</span><span style="color: #000000">)<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#undef</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF0</span><span style="color: #000000"><br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#undef</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF1</span><span style="color: #000000"><br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">#undef</span><span style="color: #000000">&nbsp;LOKI_TYPELIST_TYPE_SPEC_DEF2</span></div></div></div>&nbsp;&nbsp;&nbsp;这里用到了Loki中的Select组件来选择类型。<br /><br /><strong style="font-size: 12pt">示例<br />
</strong><strong>&nbsp;&nbsp;&nbsp;</strong>使用LOKI中的LOKI_STATIC_CHECK宏来做编译期诊断结果正确性。<br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;LOKI_TL4&nbsp;LOKI_TYPELIST_4(double,int,short,char)&nbsp;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;argc,</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">argv[])<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img id="Codehighlighter1_90_635_Open_Image" onclick="this.style.display='none'; Codehighlighter1_90_635_Open_Text.style.display='none'; Codehighlighter1_90_635_Closed_Image.style.display='inline'; Codehighlighter1_90_635_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_90_635_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_90_635_Closed_Text.style.display='none'; Codehighlighter1_90_635_Open_Image.style.display='inline'; Codehighlighter1_90_635_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_90_635_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_90_635_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;max_val&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;Loki::TL::MaxSize</span><span style="color: #000000">&lt;</span><span style="color: #000000">LOKI_TL4&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">::value;<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_STATIC_CHECK(max_val</span><span style="color: #000000">==</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(</span><span style="color: #0000ff">double</span><span style="color: #000000">),max_val_should_be_sizeof_double)<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;min_val&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;Loki::TL::MinSize</span><span style="color: #000000">&lt;</span><span style="color: #000000">LOKI_TL4&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">::value;<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_STATIC_CHECK(min_val</span><span style="color: #000000">==</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(</span><span style="color: #0000ff">char</span><span style="color: #000000">),min_val_should_be_sizeof_char)<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;Loki::TL::MaxType</span><span style="color: #000000">&lt;</span><span style="color: #000000">LOKI_TL4&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">::type&nbsp;max_type;<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_STATIC_CHECK((Loki::IsSameType</span><span style="color: #000000">&lt;</span><span style="color: #000000">max_type,</span><span style="color: #0000ff">double</span><span style="color: #000000">&gt;</span><span style="color: #000000">::value),max_type_should_be_double)<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;Loki::TL::MinType</span><span style="color: #000000">&lt;</span><span style="color: #000000">LOKI_TL4&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">::type&nbsp;min_type;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;LOKI_STATIC_CHECK((Loki::IsSameType</span><span style="color: #000000">&lt;</span><span style="color: #000000">min_type,</span><span style="color: #0000ff">char</span><span style="color: #000000">&gt;</span><span style="color: #000000">::value),min_type_should_be_char)<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div></div></div><img src ="http://www.cppblog.com/qinqing1984/aggbug/176562.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2012-05-29 01:03 <a href="http://www.cppblog.com/qinqing1984/archive/2012/05/29/176562.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模板元编程（1）：选择API</title><link>http://www.cppblog.com/qinqing1984/archive/2011/12/24/162731.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sat, 24 Dec 2011 11:08:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/12/24/162731.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/162731.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/12/24/162731.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/162731.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/162731.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: C与C++ API的比较&nbsp;&nbsp;&nbsp;在c语言中，API体现为c函数，如操作系统提供的一系列API，在c++中，API体现为自由函数，这里的自由函数是指除普通成员函数、静态成员函数、友元函数外的能在某命名空间作用域或全局空间内直接访问的函数，而这更多地体现为函数模板，如stl提供的一系列算法swap、count和sort等。相对于c API，c++ API具有类型安全和封...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/12/24/162731.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/162731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-12-24 19:08 <a href="http://www.cppblog.com/qinqing1984/archive/2011/12/24/162731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>真彩色静态框CColorStatic类的设计实现</title><link>http://www.cppblog.com/qinqing1984/archive/2011/12/18/162318.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sat, 17 Dec 2011 16:54:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/12/18/162318.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/162318.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/12/18/162318.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/162318.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/162318.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;关于cstatic控件的自绘，网上也有很多的代码及文章，更有其界面画得很漂亮的、多种多样的功能。近来我自行封装实现了一个真彩色静态框类，目标初衷是从颜色、字体、光标入手，改变原始标准cstatic的色彩风格，使界面初步美化，具有好看的效果。同时作为一个基础简单的类来维护，为后续的功能增强及美化提供参考扩展，这个CColorStatic类的特点及功能如下：（1）文...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/12/18/162318.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/162318.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-12-18 00:54 <a href="http://www.cppblog.com/qinqing1984/archive/2011/12/18/162318.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用子类化截获窗口消息</title><link>http://www.cppblog.com/qinqing1984/archive/2011/12/11/161915.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sun, 11 Dec 2011 03:07:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/12/11/161915.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/161915.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/12/11/161915.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/161915.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/161915.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;在《多标签视图类CTabView的设计实现》一文中，CTabView从CBasicSubClassWnd私有继承，重写其虚函数SubWindowProc，捕获WM_DRAWITEM和TTN_GETDISPINFO消息，从而实现了DrawItem和UpdateTooltipText虚函数回调机制，支持派生类的自定义处理，而CBasicSubClassWnd就是一个...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/12/11/161915.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/161915.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-12-11 11:07 <a href="http://www.cppblog.com/qinqing1984/archive/2011/12/11/161915.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多标签视图类CTabView的设计实现</title><link>http://www.cppblog.com/qinqing1984/archive/2011/12/11/161908.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sat, 10 Dec 2011 16:47:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/12/11/161908.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/161908.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/12/11/161908.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/161908.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/161908.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;在MFC9（在vc2008和vc2010中，已经有了CTabView的现成类）以前的版本中，有CListView,CTreeView,CEditView,CRichEditView等控件视图类，但就是没有类似的CTabView类，因工作需要，最近在做一个简单的多标签IE浏览器，开发环境是vs2005，基本框架是sdi + chtmlview + ctabview...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/12/11/161908.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/161908.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-12-11 00:47 <a href="http://www.cppblog.com/qinqing1984/archive/2011/12/11/161908.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>观察者模式之应用: 系统托盘图标类的设计</title><link>http://www.cppblog.com/qinqing1984/archive/2011/12/04/161393.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sat, 03 Dec 2011 19:15:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/12/04/161393.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/161393.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/12/04/161393.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/161393.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/161393.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;关于系统托盘图标类，网上也有很多的代码，但都不太简洁灵活易用，为了这一目的，本人自行封装了一个API版本的实现类，这个类的设计思想来源于观察者模式，从而较好地解耦了托盘通知消息的发送、接收、处理这三者间的关系，使这三者可以是同一个对象，也可以是不同的对象，具体的情况可根据业务逻辑灵活选择控制，主要包括以下几方面的特点：1）对于托盘通知消息的接收处理，提供了一个默...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/12/04/161393.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/161393.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-12-04 03:15 <a href="http://www.cppblog.com/qinqing1984/archive/2011/12/04/161393.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>default constructor和copy constructor小结</title><link>http://www.cppblog.com/qinqing1984/archive/2011/08/31/154770.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Wed, 31 Aug 2011 03:40:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/08/31/154770.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/154770.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/08/31/154770.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/154770.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/154770.html</trackback:ping><description><![CDATA[<span>&nbsp;&nbsp;&nbsp;一、default constructor---默认构造函数，亦即无参构造函数。从对象构造语义上讲，可分为以下2种：1）trivial 平凡的，可以理解为浅构造&nbsp; 2）notrivial 非平凡的，可以理解为深构造。当一个class没有显式地(explicitly)声明或定义任何constructor的时候，一个default constructor就会被编译器隐式地（implicitly)声明或定义出来。那么这个implicitly default constructor到底是trivial还是notrivial的呢？对于一个class，当存在以下4种情况时，其implicitly default constructor就是notrivial的。</span><br />
<span>&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">（1）class内含一个或多个成员对象(member object)，且这些member object中至少一个存在default constructor（无论是显式的default constructor还是隐式的notrival default constructor）</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;（2）class派生自一个继承串链，其中至少有一个base class存在default constructor（再次强调，无论是显式的default constructor还是隐式的notrival default constructor）</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;（3）class声明一个或多个虚函数(virtual function)</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;（4）class派生自一个继承串链，其中至少有一个虚基类(virtual base class)，而不管这些virtual base class是否存在default constructor</span><br />
<span>&nbsp;&nbsp;&nbsp;显而易见，这4种情况是</span><span style="color: #0000ff">正交</span><span>的，当不存在以上4种情况时，其implicitly default constructor就是trivial的。只有notrivial的default constructor才会被编译器真正生成，而trivial的不会生成。</span><br />
<br />
<span>&nbsp;&nbsp;&nbsp;二、copy constructor---拷贝构造函数，亦即带有当且仅有一个参数，类型为同类对象的构造函数。从对象拷贝语义上讲，可分为以下2种：1）bitwise copy 位拷贝，可以理解为浅拷贝&nbsp; 2）no bitwise copy 非位拷凡，可以理解为深拷贝。当一个class没有显式地声明或定义copy constructor时，一个copy constructor就会被编译器隐式地声明或定义出来。那么这个implicitly copy constructor到底是bitwise copy还是no bitwise copy的呢？对于一个class，当存在以下4种情况时，其implicitly copy constructor就是no bitwise copy的。</span><br />
<span>&nbsp;&nbsp;&nbsp;</span><span style="color: #ff0000">（1）class内含一个或多个成员对象，且这些member object中至少一个存在copy constructor（无论是显式的copy constructor还是隐式的no bitwise copy constructor）</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;（2）class派生自一个继承串链，其中至少有一个base class存在copy constructor（再次强调，无论是显式的copy constructor还是隐式的no bitwise copy constructor）</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;（3）class声明一个或多个虚函数</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;（4）class派生自一个继承串链，其中至少有一个虚基类，而不管这些virtual base class是否存在copy constructor</span><br />
<span style="color: #ff0000">&nbsp;&nbsp;&nbsp;</span><span>显而易见，这4种情况是</span><span style="color: #0000ff">正交</span><span>的，当不存在以上4种情况时，其implicitly copy constructor就是bitwise copy的。只有no bitwise copy的copy constructor才会被编译器真正生成，而bitwise copy的不会生成。</span><br />
<br />
<span>&nbsp;&nbsp;&nbsp;三、对于defualt constructor，当一个class内显式地存在constructor（包括default constructor）时，编译器不会再生成它，但如果这个class满足以上4种情况至少一种时，编译器就需要负责执行相关的初始化：对于(1)要调用成员对象的default constructor；对于(2)要调用基类的default constructor；对于(3)要设定虚函数表的指针；对于(4)要设定虚基类的指针和偏移量。而这些初始化在用户代码执行前。</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<span>&nbsp;&nbsp;&nbsp;四、对于copy constructor，当一个class内显式地存在copy constructor时，编译器不会再生成它，但如果这个class满足以上情况(3)或(和)(4)时，编译器就需要负责执行相关的拷贝：对于(3)要决定怎么设定虚函数表指针。对于(4)要决定怎么设定虚基类的指针和偏移量。同理类推，如果这个class满足情况(1)或(和)(2)，而且其成员对象或基类子对象又满足情况(3)或(和)(4)时，编译器也需要负责执行相关的拷贝了。而这些拷贝在用户代码执行前。 </span><img src ="http://www.cppblog.com/qinqing1984/aggbug/154770.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-08-31 11:40 <a href="http://www.cppblog.com/qinqing1984/archive/2011/08/31/154770.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多窗口的组合与分离（2）：实现</title><link>http://www.cppblog.com/qinqing1984/archive/2011/08/27/154481.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sat, 27 Aug 2011 07:12:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/08/27/154481.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/154481.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/08/27/154481.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/154481.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/154481.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->&nbsp;&nbsp;1/***************************************************************************************&...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/08/27/154481.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/154481.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-08-27 15:12 <a href="http://www.cppblog.com/qinqing1984/archive/2011/08/27/154481.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多窗口的组合与分离（1）：接口</title><link>http://www.cppblog.com/qinqing1984/archive/2011/07/04/150084.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Mon, 04 Jul 2011 03:14:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/07/04/150084.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/150084.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/07/04/150084.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/150084.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/150084.html</trackback:ping><description><![CDATA[<span>&nbsp;&nbsp;&nbsp;著名的千千静听音乐播放器，其界面简洁优雅、美观大方，特别是它那种几个窗口像磁石般相互吸引，当拖动主窗口时，粘在一起的其它窗口会跟随着一起移动，当拖动其它窗口时，又能脱离不粘在一起，这种窗口效果让用户操作方便，省心省力。为描述方便，本文称这种效果为多窗口的组合分离，它的主要特点是仅用鼠标任意移动窗口，就可组合或分离，当组合在一起时，移动某窗口（如主窗口，暂称为老板窗口）能整体移动，移动其口窗口（非老板窗口，暂称为工人窗口）又能将自己脱离出来。近来由于工作需要实现了类似于此的窗口效果，经过几天的测试，终于稳定。在开发过程中，考虑了以下几个问题：</span><br />
<span style="color: #ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1) &nbsp;组合分离的条件如何决定判断。<br />
</span><span style="color: #ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(2)&nbsp;&nbsp;当窗口大小改变时，包括最小化，最大化，缩放窗口等，如何保证不影响组合分离，能正常整体移动。<br />
</span><span style="color: #ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(3)&nbsp; 窗口个数是可变的，当新建或销毁窗口时，如何保证不影响组合分离，能正常整体移动(千千静听窗口个数是有限的，而且可能只是隐藏窗口）。<br />
</span><span style="color: #ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(4)&nbsp; 采用什么数据结构较好，如何维护任意两个窗口间的距离关系（相交或相切视为组合，相离视为分离）。<br />
</span><span style="color: #ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(5)&nbsp; 当拖动老板窗口时，如何拖动与其组合的所有窗口，关键是如何得到所有与其组合的窗口列表。<br />
</span><span style="color: #ff6600">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(6)&nbsp; 如何针对这种效果设计一个通用的组件类，只需调用几个方法便可搞定。<br />
</span>&nbsp;&nbsp;&nbsp;<br />
<span>&nbsp;&nbsp;&nbsp;针对以上问题，主要思路是视屏幕上任意多个窗口为顶点，以其窗口矩形中心点来看待这个窗口，如果任意两个窗口间关系为组合，则视这两个顶点间是相通的，即两个顶点存在边。如果为分离，则视两顶点间是不通的，即两顶点不存边。因此可以用无向图来存储窗口和关系，为简单起见，我用的是邻接矩阵，问题(4)得以解决。既然用邻接矩阵来存储，那么如何得到所有与老板窗口相关的组合窗口呢？由于实际多个窗口在移动过程中，会改变其组合分离关系，这就会得到多个无向图的连通分量，而我们需要的是包含老板窗口的那一个连通分量，因此可以用DFS深度搜索遍历这个无向图连通分量，起始顶点是老板窗口，遍历完后就会得所有与其组合的窗口列表，问题(5)得以解决。现在讨论问题(1)，这里有个细节问题就是组合分离的条件判断有两种情况，一是当移动窗口时的条件，称为条件1，因为实际向一个窗口A移入另一个窗口B时，要达到还没有接近窗口A时便一下子靠近A就像被A吸引的效果，当移出B时还没完全移到A窗口外面时便一下子远离就像被A排斥的效果。二是当大小改变时的条件，称为条件2，这个不同于条件1，因为它不需要那种吸引排斥的效果，也没必要，这个条件2就是简单的判断A和B矩形是否相交，API函数IntersectRect即可完成这一判断。条件1的判断如下图所示：</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/qinqing1984/picture.png" /><br />
<span>&nbsp;&nbsp;&nbsp;在B向A移入过程中,当B的中心点在矩形left,top,right,bottom范围内,可认为是发生组合,实现吸引效果；当在center矩形内，认为是已经组合了；同理，B向A移出过程中，当B的中心点在矩形left,top,right,bottom范围内,可认为是发生分离，实现排斥效果。当都不在left,top,right,bottom,center矩形范围时，认为是已经分离了。至此，问题(1)得到解决。当窗口大小改变时，需要更新邻接矩阵反映窗口间关系的变化，而后更新组合窗口列表，组合窗口列表的计算依赖于邻接矩阵，运用DFS算法来更新，这在WM_SIZE消息事件处理内完成，问题(2)得到解决。当新建窗口时，需要向无向图中增加(窗口)顶点，扩充邻接矩阵以备存储与其它窗口的关系；当销毁窗口时，需要从无向图中删除对应的顶点，而后从邻接矩阵中删除对应的关系，问题(3)得到解决。</span><br />
<span>&nbsp;&nbsp;&nbsp;上述问题(1)--(5)都已分析并得到解决，总的来说，就是以数据结构中无向图的观点和算法来建模解决这些问题的，特别是运用到了DFS搜索算法来重建已组合的所有窗口列表，只有这样，在移动老板窗口过程中，才能保证其它窗口跟随着一起移动。接下来就是最后一个问题，也就是怎么封装设计组件类，以达到方便应用的目的，综上所述，设计接口方法与以下窗口4种消息相关：<br />
</span>&nbsp;&nbsp;&nbsp;<span>1) 创建窗口发生的消息,如WM_CREATE,WM_INITDIALOG等。<br />
</span><span>&nbsp; &nbsp;2) 关闭或销毁窗口发生的消息，如WM_CLOSE，WM_DESTROY等。<br />
</span><span>&nbsp; &nbsp;3) 窗口大小改变后消息，WM_SIZE。<br />
</span><span>&nbsp; &nbsp;4) 窗口移动中消息，WM_MOVING。<br />
</span>&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;<span>另外提供一个设置获取老板窗口的方法，在应用程序中，只需在窗口4种消息处理内调用以上对应4个方法即可实现多窗口组合分离的效果，注意该类没有考虑多线程，因此是非安全的，适用于多窗口属于同一线程内的情况。</span><span>类声明如下</span>&nbsp;<br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;CWndMagnet<br />
</span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_17_1106_Open_Image" onclick="this.style.display='none'; Codehighlighter1_17_1106_Open_Text.style.display='none'; Codehighlighter1_17_1106_Closed_Image.style.display='inline'; Codehighlighter1_17_1106_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_17_1106_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_17_1106_Closed_Text.style.display='none'; Codehighlighter1_17_1106_Open_Image.style.display='inline'; Codehighlighter1_17_1106_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_17_1106_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_17_1106_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br />
</span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CWndMagnet();<br />
</span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="color: #0000ff">virtual</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">CWndMagnet();<br />
</span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br />
</span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img id="Codehighlighter1_107_125_Open_Image" onclick="this.style.display='none'; Codehighlighter1_107_125_Open_Text.style.display='none'; Codehighlighter1_107_125_Closed_Image.style.display='inline'; Codehighlighter1_107_125_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_107_125_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_107_125_Closed_Text.style.display='none'; Codehighlighter1_107_125_Open_Image.style.display='inline'; Codehighlighter1_107_125_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;SetLeadWindow(HWND&nbsp;hWnd)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_107_125_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_107_125_Open_Text"><span style="color: #000000">{&nbsp;m_hLead&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;hWnd;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_155_173_Open_Image" onclick="this.style.display='none'; Codehighlighter1_155_173_Open_Text.style.display='none'; Codehighlighter1_155_173_Closed_Image.style.display='inline'; Codehighlighter1_155_173_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_155_173_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_155_173_Closed_Text.style.display='none'; Codehighlighter1_155_173_Open_Image.style.display='inline'; Codehighlighter1_155_173_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;HWND&nbsp;GetLeadWindow()&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_155_173_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_155_173_Open_Text"><span style="color: #000000">{&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;m_hLead;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;AddMagnetWnd(HWND&nbsp;hWnd);<br />
</span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;RemoveMagnetWnd(HWND&nbsp;hWnd);<br />
</span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;OnLButtonDown(HWND&nbsp;hWnd);<br />
</span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;OnNcLButtonDown(HWND&nbsp;hWnd);<br />
</span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;OnMoving(HWND&nbsp;hWnd,&nbsp;LPRECT&nbsp;lpRect);<br />
</span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;OnSize(HWND&nbsp;hWnd,&nbsp;UINT&nbsp;uType);<br />
</span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">protected</span><span style="color: #000000">:<br />
</span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;MoveLeadWndSet(HWND&nbsp;hWnd,&nbsp;LPCRECT&nbsp;lpRect);<br />
</span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;UpdateLeadWndSet(HWND&nbsp;hWnd,&nbsp;LPCRECT&nbsp;lpRect&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;DeleteMagWnd(HWND&nbsp;hWnd);<br />
</span><span style="color: #008080">22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Add2DMatrix();<br />
</span><span style="color: #008080">23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Delete2DMatrix(HWND&nbsp;hWnd);<br />
</span><span style="color: #008080">24</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Update2DMatrix(HWND&nbsp;hWnd,&nbsp;LPRECT&nbsp;lpRect&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">);<br />
</span><span style="color: #008080">25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">26</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br />
</span><span style="color: #008080">27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;GetFirstNeighbor(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v);<br />
</span><span style="color: #008080">28</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;GetNextNeighbor(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;w);<br />
</span><span style="color: #008080">29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;DFS(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,&nbsp;std::vector</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">bool</span><span style="color: #000000">&gt;&amp;</span><span style="color: #000000">&nbsp;vecVisited,&nbsp;std::vector</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;&amp;</span><span style="color: #000000">&nbsp;vecNeighbor);<br />
</span><span style="color: #008080">30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">31</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br />
</span><span style="color: #008080">32</span><span style="color: #000000"><img id="Codehighlighter1_856_1105_Open_Image" onclick="this.style.display='none'; Codehighlighter1_856_1105_Open_Text.style.display='none'; Codehighlighter1_856_1105_Closed_Image.style.display='inline'; Codehighlighter1_856_1105_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_856_1105_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_856_1105_Closed_Text.style.display='none'; Codehighlighter1_856_1105_Open_Image.style.display='inline'; Codehighlighter1_856_1105_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s_c_iThreshold&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_856_1105_Closed_Text">/**/</span><span id="Codehighlighter1_856_1105_Open_Text"><span style="color: #808080">///</span><span style="color: #808080">&lt;&nbsp;偏移阀值<br />
</span><span style="color: #008080">33</span><span style="color: #808080"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;HWND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_hLead;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #808080">&lt;&nbsp;老板窗口<br />
</span><span style="color: #008080">34</span><span style="color: #808080"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;std::map</span><span style="color: #808080">&lt;HWND,POINT&gt;</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_map_leadWnd;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #808080">&lt;&nbsp;粘合窗口列表<br />
</span><span style="color: #008080">35</span><span style="color: #808080"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;std::map</span><span style="color: #808080">&lt;HWND,int&gt;</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_map_magWnd;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #808080">&lt;&nbsp;需要组合分离的窗口列表<br />
</span><span style="color: #008080">36</span><span style="color: #808080"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;std::vector</span><span style="color: #808080">&lt;std::vector&lt;bool&gt;</span><span style="color: #008000">&nbsp;&gt;&nbsp;m_vec_2DMatrix;&nbsp;</span><span style="color: #808080">///</span><span style="color: #808080">&lt;&nbsp;表示任意两个窗口间相交或相切的邻接矩阵<br />
</span><span style="color: #008080">37</span><span style="color: #808080"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080"></span></span><br />
<span style="color: #008080">38</span><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" /><span style="color: #000000">}</span></span><span style="color: #000000">;</span></div>
</div>
</div>
<img src ="http://www.cppblog.com/qinqing1984/aggbug/150084.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-07-04 11:14 <a href="http://www.cppblog.com/qinqing1984/archive/2011/07/04/150084.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模板类的数组特化</title><link>http://www.cppblog.com/qinqing1984/archive/2011/06/23/149242.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Thu, 23 Jun 2011 04:01:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2011/06/23/149242.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/149242.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2011/06/23/149242.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/149242.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/149242.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;一般地，泛型容器的设计实现大多只是存储了类型的单个对象，而没有存储类型的多个对象，如果有这样特定的需求，容器内的元素要求都是某个类型的多个对象，那么这时就可以考虑用模板类的数组特化来实现了，作为例程，下面C++代码描述了主模板实现Code highlighting produced by Actipro CodeHighlighter (freewa...&nbsp;&nbsp;<a href='http://www.cppblog.com/qinqing1984/archive/2011/06/23/149242.html'>阅读全文</a><img src ="http://www.cppblog.com/qinqing1984/aggbug/149242.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2011-06-23 12:01 <a href="http://www.cppblog.com/qinqing1984/archive/2011/06/23/149242.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ATL和WTL中的消息反射</title><link>http://www.cppblog.com/qinqing1984/archive/2010/06/14/117872.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Mon, 14 Jun 2010 09:50:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2010/06/14/117872.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/117872.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2010/06/14/117872.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/117872.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/117872.html</trackback:ping><description><![CDATA[<span><strong>&nbsp;&nbsp;&nbsp;WTL</strong>是窗口模板库(Windows Library Template)的简称，是一套轻量级C++ GUI库，因为它使用了C++模板封装了窗口界面操作API和消息映射处理，它扩展了ATL中的UI窗口部分，并支持如下更多的功能特性：</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(1)&nbsp;&nbsp;&nbsp;对话框和通用控件：包括对话框数据交换(DDX)，子类化，控件消息通知与反射等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(2)&nbsp;&nbsp; 工具栏和状态栏：包括工具条UI状态更新，多窗格状态条及UI状态更新等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(3)&nbsp;&nbsp; 分隔窗口：包括窗格容器，嵌套分隔，特殊绘制等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(4)&nbsp;&nbsp; 属性页和向导：包括属性表，普通属性页，向导属性页等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(5)&nbsp;&nbsp; GDI类等：包括GDI封装类，通用对话框等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(6)&nbsp;&nbsp; 使用ActiveX控件：包括使用控件类，调用控件的方法，控件事件映射处理等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(7)&nbsp;&nbsp; 高级对话框UI类：包括自绘和外观定制类，新控件类，控件UI状态更新，对话框数据验证DDV等</span><br /><span>&nbsp;&nbsp;&nbsp;&nbsp;(8)&nbsp;&nbsp; 支持拖放操作：包括拖放接口实现类，最近使用文件列表等</span><br />&nbsp; &nbsp;<span>综上所述，使用WTL几乎可以实现MFC所能实现的功能与界面，而且生成的执行文件体积更小，不需要动态链接库就可直接快速地执行。<br /><br /></span>&nbsp;&nbsp;&nbsp;<span>根据WIN32窗口原理，当事件发生的时候，一般由父窗口接收其子窗口或控件的通知或命令消息，在这里父窗口是消息接收者，子窗口或控件是消息发送者，那么谁是消息处理者呢？实际上由谁来处理消息只是代码上的逻辑，既可以在父窗口的窗口过程回调内处理，也可以在子窗口或控件的窗口过程回调内处理，在哪处理更方便合理就在哪处理，如果是在子窗口或控件窗口过程回调内处理，那么就需要作额外的处理了，也就是在父窗口中将消息反射给发送者，进而再由发送者处理。下面以父窗口为打开文件对话框，双击它的列表视图控件为例，给出运用上面<span>(1)中的控件消息通知与反射来</span>处理NM_DBLCLK消息的两种实现方式。<br /><br /></span><span>&nbsp;&nbsp;&nbsp;<strong>继承方式：由控件处理消息</strong></span><br /><span>&nbsp;&nbsp;&nbsp;从CWindowImpl模板基类派生一个子窗口或控件子类即listview子类，添加消息映射项和消息处理函数，消息映射项用REFLECTED_NOTIFY_XXX或REFLECTED_COMMAND_XXX系列反射宏实现，具体使用哪个宏，决定于是否通知或命令消息，及消息对应的ID和通知码。</span> 
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">class</span><span style="color: #000000">&nbsp;<span style="color: #000000">CFileListViewCtrl</span></span><span style="color: #000000">&nbsp;:&nbsp;</span><span style="color: #000000">public</span><span style="color: #000000">&nbsp;<span style="color: #000000">CWindowImpl</span></span><span style="color: #000000">&lt;</span><span style="color: #000000">CFileListViewCtrl</span><span style="color: #000000">, </span><span style="color: #000000">CListViewCtrl</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">2</span><span style="color: #000000"><img id="Codehighlighter1_78_329_Open_Image" onclick="this.style.display='none'; Codehighlighter1_78_329_Open_Text.style.display='none'; Codehighlighter1_78_329_Closed_Image.style.display='inline'; Codehighlighter1_78_329_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_78_329_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_78_329_Closed_Text.style.display='none'; Codehighlighter1_78_329_Open_Image.style.display='inline'; Codehighlighter1_78_329_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_78_329_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_78_329_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #000000">protected</span><span style="color: #000000">:</span><span style="color: #000000; font-size: 10pt"><br /></span><span style="color: #008080">4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BEGIN_MSG_MAP(CFileListViewCtrl)<br /></span><span style="color: #008080">5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #993366">REFLECTED_NOTIFY_CODE_HANDLER_EX</span><span style="color: #000000">(NM_DBLCLK,OnListViewDblclk)</span></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">反射通知消息处理宏</span><span style="color: #008000"><br /></span><span style="color: #008080">6</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHAIN_MSG_MAP(CListViewCtrl)<br /></span><span style="color: #008080">7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_MSG_MAP()<br /></span><span style="color: #008080">8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;LRESULT&nbsp;<span style="color: #993366">OnListViewDblclk</span></span><span style="color: #000000">(NMHDR</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;pNMHDR);&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">消息响应处理函数</span><span style="color: #008000"><br /></span><span style="color: #008080">9</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000">}</span></span><span style="color: #000000">;</span></div></div></div><span>&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;在父窗口类消息映射链中最后添加反射通知宏REFLECT_NOTIFICATIONS()项。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> 
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">class</span><span style="color: #000000">&nbsp;<span style="color: #000000">COpenFileDlg</span></span><span style="color: #000000">&nbsp;:&nbsp;</span><span style="color: #000000">public</span><span style="color: #000000">&nbsp;<span style="color: #000000">CDialogImpl</span></span><span style="color: #000000">&lt;</span><span style="color: #000000">COpenFileDlg</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;&nbsp;,&nbsp;&nbsp;</span><span style="color: #000000">public</span><span style="color: #000000">&nbsp;<span style="color: #000000">CWinDataExchange</span></span><span style="color: #000000">&lt;</span><span style="color: #000000">COpenFileDlg</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_96_653_Open_Image" onclick="this.style.display='none'; Codehighlighter1_96_653_Open_Text.style.display='none'; Codehighlighter1_96_653_Closed_Image.style.display='inline'; Codehighlighter1_96_653_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_96_653_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_96_653_Closed_Text.style.display='none'; Codehighlighter1_96_653_Open_Image.style.display='inline'; Codehighlighter1_96_653_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_96_653_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_96_653_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">public</span><span style="color: #000000">:</span><span style="color: #000000; font-size: 10pt"><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;COpenFileDlg();<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">COpenFileDlg();<br /></span><span style="color: #008080">&nbsp;6<span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span></span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #000000">enum</span><span style="color: #000000">&nbsp;　</span><span id="Codehighlighter1_154_180_Open_Text"><span style="color: #000000">{&nbsp;IDD&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;IDD_OPEN_FILE_DLG&nbsp;}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">protected</span><span style="color: #000000">:</span><span style="color: #000000; font-size: 10pt"><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BEGIN_MSG_MAP(COpenFileDlg)<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MESSAGE_HANDLER(WM_INITDIALOG,OnInitDialog)&nbsp;<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #993366">REFLECT_NOTIFICATIONS</span></span><span style="color: #000000">()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">消息反射通知宏</span><span style="color: #008000"><br /></span><span style="color: #008080">12</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_MSG_MAP()<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BEGIN_DDX_MAP(COpenFileDlg)<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DDX_CONTROL(IDC_LIST_FILE,m_list_File)<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_DDX_MAP()<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LRESULT&nbsp;OnInitDialog(UINT&nbsp;uMsg,WPARAM&nbsp;wParam,LPARAM&nbsp;lParam,BOOL</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;bHandle);<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000"><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000">CFileListViewCtrl</span></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;</span><span style="color: #000000">m_list_File;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">使用派生类实例作为成员变量</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000">}</span></span><span style="color: #000000">;</span></div></div></div><span><br />　<strong>成员方式：由父窗口处理消息</strong>&nbsp;<br /></span><span>&nbsp;&nbsp;&nbsp;直接使用ATL中的包含窗口模板类CContainedWindowT，参数为子控件的类名即listviewctrl，实例化为父窗口类的一个成员变量，在父窗口类消息映射链中添加ALT_MSG_MAP宏来实现消息分派，其参数为分派ID，这个ID为成员变量初始化时指定的常量；添加反射通知宏REFLECT_NOTIFICATIONS()，注意ALT_MSG_MAP宏必须在反射通知宏REFLECT_NOTIFICATIONS之后。&nbsp;&nbsp; </span>
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">class</span><span style="color: #000000">&nbsp;<span style="color: #000000">COpenFileDlg</span></span><span style="color: #000000">&nbsp;:&nbsp;</span><span style="color: #000000">public</span><span style="color: #000000">&nbsp;<span style="color: #000000">CDialogImpl</span></span><span style="color: #000000">&lt;</span><span style="color: #000000">COpenFileDlg</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;,　&nbsp;</span><span style="color: #000000">public</span><span style="color: #000000">&nbsp;<span style="color: #000000">CWinDataExchange</span></span><span style="color: #000000">&lt;</span><span style="color: #000000">COpenFileDlg</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_95_847_Open_Image" onclick="this.style.display='none'; Codehighlighter1_95_847_Open_Text.style.display='none'; Codehighlighter1_95_847_Closed_Image.style.display='inline'; Codehighlighter1_95_847_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_95_847_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_95_847_Closed_Text.style.display='none'; Codehighlighter1_95_847_Open_Image.style.display='inline'; Codehighlighter1_95_847_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_95_847_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_95_847_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COpenFileDlg();<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">COpenFileDlg();<br /></span><span style="color: #008080">&nbsp;6<span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span></span><span style="color: #000000">&nbsp;&nbsp;</span><span style="color: #000000">enum</span><span style="color: #000000">&nbsp;&nbsp;</span><span id="Codehighlighter1_172_198_Open_Text"><span style="color: #000000">{&nbsp;IDD&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;IDD_OPEN_FILE_DLG&nbsp;}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="color: #000000">protected</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BEGIN_MSG_MAP(COpenFileDlg)<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MESSAGE_HANDLER(WM_INITDIALOG,OnInitDialog)&nbsp;<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #993366">REFLECT_NOTIFICATIONS</span></span><span style="color: #000000">()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;消息反射通知宏</span><span style="color: #008000"><br /></span><span style="color: #008080">12</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #993366">ALT_MSG_MAP</span></span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;消息分派宏</span><span style="color: #008000"><br /></span><span style="color: #008080">13</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #993366">REFLECTED_NOTIFY_CODE_HANDLER_EX</span></span><span style="color: #000000">(NM_DBLCLK,OnListViewDblclk)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp; 反射通知消息处理宏</span><span style="color: #008000"><br /></span><span style="color: #008080">14</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_MSG_MAP()<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BEGIN_DDX_MAP(COpenFileDlg)<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DDX_CONTROL(IDC_LIST_FILE,m_list_File)<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_DDX_MAP()<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LRESULT&nbsp;OnInitDialog(UINT&nbsp;uMsg,WPARAM&nbsp;wParam,LPARAM&nbsp;lParam,BOOL</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;bHandle);<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LRESULT&nbsp;<span style="color: #993366">OnListViewDblclk</span></span><span style="color: #000000">(NMHDR</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;pNMHDR);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000">//</span><span style="color: #008000">消息响应处理函数</span><br /></span><span style="color: #008080">22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="color: #000000">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000">CContainedWindowT</span></span><span style="color: #000000">&lt;</span><span style="color: #000000">CListViewCtrl</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;&nbsp; m_list_File;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;实例化包含窗口模板类作为成员变量</span><span style="color: #008000"><br /></span><span style="color: #008080">24</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000">&nbsp;}</span></span><span style="color: #000000">;</span></div></div></div><span><br />&nbsp;&nbsp;&nbsp;在父窗口内需要初始化m_list_File以指定分派ID号。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> 
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">COpenFileDlg</span><span>:: </span><span style="color: #000000">COpenFileDlg</span><span>():</span><br /><span style="color: #008080">2</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000">m_list_File(</span><span style="color: #000000">t</span><span style="color: #000000">his</span><span style="color: #000000">,</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #008000">// 指定消息分派ID为1</span><br /></span><span style="color: #008080">3</span><span style="color: #000000"><img id="Codehighlighter1_50_55_Open_Image" onclick="this.style.display='none'; Codehighlighter1_50_55_Open_Text.style.display='none'; Codehighlighter1_50_55_Closed_Image.style.display='inline'; Codehighlighter1_50_55_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_50_55_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_50_55_Closed_Text.style.display='none'; Codehighlighter1_50_55_Open_Image.style.display='inline'; Codehighlighter1_50_55_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_50_55_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_50_55_Open_Text"><span style="color: #000000">{</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;<br /></span><span style="color: #000000">4<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div></div></div><img src ="http://www.cppblog.com/qinqing1984/aggbug/117872.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2010-06-14 17:50 <a href="http://www.cppblog.com/qinqing1984/archive/2010/06/14/117872.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入MFC中WM_COMMAND的传递</title><link>http://www.cppblog.com/qinqing1984/archive/2009/12/19/103535.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Sat, 19 Dec 2009 13:29:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2009/12/19/103535.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/103535.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2009/12/19/103535.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/103535.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/103535.html</trackback:ping><description><![CDATA[<p style="font-size: 10pt"><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;MFC将windows消息系统进行了高度的抽象和封装，其根本原理是运用C++的高级特性并结合一定的设计模式（如工厂模式，模板方法等）来实现的。一般的windows消息（WM_XXX），则一定是由派生类流向基类，没有旁流的可能。如果是命令消息（WM_COMMAND），那就有比较奇特的路线了。下面就针对多文档/单文档（Document-View）、对话框两种应用程序比较讨论WM_COMMAND消息的传递处理过程。讨论前首先得明确命令消息的来源，命令消息一般是用户选择某个菜单项，或一个加速键被翻译，或一个子控件发送一个通知消息给它的父窗口时产生的。对一个菜单而言，消息接收者是Frame窗口或拥有它的对话框；对一个工具栏而言，消息接收者是它的父窗口。两种应用程序命令消息处理流程如下图所示。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<div align="center"><img style="width: 708px; height: 284px" border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/qinqing1984/wm_command.PNG" width="708" height="284" /></div><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;从上图可知，文档视图型的处理路线是先向下再向上，而对话框型的路线是一直向上，消息接收者只有一个，而处理者次序有多个，每个处理者内部首先都是调用根基类CCmdTarget的OnCmdMsg虚函数，在这个函数内逐级向基类遍历消息映射表，根据命令ID和通知码找到对应的消息映射结构体AFX_MSGMAP_ENTRY，如果找到再处理这个命令消息，否则返回FALSE，退回到this对象所在的OnCmdMsg函数进行下一步处理。如果到最后一层都没有找到对应命令的消息映射，则返回到系统的默认处理DefWindowProc。再综合考虑下，如果一个对话框接收到了一个命令消息例如是点击它的子控件工具栏某个按钮发出的，而这个对话框类没有添加相应的ON_COMMAND映射，就会进入到它的父窗口类OnCmdMsg函数进行处理，如果这个父窗口正好是Frame窗口，那么命令消息的处理流程就由上图右边转到左边了。而最终命令消息能否得处理，就看上图5种对象（Frame、View、Document、Dialog、App、Thread）是否添加了对应的ON_COMMAND映射。<br /></span>&nbsp; &nbsp;<br />&nbsp;&nbsp;&nbsp;<span style="font-size: 10pt">到此为止，我们已经明确了WM_COMMAND消息的处理流程，但是发现最终处理却是由收到消息的窗口传递的，不是消息通知者自己处理的，有的时候为了提高代码的封装性，可能需要自己处理这些命令比较方便，比如有一个工具栏CPlayToolBar子类从CToolBar继承，有播放、暂停、停止3个按钮，它的父窗口是CPlayDialog对话框。按照常规，这3个按钮命令事件的处理一般是在CPlayDialog类中3个ON_COMMAND映射宏和处理函数的，但如果在CPlayToolBar类中添加3个ON_COMMAND映射宏和处理函数，是得不到处理的，其原因在于对话框型的路线是一直向上，再者MFC中没有对应的命令反射ON_COMMAND_REFLECT这个宏。为了能使CPlayToolBar类自己处理这3个按钮命令事件，就需要从CPlayDialog类中转移路线，使之流向其子窗口工具栏，这样CPlayToolbar 类就得到了自己处理的机会。具体操作是重载CPlayToolBar和CPlayDialog的OnCommand虚函数,&nbsp;&nbsp;CPlayDialog代码如下所示：</span> 
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">&nbsp;</span><span style="color: #000000; font-size: 10pt">1</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;BOOL&nbsp;&nbsp;&nbsp;CPlayDialog::OnCommand(WPARAM&nbsp;wParam,&nbsp;LPARAM&nbsp;lParam)<br /><img id="Codehighlighter1_68_323_Open_Image" onclick="this.style.display='none'; Codehighlighter1_68_323_Open_Text.style.display='none'; Codehighlighter1_68_323_Closed_Image.style.display='inline'; Codehighlighter1_68_323_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_68_323_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_68_323_Closed_Text.style.display='none'; Codehighlighter1_68_323_Open_Image.style.display='inline'; Codehighlighter1_68_323_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span style="color: #000000; font-size: 10pt">2</span><span style="color: #000000">&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_68_323_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_68_323_Open_Text"><span style="color: #000000; font-size: 10pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">3</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-size: 10pt">if</span><span style="color: #000000; font-size: 10pt">&nbsp;(lParam</span><span style="color: #000000; font-size: 10pt">==</span><span style="color: #000000; font-size: 10pt">(LPARAM)m_playtoolbar.m_hWnd)<br /><img id="Codehighlighter1_133_224_Open_Image" onclick="this.style.display='none'; Codehighlighter1_133_224_Open_Text.style.display='none'; Codehighlighter1_133_224_Closed_Image.style.display='inline'; Codehighlighter1_133_224_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_133_224_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_133_224_Closed_Text.style.display='none'; Codehighlighter1_133_224_Open_Image.style.display='inline'; Codehighlighter1_133_224_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;</span><span style="color: #000000; font-size: 10pt">4</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_133_224_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_133_224_Open_Text"><span style="color: #000000; font-size: 10pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">5</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_playtoolbar.OnCommand(wParam,lParam);&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-size: 10pt">//m_playtoolbar为CPlayToolBar对象，</span><span style="color: #008000; font-size: 10pt">注意使OnCommand成为公有成员</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000">&nbsp;</span><span style="color: #000000; font-size: 10pt">6</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">7</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-size: 10pt">else</span><span style="color: #000000"><br /><img id="Codehighlighter1_249_319_Open_Image" onclick="this.style.display='none'; Codehighlighter1_249_319_Open_Text.style.display='none'; Codehighlighter1_249_319_Closed_Image.style.display='inline'; Codehighlighter1_249_319_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_249_319_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_249_319_Closed_Text.style.display='none'; Codehighlighter1_249_319_Open_Image.style.display='inline'; Codehighlighter1_249_319_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;</span><span style="color: #000000; font-size: 10pt">8</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_249_319_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_249_319_Open_Text"><span style="color: #000000; font-size: 10pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">9</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-size: 10pt">return</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;CDialog::OnCommand(wParam,&nbsp;lParam);<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000; font-size: 10pt">10</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000; font-size: 10pt">11&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">}</span></span></div></div></div><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;CPlayToolBar类代码如下所示</span> 
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />&nbsp;<span style="color: #000000; font-size: 10pt">1&nbsp;&nbsp;&nbsp; BEGIN_MESSAGE_MAP(CPlayToolBar,&nbsp;CToolBar)<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">2</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON_COMMAND(ID_PLAY,&nbsp;&nbsp;Play)<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">3</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON_COMMAND(ID_PAUSE,&nbsp;&nbsp;Pause)<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">4</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON_COMMAND(ID_STOP,&nbsp;&nbsp;Stop)<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;5&nbsp;&nbsp;&nbsp; END_MESSAGE_MAP()<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">6</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;7&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff; font-size: 10pt">void</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;CPlayToolBar::Play()<br /><img id="Codehighlighter1_209_217_Open_Image" onclick="this.style.display='none'; Codehighlighter1_209_217_Open_Text.style.display='none'; Codehighlighter1_209_217_Closed_Image.style.display='inline'; Codehighlighter1_209_217_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_209_217_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_209_217_Closed_Text.style.display='none'; Codehighlighter1_209_217_Open_Image.style.display='inline'; Codehighlighter1_209_217_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span style="color: #000000; font-size: 10pt">8</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_209_217_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_209_217_Open_Text"><span style="color: #000000; font-size: 10pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;</span><span style="color: #000000; font-size: 10pt">9</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">10&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-size: 10pt">void</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;CPlayToolBar::Pause()<br /><img id="Codehighlighter1_255_262_Open_Image" onclick="this.style.display='none'; Codehighlighter1_255_262_Open_Text.style.display='none'; Codehighlighter1_255_262_Closed_Image.style.display='inline'; Codehighlighter1_255_262_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_255_262_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_255_262_Closed_Text.style.display='none'; Codehighlighter1_255_262_Open_Image.style.display='inline'; Codehighlighter1_255_262_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #000000; font-size: 10pt">11</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_255_262_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_255_262_Open_Text"><span style="color: #000000; font-size: 10pt">{<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000; font-size: 10pt">12</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">13</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-size: 10pt">void</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;CPlayToolBar::Stop()<br /><img id="Codehighlighter1_302_310_Open_Image" onclick="this.style.display='none'; Codehighlighter1_302_310_Open_Text.style.display='none'; Codehighlighter1_302_310_Closed_Image.style.display='inline'; Codehighlighter1_302_310_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_302_310_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_302_310_Closed_Text.style.display='none'; Codehighlighter1_302_310_Open_Image.style.display='inline'; Codehighlighter1_302_310_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #000000; font-size: 10pt">14</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_302_310_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_302_310_Open_Text"><span style="color: #000000; font-size: 10pt">{&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000; font-size: 10pt">15</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;}</span></span></div></div></div><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;现在，3个按钮命令事件能在CPlayToolBar类中独立处理了，这样一来就提高了代码的封装性，简化了父窗口CPlayDialog类的处理。</span><img src ="http://www.cppblog.com/qinqing1984/aggbug/103535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2009-12-19 21:29 <a href="http://www.cppblog.com/qinqing1984/archive/2009/12/19/103535.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>basic_string内存泄露问题之分析解决</title><link>http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Thu, 06 Aug 2009 17:31:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/92479.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html#Feedback</comments><slash:comments>19</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/92479.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/92479.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;最近在工作中,写一计算杆塔绝缘子中心点的GPS坐标程序时，定义了一结构，里面用到了string类型来存储杆塔所属线路号、杆塔号，杆塔模型名称。代码如下: 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080; font-size: 10pt">&nbsp;1</span><img id="Codehighlighter1_0_20_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_20_Open_Text.style.display='none'; Codehighlighter1_0_20_Closed_Image.style.display='inline'; Codehighlighter1_0_20_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_0_20_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_0_20_Closed_Text.style.display='none'; Codehighlighter1_0_20_Open_Image.style.display='inline'; Codehighlighter1_0_20_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"><span id="Codehighlighter1_0_20_Open_Text"><span style="color: #008000; font-size: 10pt">/*</span><span style="color: #008000"><br /></span><span style="color: #008080; font-size: 10pt">&nbsp;2</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #008000; font-size: 10pt">&nbsp;&nbsp;@brief&nbsp;杆塔信息结构<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;3</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #008000; font-size: 10pt">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080; font-size: 10pt">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">typedef&nbsp;</span><span style="color: #0000ff; font-size: 10pt">struct</span><span style="color: #000000">&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">_TOWER_INFO</span><br /></span><span style="color: #008080; font-size: 10pt">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_49_575_Open_Image" onclick="this.style.display='none'; Codehighlighter1_49_575_Open_Text.style.display='none'; Codehighlighter1_49_575_Closed_Image.style.display='inline'; Codehighlighter1_49_575_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_49_575_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_49_575_Closed_Text.style.display='none'; Codehighlighter1_49_575_Open_Image.style.display='inline'; Codehighlighter1_49_575_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_49_575_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_49_575_Open_Text"><span style="color: #000000; font-size: 10pt">{<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;6</span><span style="color: #000000"><img id="Codehighlighter1_77_516_Open_Image" onclick="this.style.display='none'; Codehighlighter1_77_516_Open_Text.style.display='none'; Codehighlighter1_77_516_Closed_Image.style.display='inline'; Codehighlighter1_77_516_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_77_516_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_77_516_Closed_Text.style.display='none'; Codehighlighter1_77_516_Open_Image.style.display='inline'; Codehighlighter1_77_516_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff; font-size: 10pt">string</span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp; strLineNo;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000; font-size: 10pt">///&lt;&nbsp;线路号</span>&nbsp;<br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp;7<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">string</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp;&nbsp;&nbsp; strTowerNo;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 10pt">&nbsp;&nbsp; ///&lt;&nbsp;杆塔号</span><br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp;8<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">string</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp;&nbsp;&nbsp; strTowerType;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 10pt">///&lt;&nbsp;杆塔类型<br /></span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp;9<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">double</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp; dDangDistance;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 10pt">///&lt;&nbsp;档距</span><br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">10<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">double</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp; dHCHeight;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #008000; font-size: 10pt">///&lt;&nbsp;呼称高</span><br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">11<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">double</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp; dLongitude;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #008000; font-size: 10pt">///&lt;&nbsp;经度<br /></span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">12<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">double</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp; dLatitude;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #008000; font-size: 10pt">///&lt;&nbsp;纬度</span><br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">13<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">double</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp; dAltitude;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #008000; font-size: 10pt">///&lt;&nbsp;海拔高度</span><br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">14<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">double</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp; dLineCorners;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #008000; font-size: 10pt">///&lt;&nbsp;线路转角</span>&nbsp;<br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">15<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">long&nbsp;</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&nbsp;&nbsp;&nbsp;&nbsp; lCornerDirection;&nbsp;<span style="color: #008000; font-size: 10pt">&nbsp;///&lt;&nbsp;左转还是右转:&nbsp;0不转,&nbsp;1左转,&nbsp;2右转</span><br /></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">16<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #0000ff; font-size: 10pt">vector</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&lt;<span style="color: #0000ff; font-size: 10pt">INSULATOR_INFO::CENTER_POINT_INFO</span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">&gt;&nbsp;&nbsp;vecInsulatorCenterPointInfo;&nbsp;<span style="color: #008000; font-size: 10pt">///&lt;&nbsp;杆塔所有绝缘子中心点信息<br /></span></span><span style="font-size: 10pt" id="Codehighlighter1_49_575_Open_Text">17<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp; <span style="color: #000000">&nbsp;&nbsp;&nbsp;<span style="color: #993366; font-size: 10pt">_TOWER_INFO</span></span><span style="color: #000000; font-size: 10pt">()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_532_572_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_532_572_Open_Text"><span style="color: #000000; font-size: 10pt">{&nbsp;memset(</span><span style="color: #0000ff; font-size: 10pt">this</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">0</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #0000ff; font-size: 10pt">sizeof</span><span style="color: #000000; font-size: 10pt">(_TOWER_INFO));&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff0000; font-size: 10pt">//该行代码可能会引起string内存泄露</span></span></span><span style="color: #000000"><br /></span><span style="color: #008080; font-size: 10pt">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #000000"><font color="#008080"><span style="font-size: 10pt">19</span></font><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">TOWER_INFO,</span><span style="color: #000000; font-size: 10pt">*</span><span style="color: #000000; font-size: 10pt">PTOWER_INFO;</span></div></span><span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;在后面对该结构的string型变量有赋值操作, 代码如下<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #000000; font-size: 10pt">1<img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp; ......<br /></span><span style="color: #000000; font-size: 10pt">2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TOWER_INFO&nbsp;cur_tower_center_info;<br /></span><span style="color: #000000"><font color="#008080"><span style="font-size: 10pt">3</span></font><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;cur_tower_center_info.strLineNo&nbsp;</span><span style="color: #000000; font-size: 10pt">=</span><span style="color: #000000; font-size: 10pt">&nbsp;sheetLine</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000; font-size: 10pt">Cell(i,&nbsp;</span><span style="color: #000000; font-size: 10pt">2</span><span style="color: #000000; font-size: 10pt">)</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000; font-size: 10pt">GetText();&nbsp;</span><span style="color: #008000; font-size: 10pt">//</span><span style="color: #008000; font-size: 10pt">调度码</span><span style="color: #008000"><br /></span><span style="color: #008000"><font color="#008080"><span style="font-size: 10pt">4</span></font><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;cur_tower_center_info.strTowerNo&nbsp;</span><span style="color: #000000; font-size: 10pt">=</span><span style="color: #000000; font-size: 10pt">&nbsp;sheetLine</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000; font-size: 10pt">Cell(i,&nbsp;</span><span style="color: #000000; font-size: 10pt">7</span><span style="color: #000000; font-size: 10pt">)</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000; font-size: 10pt">GetText();&nbsp;</span><span style="color: #008000; font-size: 10pt">//</span><span style="color: #008000; font-size: 10pt">杆塔号</span><span style="color: #008000"><br /></span><span style="color: #008000"><font color="#008080"><span style="font-size: 10pt">5</span></font><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;cur_tower_center_info.strTowerType&nbsp;</span><span style="color: #000000; font-size: 10pt">=</span><span style="color: #000000; font-size: 10pt">&nbsp;sheetLine</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000; font-size: 10pt">Cell(i,&nbsp;</span><span style="color: #000000; font-size: 10pt">8</span><span style="color: #000000; font-size: 10pt">)</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000; font-size: 10pt">GetText();&nbsp;</span><span style="color: #008000; font-size: 10pt">//</span><span style="color: #008000; font-size: 10pt">杆塔类型<br /></span><span style="color: #008000; font-size: 10pt">6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ......</span></div></span><span style="font-size: 12pt"><span style="color: #000000; font-size: 10pt">&nbsp;&nbsp;&nbsp;运行程序,待程序结束后,发现有内存泄露,提示信息如下<br /></span><span style="font-size: 12pt">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080; font-size: 10pt">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000; font-size: 10pt">Detected&nbsp;memory&nbsp;leaks</span><span style="color: #000000; font-size: 10pt">!</span><span style="color: #000000"><br /></span><span style="color: #008080; font-size: 10pt">&nbsp;2</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">Dumping&nbsp;objects&nbsp;</span><span style="color: #000000; font-size: 10pt">-&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080; font-size: 10pt">&nbsp;3</span><span style="color: #000000"><img id="Codehighlighter1_42_49_Open_Image" onclick="this.style.display='none'; Codehighlighter1_42_49_Open_Text.style.display='none'; Codehighlighter1_42_49_Closed_Image.style.display='inline'; Codehighlighter1_42_49_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_42_49_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_42_49_Closed_Text.style.display='none'; Codehighlighter1_42_49_Open_Image.style.display='inline'; Codehighlighter1_42_49_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_42_49_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_42_49_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235250</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x01774A60</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_169_176_Open_Image" onclick="this.style.display='none'; Codehighlighter1_169_176_Open_Text.style.display='none'; Codehighlighter1_169_176_Closed_Image.style.display='inline'; Codehighlighter1_169_176_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_169_176_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_169_176_Closed_Text.style.display='none'; Codehighlighter1_169_176_Open_Image.style.display='inline'; Codehighlighter1_169_176_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_169_176_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_169_176_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235237</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x01774CB0</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;7</span><span style="color: #000000"><img id="Codehighlighter1_296_303_Open_Image" onclick="this.style.display='none'; Codehighlighter1_296_303_Open_Text.style.display='none'; Codehighlighter1_296_303_Closed_Image.style.display='inline'; Codehighlighter1_296_303_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_296_303_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_296_303_Closed_Text.style.display='none'; Codehighlighter1_296_303_Open_Image.style.display='inline'; Codehighlighter1_296_303_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_296_303_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_296_303_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235234</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x01774A10</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_423_430_Open_Image" onclick="this.style.display='none'; Codehighlighter1_423_430_Open_Text.style.display='none'; Codehighlighter1_423_430_Closed_Image.style.display='inline'; Codehighlighter1_423_430_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_423_430_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_423_430_Closed_Text.style.display='none'; Codehighlighter1_423_430_Open_Image.style.display='inline'; Codehighlighter1_423_430_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_423_430_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_423_430_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235184</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x01774200</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">11</span><span style="color: #000000"><img id="Codehighlighter1_550_557_Open_Image" onclick="this.style.display='none'; Codehighlighter1_550_557_Open_Text.style.display='none'; Codehighlighter1_550_557_Closed_Image.style.display='inline'; Codehighlighter1_550_557_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_550_557_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_550_557_Closed_Text.style.display='none'; Codehighlighter1_550_557_Open_Image.style.display='inline'; Codehighlighter1_550_557_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_550_557_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_550_557_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235171</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x01774450</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">13</span><span style="color: #000000"><img id="Codehighlighter1_677_684_Open_Image" onclick="this.style.display='none'; Codehighlighter1_677_684_Open_Text.style.display='none'; Codehighlighter1_677_684_Closed_Image.style.display='inline'; Codehighlighter1_677_684_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_677_684_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_677_684_Closed_Text.style.display='none'; Codehighlighter1_677_684_Open_Image.style.display='inline'; Codehighlighter1_677_684_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_677_684_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_677_684_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235168</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x017741B0</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">15</span><span style="color: #000000"><img id="Codehighlighter1_804_811_Open_Image" onclick="this.style.display='none'; Codehighlighter1_804_811_Open_Text.style.display='none'; Codehighlighter1_804_811_Closed_Image.style.display='inline'; Codehighlighter1_804_811_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_804_811_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_804_811_Closed_Text.style.display='none'; Codehighlighter1_804_811_Open_Image.style.display='inline'; Codehighlighter1_804_811_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_804_811_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_804_811_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235118</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x017739A0</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">17</span><span style="color: #000000"><img id="Codehighlighter1_931_938_Open_Image" onclick="this.style.display='none'; Codehighlighter1_931_938_Open_Text.style.display='none'; Codehighlighter1_931_938_Closed_Image.style.display='inline'; Codehighlighter1_931_938_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_931_938_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_931_938_Closed_Text.style.display='none'; Codehighlighter1_931_938_Open_Image.style.display='inline'; Codehighlighter1_931_938_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_931_938_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_931_938_Open_Text"><span style="color: #000000; font-size: 10pt">{</span><span style="color: #000000; font-size: 10pt">235105</span><span style="color: #000000; font-size: 10pt">}</span></span><span style="color: #000000; font-size: 10pt">&nbsp;normal&nbsp;block&nbsp;at&nbsp;</span><span style="color: #000000; font-size: 10pt">0x01773BF0</span><span style="color: #000000; font-size: 10pt">,&nbsp;</span><span style="color: #000000; font-size: 10pt">16</span><span style="color: #000000; font-size: 10pt">&nbsp;bytes&nbsp;</span><span style="color: #0000ff; font-size: 10pt">long</span><span style="color: #000000; font-size: 10pt">.<br /></span><span style="color: #008080; font-size: 10pt">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /></span><span style="color: #000000; font-size: 10pt">&nbsp;Data:&nbsp;</span><span style="color: #000000; font-size: 10pt">&lt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; font-size: 10pt">&gt;</span><span style="color: #000000; font-size: 10pt">&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;CD&nbsp;<br /></span><span style="color: #008080; font-size: 10pt">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span style="color: #000000; font-size: 10pt">..</span></div><span style="font-size: 10pt">&nbsp;&nbsp; 经过一番源代码跟踪调试后,发现原因在于TOWER_INFO结构体的构造函数内调用了memset(this, 0, sizeof(_TOWER_INFO);使得string内部指针_Bx._Ptrr值为0,_Myres为0,在这种情况下当string对象被赋值为小字符串(字节数包括结束符小于等于16的字符串)时,因新申请的内存在后来得不到释放,所以这块内存被泄露了,根据string类内存管理算法(ms vc版本)得知这块内存大小总是16个字节.但当被赋值为大字符串(字节数包括结束符大于16的字符串)时,反而没有内存泄露,这是因为新申请的内存在析构或下次赋值时总能被释放.<br /></span><span style="font-size: 10pt">&nbsp;&nbsp;从该泄露问题的分析解决过程中，总结得到规律：不要轻易零初始化string, vector等stl标准容器及具有动态内存管理的类。</span></span></span><img src ="http://www.cppblog.com/qinqing1984/aggbug/92479.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2009-08-07 01:31 <a href="http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>串口通讯与超时设置</title><link>http://www.cppblog.com/qinqing1984/archive/2009/04/17/80282.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Fri, 17 Apr 2009 11:15:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2009/04/17/80282.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/80282.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2009/04/17/80282.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/80282.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/80282.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;在WINDOWS NT4.0 以上操作系统中，串口通讯有2种模式：同步方式和异步方式。由<span style="color: red;">CreateFile</span>中的<font color="#0000ff"><span style="color: red;">dwFlagsAndAttributes</span></font>参数决定，<span ?=""><span ?=""><span ?=""><span ?=""><span style="href_cetemp: "  ?=""><span style="href_cetemp: "  ?=""><span style="href_cetemp: "  ?=""><span style="href_cetemp: "  ?=""><span style="href_cetemp: "  ?=""><span  ?=""><span  ?=""><span  ?=""><span style="href_cetemp: "  ?=""><span style="href_cetemp: "  ?="">若指定<span style="color: red;">FILE_FLAG_OVERLAPPED</span><span style="color: #000000;">标志则为异步方式，否则为同步方式。当为同步模式时,调用</span><span style="color: red;">ReadFile</span>或<span style="color: red;">WriteFile</span><span style="color: #000000;">会阻塞调用线程直到读完或写完指定量的数据才返回，这样就有可能出现无法退出程序的现象，解决方法是为读写操作设置超时，注意这种超时指的是</span><span style="color: red;">ReadFile</span><span style="color: #000000;">或</span><span style="color: red;">WriteFile</span><span style="color: #000000;">函数的返回时间，仅对同步模式有效。代码如下</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">以下m_pComPort为本人自己封装的C++串口类CComPort的指针<br />
</span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;FALSE表示以同步方式打开</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pComPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">Open(</span><span style="color: #000000">2</span><span style="color: #000000">,&nbsp;FALSE,&nbsp;</span><span style="color: #000000">38400</span><span style="color: #000000">);<br />
</span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">设置读写超时为5秒</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;7</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMMTIMEOUTS&nbsp;&nbsp;timeout&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span id="Codehighlighter1_191_195_Open_Text"><span style="color: #000000">{&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;}</span></span><span style="color: #000000">;<br />
</span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout.ReadTotalTimeoutConstant&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5000</span><span style="color: #000000">;<br />
</span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout.WriteTotalTimeoutConstant&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">5000</span><span style="color: #000000">;<br />
</span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">SetTimeouts(timeout);<br />
</span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /><br />
</span><span style="color: #008080">12</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;&nbsp;szData[</span><span style="color: #000000">1024</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span id="Codehighlighter1_382_386_Open_Text"><span style="color: #000000">{&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;}</span></span><span style="color: #000000">;<br />
</span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">读数据</span><span style="color: #008000"><br />
</span><span style="color: #008080">14</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwRet&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ReadComm(szData,&nbsp;</span><span style="color: #000000">1024</span><span style="color: #000000">);<br />
</span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">写数据</span><span style="color: #008000"><br />
</span><span style="color: #008080">16</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwRet&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">WriteComm(szData,&nbsp;</span><span style="color: #000000">1024</span><span style="color: #000000">);<br />
</span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">关闭串口</span><span style="color: #008000"><br />
</span><span style="color: #008080">18</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">Close();</span></div>
</div>
</div>
&nbsp;&nbsp;&nbsp;当为异步模式时，由于读写操作会立即返回，因此设置超时指的是设置等待操作完成的时间，而不是<span style="color: red;">ReadFile</span>或<span style="color: red;">WriteFile</span>函数返回的时间，代码如下<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">以下m_pComPort为本人自己封装的C++串口类CComPort的指针<br />
</span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;TRUE表示以异步方式打开</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pComPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">Open(</span><span style="color: #000000">2</span><span style="color: #000000">,&nbsp;TRUE,&nbsp;</span><span style="color: #000000">38400</span><span style="color: #000000">);<br />
</span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">设置读写等待超时为5秒</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;7</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;&nbsp;szData[</span><span style="color: #000000">1024</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_192_196_Closed_Text"></span><span id="Codehighlighter1_192_196_Open_Text"><span style="color: #000000">{&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;}</span></span><span style="color: #000000">;<br />
</span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">当第3个参数为0时，读写操作会立即返回<br />
</span><span style="color: #008080">10</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">读数据</span><span style="color: #008000"><br />
</span><span style="color: #008080">11</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwRet&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">ReadComm(szData,&nbsp;</span><span style="color: #000000">1024</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">5000</span><span style="color: #000000">);<br />
</span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">写数据</span><span style="color: #008000"><br />
</span><span style="color: #008080">13</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwRet&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">WriteComm(szData,&nbsp;</span><span style="color: #000000">1024</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">5000</span><span style="color: #000000">);<br />
</span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">关闭串口</span><span style="color: #008000"><br />
</span><span style="color: #008080">15</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pCommPort</span><span style="color: #000000">-&gt;</span><span style="color: #000000">Close();</span></div>
</div>
</div>
&nbsp;&nbsp;&nbsp;这里的<span style="color: red;">ReadComm</span>和<span style="color: red;">WriteComm</span>的实现内部针对不同模式作了不同处理，异步模式时即调用了<span style="color: red;">WaitForSingleObject</span>等待函数来设置超时。同步模式时即调用不带重叠结构的<span style="color: red;">ReadFile</span>或<span style="color: red;">WriteFile</span>函数来接收或发送指定量的数据。另外在这介绍下串口虚拟软件vspd，这个软件能模拟在同一台计算机上进行两个串口的通讯，有利于没有实际设备情况下的串口调试。<img src ="http://www.cppblog.com/qinqing1984/aggbug/80282.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2009-04-17 19:15 <a href="http://www.cppblog.com/qinqing1984/archive/2009/04/17/80282.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>工作线程与消息循环</title><link>http://www.cppblog.com/qinqing1984/archive/2009/04/15/80038.html</link><dc:creator>春秋十二月</dc:creator><author>春秋十二月</author><pubDate>Wed, 15 Apr 2009 10:11:00 GMT</pubDate><guid>http://www.cppblog.com/qinqing1984/archive/2009/04/15/80038.html</guid><wfw:comment>http://www.cppblog.com/qinqing1984/comments/80038.html</wfw:comment><comments>http://www.cppblog.com/qinqing1984/archive/2009/04/15/80038.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/qinqing1984/comments/commentRss/80038.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qinqing1984/services/trackbacks/80038.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt">&nbsp;&nbsp;&nbsp;首先声明，这里的工作线程与UI线程是相对的，即没有任何窗口的。如果需要与主线程或其它辅助线程通讯，有几种方法如事件、消息和信号等，也可以是以上几种方法的综合运用。下面就列出以下3种通讯方法的代码框架。<br /><br /></span>&nbsp; &nbsp;<span style="font-size: 10pt"><strong>只用消息通讯</strong><br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">&nbsp;&nbsp;DWORD&nbsp;ThreadProc(LPVOID&nbsp;lParam)<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_36_532_Open_Image" onclick="this.style.display='none'; Codehighlighter1_36_532_Open_Text.style.display='none'; Codehighlighter1_36_532_Closed_Image.style.display='inline'; Codehighlighter1_36_532_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_36_532_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_36_532_Closed_Text.style.display='none'; Codehighlighter1_36_532_Open_Image.style.display='inline'; Codehighlighter1_36_532_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_36_532_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_36_532_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">创建线程消息队列</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSG&nbsp;msg;<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PeekMessage(</span><span style="color: #000000">&amp;</span><span style="color: #000000">msg,&nbsp;NULL,&nbsp;WM_USER,&nbsp;WM_USER,&nbsp;PM_NOREMOVE);<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">通知其它线程消息队列已创建好</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetEvent(hEvent);&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img id="Codehighlighter1_205_515_Open_Image" onclick="this.style.display='none'; Codehighlighter1_205_515_Open_Text.style.display='none'; Codehighlighter1_205_515_Closed_Image.style.display='inline'; Codehighlighter1_205_515_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_205_515_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_205_515_Closed_Text.style.display='none'; Codehighlighter1_205_515_Open_Image.style.display='inline'; Codehighlighter1_205_515_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_205_515_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_205_515_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetMessage(</span><span style="color: #000000">&amp;</span><span style="color: #000000">msg,&nbsp;NULL,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">);<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">switch</span><span style="color: #000000">(msg.message)<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img id="Codehighlighter1_282_509_Open_Image" onclick="this.style.display='none'; Codehighlighter1_282_509_Open_Text.style.display='none'; Codehighlighter1_282_509_Closed_Image.style.display='inline'; Codehighlighter1_282_509_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_282_509_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_282_509_Closed_Text.style.display='none'; Codehighlighter1_282_509_Open_Image.style.display='inline'; Codehighlighter1_282_509_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_282_509_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_282_509_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;WM_QUIT:<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">自定义消息1处理</span><span style="color: #008000"><br /></span><span style="color: #008080">18</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;WM_USER&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">:<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">自定义消息2处理</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;WM_USER&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">101</span><span style="color: #000000">:<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;}</span></span></div></div></div></span><span style="font-size: 10pt">&nbsp;<br /></span>&nbsp; &nbsp;<span style="font-size: 10pt"><strong>只用事件通讯&nbsp;</strong><br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">&nbsp;&nbsp;DWORD&nbsp;ThreadProc(LPVOID&nbsp;lParam)<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_36_503_Open_Image" onclick="this.style.display='none'; Codehighlighter1_36_503_Open_Text.style.display='none'; Codehighlighter1_36_503_Closed_Image.style.display='inline'; Codehighlighter1_36_503_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_36_503_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_36_503_Closed_Text.style.display='none'; Codehighlighter1_36_503_Open_Image.style.display='inline'; Codehighlighter1_36_503_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_36_503_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_36_503_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwIndex;<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img id="Codehighlighter1_87_500_Open_Image" onclick="this.style.display='none'; Codehighlighter1_87_500_Open_Text.style.display='none'; Codehighlighter1_87_500_Closed_Image.style.display='inline'; Codehighlighter1_87_500_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_87_500_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_87_500_Closed_Text.style.display='none'; Codehighlighter1_87_500_Open_Image.style.display='inline'; Codehighlighter1_87_500_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_87_500_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_87_500_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwIndex&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;WaitForMultipleObjects(cObjects,&nbsp;pObjects,&nbsp;FALSE,&nbsp;INFINTE);<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(WAIT_OBJECT&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;dwIndex)<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img id="Codehighlighter1_224_274_Open_Image" onclick="this.style.display='none'; Codehighlighter1_224_274_Open_Text.style.display='none'; Codehighlighter1_224_274_Closed_Image.style.display='inline'; Codehighlighter1_224_274_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_224_274_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_224_274_Closed_Text.style.display='none'; Codehighlighter1_224_274_Open_Image.style.display='inline'; Codehighlighter1_224_274_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_224_274_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_224_274_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">假设为退出事件</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(WAIT_OBJECT&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;dwIndex)<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img id="Codehighlighter1_331_367_Open_Image" onclick="this.style.display='none'; Codehighlighter1_331_367_Open_Text.style.display='none'; Codehighlighter1_331_367_Closed_Image.style.display='inline'; Codehighlighter1_331_367_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_331_367_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_331_367_Closed_Text.style.display='none'; Codehighlighter1_331_367_Open_Image.style.display='inline'; Codehighlighter1_331_367_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_331_367_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_331_367_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">事件1受信，处理之</span><span style="color: #008000"><br /></span><span style="color: #008080">14</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(WAIT_OBJECT&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;cObjects&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;dwIndwx)<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img id="Codehighlighter1_445_493_Open_Image" onclick="this.style.display='none'; Codehighlighter1_445_493_Open_Text.style.display='none'; Codehighlighter1_445_493_Closed_Image.style.display='inline'; Codehighlighter1_445_493_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_445_493_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_445_493_Closed_Text.style.display='none'; Codehighlighter1_445_493_Open_Image.style.display='inline'; Codehighlighter1_445_493_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_445_493_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_445_493_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">事件cObjects&nbsp;-&nbsp;1受信,&nbsp;处理之</span><span style="color: #008000"><br /></span><span style="color: #008080">19</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;}</span></span></div></div></div><br /></span>&nbsp; &nbsp;<span style="font-size: 10pt"><strong>用消息和事件通讯</strong><br />
<div align="center">
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px">
<div align="left"><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" /><span style="color: #000000">&nbsp;DWORD&nbsp;ThreadProc(LPVOID&nbsp;lParam)<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img id="Codehighlighter1_34_1000_Open_Image" onclick="this.style.display='none'; Codehighlighter1_34_1000_Open_Text.style.display='none'; Codehighlighter1_34_1000_Closed_Image.style.display='inline'; Codehighlighter1_34_1000_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_34_1000_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_34_1000_Closed_Text.style.display='none'; Codehighlighter1_34_1000_Open_Image.style.display='inline'; Codehighlighter1_34_1000_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_34_1000_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_34_1000_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(TRUE)<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img id="Codehighlighter1_56_999_Open_Image" onclick="this.style.display='none'; Codehighlighter1_56_999_Open_Text.style.display='none'; Codehighlighter1_56_999_Closed_Image.style.display='inline'; Codehighlighter1_56_999_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_56_999_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_56_999_Closed_Text.style.display='none'; Codehighlighter1_56_999_Open_Image.style.display='inline'; Codehighlighter1_56_999_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_56_999_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_56_999_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;ret&nbsp;;&nbsp;<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSG&nbsp;msg&nbsp;;&nbsp;<br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(PeekMessage(</span><span style="color: #000000">&amp;</span><span style="color: #000000">msg,&nbsp;NULL,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;PM_REMOVE))&nbsp;<br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img id="Codehighlighter1_172_476_Open_Image" onclick="this.style.display='none'; Codehighlighter1_172_476_Open_Text.style.display='none'; Codehighlighter1_172_476_Closed_Image.style.display='inline'; Codehighlighter1_172_476_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_172_476_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_172_476_Closed_Text.style.display='none'; Codehighlighter1_172_476_Open_Image.style.display='inline'; Codehighlighter1_172_476_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_172_476_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_172_476_Open_Text"><span style="color: #000000">{&nbsp;<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">switch</span><span style="color: #000000">(msg.message)<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img id="Codehighlighter1_214_466_Open_Image" onclick="this.style.display='none'; Codehighlighter1_214_466_Open_Text.style.display='none'; Codehighlighter1_214_466_Closed_Image.style.display='inline'; Codehighlighter1_214_466_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_214_466_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_214_466_Closed_Text.style.display='none'; Codehighlighter1_214_466_Open_Image.style.display='inline'; Codehighlighter1_214_466_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_214_466_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_214_466_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">线程退出消息,直接返回</span><span style="color: #008000"><br /></span><span style="color: #008080">13</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;WM_QUIT:<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">自定义消息1处理</span><span style="color: #008000"><br /></span><span style="color: #008080">17</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;WM_USER&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">:<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">自定义消息2处理</span><span style="color: #008000"><br /></span><span style="color: #008080">20</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">case</span><span style="color: #000000">&nbsp;WM_USER&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">101</span><span style="color: #000000">:<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">24</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;MsgWaitForMultipleObjects(cObjects,&nbsp;lphObjects,&nbsp;FALSE,INFINITE,QS_POSTMESSAGE);&nbsp;<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ret&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;(WAIT_OBJECT_0&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;cObjects))<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img id="Codehighlighter1_628_699_Open_Image" onclick="this.style.display='none'; Codehighlighter1_628_699_Open_Text.style.display='none'; Codehighlighter1_628_699_Closed_Image.style.display='inline'; Codehighlighter1_628_699_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_628_699_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_628_699_Closed_Text.style.display='none'; Codehighlighter1_628_699_Open_Image.style.display='inline'; Codehighlighter1_628_699_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_628_699_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_628_699_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">有新的消息到来,继续到上步PeekMessage处理</span><span style="color: #008000"><br /></span><span style="color: #008080">28</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img id="Codehighlighter1_724_977_Open_Image" onclick="this.style.display='none'; Codehighlighter1_724_977_Open_Text.style.display='none'; Codehighlighter1_724_977_Closed_Image.style.display='inline'; Codehighlighter1_724_977_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_724_977_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_724_977_Closed_Text.style.display='none'; Codehighlighter1_724_977_Open_Image.style.display='inline'; Codehighlighter1_724_977_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_724_977_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_724_977_Open_Text"><span style="color: #000000">{&nbsp;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">是事件受信了</span><span style="color: #008000"><br /></span><span style="color: #008080">33</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ret&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;WAIT_OBJECT_O)<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img id="Codehighlighter1_793_820_Open_Image" onclick="this.style.display='none'; Codehighlighter1_793_820_Open_Text.style.display='none'; Codehighlighter1_793_820_Closed_Image.style.display='inline'; Codehighlighter1_793_820_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_793_820_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_793_820_Closed_Text.style.display='none'; Codehighlighter1_793_820_Open_Image.style.display='inline'; Codehighlighter1_793_820_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_793_820_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_793_820_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">35</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">36</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ret&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;WAIT_OBJECT_O&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">37</span><span style="color: #000000"><img id="Codehighlighter1_877_889_Open_Image" onclick="this.style.display='none'; Codehighlighter1_877_889_Open_Text.style.display='none'; Codehighlighter1_877_889_Closed_Image.style.display='inline'; Codehighlighter1_877_889_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_877_889_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_877_889_Closed_Text.style.display='none'; Codehighlighter1_877_889_Open_Image.style.display='inline'; Codehighlighter1_877_889_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_877_889_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_877_889_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">39</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(ret&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;WAIT_OBJECT_O&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;cObjects&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img id="Codehighlighter1_956_968_Open_Image" onclick="this.style.display='none'; Codehighlighter1_956_968_Open_Text.style.display='none'; Codehighlighter1_956_968_Closed_Image.style.display='inline'; Codehighlighter1_956_968_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_956_968_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_956_968_Closed_Text.style.display='none'; Codehighlighter1_956_968_Open_Image.style.display='inline'; Codehighlighter1_956_968_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_956_968_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_956_968_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">42</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">43</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">44</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;}</span></span></span></div></div></div></span><span style="font-size: 10pt;"><span id="Codehighlighter1_33_986_Open_Text">&nbsp;&nbsp;&nbsp;上面用到了GetMessage和PeekMessage&nbsp;函数，这两者都是从消息队列取出消息，不同的是GetMessage从消息队列删除消息，并且阻塞调用线程。PeekMessage则是查询消息队列，如果有消息就取出，没有消息也立即返回，是否从消息队列删除消息由最后一个参数决定:PM_REMOVE表示删除，PM_NOREMOVE表示不删除。可以简单地认为，GetMessage是同步的，PeekMessage是异步的。</span></span><img src ="http://www.cppblog.com/qinqing1984/aggbug/80038.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qinqing1984/" target="_blank">春秋十二月</a> 2009-04-15 18:11 <a href="http://www.cppblog.com/qinqing1984/archive/2009/04/15/80038.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>