Function chaining is a construct in C++. It constructs a chain of function calls in one statement. Return value of the previous function call is used to call the next function and hence a chain of function calls is created. Following is an example of it:
obj.fun1() is an expression which evaluates to the return-value of func1(). On this returned-value func2() is called which again evaluates to its return value. Similarly func3().
Function chaining is good as well as bad. It may make the code look compact and nice but excessive use may end up with readability and maintenance issues. The code may be difficult to debug also. One can’t see the intermediate return values of the function calls.
Apart for the readability and debugging issues, it can also result into a serious memory issue. Lately, I faced it while converting from a QString to a char* C string. Here is the code:
// function QString::toLocal8Bit() returns QByteArray
// QByteArray::data() returns pointer to internal data
const char *c_str = str.toLocal8Bit().data();
// c_str is now a dangling pointer referring to an invalid memory
In the above code c_str points to an invalid memory.
It happens because the function chaining involves unnamed temporary objects. These unnamed temporary objects are returned by the function calls. And these are again used to call the next function call. But in this case the last temporary object is not stored and is deleted after the statement is complete.
This code can be fixed by avoiding the function chaining and storing the intermediate object.
Lately, I found a silly mistake which I have repeated at a couple of places in my Qt code. I thought of noting it down.
I have a QStringList (list) and I want to find the index of a string (str) in it.
// list - QStringList
// str - QString
int index = list.indexOf(QRegExp(str));
// this function accepts QRegExp only, though
// in the new releases of Qt an overload
// accepting QString is also available
Nothing wrong with this, it has been working great for me and I have used it at many places. But, this works as long as str doesn’t contain any RegExp special characters. If the str contains special characters, its meaning changes. For instance dealing with filePaths, filepaths may have special characters in it like the character +.
The meaning of QRegExp(str) changes if str has the character +:
As usual, thanks to the authors, community and the users who have been driving the project.
QXmpp is a cross-platform C++ XMPP client (and server!) library based on Qt and C++. It is an open source project licensed under a permissive license LGPL. As of today, the project is around two years old.
QXmpp repository contains many examples. One of them, GuiClient, is a full-fledged graphical XMPP client. I developed it for testing out the functionality and usability of the QXmpp API. This example supports most of the XEPs (XMPP Extension Protocols) supported by QXmpp.
To ease up the debugging, I have added a console dialog to display all the sent/received stanzas. Have a look at the Debugging Console.
Settings button can be used to launch this dialog.
User can control the logging by using the Enable Check Box.
Sent and Received messages are color coded to differentiate between these.
At times, you need a simple encryption and decryption functionality to secure sensitive information. In my case, I needed to store passwords on disk to implement the “Remember Password” functionality of the GuiClient example of QXmpp.
Instead of going for a 3rd party library where you just need a very basic crypto functionality I will suggest using XOR encryption. XOR encryption is pretty easy to implement. I will also present my implementation of this algorithm using Qt C++.
XOR (Exclusive OR) Encryption or XOR Cipher is a simple symmetric encryption algorithm. It operates according the principle that XORing a data twice with the same key results in the same data.
The first XOR of the data and key gives the encrypted data. Then the decryption involves XORing the encrypted data with the same key.
EncryptedData = Data ^ Key Data = EncryptedData ^ Key
Data: data to be encrypted
Key: secret key or password
EncryptedData: data after encryption
^ represents Exclusive-OR (XOR) operation
Let us use two binary numbers for Data and Key. The
Last year, I founded an open source project QXmpp. It is an XMPP client library based on Qt. It is licensed under a permissive license LGPL. The project is now more than a year old. The very first public release QXmpp 0.1.0 was made on June 14, 2009. And last Sunday, we released QXmpp 0.2.0.
QXmpp 0.2.0 comes with numerous features (many XEPs and new authentication schemes), many bug fixes, architectural improvements and Doxygen documentation. Have a look at the Changelog for a complete list of new features and changes in this release.
Thanks to the authors, group and our users who have contributed in the form of patches, bug reports and suggestions.