2012-04-11

How rvalue references and moving in c++ behave

I had enough of decyphering the cryptic standard of c++11 today and decided I'll just write a bunch of code to tell me how it *really* works.


Before going forward, you should look at the code or get the code and/or see the output.

So, the above code is pretty self-explanatory. Now here are the findings:

If you return by value, a move constructor will be called if it is defined.

If you return by rvalue reference (refref), a move constructor will also be called, but don't do that, because you cannot safely return objects constructed in functions from functions as references.

Basically, that's it :)

2009-08-11

Windows7 highlingts

This is a list of awesome features I found in windows7:

The "Characters" wallpaper slideshow. This is just SO awesome - every page is bizarre, stylized and whenever the wallpaper changed I spent a couple of minutes just gazing at the detail. Microsoft, I might not like what you're doing with your POSIX compliance (read: nothing), but the wallpapers are awesome.

Aero "halfimize" - drag your window to the left or right edge of the screen and the window will cover the vertical left or right half of the screen. Very, very useful. I'm expecting kde4's kwin to copy this with a plugin very soon, or hope it does. I miss it.

I might expand on this list some more in the future. I also refrained from stating any negative stuff, because this post is a list of awesome features.

Starting java development on N97

Setting: A desk, a new notebook with windows7 (rc) and archlinux, and a Nokia N97.

Job: make an app.

I first tried to get the environment working on archlinux with pulsar, the (supposedly) awesome eclipse-based ide for phone-based development, but I couldn't get all the components necessary (that being an emulator and sdk - the whole of the problem lies with Nokia, as sun provides its wireless toolkit for linux).

So, the job moved over to windows 7. Pulse installed without trouble, and I got a ready-made MIDIlet project (RSSReader) off of sourceforge to try and compile just for kicks.

The RSSReader binaries installed perfectly on the N97, I just copied the jar over and installed it via the phone's application management features. It ran.

I used the nokia instructions page to guide my way of installing all the software, but pulse had its own mind sometimes and fetched and installed the S60 SDK itself.

For reference, I got pulsar 20090619-0625, the 1.1 Nokia SDK and Sun wireless toolkit 2.5.2.

Just clicking around and randomly installing software yielded me a useless system. Whenever I tried to compile and run a MIDIlet, a "JAD file could not be initialised" error popped up and that led me to actually go read the installation notes document.

Highlights:
  • You need at least perl 5.6 (Strawberry perl shipped with 5.10 at the time, so no sweat there)
  • You need an ARM compiler (though not for Java), but the ARM toolchain is installed by the SDK installer if you want (it asks).
  • Installation path must not contain whitespace (so no "Program Files (x86)")
After that it's click-next-until-your-fingers-hurt. When finished, it's pretty important to verify your installation. Open the prompt and type "devices", If it shows you your phone, you're on the right track.

I couldn't compile the c++ app because of the "nothing to do" error, but hey - at least the emulator ran (command "epoc"). I'm not going to be developing c++ apps yet anyway.

I then used the nokia wiki page to set up a project.

Since I failed at compiling an existing app, I thought I'd try a hello world project. It ran further than the RSSReader project, but it still stopped when the emulator gateway wasn't running. The gist was in getting it to use the emulator:
Go to Project->Properties->Java ME. Select S60Device from the menu and click Edit. (if you don't have anything in the menu, add the device from C:\S60\... ...\Devices\Nokia N97 something). In the next window, under Device, select S60Emulator.

After lots of useless clicking doing pretty much nothing, it started working on the n'th try. After that, it even wanted to connect to the emulator, which spawned about 5 dialogs with the windows firewall blocking local connections. Just click through.

It also seems to work better if you run it as debug, not run on the 1st try.

It also times out if you don't click through the connection -> winsock thing on the emulator. Otherwise, the hello world app works marvelously. In fact, it compiles and runs and promptly exits, but it runs.

Well, I'm off to developing the app now. Took me only two days. :)

2009-02-28

Firefox search in new tab

Today, kde somehow (I'm also blaming xfs for this) managed to delete about half my personal settings, making firefox think it had no skin, but still have its extensions, but extensions' config was gone etc. Madness.

My firefox was nicely set up from the olden days of ff3 so that searches from the firefox search bar opened in a new tab. Of course, since the config was gone... Voila, it started sucking again.

So, for everyone that wants their searches to open a new tab (damn useful if you're searching wikipedia AND google AND something else at the same time and hate to hit ctrl+t every time you search, here's the remedy, since they have removed it from the preferences:

a) enter about:config in the adressbar and hit enter.
a.1) ignore the "this will void your warranty" idiocy if it shows up and click through.
b) type "search" into the Filter: field.
c) doubleclick the browser.search.openintab line to set the value to true

and voila, that's it. Your searches will now open a new tab. I seriously don't know why that isn't the default.

Regards,

Gašper

2009-02-03

A gentoo guy installing archlinux off a daily Jaunty Jackalope livecd

What the title says.

I've had enough of compiling and grinding my disks and decided to jump distro today. I loved using gentoo for the last few years, but I really wanted to try something new, something exciting and I stumbled across arch. On first glance, it looked like gentoo without compiling. Well, that's what I was searching for, so I downloaded their install cd and set to work.

I first backed up my root partition. 16 gigs of programs and other junk got squeezed in a 2.7 gig tar.bz2. Pretty awsome. This of course does not include any personal data, I'll be keeping my home dirs and other partitions, it's just the system that's getting a makeover.

I booted arch. I wanted to try ext4 which has been getting so much press lately, and the default arch livecd didn't have the new e2fsprogs that supported it, but pacman (the arch package manager) was easy to bribe into updating it (on the ramdisk). Hurrah for pacman. This, of course, did not save me from the next jiffy - 2.6.25 kernel.

So, here I am, an hour later, sitting behind a Ubuntu Jaunty Jackalope daily build, installing arch on an ext4 partition. Yay me.

So, how does one do that? One follows the http://wiki.archlinux.org/index.php/Install_From_Existing_Linux guide, of course. Right.

First, you need a whole bunch of stuff from an arch cd, so mount your iso image to /mnt/archiso, then mount the squashfs image on it to /mnt/arch. My new system is going to be /mnt/newroot. In my case, the iso image was on my old home partition, and so I had to mount that as well, and I then find myself in the following situation:
/mnt/home (with archiso.iso) - mounted home drive
/mnt/archcd (with archlive.sqfs) - mounted iso image
/mnt/arch - mounted squashfs image
and so I come to my accessible, chrootable and fine arch livecd running on top of ubuntu jaunty.

For the people seeking help, here's the sequence of commands to accomplish this:
sudo mkdir /mnt/home /mnt/arch /mnt/archcd
sudo mount /dev/sdc4 /mnt/home
sudo mount -o loop /mnt/home/atom/archiso.iso /mnt/archcd
sudo mount -o loop /mnt/archcd/archlive.sqfs /mnt/arch

now we just mount our /dev, /proc and /sys over so that everything on the livecd can work...



sudo mount --bind /sys /mnt/arch/sys
sudo mount --bind /proc /mnt/arch/proc
sudo mount --bind /dev /mnt/arch/dev
sudo chroot /mnt/arch
To tell you the truth, I didn't really know how the system would react since it has no root account, but it seemed to run well enough. My prompt was useless (bash-3.2#), but 

export PS1='\u@\h${pwd} # '

quickly fixed that. However, the resulting filesystem was still read-only and so the installer failed to run. I was left to find another way.

And so it was back to following the wiki. It was then I discovered (late, I know) that the sudo way was idiotic for any serious sysadmin work. I needed root and I needed it then. so I tried sudo bash, and it worked! I now had a root bash shell :)

And then I ran into a lot of problems and started fixing them... and never got around to writing all of it down. So, I fail. The end result was an installed arch system, together with everything I could wish for. Arch was finally installed without even touching the installer - something I was told is impossible (not to mention unsupported) on fedora I was installing on another computer at the time.

Ogre was still a bit of a pain to install until I figured out how arch does it, and pacman is a joy to use.

All in all, I've been using arch for a few weeks now, and have been pretty happy about it.

2009-01-29

Terry Pratchett - The Nation review

Summary: There is our multiverse. And there is a universe in that multiverse, not unlike our own. It too, has a solar system, and a Jupiter, and an Earth (with the Moon) and an England, which is pretty much an Empire at the point. It has a Newton and a Darwin and even a Royal Society. And it has its share of snotty bastard sailors and an Eastern Pellagic ocean with its share of freckles that our Earth forgot to make because there was too much going on with the big freckles. That earth decided to sneeze right about then, and it had an apocalypse. Not *the* Apocalhpse, mind, just an apocalypse. And that apocalypse left a not-yet-a-man Mau and a pettycoat-galore 13-year-old aristocrat girl Ermintrude, em, Daphne, that has an internal grandmother to get rid of on the island of the Nation al alone. And it is *not* a love story.

It would be a crime to call it witty. It is, but that is not the point. Neither is the point in the book being smart. It is, but in the non-smart, just wise Pratchett way. It is not written for its story, or smarts, or anything mundane like that. It is written for the message, and the message is clear... In a Pratchett kind of way.

This is one of the books you know you'd love to read to your children as a bed-time story, but know you'll never be able to - it would raise too many questions you'd really tire of answering all the time - but that is the point. So I think I'm going to read it to my kids anyway (when I have them). It is precicely the book's message - people must start questioning themselves about the universe again. Either that, or live in blissful ignorance that I see on the faces of so many of my students and classmates. Some of them just want to get finished with the class and move on. No teacher should have to endure that kind of thing. Curiosity should be the way of the student, not blind obedience and learning by heart. But, I'm trailing off. Let's get back to the book.

The language, too, is just beautiful. It is not marvellous. It is not shakespearean English. It is just beautifully woven into a flowing story that moves just at the right pace - slow enough that you have time to reflect, and quickly enough that it doesn't bore you.

It is full of truths, half-truths and observations about the world that make your sleepy inner-geek wake up and stand to attention. It speaks to your tired knowledge-hungry mind to go find answers to the difficult questions, like starting your *own* hypotheses on why we're here, was there a god, does that even matter and where you put your favorite munchies. It does not shed much light on the subject, as it should be. It is by no means a religious text, unless you are a convinced atheist. I can see this book planting the seed of doubt in any convinced believer, whether it is a follower of an actual religion or an anti-religion (ex. atheists). It even briefly mentions Dawkins, in a humorous way, which is not at all obtrusive.

All in all, I'd recommend it, especially if you have never read Pratchett before. It is a one-stop shop for his view of the world, where all magic is headology and all belief is just explanation, and sometimes explanations are meaningless and questions are the wrong ones. Where all people are just people and differences in character separate them way more than any cultural upbringing, skin color or even belief system. It can get pretty bloody for all the right reasons and pretty bad for all the wrong ones. Plus, I love the way he mixes the personal view of the world with the objective one, if that even exists.

In short, I think this book is awsome, you should go read it, and you should keep it on your bookshelf for your kids to read when the old sod has long since stopped appearing on headlines and your kids have a choice between this book and the Russian Folk Tales (which, too, I wholeheartedly recommend) because they don't want to go to sleep and they got banned off their computer for a week for not washing the dishes frequently enough.

Regards,

Gašper

2009-01-25

Function call multiplexing in python

I ran into a bit of a snag today when learning PyQt - I had n listeners registered with my object, and when an event occurred, I had to run (notify) all of them. Now, I'm not a big fan of for-loops and it got me thinking about how I could run them all and have neat syntax at the same time.

What I came up with was this:

class Multiplexer(object):
def __init__(self, objects, function):
self.objects = objects
self.function = function

def __call__(self, *args, **kwargs):
return [getattr(object, self.function)(*args, **kwargs) for object in self.objects]

Naturally, some people asked "Why __call__? Why not dispatch() or something similar?"

The answer is pretty straight-forward, really - I wanted my code to look like this:
# listeners is a collection of listener objects
listeners.gotGasEvent(newValue1, newValue2)

This would, ideally, call *each* of the listener's gotGasEvent() functions and thereby notify their respective notifyees of the event. What this line does looks obvious, with no clutter and to the point. Still, I had some more things to sort out before such juggling would be possible. This was solved by the following ListenerCollection class:

class ListenerCollection(object):
def __init__(self):
self.listeners = {}
def addListener(self, name, listener):
self.listeners[name] = listener

def removeListener(self, name):
del self.listeners[name]

def __getattribute__(self, attribute):
try:
return object.__getattribute__(self, attribute)
except AttributeError:
return Multiplexer( self.listeners.itervalues(), attribute )
The code is pretty straightforward - there's a dictionary of listeners, and the only thing it forbids you is that you can't name an event "removeListener" or "addListener". The neat thing is that we can now do what we wanted to do above without any problems:

class IAmEventful(object):
def __init__(self):
self.listeners = ListenerCollection()

def feedMe(self, food):
# Phone people gave food!
self.listeners.receivedFood(food)
There we go, a thousand listeners served. (with food)

Regards,

Gašper