std::async, and copies – gotcha!

Disclaimer: I love C++11! It’s the best thing that happened to C++ since I’m using it (mid-90’s). I’ve been hoping for lambda‘s in C++ since I discovered and started using Lisp. Elisions and move semantics – together with unique and shared pointers – pretty much free you from thinking about memory management! It’s just amazing! Check out “Going Native” talks on YouTube, incl. amazing talks by the likes of Stroustrup, Sutter, Lavavej, et al.

If you don’t want to read my rant, here’s the point – function objects are objects – they can get copied, unless you specify otherwise, so beware that you’re working on a copy, or pass std::ref.

Continue reading

Get MainWindow in Qt Widgets

Update 2017/04/27 (thanks to grholk!):

The proper way would not be to check the objectName, which can change at any time, but rather to check if the widget is an instance of QMainWindow. Take advantage of what Qt makes available by using a foreach.

QMainWindow* getMainWindow()
    foreach(QWidget *widget, qApp->topLevelWidgets())
        if (QMainWindow *mainWindow = qobject_cast(widget))
            return mainWindow;
    return NULL;

Old version:

Provided how much you can do with Qt Widgets, or Qt in general, it’s no surprise that such “specialized” function is not present (the QApplication::activeWindow() does not make it, as the window might not be active).

#include <QApplication>
#include <QDateTime>

QMainWindow* getMainWindow()
    QWidgetList widgets = qApp->topLevelWidgets();
    for (QWidgetList::iterator i = widgets.begin(); i != widgets.end(); ++i)
        if ((*i)->objectName() == "MainWindow")
            return (QMainWindow*) (*i);
    return NULL;

If, for whatever reason, you changed the “objectName” property of the main window, update the above if with this name.

Automatic fclose() in C++11

This post is more of a mocking than serious advice, provided that a) STL includes <fstream> header with std::ifstream and std::ofstream classes, that do the same without any additional effort, and b) mixing templates and libC is just plain weird in my book. Continue reading

Makefile Generator, version 2

genmake, the Not so Simple Makefile Generator Bash Script, is now hosted on Google Code (for older version, check this post).

The version 2 now supports:

  • executables, static libraries, and dynamic link libraries, based on the output file name format,
  • automatic increment of build number on each build (the header file including the version and build number is automatically generated; requires Awk),
  • several build targets – debug, debug profile, release, release profile,
  • easily specifying additional flags passed to compiler,
  • passing list of link libraries used for build,
  • selectively excluding files from build, and more…

Happy coding! 🙂

Increase build number in C/C++ header (using Awk)

This trick works with most IDE’s that use pre-build step, but easiest I guess is to use it in a Makefile. Feel free to modify the “script” in any way it suites your purposes.

Suppose you have a build number defined as #define BuildNum 1 in BuildNum.h header.

All you have to do is add the following command in your make all section in Makefile ahead of other dependencies (it’s one line, broken here for visibility):

awk '$2 !~ /BuildNum/ {print} $2 ~ /BuildNum/ {print "#define BuildNum "$3+1}'\
    BuildNum.h > BuildNum.h~; mv BuildNum.h~ BuildNum.h

Yes, it’s a clumsy solution, but it does the work just fine… 😀 Sure, you can catch the lines in array, and afterwards print them one by one back to file, etc. … but it’s just not at all pretty bash script (enhancements and suggestions welcome!).

Notice that the command is pretty much dependent on the format of the line the BuildNum is defined on, and that “BuildNum” should not appear anywhere else as a second word on the line!