This is the personal webiste of Sakari Bergen. I'm a fourth year Communications Engineering student at the Helsinki University of technology. Most material on this site is related to my hobbies, specifically software, electronics and music. I hope you find something useful and enjoy your visit!
Emulating special constructors for const objects with a wapper class in C++
2009-11-07I spent a few hours today figuring out how to make the cleanest possible const-correct code for a class that contains a reference or pointer member, whose constness is protected by accessor functions. Here is an example class:
- class Example
- {
- public:
- Example (Data & data) : data (data) {}
- Data & data() { return data; }
- Data const & data() const { return data; }
- /* More stuff here in a real situation ... */
- private:
- Data & data;
- /* More stuff here in a real situation ... */
- };
The problem with this class arises when you want to instantiate a const Example from a const Data object. The constructor wanẗ́s a non-const reference in any case, and C++ does not support special constructors for const objects. The cleanest solution I came up with, was making a ConstExample class (similar to const_iterator in STL). I ended up implementing a simple wrapper with a bunch of overridden operators:
- class ConstExample
- {
- public:
- ConstExample (Data const & data)
- : example (const_cast<Data &>(data)) {}
- inline operator Example const & () { return example; }
- inline Example const * operator& () { return &example; }
- inline Example const * operator-> () { return &example; }
- inline Example const & operator() () { return example; }
- private:
- Example example;
- };
- A const_cast is involved, but that is rather inevitable because of the limitations of C++
- The first two operators make it possible to pass the object as a const reference or pointer to an Example object.
- The third and fourth make it possible to call const functions (or reference members) of Example. You'll probably want to use only one of these, if any. In my case, the object will mostly just be passed to functions as a reference.
This might not be the best way to do things in all cases. Sometimes splitting the main class into a const base class, which is inherited by a non-const class might be a better solution (e.g. Symbians descriptor classes). It all depends on the use case and semantics of const.
The environment I'm using this pattern in, has separate overrides for functions that handle non-const and const data. The const data often needs to be copied for processing, while the non-const version can save memory by doing in-place processing. Also, most of the time const objects are constructed from non-const data, only to tell others that the data should not be changed. So, in my case this pattern is very transparent.
I also considered a static factory function (i.e. Example const e = Example::Const (data);) and some kind of solution using template specializations, but ended up using a wrapper class instead.
If you know of some other solution (or a very clean template-based solution) please leave a comment!
Psychobass project report and Boids JAR-file
2009-09-29So, I finally took the time to finish up my psychobass plugin project's report and make a runnable JAR-file of my Boids application.
If you like staring at flames etc, I suggest trying out the Boids app. It's actually quite much fun looking at it. Beware of the fixed 1200x800 resolution though...
The plugin project report is less fun, but if you are interested in dsp, it might be worth looking at...
Boids
2009-05-16As I mentioned in my previous post, I made a Boids simulation in Java as a school project. The course is now over, so I published the source code under the GPL. It can be accessed via svn from http://svn.beatwaves.net/svn/boids.
It uses the Processing library for the UI and you will need to download the Processing 'core.jar' separately to build and run the application. Processing is released under the LGPL, so I could include the jar in my svn repository also, if I only found the right source code to distribute with it. But I didn't find it right away and I'm lazy, so you'll need to download this your self :)
The version in svn uses the Processing P3D renderer by default, but it should be fairly easy to get it to work with the OpenGL renderer also. You just need the right libraries in your classpath and change (comment/uncomment) a few lines in the source code.
I found out after coding this, that someone else made a very similar implementation in Java also. Great minds think alike and so on...
For me this was just a (rather interesting) school project and I'm not interested in continuing work with it. But if you find it (or parts of it) worth developing further, please leave me a comment and tell me what you are doing with it!
What I've been up to lately
2009-03-28-
The Psycho Acoustic Bass Plugin
I promised to develop it in a public svn repository, but never published the URL. Sorry about that. Anyway, it is now in a working state and available here. It doesn't do miracles, but it does do something. I'll blog more about this when I get some feedback from the project report... -
Suunto heart rate monitors in Linux/OS X
I bought myself a Suunto Heart rate monitor with a "pc pod" for connecting it to a computer. However, Suunto provides only Windows software for getting training data out of the device. Long story short, I ended up writing a small ANT driver for Linux and have been able to get some data out of the device. However the data format is rather cryptic, and I've been in contact with Suunto about the possibilities of getting a Linux/OS X version of their (rather good looking) Training SDK out. I haven't yet gotten a good response from them, but once I do, I'll blog... -
Futurice
In the beginning of the year I started working part-time as a software developer at Futurice. I've been doing mainly mobile software development, and so far I've enjoyed it :) -
Ardour 2.8 is out!
It looks like this time I haven't made a single commit to the 2.X tree since the last release. Partly because of my new job and lots of school work I haven't had quite as much time to work with Ardour as I could have wished. However, I have been doing some little work here and there in the 3.0 branch. Nothing big though. -
Boids (flocks herds etc.)
This is a school project that I got a bit obsessed about :) The idea is to make a flocking model where each character in the flock acts autonomously, together forming clear flocking behavior. This is a Java project which is not ready yet, but you can nevertheless check it out here if you wish. It uses the great Processing framework (from "Java mode") for the 3D graphics. More on this once The course finishes. I'll also make the source code publicly available :)
Psycho acoustic bass response extension plugin project
2008-11-19So, like I said yesterday, I will be creating a LV2 plugin as a school project. It will be a Psycho Acoustic Bass Response Extension Plugin aka. PABREP (yes, I just made up that acronym).
The basic idea is to create or enhance harmonic content from signal components that fall below the low frequency cut-off frequency of a sound reproduction system. These harmonics will then create a sensation of better bass because of the missing fundamental phenomenon (or at least I hope so). The method has been discussed in an AES paper, which I will be basing my work on.
For more details see my brief project plan. The plugin will be GPL and I'll develop it in a public svn repository. More info on that later...