Thursday, May 30, 2013

unused parameters in function

When you have an interface with an abstract base class Base and several classes that have derived from it. For a while this setup works out well, but at some point you have another class that inherits from Base and also has its definitions for the virtual functions. For one of the virtual functions you do not need all of the function arguments. This will cause a compiler warning in case you compile with gcc
and set the flags "-Wall -Wextra".

class Base {
public:
  virtual bool some_func(bool param) = 0;
};

class DerivedClass1 : public Base {
public:
  virtual bool some_func(bool param) {return param;}
};

class DerivedClass2 : public Base {
public:
  virtual bool some_func(bool param) {return true;}
};


It is obvious that you want to get rid of all compiler warnings. One possible solution is to make a void cast on param (taken from stackoverflow):

class DerivedClass2 : public Base {
public:
  virtual bool some_func(bool param) {(void)param; return true;}
};


Another solution that is - in my opinion - more elegant, is to just specifiy the parameter type without giving a variable name for the functions that do not use that parameter (also from stackoverflow):

class DerivedClass2 : public Base {
public:
  virtual bool some_func(bool /*param*/) {return true;}
};

Saturday, May 11, 2013

bash welcome message

Hi there,

I haven't had a blog post in quite a while due to being busy.
Last night I did the least relevant to this blog/machine learning, yet probably most awesome bit of python/shell scripting.
Basically I wanted my welcome message to be an ASCII art of an Arnie movie screenshot. However, I was very disappointed with most results of the online convertes and the lack of color made it look really bad. http://www.text-image.com/convert/ gave really good results for to html conversion. I took the source code and made a python script that parses the html string to replace the relevant color- and linebreak information with appropriate bash escape characters.
The results are really cool and now I have a welcome screen chosen randomly from those (so far only) 3 "pictures":




Useful links:
http://www.text-image.com/ (image2ascii)
http://www.commandlinefu.com/commands/view/5879/show-numerical-values-for-each-of-the-256-colors-in-bash (show 256 colors in bash)
http://www.enigmacurry.com/2009/01/20/256-colors-on-the-linux-terminal/ (how to get a bash-to-html color table with emacs)
http://tldp.org/LDP/abs/html/colorizing.html (linux documentation on bash colors)

Those were the links that helped me make the script. It is still slow and maybe I will speed it up some day. If you want to use it, and do not want to install python-Levenshtein, just delete any import and function calling that module (it's not needed). Levenshtein is on pypi.