<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7225354741100420221</id><updated>2011-07-08T07:08:24.512-07:00</updated><category term='swig'/><category term='jni'/><category term='problems'/><category term='solaris'/><category term='java'/><category term='x-server'/><title type='text'>Dmitriy Matveev thoughts</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dmatveev.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7225354741100420221/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dmatveev.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>dmitriymatveev</name><uri>http://www.blogger.com/profile/09361552159832972096</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7225354741100420221.post-9069864347149542947</id><published>2010-05-12T05:38:00.000-07:00</published><updated>2010-05-12T06:54:23.006-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='jni'/><category scheme='http://www.blogger.com/atom/ns#' term='swig'/><title type='text'>Better SWIG wrappers: variants-like types and enums</title><content type='html'>Today I was trying to improve our JNI wrappers for some complex structure. The structure is a tree-like object with different kinds of nodes with variant-like data. The structure represents a C/C++ source file parse tree, so it have a lot of nodes and by the library design all of these nodes should be as compact as possible, so two different node types may be represented by two different structures, like this:&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;enum FooKind {&lt;br /&gt; foo1,&lt;br /&gt; foo2,&lt;br /&gt; foo3,&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;struct Foo {&lt;br /&gt; ... // some data common for all Foos&lt;br /&gt; unsigned int kind:2; // reducing field size&lt;br /&gt; union {&lt;br /&gt;    T1 field1; // when kind == foo1&lt;br /&gt;    T2 field2; // when kind == foo2&lt;br /&gt;    T3 field3; // when kind == foo3&lt;br /&gt; } variant;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;enum BarKind {&lt;br /&gt; bar1,&lt;br /&gt; bar2,&lt;br /&gt; ...&lt;br /&gt; bar10,&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;struct Bar {&lt;br /&gt; ... // some data common for all Bars&lt;br /&gt; unsigned int kind:2; // reducing field size&lt;br /&gt; union {&lt;br /&gt;    T4 field1; // when kind == bar1&lt;br /&gt;    ...&lt;br /&gt; } variant;&lt;br /&gt;};&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Such structures may be good or not, but the wrappers generated for "kind" fields will be unsafe. We actually previously had some problems related to misuses of node kinds which led to incorrect access to the fields of variant, corrupted data, non deterministic bugs which were hard to reproduce and JVM crashes (these are my favourite).&lt;br /&gt;That errors could have been prevented by type-safe wrappers for enums. I didn't wanted to manually edit our wrappers generated by SWIG (yeah, we do this sometimes) so I've decided to find a way to force SWIG do this for me. I don't had much SWIG experience, but I've already implemented some tricks with typemaps, so I've decided to get use of them once more. The basic idea was following: "The only place where I really need type-safe enums is my proxy class, so I need to do something with it's getter for dangerous field". I've started with only one type and my typemap code was like following:&lt;br /&gt;&lt;blockquote&gt;%typemap(jni) unsigned int kind "jbyte" // It's smaller than the original!&lt;br /&gt;%typemap(jtype) unsigned int kind "byte"&lt;br /&gt;%typemap(jstype) unsigned int kind "FooKind"&lt;br /&gt;%typemap(javaout) unsigned int kind {&lt;br /&gt;   return MyNumber.swigToEnum($jnicall)&lt;br /&gt; }&lt;/blockquote&gt;&lt;br /&gt;Above approach is simple, powerful(it's typemaps!), but have at least two disadvantages:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;The "&lt;a href="http://swig.org/Doc1.3/Java.html#enumerations"&gt;enum implementation&lt;/a&gt;" is hardcoded in javaout typemap making it's harder to change&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Above typemap will match all the entities named "kind" with "unsigned int" type&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;I haven't found a way to to match only field from Foo type and use different typemap for Bar, but things like '%typemap(jstype) unsigned int Foo::kind "FooKind"' wasn't working, thus entire approach became unusable for my case.&lt;br /&gt;&lt;br /&gt;The other way to affect generated wrapper code is type extensions. I've got an idea to ignore existent field and add my own with proper type:&lt;br /&gt;&lt;blockquote&gt;%ignore Foo::kind;&lt;br /&gt;%extend Foo {&lt;br /&gt; FooKind kind;&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Above thing didn't worked because I've tried to extend the type with the field which being ignored. Occasionally I've found that if you will extend some type with the field which already defined in it, then only first declaration (yours extension) will be used and SWIG will produce warning about field defined more than once. So following code actually may be a solution for a problem:&lt;br /&gt;&lt;blockquote&gt;%extend Foo {&lt;br /&gt; FooKind kind;&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Ones who hate warnings can ignore old field and generate a new field with the desired type and different name. In that case SWIG will reference a undefined function in a getter for a new field, so it will be necessary to implement one more item:&lt;br /&gt;&lt;blockquote&gt;%{&lt;br /&gt;static FooKind Foo_safeKind_get(Foo *self) {&lt;br /&gt; return self-&gt;kind;&lt;br /&gt;}&lt;br /&gt;%}&lt;br /&gt;&lt;br /&gt;%ignore Foo::kind;&lt;br /&gt;%extend Foo {&lt;br /&gt; FooKind safeKind;&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;Of course above code snippet can be put into macros to keep it simple:&lt;br /&gt;&lt;blockquote&gt;%define CHANGE_FIELD_TYPE(type, field, newFieldType)&lt;br /&gt;&lt;br /&gt;%{&lt;br /&gt;static newFieldType type ## _safe ## field ## _get(type *self) {&lt;br /&gt; return self-&gt;field;&lt;br /&gt;}&lt;br /&gt;%}&lt;br /&gt;&lt;br /&gt;%ignore type::field;&lt;br /&gt;%extend type {&lt;br /&gt; newFieldType safe ## field;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;%enddef&lt;br /&gt;&lt;br /&gt;CHANGE_FIELD_TYPE(Foo, kind, FooKind)&lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7225354741100420221-9069864347149542947?l=dmatveev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dmatveev.blogspot.com/feeds/9069864347149542947/comments/default' title='Комментарии к сообщению'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7225354741100420221&amp;postID=9069864347149542947' title='Комментарии: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7225354741100420221/posts/default/9069864347149542947'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7225354741100420221/posts/default/9069864347149542947'/><link rel='alternate' type='text/html' href='http://dmatveev.blogspot.com/2010/05/better-swig-wrappers-variants-like.html' title='Better SWIG wrappers: variants-like types and enums'/><author><name>dmitriymatveev</name><uri>http://www.blogger.com/profile/09361552159832972096</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7225354741100420221.post-2389436574069368776</id><published>2009-01-16T00:40:00.000-08:00</published><updated>2009-01-16T08:26:25.087-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='problems'/><category scheme='http://www.blogger.com/atom/ns#' term='x-server'/><category scheme='http://www.blogger.com/atom/ns#' term='solaris'/><title type='text'>Solution for: WARNING **: Error converting text from IM to UTF-8: Conversion  from character set '646' to 'UTF-8' is not supported</title><content type='html'>I had some problems with applications ran under X server on Solaris systems. When I tried to type any characters into some textbox in that applications the characters wasn't entered and the console has the following output:&lt;br /&gt;&lt;br /&gt;One message after application start:&lt;br /&gt;GLib: Cannot convert message: Conversion from character set 'UTF-8' to '646' is&lt;br /&gt;not supported&lt;br /&gt;&lt;br /&gt;And many messages like this:&lt;br /&gt;** (gedit:14860): WARNING **: Error converting text from IM to UTF-8: Conversion&lt;br /&gt; from character set '646' to 'UTF-8' is not supported&lt;br /&gt;&lt;br /&gt;I haven't found any solution on the internet, but that was pretty simple to fix. It's enough to right-click in any textbox of application with that problems, and select "Default" menu item from the "Input methods" submenu of opened context menu. Hope someone will find this helpful.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7225354741100420221-2389436574069368776?l=dmatveev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dmatveev.blogspot.com/feeds/2389436574069368776/comments/default' title='Комментарии к сообщению'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7225354741100420221&amp;postID=2389436574069368776' title='Комментарии: 0'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7225354741100420221/posts/default/2389436574069368776'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7225354741100420221/posts/default/2389436574069368776'/><link rel='alternate' type='text/html' href='http://dmatveev.blogspot.com/2009/01/solution-for-warning-error-converting.html' title='Solution for: WARNING **: Error converting text from IM to UTF-8: Conversion  from character set &apos;646&apos; to &apos;UTF-8&apos; is not supported'/><author><name>dmitriymatveev</name><uri>http://www.blogger.com/profile/09361552159832972096</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
