[Gambas-devel] QMap<QString,QString> my implementation in DCOP class file.

Benoit Minisini gambas at ...1...
Thu Feb 24 10:37:25 CET 2005


On Thursday 24 February 2005 09:58, Carlo Sorda wrote:
> Hi,
> I have try to manage QMap<QString,QString> type in CApplication.cpp.
> I have implemented it like this:
>
> ...
> #include <qmap.h>
> ...
> enum {
>   QT_T_VOID,
>    ...
>   QT_T_QMAP_QSTRING,
> };
> ...
>   { "QMap<QString,QString>",(GB_TYPE)"Collection"   },
>    { NULL }
> };
> ...
> //My implementation.
>   else if (type == QT_T_QMAP_QSTRING)
>    {
>      GB_COLLECTION aCollection;
>      GB_STRING aKey;
>      GB_STRING aValue;
>      //Redefine a type for QMap of QString
>      typedef QMap<QString,QString> Map;
>      Map r;
>      reply >> r;
>
>      //Create a new Collection
>      GB.Collection.New(&aCollection,GB_COMP_BINARY);
>
>      //Create an Map Iterator for navigate a Map
>      Map::Iterator it;
>      for (it =r.begin() ;it != r.end();it++)
>      {
>        #ifdef DEBUG_ME
>        	//This is correct
>        	qDebug("QMAP %s->%s", it.key().latin1(),it.data().latin1());
>        #endif
>
>        //Create e new GB_STRING that contain a Key (QString)
>        GB.NewString((char **)&aKey,it.key().latin1(),0);
>        //Generate a new GB_STRING theat cantain a Value (QString)
>        GB.NewString((char **)&aValue,it.data().latin1(),0);
>
>        //aValue is NULL??? WHY
>        qDebug("aValue: %s-%s",aKey,aValue);
>        //Here is the crash
>        //GB.Collection.Set(&aCollection,&aKey,GB.StringLength((char
> *)&aKey),(GB_VARIANT *)&aValue);
>       
> //GB.Collection.Set(&aCollection,it.key().latin1(),it.key().length(),(GB_VA
>RIANT*)it.data().latin1()); }
>      //return a Collection
>      GB.ReturnObject(aCollection);
>    }
>
> Can you help me?
>
>

Yes :-)

The third parameter of GB.Collection.Set is a GB_VARIANT, which is not a 
GB_STRING: you cannot cast the second to the first wihtout a crash.

The key is a normal string, not a GB_STRING. The GB_* structures depict the 
way value are stored in the interpreter stack.

I suggest the following (I didn't try the code):

  else if (type == QT_T_QMAP_QSTRING)
   {
     GB_COLLECTION aCollection;
     char *key;
     GB_VARIANT value;

     //Redefine a type for QMap of QString
     // Why did you redefine it ?
     typedef QMap<QString,QString> Map;
     Map r;
     reply >> r;

     //Create a new Collection
     GB.Collection.New(&aCollection, GB_COMP_BINARY);

     // initialize value
     value.type = GB_T_VARIANT;

     //Create an Map Iterator for navigate a Map
     Map::Iterator it;
     for (it =r.begin() ;it != r.end();it++)
     {
       #ifdef DEBUG_ME
        //This is correct
        qDebug("QMAP %s->%s", it.key().latin1(),it.data().latin1());
       #endif

       // This is unneeded
       // GB.NewString((char **)&aKey,it.key().latin1(),0);
       
       //Generate a new GB_VARIANT that contains the string value
       // Gambas uses UTF-8
       GB.NewString(&value.value._string.value, it.utf8().data(), 0);

       GB.Collection.Set(&aCollection, it.key().utf8().data(), 0, &value);
     }

     //return a Collection
     GB.ReturnObject(aCollection);
   }

Tell me the result - I'm not sure of *.utf8().data() to get the (char *) 
representation of the utf8 string.

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the Devel mailing list