A library is binary compatible, if a program linked dynamically to a former version of the library continues running with newer versions of the library without the need to recompile.
If a program needs to be recompiled to run with a new version of library but doesn't require any further modifications, the library is source compatible.
Binary compatibility saves a lot of trouble. It makes it much easier to distribute software for a certain platform. Without ensuring binary compatibility between releases, people will be forced to provide statically linked binaries. Static binaries are bad because they
In the KDE project, we will provide binary compatibility within the life-span of a major release.
You cannot ...
void functionname( int a ); void functionname( int a, int b ); //BCI: merge with int b = 0 |
The biggest problem when writing libraries is, that one cannot safely add data members since this would change the size and layout of every class, struct, or array containing objects of the type, including subclasses.
One exception are bitflags. If you use bitflags for enums or bools, you can safely round up to at least the next byte minus 1. A class with members
uint m1 : 1; uint m2 : 3; uint m3 : 1; |
uint m1 : 1; uint m2 : 3; uint m3 : 1; uint m4 : 2; // new member |
Bitflags and predefined reserved variables are nice, but far from being sufficient. This is where the d-pointer technique comes into play. The name "d-pointer" stems from Trolltech's Arnt Gulbrandsen, who first introduced the technique into Qt, making it one of the first C++ GUI libraries to maintain binary compatibility even between bigger release. The technique was quickly adapted as general programming pattern for the KDE libraries by everyone who saw it. It's a great trick to be able to add new private data members to a class without breaking binary compatibiliy.
Remark: The d-pointer pattern has been described many times in computer science history under various names, e.g. as pimpl, as handle/body or as cheshire cat. Google helps finding online papers for any of these, just add C++ to the search terms.
In your class definition for class Foo, define a forward declaration
class FooPrivate; |
private: FooPrivate* d; |
class FooPrivate { public: FooPrivate() : m1(0), m2(0) {}; int m1; int m2; QString s; }; |
d = new FooPrivate; |
delete d; |
You may not want all member variables to live in the private data object, though. For very often used members, it's faster to put them directly in the class, since inline functions cannot access the d-pointer data. Also note that all data covered by the d-pointer is obviously private. For public or protected access, provide both a set and a get function. Example
QString Foo::string() const { return d->s; } void setString( const QString& s ) { d->s = s; } |
If you don't have free bitflags, reserved variables and no d-pointer either, but you absolutely have to add a new private member variable, there are still some possibilities left. If your class inherits QObject, you can for example place the additional data in a special child and find it by traversing over the list of children. You can access the list of children with QObject::children(). However, a fancier and usually faster approach is to use a hashtable to store a mapping between your object and the extra data. For this purpose, Qt provides a pointer-based dictionary called QPtrDict.
The basic trick in your class implementation of class Foo is:
Note that some compilers/linkers (almost all, unfortunately) do not manage to create static objects in shared libraries. They simply forget to call the constructor. Therefore you need a static pointer to a QPtrDict, and an access function:
// BCI: Add a real d-pointer static QPtrDict<FooPrivate>* d_ptr = 0; static void cleanup_d_ptr() { delete d_ptr; } static FooPrivate* d( const Foo* foo ) { if ( !d_ptr ) { d_ptr = new QPtrDict<FooPrivate> qAddPostRoutine( cleanup_d_ptr ); } FooPrivate* ret = d_ptr->find( (void*) foo ); if ( ! ret ) { ret = new FooPrivate; d_ptr->replace( (void*) foo, ret ); } return ret; } static void delete_d( const Foo* foo ) { if ( d_ptr ) d_ptr->remove( (void*) foo ); } |
d(this)->m1 = 5; |
delete_d(this); |
Matthias Ettrich [email protected]