From a8db4e1598973721cafe4045df1f3c31f5f96b20 Mon Sep 17 00:00:00 2001 From: rdale Date: Mon, 24 Nov 2008 15:34:08 +0000 Subject: [PATCH] * Added a QMap marshaller * Added a DEF_MAP_MARSHALLER() macro for defining marshallers for QMaps with QString keys and value types as the values git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/KDE/kdebindings@888478 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- ruby/korundum/ChangeLog | 3 ++ ruby/korundum/src/kdehandlers.cpp | 111 +------------------------------------- ruby/qtruby/ChangeLog | 4 ++ ruby/qtruby/src/marshall_macros.h | 87 +++++++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 110 deletions(-) diff --git a/ruby/korundum/ChangeLog b/ruby/korundum/ChangeLog index 0adac6bc..b0467917 100644 --- a/ruby/korundum/ChangeLog +++ b/ruby/korundum/ChangeLog @@ -1,3 +1,6 @@ +2008-11-24 Richard Dale + * Added a QMap marshaller + 2008-10-21 Richard Dale * Special case the KDE::StandardAction.open method so it gets called and not diff --git a/ruby/korundum/src/kdehandlers.cpp b/ruby/korundum/src/kdehandlers.cpp index 6d6c61e7..4f9990e9 100644 --- a/ruby/korundum/src/kdehandlers.cpp +++ b/ruby/korundum/src/kdehandlers.cpp @@ -282,114 +282,7 @@ DEF_VALUELIST_MARSHALLER( KUserGroupList, QList, KUserGroup ) DEF_VALUELIST_MARSHALLER( KUserList, QList, KUser ) DEF_VALUELIST_MARSHALLER( QColorList, QList, QColor ) -/* -template -void marshall_Map(Marshall *m) { - switch(m->action()) { - case Marshall::FromVALUE: - { - VALUE hash = *(m->var()); - if (TYPE(hash) != T_HASH) { - m->item().s_voidp = 0; - break; - } - - QMap * map = new QMap; - - // Convert the ruby hash to an array of key/value arrays - VALUE temp = rb_funcall(hash, rb_intern("to_a"), 0); - - for (long i = 0; i < RARRAY(temp)->len; i++) { - VALUE key = rb_ary_entry(rb_ary_entry(temp, i), 0); - VALUE value = rb_ary_entry(rb_ary_entry(temp, i), 1); - - smokeruby_object *o = value_obj_info(key); - if( !o || !o->ptr) - continue; - void * key_ptr = o->ptr; - key_ptr = o->smoke->cast(key_ptr, o->classId, o->smoke->idClass(KeySTR)); - - o = value_obj_info(value); - if( !o || !o->ptr) - continue; - void * val_ptr = o->ptr; - val_ptr = o->smoke->cast(val_ptr, o->classId, o->smoke->idClass(ValueSTR)); - - (*map)[(Qt::Key)*(Qt::Key*)key_ptr] = (Value)*(Value*)val_ptr; - } - - m->item().s_voidp = map; - m->next(); - - if(m->cleanup()) - delete map; - } - break; - case Marshall::ToVALUE: - { - QMap *map = (QMap*)m->item().s_voidp; - if(!map) { - *(m->var()) = Qnil; - break; - } - - VALUE hv = rb_hash_new(); - - int key_ix = m->smoke()->idClass(KeySTR); - const char * key_className = m->smoke()->binding->className(key_ix); - - int val_ix = m->smoke()->idClass(ValueSTR); - const char * val_className = m->smoke()->binding->className(val_ix); - - ItemMapIterator it; - for (it = map->begin(); it != map->end(); ++it) { - void *key_p = new Key(it.key()); - VALUE key_obj = getPointerObject(key_p); - smokeruby_object * o; - - if (key_obj == Qnil) { - o = ALLOC(smokeruby_object); - o->classId = m->smoke()->idClass(KeySTR); - o->smoke = m->smoke(); - o->ptr = key_p; - o->allocated = true; - key_obj = set_obj_info(key_className, o); - } - - void *val_p = new Value(it.data()); - VALUE value_obj = getPointerObject(val_p); - - if (value_obj == Qnil) { - o = ALLOC(smokeruby_object); - o->classId = m->smoke()->idClass(ValueSTR); - o->smoke = m->smoke(); - o->ptr = val_p; - o->allocated = true; - value_obj = set_obj_info(val_className, o); - } - - rb_hash_aset(hv, key_obj, value_obj); - } - - *(m->var()) = hv; - m->next(); - - if(m->cleanup()) - delete map; - } - break; - default: - m->unsupported(); - break; - } -} - -#define DEF_MAP_MARSHALLER(MapIdent,Qt::Key,Value) namespace { char KeyIdent##STR[] = #Qt::Key; char ValueIdent##STR[] = #Value; } \ - Marshall::HandlerFn marshall_##MapIdent = marshall_Map::Iterator,KeyIdent##STR, ValueIdent##STR>; - -DEF_MAP_MARSHALLER( QMapKEntryKeyKEntry, KEntryKey, KEntry ) - -*/ +DEF_MAP_MARSHALLER( QMapQStringKTimeZone, KTimeZone ) TypeHandler KDE_handlers[] = { { "KFileItemList", marshall_KFileItemList }, @@ -459,6 +352,6 @@ TypeHandler KDE_handlers[] = { { "QList&", marshall_KXMLGUIClientList }, { "QList", marshall_QColorList }, { "QList&", marshall_QColorList }, - + { "QMap", marshall_QMapQStringKTimeZone }, { 0, 0 } }; diff --git a/ruby/qtruby/ChangeLog b/ruby/qtruby/ChangeLog index c78dabb6..943478a4 100644 --- a/ruby/qtruby/ChangeLog +++ b/ruby/qtruby/ChangeLog @@ -1,3 +1,7 @@ +2008-11-24 Richard Dale + * Added a DEF_MAP_MARSHALLER() macro for defining marshallers for + QMaps with QString keys and value types as the values + 2008-11-17 Richard Dale * Comparisons between Qt instances and nil were causing QtRuby to crash. Fixes bug reported by Stefano Crocco. diff --git a/ruby/qtruby/src/marshall_macros.h b/ruby/qtruby/src/marshall_macros.h index 5ec6c7ca..29b87164 100644 --- a/ruby/qtruby/src/marshall_macros.h +++ b/ruby/qtruby/src/marshall_macros.h @@ -22,12 +22,17 @@ #include #include #include +#include +#include #include "marshall.h" #define DEF_HASH_MARSHALLER(HashIdent,Item) namespace { char HashIdent##STR[] = #Item; } \ Marshall::HandlerFn marshall_##HashIdent = marshall_Hash; +#define DEF_MAP_MARSHALLER(MapIdent,Item) namespace { char MapIdent##STR[] = #Item; } \ + Marshall::HandlerFn marshall_##MapIdent = marshall_Map; + #define DEF_LIST_MARSHALLER(ListIdent,ItemList,Item) namespace { char ListIdent##STR[] = #Item; } \ Marshall::HandlerFn marshall_##ListIdent = marshall_ItemList; @@ -513,7 +518,7 @@ void marshall_Hash(Marshall *m) { o->classId = m->smoke()->idClass(ValueSTR).index; o->smoke = m->smoke(); o->ptr = val_p; - o->allocated = true; + o->allocated = false; value_obj = set_obj_info(val_className, o); } rb_hash_aset(hv, rb_str_new2(((QString*)&(it.key()))->toLatin1()), value_obj); @@ -532,5 +537,85 @@ void marshall_Hash(Marshall *m) { } } +template +void marshall_Map(Marshall *m) { + switch(m->action()) { + case Marshall::FromVALUE: + { + VALUE hv = *(m->var()); + if (TYPE(hv) != T_HASH) { + m->item().s_voidp = 0; + break; + } + + QMap * map = new QMap; + + // Convert the ruby hash to an array of key/value arrays + VALUE temp = rb_funcall(hv, rb_intern("to_a"), 0); + + for (long i = 0; i < RARRAY(temp)->len; i++) { + VALUE key = rb_ary_entry(rb_ary_entry(temp, i), 0); + VALUE value = rb_ary_entry(rb_ary_entry(temp, i), 1); + + smokeruby_object *o = value_obj_info(value); + if (o == 0 || o->ptr == 0) { + continue; + } + void * val_ptr = o->ptr; + val_ptr = o->smoke->cast(val_ptr, o->classId, o->smoke->idClass(ValueSTR).index); + + (*map)[QString(StringValuePtr(key))] = *((Value*)val_ptr); + } + + m->item().s_voidp = map; + m->next(); + + if (m->cleanup()) { + delete map; + } + } + break; + case Marshall::ToVALUE: + { + QMap *map = (QMap*) m->item().s_voidp; + if (map == 0) { + *(m->var()) = Qnil; + break; + } + + VALUE hv = rb_hash_new(); + + int val_ix = m->smoke()->idClass(ValueSTR).index; + const char * val_className = qtruby_modules[m->smoke()].binding->className(val_ix); + QMapIterator it(*map); + while (it.hasNext()) { + it.next(); + void *val_p = (void *) &(it.value()); + VALUE value_obj = getPointerObject(val_p); + + if (value_obj == Qnil) { + smokeruby_object *o = ALLOC(smokeruby_object); + o->classId = m->smoke()->idClass(ValueSTR).index; + o->smoke = m->smoke(); + o->ptr = val_p; + o->allocated = false; + value_obj = set_obj_info(val_className, o); + } + rb_hash_aset(hv, rb_str_new2(((QString*)&(it.key()))->toLatin1()), value_obj); + } + + *(m->var()) = hv; + m->next(); + + if (m->cleanup()) { + delete map; + } + } + break; + default: + m->unsupported(); + break; + } +} #endif -- 2.11.4.GIT