First post, yay. In this blog I am going to share projects, snippets and thoughts about C++ projects mainly focused on solving problems using the Boost C++ Libraries. Basically whenever I write something in a compiled programming language, C++ will be my weapon of choice. The primary reason for this actually are the Boost libraries. I am constantly switching between operating systems (I usually develop stuff on a Linux machine at home, and am using a Windows machine at work), and I require the code I write to be operable on both systems without further ado. The Boost libraries offer me this, and on top of it very generic and straight forward structures and patterns for pretty much everything I want to do. David Johnson from Integrated Computer Solutions put it this way in his talk during the Qt Developer Days 2009:
"If you know the STL, think of Boost as being the STL+."
The things I will be covering here are compiled and unit tested by me, but just by me. There might still be bugs or performance issues with some of them, and I would appreciate if you pointed these out in a comment should you find one. I have compiled and tested the code using Boost 1.49.0. If you are using a different version, you usually are fine as long as the libraries used are part of that version (you can check that here), but if there are problems, this might be the reason for it.

For the first post, I will demonstrate the power of Boost('s smart pointers) with a very short snippet. The context: Nowadays, web services are everywhere and SOAP is widely spread. I am using gSOAP for developing clients for web services. SOAP services are usually defined in form of WSDL files. Parts of structures or whole structures sent or received may be defined as optional, and gSOAP will translate optional as a pointer to that structure. A null pointer evaluates to the optional value has not been set. While this seems convenient at first thought, it leads to the troublesome recognition that you will often have to deal with massive pointer structures. You can, of course, allocate memory for your structures with the new operator, but you might to forget to free the allocated memory afterwards, which would cause your application to leak. We can go around this issue using the Boost smart pointers:

#include <boost shared_ptr.hpp>
#include <vector>

struct DemoStruct {
    int *some_int;
    double *some_double;

typedef std::vector<boost::shared_ptr<void> > MemoryManager;

template <class T> T* create_managed(T *value, MemoryManager &mm) {
    return value;

int main(int, char**) {
    MemoryManager mm;
    DemoStruct *demo_struct = create_managed(new DemoStruct, mm);
    demo_struct->some_int = create_managed(new int(42), mm);
    demo_struct->some_double = create_managed(new double(4.2), mm);
    return 0;

As you can see, we have written a generic function that deals with the memory management using the Boost smart pointer library with just two lines of code.

Next time, we will look at a more elaborated project - I have in mind a file listener service that parses XML files in a certain directory to do something with the information inside. Thanks for reading and stay tuned for more fun with Boost.

No comments:

Post a Comment