Back to index

lightning-sunbird  0.9+nobinonly
TypeDesc.cpp
Go to the documentation of this file.
00001 
00002 #include "stdafx.h"
00003 
00004 #include "TypeDesc.h"
00005 
00006 TypeDesc::TypeDesc(ITypeInfo* pti, TYPEDESC* ptdesc)
00007 {
00008        if (ptdesc->vt == VT_PTR)
00009        {
00010               // ptdesc->lptdesc points to a TYPEDESC that specifies the thing pointed to
00011         mType = T_POINTER;
00012         mData.mPtr = new TypeDesc(pti, ptdesc->lptdesc);
00013        }
00014        else if ((ptdesc->vt & 0x0FFF) == VT_CARRAY)
00015        {
00016         mType = T_UNSUPPORTED;
00017         mData.mName = strdup("VT_CARRAY");
00018         /*
00019         // ptdesc->lpadesc points to an ARRAYDESC
00020               str = TYPEDESCtoString(pti, &ptdesc->lpadesc->tdescElem );
00021 
00022               // Allocate cDims * lstrlen("[123456]")
00023         char buf[20];
00024               for (USHORT n = 0; n < ptdesc->lpadesc->cDims; n++)
00025               {
00026             sprintf(buf, "[%d]", ptdesc->lpadesc->rgbounds[n].cElements);
00027                      str += buf;
00028               }
00029               return str;
00030         */
00031        }
00032     else if ((ptdesc->vt & 0x0FFF) == VT_SAFEARRAY)
00033        {
00034         mType = T_UNSUPPORTED;
00035         mData.mName = strdup("VT_SAFEARRAY");
00036         /*
00037               str = "SAFEARRAY(" + TYPEDESCtoString( pti, ptdesc->lptdesc ) + ")";
00038               return str;
00039         */
00040        }
00041     else  switch(ptdesc->vt)
00042     {
00043     case VT_VOID:
00044         mType = T_VOID;
00045         break;
00046     case VT_HRESULT:
00047         mType = T_RESULT;
00048         break;
00049     case VT_I1:
00050         mType = T_INT8;
00051         break;
00052     case VT_I2:
00053         mType = T_INT16;
00054         break;
00055     case VT_INT:
00056     case VT_I4:
00057         mType = T_INT32;
00058         break;
00059     case VT_I8:
00060         mType = T_INT64;
00061         break;
00062     case VT_UI1:
00063         mType = T_UINT8;
00064         break;
00065     case VT_UI2:
00066         mType = T_UINT16;
00067         break;
00068     case VT_UINT:
00069     case VT_UI4:
00070         mType = T_UINT32;
00071         break;
00072     case VT_UI8:
00073         mType = T_UINT64;
00074         break;
00075     case VT_BSTR:
00076     case VT_LPWSTR:
00077         mType = T_WSTRING;
00078         break;
00079     case VT_LPSTR:
00080         mType = T_STRING;
00081         break;
00082     case VT_UNKNOWN:
00083     case VT_DISPATCH:
00084         mType = T_INTERFACE;
00085         break;
00086     case VT_BOOL:
00087         mType = T_BOOL;
00088         break;
00089     case VT_R4:
00090         mType = T_FLOAT;
00091         break;
00092     case VT_R8:
00093         mType = T_DOUBLE;
00094         break;
00095     case VT_EMPTY:
00096         mType = T_UNSUPPORTED;
00097         mData.mName = strdup("VT_EMPTY");
00098         break;
00099     case VT_NULL:
00100         mType = T_UNSUPPORTED;
00101         mData.mName = strdup("VT_NULL");
00102         break;
00103     case VT_CY:
00104         mType = T_UNSUPPORTED;
00105         mData.mName = strdup("VT_CY");
00106         break;
00107     case VT_DATE:
00108         mType = T_UNSUPPORTED;
00109         mData.mName = strdup("VT_DATE");
00110         break;
00111     case VT_ERROR:
00112         mType = T_UNSUPPORTED;
00113         mData.mName = strdup("VT_ERROR");
00114         break;
00115     case VT_VARIANT:
00116         mType = T_UNSUPPORTED;
00117         mData.mName = strdup("VT_VARIANT");
00118         break;
00119     case VT_USERDEFINED:
00120         mType = T_UNSUPPORTED;
00121         mData.mName = strdup("VT_USERDEFINED");
00122         break;
00123     default:
00124         {
00125             char szBuf[50];
00126             sprintf(szBuf, "VT = %08x", ptdesc->vt);
00127             mType = T_UNSUPPORTED;
00128             mData.mName = strdup(szBuf);
00129         }
00130         break;
00131     }
00132 }
00133 
00134 TypeDesc::~TypeDesc()
00135 {
00136     if (mType == T_ARRAY)
00137     {
00138         delete mData.mArray.mElements;
00139     }
00140     else if (mType == T_POINTER)
00141     {
00142         delete mData.mPtr;
00143     }
00144     else if (mType == T_UNSUPPORTED)
00145     {
00146         free(mData.mName);
00147     }
00148 }
00149 
00150 std::string TypeDesc::ToCString()
00151 {
00152     std::string str;
00153 
00154        if (mType == T_POINTER)
00155        {
00156               // ptdesc->lptdesc points to a TYPEDESC that specifies the thing pointed to
00157               str = mData.mPtr->ToXPIDLString();
00158               str += " *";
00159               return str;
00160        }
00161        else if (mType == T_ARRAY)
00162        {
00163         // TODO
00164         str = "void * /* T_ARRAY */";
00165               return str;
00166        }
00167     else switch (mType) {
00168     case T_VOID:    return "void";
00169     case T_RESULT:  return "nsresult";
00170     case T_CHAR:    return "char";
00171     case T_WCHAR:   return "PRUnichar";
00172     case T_INT8:    return "PRInt8";
00173     case T_INT16:   return "PRInt16";
00174     case T_INT32:   return "PRInt32";
00175     case T_INT64:   return "PRInt64";
00176     case T_UINT8:   return "PRUint8";
00177     case T_UINT16:  return "PRUint16";
00178     case T_UINT32:  return "PRUint32";
00179     case T_UINT64:  return "PRUint64";
00180     case T_STRING:  return "char*";
00181     case T_WSTRING: return "PRUnichar*";
00182     case T_FLOAT:   return "float";
00183     case T_DOUBLE:  return "double";
00184     case T_BOOL:    return "PRBool";
00185     case T_INTERFACE: return "nsISupports *";
00186     default:
00187     case T_UNSUPPORTED:
00188         {
00189             std::string str = "/*";
00190             str += mData.mName;
00191             str += " */ void ";
00192             return str;
00193         }
00194    }
00195 }
00196 
00197 std::string TypeDesc::ToXPIDLString()
00198 {
00199     std::string str;
00200 
00201        if (mType == T_POINTER)
00202        {
00203               // ptdesc->lptdesc points to a TYPEDESC that specifies the thing pointed to
00204               str = mData.mPtr->ToXPIDLString();
00205               str += " *";
00206               return str;
00207        }
00208        else if (mType == T_ARRAY)
00209        {
00210         // TODO
00211         str = "void * /* T_ARRAY */";
00212               return str;
00213        }
00214     else switch (mType) {
00215     case T_VOID:    return "void";
00216     case T_RESULT:  return "nsresult";
00217     case T_CHAR:    return "char";
00218     case T_WCHAR:   return "wchar";
00219     case T_INT8:    return "octet";
00220     case T_INT16:   return "short";
00221     case T_INT32:   return "long";
00222     case T_INT64:   return "long long";
00223     case T_UINT8:   return "octect";
00224     case T_UINT16:  return "unsigned short";
00225     case T_UINT32:  return "unsigned long";
00226     case T_UINT64:  return "unsigned long long";
00227     case T_STRING:  return "string";
00228     case T_WSTRING: return "wstring";
00229     case T_FLOAT:   return "float";
00230     case T_DOUBLE:  return "double";
00231     case T_BOOL:    return "boolean";
00232     case T_INTERFACE: return "nsISupports";
00233     default:
00234     case T_UNSUPPORTED:
00235         {
00236             std::string str = "/* ";
00237             str += mData.mName;
00238             str += " */ void";
00239             return str;
00240         }
00241    }
00242 }