The Lazy Programmer

June 7, 2010

An XML Writer for C++

Filed under: C++,XML — ferruccio @ 9:31 am
Tags: ,

I’ve worked on a couple of projects where XML files were generated via printfs or iostreams and this has always turned out to be a mistake. Maintaining such code can be tricky and you always spend an inordinate amount of time debugging the same type of problems over and over again. Usually a character with special meaning to XML (such as < or >) winds up not getting quoted correctly or you forget an end element tag.

The project I’m currently working on involves a search engine which returns its results in XML over an HTTP connection. I decided to use an XML writer rather than make the same mistake again. After looking around for an open source solution and not finding anything suitable for my needs, I decided to roll my own and place the project on Google Code.

To use xml-writer, simply drop xml_writer.h into your project directory and #include it as necessary. It is a very simple (less than 200 LOC) class. The accompanying unit tests also serve as documentation for its use.

Briefly, you instantiate an xml::writer object bound to an std::ostream. This allows you to send the resulting XML to a string (std::stringstream), a file (std::fostream) or any ostream compatible object. In my case, I created a stream object which writes directly to an HTTP connection.

You instantiate xml::element objects to actually create the XML element tags. An xml::element object has overloaded attr() methods to write element attributes and contents() to write element contents. When an xml::element object goes out of scope, it will write the necessary end element tag.

A simple example:

#include "xml_writer.h"
#include <sstream>
#include <iostream>

using namespace std;

int main()
    stringstream ss;
    xml::writer xw(ss);
        xml::element record("record", xw);
        record.attr("name", "fred").attr("age", 35);
        record.contents("yabba dabba doo!");
    cout << ss.str() << endl;
    return 0;

will result in the following output:

<?xml version=”1.0″ encoding=”utf-8″?><record name=”fred” age=”35″>yabba dabba doo!</record>

NB: the extra scope created to encapsulate the record element was necessary so that the xml::element destructor is called before the resulting string is output. xml::element should always be enclosed in some kind of local scope for this to work properly.

August 9, 2009

Dynamic C++ Update

Filed under: C#,Dynamic-Typing — ferruccio @ 2:51 pm
Tags: ,

I’ve been tinkering with my Dynamic C++ project on occasion in order to get it to build successfully under OSX without much luck. Most of it built just fine, but there were a bunch of places where the boost::variant::apply_visitor() function was giving me all sorts of grief.
The original problem was that I was passing an instance of a locally defined struct as the functor argument to apply_visitor(), such as:

unsigned int var::count() const {
    struct count_visitor : public boost::static_visitor<unsigned int> {
        unsigned int operator () (null_t) const { throw exception("invalid .count() operation on $"); }
        unsigned int operator () (int_t) const { throw exception("invalid .count() operation on int"); }
        unsigned int operator () (double_t) const { throw exception("invalid .count() operation on double"); }
        unsigned int operator () (string_t s) const { return>length(); }
        unsigned int operator () (list_ptr l) const { return l->size(); }
        unsigned int operator () (array_ptr a) const { return a->size(); }
        unsigned int operator () (set_ptr s) const { return s->size(); }
        unsigned int operator () (dict_ptr d) const { return d->size(); }

    return boost::apply_visitor(count_visitor(), _var);


August 10, 2008

Adding a lock() statement to C++

Filed under: C#,Multi-threading,Programming — ferruccio @ 9:43 am
Tags: , ,

One of the things I like about C# that I miss in C++ is the lock() statement. It provides a simple way to control access to an object across multiple threads.

When I first started writing Windows apps, I used the Windows critical section API directly. After getting tired of EnterCriticalSection/LeaveCriticalSection, (I suspect like everybody else) I created a CriticalSection class that was a simple wrapper around the raw API.

Eventually, I got around to using mutexes from the Boost Thread class. You can create a scope guard whose destructor releases its referenced object, but it still lacks the simple elegance and clarity of being able to say:

lock (some-object) {
    do something to some-object


August 3, 2008

Formatting Output with Boost

Filed under: C#,Programming — ferruccio @ 9:20 pm
Tags: , ,

Sometimes a GUI is overkill for a project. You just need a simple tool to do some task. Perhaps it needs to be scripted. So you whip up a console mode program and you eventually have to output something. At this point, many developers will simply ignore the C++ iostreams library and reach for good old printf(). I can certainly understand why. The iostreams objects are easy enough to use for simple formatting tasks. However, when you need to do something more sophisticated, you will often find yourself digging through reference material, muttering "this should be easy…"


June 4, 2008

C++: Using Boost ranges to simplify enumerations

Filed under: C#,Programming — ferruccio @ 8:58 pm
Tags: , , ,

Let’s say you’ve created a class which encapsulates an STL-style container and you now need to expose the data in that container. There are a few possibilities which we’ve probably all tried and found to be unsatifactory.


April 22, 2008

Using Boost to tokenize strings

Filed under: C#,Programming — ferruccio @ 9:55 pm
Tags: , ,

Like most C++ programmers that came from a C background, whenever I needed to parse a string into a bunch of tokens, I reached for strtok(). Sure, it’s not thread-safe. But I can take care of that later if it becomes an issue, right?

Eventually, I ran across the STL and learned to put my strings and other objects into containers. But strtok would eventually rear it’s ugly head to break the abstraction of STL strings just so I could split up a string without putting in too much effort.

At some point, I started playing with Boost. If you’re a C++ programmer and you don’t know what Boost is, you need to go to and take a look at it right now because I guarantee that you are doing way too much unnecessary work without it. Go ahead, I’ll wait until you’re done looking around.


April 17, 2008

Benchmarking stack_array<T>

Filed under: C#,Programming — ferruccio @ 10:45 pm
Tags: , ,

In my last post I presented a stack_array template/macro combination that I use to create dynamically sized arrays on the stack. By allocating the space on the stack we get the dual benefits of not having to worry about freeing memory and better performance. I knew the performance had to be better, but how much better? Would I have been better off just using STL vectors?

March 22, 2008

C++ Exception Specifications

Filed under: C#,Programming — ferruccio @ 4:44 pm
Tags: ,

Every once in a while C++ surprises me. Something that I thought I understood turns out not be exactly what I thought it was.

In C++, a function may have an exception specification which tells the compiler what exceptions the function may throw. e.g.

void test(void) throw(std::exception)

Tells the compiler that the function test() may throw an exception of type std::exception.


February 6, 2008

Linking C++ objects using references

Filed under: C#,Programming — ferruccio @ 9:15 pm
Tags: ,

A very common pattern in C++ object-oriented programming is to link objects together using pointers. For example, let’s say you have object that does some kind of processing (we’ll call it a Processor) and a Stream object that the Processor uses to report the results of its work.

A typical way to implement this would be:

class Processor
public :
   Processor(Stream *stream = 0)
      m_stream = stream;

   void DoWork(void)
      if (m_stream != 0)

private :
   Stream*  m_stream;


January 27, 2008

Typesafe session variables in ASP.NET

Filed under: Windows — ferruccio @ 4:17 pm
Tags: , ,

Lately I’ve been working on web applications using C# and ASP.NET and it struck me that since session variable are nothing more than hash tables that map strings to objects, they are prone to all sorts of programming errors.

  1. The variable "names" are strings that are passed as an index to the Session object. This has the same effect as having a language that does not require variables to be declared before use. You are always a single typo away from a hard to find bug. Also, Intellisense can’t help you when you’re trying to remember the name of particular session variable.
  2. If you try to use a session variable without setting it first, .NET will throw an exception. This usually results in session variable references being wrapped in try/catch blocks. Just in case.
  3. Session[] returns an object. So there is usually some sort of conversion to the actual desired type necessary. This is a minor point, but it does clutter the code with a lot of ToString(), Parse() and Convert*() method calls.


Blog at