Recursively defined lambda functions in C++11

A neat feature, which was introduced in since C++11 is the ability to define a lambda functions[1] in combination with delegates represented by std::function[2]. The following exemplary code snippet demonstrates how a recursive lambda function can be constructed this way:

#include <functional>
#include <iostream>

int main () {
  std::function<void(int)> count_down;

  count_down = [&count_down] (int num) -> void {
    if (num > 0) {
      std::cout << "i:" << num << std::endl;
      
      count_down (num - 1);
    }
  };

  count_down (6);
}

Using GCC, the above code can be compiled with:

$ g++ testcase.cpp -o testcase --std=c++11

[1] http://en.cppreference.com/w/cpp/language/lambda
[2] http://en.cppreference.com/w/cpp/utility/functional/function

Advertisements

Quickly applying patches on Gentoo

Sometimes, it’s necessary to apply a patch for a given package and there is no time and no desire to fork an ebuild to a custom overlay. Luckily Gentoo provides an easy way to so. All you need to do is to place a patch into the directory /etc/portage/patches/[category]/[ebuild’s name]/new-fix.patch. Sadly, not all ebuilds in the portage tree has a build-in support for this feature and to be on the safe side you can put the following hook into the portage’s bashrc file:

# cat /etc/portage/bashrc 
post_src_prepare() {
    if type epatch_user &> /dev/null ; then
    	epatch_user
    fi
}

Reference: Gentoo wiki

Enabling multilib abi_x86_32 support on the stable Gentoo system

Currently the abi_x86_32 use flag is masked on the stable Gentoo system. Therefore turning the 32 option in the ABI_X86 variable, will not bring you further:

# cat /etc/portage/make.conf
...
ABI_X86="32 64"
...

and as a result the line

# emerge --ask --verbose --newuse --update --deep world

will show you an endless number of conflicts, which can not be directly resolved by portage. If you never the less wish to enable the 32 bit library support on your system, especially to make your 32 bit applications running well under wine, you should force the abi_x86_32 desired use flag. One of the possible solutions to achieve this would be:

# cat /etc/portage/profile/use.mask 
-abi_x86_32

Moreover, more additional packages are required to have a full multilib featured support. Many of them has not been marked as stable yet. An up to date list of this packages at the moment of writing can be found here:

# cat /etc/portage/package.keywords/multilib
=x11-libs/libxcb-1.9*
=x11-libs/xpyb-1.3.1-r3
=dev-libs/libusb-1.0.9-r2
=sys-libs/e2fsprogs-libs-1.42.7-r1
=sys-fs/e2fsprogs-1.42.8

=media-libs/libjpeg-turbo-1.3.0-r2 
=media-libs/libdvdnav-4.2.0-r1 
=media-libs/a52dec-0.7.4-r7 
=media-libs/libmpeg2-0.5.1-r2 
=sys-apps/attr-2.4.47-r1 
=media-sound/musepack-tools-465-r1 
=net-libs/libasyncns-0.8-r3 
=virtual/jpeg-0-r2 
=media-libs/libtheora-1.1.1-r1 
=media-sound/pulseaudio-4.0-r1 
=media-libs/webrtc-audio-processing-0.1-r1 
=dev-libs/fribidi-0.19.5-r2 
=app-arch/bzip2-1.0.6-r4 
=sys-libs/libavc1394-0.5.4-r1 
=media-libs/libid3tag-0.15.1b-r4 
=dev-libs/udis86-1.7-r2 
=media-sound/jack-audio-connection-kit-0.121.3-r1 
=media-sound/twolame-0.3.13-r1 
=media-libs/libdvdread-4.2.0-r1 
=media-plugins/alsa-plugins-1.0.27-r1 
=dev-libs/libcdio-paranoia-0.90_p1-r1 
=media-sound/gsm-1.0.13-r1 
=sys-libs/e2fsprogs-libs-1.42.8 
=dev-libs/json-c-0.11-r1 
=dev-libs/libpcre-8.33-r1 
=virtual/libusb-0-r1 
=sys-apps/dbus-1.6.18-r1 
=dev-libs/libusb-compat-0.1.5-r2 
=media-libs/faac-1.28-r4 
=media-libs/libsidplay-1.36.59-r1 
=media-libs/ladspa-sdk-1.13-r2 
=media-libs/libmms-0.6.2-r1 
=media-libs/libmad-0.15.1b-r8 
=sys-devel/llvm-3.3-r1 
=media-libs/libmikmod-3.3.3 
=virtual/libiconv-0-r1 
=sys-libs/gdbm-1.10-r1 
=media-libs/libmodplug-0.8.8.4-r1 
=sys-libs/gpm-1.20.7-r2 
=media-plugins/alsaequal-0.6-r1 
=media-libs/libsndfile-1.0.25-r1 
=media-libs/libdvdcss-1.2.13-r1 
=virtual/glu-9.0-r1 
=media-libs/libiec61883-1.2.0-r1 
=app-admin/eselect-cdparanoia-0.1 
=media-libs/flac-1.2.1-r5 
=media-video/ffmpeg-0.10.10 
=media-libs/libsamplerate-0.1.8-r1 
=media-libs/xvid-1.3.2-r1 
=media-libs/speex-1.2_rc1-r2 
=media-libs/libao-1.1.0-r2 
=media-libs/x264-0.0.20130912 
=dev-libs/gmp-5.1.3-r1 
=media-libs/libdca-0.0.5-r3 
=media-libs/libsidplay-2.1.1-r4 
=app-text/libpaper-1.1.24-r2 
=virtual/libusb-1-r1 
=media-libs/glu-9.0.0-r1 
=media-sound/cdparanoia-3.10.2-r6 
=media-libs/libcuefile-477-r1 
=media-libs/libv4l-1.0.0 
=x11-libs/libvdpau-0.7 
=media-sound/lame-3.99.5-r1 
=media-libs/alsa-lib-1.0.27.2 
=media-libs/freeglut-2.8.1-r1 
=dev-libs/libffi-3.0.13-r1 
=virtual/libffi-3.0.13-r1 
=sys-libs/libraw1394-2.1.0-r1 
=media-sound/wavpack-4.60.1-r1 
=media-libs/libreplaygain-477-r1 
=dev-libs/libcdio-0.90-r1 
=sys-libs/ncurses-5.9-r3 
=x11-proto/dri2proto-2.8-r1 
=media-plugins/caps-plugins-0.9.15 
=dev-libs/liboil-0.3.17-r2 
=media-libs/faad2-2.7-r3 
=sys-libs/libieee1284-0.2.11-r3 
=dev-libs/elfutils-0.156 
=media-libs/mesa-9.2.4 
=sys-apps/tcp-wrappers-7.6.22-r1 
=virtual/opengl-7.0-r1 
=media-libs/glew-1.10.0-r2 
=media-plugins/swh-plugins-0.4.15-r3 
=media-libs/libvisual-0.4.0-r3 
=media-libs/vo-aacenc-0.1.3 
=sys-libs/libcap-2.22-r2 
=x11-proto/xf86driproto-2.1.1-r1 
=media-libs/libshout-2.3.1-r1 
=media-libs/libdv-1.0.0-r3 
=media-sound/mpg123-1.16.0 
=media-libs/libogg-1.3.1 
=media-libs/libmimic-1.0.4-r2 
=media-libs/libvorbis-1.3.3-r1 
=media-libs/libvpx-1.2.0_pre20130625 
=x11-proto/glproto-1.4.16-r1 
=media-libs/portaudio-19_pre20111121-r1

If emerge still has troubles to resolve all conflict, try to temporarily remove all emul-linux-x86-* ebuilds first, and all conflict should go away. Enjoy Gentoo!!!

GSOC with gnome-clocks – Final Report

Well people, the summer is sadly long gone, at least for me, and my summer of coding is approaching its end as well. I must say, that I am very thankful to Google for this opportunity to work in concert with my preferred desktop environment, which is still GNOME. I am glad, that I have seized this frame to finally move myself toward the first contribution to the GNOME project. I really enjoy this time and it’s a great pleasure for me, may be due my awesome mentor Paolo Borelli, I luckily have chosen for me. Thank you. 🙂 Moreover, during this period I have improved my knowledge in different areas and gained a lot of experience.

About the project results: as were reported many times geolocation support has managed to land in last minute to the 3.10 release, which is for sure great!
Moreover, there is even a not yet documented option for our users to toggle the geolocation service:

$ echo "Turn off geolocation support"
$ gsettings set org.gnome.clocks geolocation 'false'
$ echo "Turn on again"
$ gsettings set org.gnome.clocks geolocation 'true'

There is an experimental city images support in gnome-clocks available in the wip/cityimages branch try it out.
Screenshot from 2013-09-21 18:53:33 A youtube video is also available: city images – preview. The video illustrates the functionality of the implemented image providers: flickr image provider requests the corresponding images from the gnome-clocks flickr group, and downloads them for you, so the next time you launch the application they appear instantaneously. The capabilities of the local image provider are also demonstrated: It means, that you always have a possibility to overwrite the suggested image by the flickr provider with your own, you would like to see in gnome-clocks. Therefore you just need to put the corresponding image file into the folder:

~/.local/share/gnome-clocks/city-images/

Be aware of the following name convention for the image files. They should be named like: “berlin-germany-day.jpg” At the moment you can have two images for each location; one for a day and one for a night, the latter having the suffix “…-night.jpg” in its file name.

More information about gnome-clocks enhancements are available on the project page. Take care 🙂

Aaaah, and one more thank you for my sponsored travel to the GUADEC this year, it was just amazing 🙂
sponsored-badge-simple

Maintenance of the Project’s Page

After a few reminders I have finally managed to update during the last week my Summer of code page for the so lovely Clocks project. Now, it contains a lot of more contents instead of many promising “todo” statements. Since then I try to keep it up to date by including exemplary new code snippets or by informing you about ongoing activities. So I hope that, besides me, you can also find it useful for you. 🙂

Vala support for the Libgweather

The recent version of libgweather library from git master comes with the Vala bindings support. At least the Clocks App (gnome-clocks) will heavily rely on this bindings support and therefore we are very thankful to Giovanni Campagna for his fruitful cooperation and good and friendly support.

Please note additionally, that the new version of libgweather will deprecate some function calls. To obtain a new instance of the Location object the function “gweather_location_get_world” should be used instead of “gweather_location_new_world”. “gweather_location_new_world” is deprecated and should be omitted in the newly written code. There are many more, so for more details please refer to the official documentation.

In favour of this good announcement I would like to share with you a small sample code written in Vala, in which some capabilities of the libgweather library are briefly demonstrated: This example reveals how to obtain a timezone name for almost all corresponding major cities.
libgweather
Sample app:

public class LocationInfo {
    private Gtk.Window window;
    private Gtk.Box box;
    private GWeather.LocationEntry location_entry;
    private Gtk.Label city_label;
    private Gtk.Label timezone_label;

    public LocationInfo () {
        Gtk.Builder builder = new Gtk.Builder ();

        try {
            builder.add_from_file ("window.ui");
        } catch (Error e) {}

        this.window = builder.get_object ("window") as Gtk.Window;

        this.window.title = "libgweather testing";
        this.window.border_width = 10;
        this.window.window_position = Gtk.WindowPosition.CENTER;
        this.window.destroy.connect (Gtk.main_quit);

        this.box = builder.get_object ("box") as Gtk.Box;
        this.location_entry =
          new GWeather.LocationEntry (GWeather.Location.get_world ());
        this.location_entry.set_activates_default (true);
        this.location_entry.changed.connect (this.location_defined);
        this.box.pack_start (location_entry);

        this.city_label = builder.get_object ("city") as Gtk.Label;
        this.timezone_label = builder.get_object ("time_zone") as Gtk.Label;
    }

    public void show () {
        this.window.show_all ();
    }

    private void location_defined () {
        GWeather.Location? l = null;
        GWeather.Timezone? t = null;

        if (this.location_entry.get_text () != "") {
            l = this.location_entry.get_location ();
        }

		if (l != null) {
			t = l.get_timezone ();

			this.city_label.set_text (l.get_city_name ());

			if (t != null)
				this.timezone_label.set_text (t.get_tzid ());
			else
				this.timezone_label.set_text ("Unknown");
		}
		else {
			this.city_label.set_text ("");
			this.timezone_label.set_text ("");
		}
	}

    public static void main (string[] args)	{
        Gtk.init (ref args);

        var location_info = new LocationInfo ();
        location_info.show ();
	
		Gtk.main ();
	}
}

The code can be compiled with the following Makefile:

all:
	valac --pkg gtk+-3.0 --target-glib=2.38 gweather-sample.vala \
		--pkg gweather-3.0 -X -DGWEATHER_I_KNOW_THIS_IS_UNSTABLE

The source code can be found here: libgweather-vala.tar.gz.