C++, Java

After about a year and a half of doing pretty much pure java (though also some time on
wx4j (a Java binding for a C++ GUI toolkit), I started a new job doing primarily C++ development. It’s also win32 instead of linux/crossplatform, but that’s not super germane, really, since so far I haven’t done much UI programming. In fact, I’ve been lucky enough to be able to keep almost all the win32 API at arms length, which from what I have had to deal with of it is quite lucky.

Things I knew I would like:
1) const correctness.

2) operator overloading.
It’s one thing to be able to implement operator[](const string& key) {}…. but being able to implement operator const string& () is really awesome. For example, I have a simple readonly XML object model:

class Element
{
        /* subscript operations not const-safe */
    mutable map<string, vector<Element *> > children;
    string content;
    string name;
public:
    /* only called externally to create the root */
    Element(const string& name);
    ~Element();
    /* input functions */
    Element* add_child(const string& name);
    void add_content(const string& newdata);
    void close(void);
    /* accessors */
    const Element& operator[](const string& key) const { return get(key); }
    const Element& get(const string& key, size_t pos = 0) const;
    operator const string&() const { return content; }
    size_t childcount(const string& key) const {return children[key].size(); }
};

the implementation of this is mostly pretty straightforward; the .cpp file defines a const Element empty_element, which get() returns if the key isn’t found or the offset is out of bounds. close() sets a flag which add_child and add_content both honor. For a long-lived object, it would make sense to have the vectors and map resize themselves to their current exact size on close(), but for my application that’s not really necessary because the object is destroyed pretty quickly. The only pain in the ass is that the destructor has to loop through the map and the value vectors and free all the memory, but that’s pretty easy.

Obviously it’s only a subset of the real XML API — I don’t support attributes or namespaces at all. But for what I’m doing, it’s sufficient, and it sure beats the hell out of MSXML DOM, which is full of COM crud.

And while I’m on the subject — this really was necessary. The DOM API is fairly inadequate, but mix that in with the need to cast every single input string to a fucking variant, and I’m ready to kill everyone who’s ever worked within 10 miles of microsoft HQ.

Things I miss:

IntelliJ. Dear god. I am going crazy without the ability to do certain refactorings. I create a ton of const string& references because getting the content of an element was much harder with the first iteration of the API. I’d like to just highlight such a reference and choose “remove variable”, which in IntelliJ would result in a prompt to replace each usage of the variable with the initializer. Instead, I’m manually copying and pasting… or worse. Oh well.

Other than that, not much. If I end up doing stuff more involved with the UI than just the current XML -> PDF report generation stuff I’m doing right now, I’ll probably start screaming, but that’s not C++’s fault — it’s Microsoft’s.

This entry was posted in tech. Bookmark the permalink.

Comments are closed.