﻿<?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++博客-拈花一笑-文章分类-C++</title><link>http://www.cppblog.com/Dutyboy/category/13973.html</link><description>拈花一笑，淡然之……</description><language>zh-cn</language><lastBuildDate>Thu, 03 Jun 2010 05:49:52 GMT</lastBuildDate><pubDate>Thu, 03 Jun 2010 05:49:52 GMT</pubDate><ttl>60</ttl><item><title>参考VARIANT的简单代码实现</title><link>http://www.cppblog.com/Dutyboy/articles/117101.html</link><dc:creator>灭神佛</dc:creator><author>灭神佛</author><pubDate>Thu, 03 Jun 2010 05:40:00 GMT</pubDate><guid>http://www.cppblog.com/Dutyboy/articles/117101.html</guid><wfw:comment>http://www.cppblog.com/Dutyboy/comments/117101.html</wfw:comment><comments>http://www.cppblog.com/Dutyboy/articles/117101.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Dutyboy/comments/commentRss/117101.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Dutyboy/services/trackbacks/117101.html</trackback:ping><description><![CDATA[<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">不知道大家使用过VARIANT没有，本人使用的时候因为其中涉及到的库文件和头文件比较复杂，于是自己就仿照它写了两个简单的文件。需要使用的时候可以之间包含这两个文件即可，使用方法和VARIANT类似，本文纯粹是为了抛砖引玉，希望大家多提意见修改，谢谢！<br><br>首先是结构体以及枚举的定义文件，Type.h<br></p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">#pragma once</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">enum enumTypeVariant<br>{<br>&nbsp;MYVT_EMPTY&nbsp; = 0,<br>&nbsp;MYVT_NULL&nbsp;= 1,<br>&nbsp;MYVT_I2&nbsp;&nbsp;= 2,<br>&nbsp;MYVT_I4&nbsp;&nbsp;= 3,<br>&nbsp;MYVT_R4&nbsp;&nbsp;= 4,<br>&nbsp;MYVT_R8&nbsp;&nbsp;= 5,<br>&nbsp;MYVT_BOOL&nbsp;= 6,<br>&nbsp;MYVT_I1&nbsp;&nbsp;= 7,<br>&nbsp;MYVT_UI1&nbsp;= 8,<br>&nbsp;MYVT_UI2&nbsp;= 9,<br>&nbsp;MYVT_UI4&nbsp;= 10,<br>&nbsp;MYVT_INT&nbsp;= 13,<br>&nbsp;MYVT_UINT&nbsp;= 14,<br>&nbsp;MYVT_VOID&nbsp;= 15,</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;MYVT_OSGVEC2&nbsp;= 16,<br>&nbsp;MYVT_OSGVEC3&nbsp;= 17,<br>&nbsp;MYVT_OSGVEC4&nbsp;= 18,<br>&nbsp;MYVT_OSGMATRIXD = 19<br>} ;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">typedef short&nbsp;VARIANT_BOOL;<br>typedef float*&nbsp;OSGVEC2;<br>typedef float*&nbsp;OSGVEC3;<br>typedef float*&nbsp;OSGVEC4;<br>typedef double* OSGMATRIXD;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">typedef unsigned short VARTYPE;<br>typedef struct tagTypeDefine MYVARIANT;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">struct tagTypeDefine<br>{<br>&nbsp;VARTYPE vt;<br>&nbsp;union <br>&nbsp;{<br>&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lVal;<br>&nbsp;&nbsp;short&nbsp;&nbsp;&nbsp;&nbsp; iVal;<br>&nbsp;&nbsp;float&nbsp;&nbsp;&nbsp;&nbsp; fltVal;<br>&nbsp;&nbsp;double&nbsp;&nbsp;&nbsp;&nbsp; dblVal;<br>&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp; cVal;<br>&nbsp;&nbsp;unsigned short uiVal;<br>&nbsp;&nbsp;unsigned long&nbsp; ulVal;<br>&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intVal;<br>&nbsp;&nbsp;unsigned int&nbsp;&nbsp; uintVal;<br>&nbsp;&nbsp;VARIANT_BOOL&nbsp;&nbsp; boolVal;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;&nbsp;OSGVEC2&nbsp;&nbsp;osgVec2Val;<br>&nbsp;&nbsp;OSGVEC3&nbsp;&nbsp;osgVec3Val;<br>&nbsp;&nbsp;OSGVEC4&nbsp;&nbsp;osgVec4Val;<br>&nbsp;&nbsp;OSGMATRIXD&nbsp; osgMatrixdVal;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;};<br>};<br>其中涉及到osg的向量，因为要使用，所以自己加上去的；<br><br>然后是CTypeDefine.h文件：<br><br>#pragma once</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">#include "Type.h"<br>#include &lt;osg/Node&gt;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">class CTypeDefine : public::tagTypeDefine<br>{<br>public:<br>&nbsp;CTypeDefine() throw();<br>&nbsp;CTypeDefine(int vIntSrc) throw();<br>&nbsp;CTypeDefine(float vFloatSrc) throw();<br>&nbsp;CTypeDefine(double vDoubleSrc) throw();<br>&nbsp;CTypeDefine(bool vBoolSrc) throw();<br>&nbsp;CTypeDefine(const osg::Vec2&amp; vOsgVec2Src) throw();<br>&nbsp;CTypeDefine(const osg::Vec3&amp; vOsgVec3Src) throw();<br>&nbsp;CTypeDefine(const osg::Vec4&amp; vOsgVec4Src) throw();<br>&nbsp;CTypeDefine(const osg::Matrixd&amp; vOsgMatrixdSrc) throw();</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;CTypeDefine&amp; operator=(int vIntSrc);<br>&nbsp;CTypeDefine&amp; operator=(float vFloatSrc);<br>&nbsp;CTypeDefine&amp; operator=(double vDoubleSrc);<br>&nbsp;CTypeDefine&amp; operator=(bool vBoolSrc);<br>&nbsp;CTypeDefine&amp; operator=(osg::Vec2 vOsgVec2Src);<br>&nbsp;CTypeDefine&amp; operator=(osg::Vec3 vOsgVec3Src);<br>&nbsp;CTypeDefine&amp; operator=(osg::Vec4 vOsgVec4Src);<br>&nbsp;CTypeDefine&amp; operator=(osg::Matrixd vOsgMatrixdSrc);</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;MYVARIANT&amp; GetVARIANT() throw();<br>&nbsp;<br>&nbsp;float* getVec2()&nbsp;&nbsp;&nbsp; {return m_dataType.m_arrayVec2;}<br>&nbsp;float* getVec3()&nbsp;{return m_dataType.m_arrayVec3;}<br>&nbsp;float* getVec4()&nbsp;{return m_dataType.m_arrayVec4;}<br>&nbsp;double* getMat()&nbsp;&nbsp;&nbsp; {return m_dataType.m_matrixd;}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">private:<br>&nbsp;union DATATYPE{<br>&nbsp;&nbsp;float&nbsp; m_arrayVec2[2];<br>&nbsp;&nbsp;float&nbsp; m_arrayVec3[3];<br>&nbsp;&nbsp;float&nbsp; m_arrayVec4[4];<br>&nbsp;&nbsp;double m_matrixd[16];<br>&nbsp;}m_dataType;<br>};</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine::CTypeDefine()<br>{<br>&nbsp;this-&gt;vt = MYVT_EMPTY;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine::CTypeDefine(int vIntSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_INT;<br>&nbsp;this-&gt;intVal = vIntSrc;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine::CTypeDefine(float vFloatSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_R4;<br>&nbsp;this-&gt;fltVal = vFloatSrc;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine::CTypeDefine(double vDoubleSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_R8;<br>&nbsp;this-&gt;dblVal = vDoubleSrc;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine::CTypeDefine(bool vBoolSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_BOOL;<br>&nbsp;this-&gt;boolVal = vBoolSrc;<br>}<br>//*************************************************************************************************************************************************<br>//Function: initializing for osg::Vec2<br>inline CTypeDefine::CTypeDefine(const osg::Vec2&amp; vOsgVec2Src)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGVEC2;<br>&nbsp;m_dataType.m_arrayVec2[0] = vOsgVec2Src[0];<br>&nbsp;m_dataType.m_arrayVec2[1] = vOsgVec2Src[1];<br>&nbsp;this-&gt;osgVec2Val = m_dataType.m_arrayVec2;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: initializing for osg::Vec3<br>inline CTypeDefine::CTypeDefine(const osg::Vec3&amp; vOsgVec3Src)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGVEC3;<br>&nbsp;m_dataType.m_arrayVec3[0] = vOsgVec3Src[0];<br>&nbsp;m_dataType.m_arrayVec3[1] = vOsgVec3Src[1];<br>&nbsp;m_dataType.m_arrayVec3[2] = vOsgVec3Src[2];<br>&nbsp;this-&gt;osgVec3Val = m_dataType.m_arrayVec3;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: initializing for osg::Vec4<br>inline CTypeDefine::CTypeDefine(const osg::Vec4&amp; vOsgVec4Src)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGVEC4;<br>&nbsp;for (int i=0; i&lt;4; i++)<br>&nbsp;{<br>&nbsp;&nbsp;m_dataType.m_arrayVec4[i] = vOsgVec4Src[i];<br>&nbsp;}<br>&nbsp;this-&gt;osgVec4Val = m_dataType.m_arrayVec4;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: initializing for osg::Matrixd<br>inline CTypeDefine::CTypeDefine(const osg::Matrixd&amp; vOsgMatrixdSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGMATRIXD;<br>&nbsp;for (int i=0; i&lt;4; i++)<br>&nbsp;{<br>&nbsp;&nbsp;for (int k=0; k&lt;4; k++)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;m_dataType.m_matrixd[i*4 + k] = vOsgMatrixdSrc(i,k);<br>&nbsp;&nbsp;}<br>&nbsp;}<br>&nbsp;this-&gt;osgMatrixdVal = m_dataType.m_matrixd;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine&amp; CTypeDefine::operator =(int vIntSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_INT;<br>&nbsp;this-&gt;intVal = vIntSrc;<br>&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine&amp; CTypeDefine::operator =(float vFloatSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_R4;<br>&nbsp;this-&gt;fltVal = vFloatSrc;<br>&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine&amp; CTypeDefine::operator =(double vDoubleSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_R8;<br>&nbsp;this-&gt;dblVal = vDoubleSrc;<br>&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline CTypeDefine&amp; CTypeDefine::operator =(bool vBoolSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_BOOL;<br>&nbsp;this-&gt;boolVal = vBoolSrc;<br>&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: overloading the operator "=" for osg::Vec2<br>inline CTypeDefine&amp; CTypeDefine::operator=(osg::Vec2 vOsgVec2Src)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGVEC2;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;m_dataType.m_arrayVec2[0] = vOsgVec2Src[0];<br>&nbsp;m_dataType.m_arrayVec2[1] = vOsgVec2Src[1];<br>&nbsp;this-&gt;osgVec2Val =&nbsp;m_dataType.m_arrayVec2;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: overloading the operator "=" for osg::Vec3<br>inline CTypeDefine&amp; CTypeDefine::operator=(osg::Vec3 vOsgVec3Src)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGVEC3;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;m_dataType.m_arrayVec3[0] = vOsgVec3Src[0];<br>&nbsp;m_dataType.m_arrayVec3[1] = vOsgVec3Src[1];<br>&nbsp;m_dataType.m_arrayVec3[2] = vOsgVec3Src[2];<br>&nbsp;this-&gt;osgVec3Val = m_dataType.m_arrayVec3;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: overloading the operator "=" for osg::Vec4<br>inline CTypeDefine&amp; CTypeDefine::operator=(osg::Vec4 vOsgVec4Src)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGVEC4;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;for (int i=0; i&lt;4; i++)<br>&nbsp;{<br>&nbsp;&nbsp;m_dataType.m_arrayVec4[i] = vOsgVec4Src[i];<br>&nbsp;}<br>&nbsp;this-&gt;osgVec4Val = m_dataType.m_arrayVec4;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">//*************************************************************************************************************************************************<br>//Function: overloading the operator "=" for osg::Matrixd<br>inline CTypeDefine&amp; CTypeDefine::operator=(osg::Matrixd vOsgMatrixdSrc)<br>{<br>&nbsp;this-&gt;vt = MYVT_OSGMATRIXD;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;for (int i=0; i&lt;4; i++)<br>&nbsp;{<br>&nbsp;&nbsp;for (int k=0; k&lt;4; k++)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;m_dataType.m_matrixd[i*4 + k] = vOsgMatrixdSrc(i,k);<br>&nbsp;&nbsp;}<br>&nbsp;}<br>&nbsp;this-&gt;osgMatrixdVal = m_dataType.m_matrixd;</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">&nbsp;return *this;<br>}</p>
<p style="FONT-FAMILY: 微软雅黑; COLOR: #339966">inline MYVARIANT&amp; CTypeDefine::GetVARIANT() throw() <br>{<br>&nbsp;return *(MYVARIANT*) this;<br>}<br></p>
<img src ="http://www.cppblog.com/Dutyboy/aggbug/117101.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Dutyboy/" target="_blank">灭神佛</a> 2010-06-03 13:40 <a href="http://www.cppblog.com/Dutyboy/articles/117101.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>