不知道大家使用过VARIANT没有,本人使用的时候因为其中涉及到的库文件和头文件比较复杂,于是自己就仿照它写了两个简单的文件。需要使用的时候可以直接包含这两个文件即可,使用方法和VARIANT类似,本文纯粹是为了抛砖引玉,希望大家多提意见修改,谢谢!
首先是结构体以及枚举的定义文件,Type.h
1#pragma once
2
3enum enumTypeVariant
4{
5 MYVT_EMPTY = 0,
6 MYVT_NULL = 1,
7 MYVT_I2 = 2,
8 MYVT_I4 = 3,
9 MYVT_R4 = 4,
10 MYVT_R8 = 5,
11 MYVT_BOOL = 6,
12 MYVT_I1 = 7,
13 MYVT_UI1 = 8,
14 MYVT_UI2 = 9,
15 MYVT_UI4 = 10,
16 MYVT_INT = 13,
17 MYVT_UINT = 14,
18 MYVT_VOID = 15,
19
20 MYVT_OSGVEC2 = 16,
21 MYVT_OSGVEC3 = 17,
22 MYVT_OSGVEC4 = 18,
23 MYVT_OSGMATRIXD = 19
24} ;
25
26typedef short VARIANT_BOOL;
27typedef float* OSGVEC2;
28typedef float* OSGVEC3;
29typedef float* OSGVEC4;
30typedef double* OSGMATRIXD;
31
32typedef unsigned short VARTYPE;
33typedef struct tagTypeDefine MYVARIANT;
34
35struct tagTypeDefine
36{
37 VARTYPE vt;
38 union
39 {
40 long lVal;
41 short iVal;
42 float fltVal;
43 double dblVal;
44 char cVal;
45 unsigned short uiVal;
46 unsigned long ulVal;
47 int intVal;
48 unsigned int uintVal;
49 VARIANT_BOOL boolVal;
50
51 OSGVEC2 osgVec2Val;
52 OSGVEC3 osgVec3Val;
53 OSGVEC4 osgVec4Val;
54 OSGMATRIXD osgMatrixdVal;
55 };
56};
其中涉及到osg的向量,因为要使用,所以自己加上去的;
然后是CTypeDefine.h文件:
#pragma once
#include "Type.h"
#include <osg/Node>
class CTypeDefine : public::tagTypeDefine
{
public:
CTypeDefine() throw();
CTypeDefine(int vIntSrc) throw();
CTypeDefine(float vFloatSrc) throw();
CTypeDefine(double vDoubleSrc) throw();
CTypeDefine(bool vBoolSrc) throw();
CTypeDefine(const osg::Vec2& vOsgVec2Src) throw();
CTypeDefine(const osg::Vec3& vOsgVec3Src) throw();
CTypeDefine(const osg::Vec4& vOsgVec4Src) throw();
CTypeDefine(const osg::Matrixd& vOsgMatrixdSrc) throw();
CTypeDefine& operator=(int vIntSrc);
CTypeDefine& operator=(float vFloatSrc);
CTypeDefine& operator=(double vDoubleSrc);
CTypeDefine& operator=(bool vBoolSrc);
CTypeDefine& operator=(osg::Vec2 vOsgVec2Src);
CTypeDefine& operator=(osg::Vec3 vOsgVec3Src);
CTypeDefine& operator=(osg::Vec4 vOsgVec4Src);
CTypeDefine& operator=(osg::Matrixd vOsgMatrixdSrc);
MYVARIANT& GetVARIANT() throw();
float* getVec2() {return m_dataType.m_arrayVec2;}
float* getVec3() {return m_dataType.m_arrayVec3;}
float* getVec4() {return m_dataType.m_arrayVec4;}
double* getMat() {return m_dataType.m_matrixd;}
private:
union DATATYPE{
float m_arrayVec2[2];
float m_arrayVec3[3];
float m_arrayVec4[4];
double m_matrixd[16];
}m_dataType;
};
inline CTypeDefine::CTypeDefine()
{
this->vt = MYVT_EMPTY;
}
inline CTypeDefine::CTypeDefine(int vIntSrc)
{
this->vt = MYVT_INT;
this->intVal = vIntSrc;
}
inline CTypeDefine::CTypeDefine(float vFloatSrc)
{
this->vt = MYVT_R4;
this->fltVal = vFloatSrc;
}
inline CTypeDefine::CTypeDefine(double vDoubleSrc)
{
this->vt = MYVT_R8;
this->dblVal = vDoubleSrc;
}
inline CTypeDefine::CTypeDefine(bool vBoolSrc)
{
this->vt = MYVT_BOOL;
this->boolVal = vBoolSrc;
}
//*************************************************************************************************************************************************
//Function: initializing for osg::Vec2
inline CTypeDefine::CTypeDefine(const osg::Vec2& vOsgVec2Src)
{
this->vt = MYVT_OSGVEC2;
m_dataType.m_arrayVec2[0] = vOsgVec2Src[0];
m_dataType.m_arrayVec2[1] = vOsgVec2Src[1];
this->osgVec2Val = m_dataType.m_arrayVec2;
}
//*************************************************************************************************************************************************
//Function: initializing for osg::Vec3
inline CTypeDefine::CTypeDefine(const osg::Vec3& vOsgVec3Src)
{
this->vt = MYVT_OSGVEC3;
m_dataType.m_arrayVec3[0] = vOsgVec3Src[0];
m_dataType.m_arrayVec3[1] = vOsgVec3Src[1];
m_dataType.m_arrayVec3[2] = vOsgVec3Src[2];
this->osgVec3Val = m_dataType.m_arrayVec3;
}
//*************************************************************************************************************************************************
//Function: initializing for osg::Vec4
inline CTypeDefine::CTypeDefine(const osg::Vec4& vOsgVec4Src)
{
this->vt = MYVT_OSGVEC4;
for (int i=0; i<4; i++)
{
m_dataType.m_arrayVec4[i] = vOsgVec4Src[i];
}
this->osgVec4Val = m_dataType.m_arrayVec4;
}
//*************************************************************************************************************************************************
//Function: initializing for osg::Matrixd
inline CTypeDefine::CTypeDefine(const osg::Matrixd& vOsgMatrixdSrc)
{
this->vt = MYVT_OSGMATRIXD;
for (int i=0; i<4; i++)
{
for (int k=0; k<4; k++)
{
m_dataType.m_matrixd[i*4 + k] = vOsgMatrixdSrc(i,k);
}
}
this->osgMatrixdVal = m_dataType.m_matrixd;
}
inline CTypeDefine& CTypeDefine::operator =(int vIntSrc)
{
this->vt = MYVT_INT;
this->intVal = vIntSrc;
return *this;
}
inline CTypeDefine& CTypeDefine::operator =(float vFloatSrc)
{
this->vt = MYVT_R4;
this->fltVal = vFloatSrc;
return *this;
}
inline CTypeDefine& CTypeDefine::operator =(double vDoubleSrc)
{
this->vt = MYVT_R8;
this->dblVal = vDoubleSrc;
return *this;
}
inline CTypeDefine& CTypeDefine::operator =(bool vBoolSrc)
{
this->vt = MYVT_BOOL;
this->boolVal = vBoolSrc;
return *this;
}
//*************************************************************************************************************************************************
//Function: overloading the operator "=" for osg::Vec2
inline CTypeDefine& CTypeDefine::operator=(osg::Vec2 vOsgVec2Src)
{
this->vt = MYVT_OSGVEC2;
m_dataType.m_arrayVec2[0] = vOsgVec2Src[0];
m_dataType.m_arrayVec2[1] = vOsgVec2Src[1];
this->osgVec2Val = m_dataType.m_arrayVec2;
return *this;
}
//*************************************************************************************************************************************************
//Function: overloading the operator "=" for osg::Vec3
inline CTypeDefine& CTypeDefine::operator=(osg::Vec3 vOsgVec3Src)
{
this->vt = MYVT_OSGVEC3;
m_dataType.m_arrayVec3[0] = vOsgVec3Src[0];
m_dataType.m_arrayVec3[1] = vOsgVec3Src[1];
m_dataType.m_arrayVec3[2] = vOsgVec3Src[2];
this->osgVec3Val = m_dataType.m_arrayVec3;
return *this;
}
//*************************************************************************************************************************************************
//Function: overloading the operator "=" for osg::Vec4
inline CTypeDefine& CTypeDefine::operator=(osg::Vec4 vOsgVec4Src)
{
this->vt = MYVT_OSGVEC4;
for (int i=0; i<4; i++)
{
m_dataType.m_arrayVec4[i] = vOsgVec4Src[i];
}
this->osgVec4Val = m_dataType.m_arrayVec4;
return *this;
}
//*************************************************************************************************************************************************
//Function: overloading the operator "=" for osg::Matrixd
inline CTypeDefine& CTypeDefine::operator=(osg::Matrixd vOsgMatrixdSrc)
{
this->vt = MYVT_OSGMATRIXD;
for (int i=0; i<4; i++)
{
for (int k=0; k<4; k++)
{
m_dataType.m_matrixd[i*4 + k] = vOsgMatrixdSrc(i,k);
}
}
this->osgMatrixdVal = m_dataType.m_matrixd;
return *this;
}
inline MYVARIANT& CTypeDefine::GetVARIANT() throw()
{
return *(MYVARIANT*) this;
}