Recently I’ve been playing the Twitter API through C++. I read through the Getting Started Guide and discovered that I had cURL installed on my system. It was so easy to grab my profile that I couldn’t resist having a play around to see if I could get timelines and update my status.
cURL has a an API available for a variety of languages that is really simple to use and it’s been really easy to implement some basic Twitter functionality into a command-line client. Using TinyXml to parse the results of HTTP requests I can grab posts and user information. Once it’s setup you can make a simple status update with a single method call.
The problem I have now is how to parse the temporal information. It’s one thing to grab simple text but there is a lot of numeric-data embedded in it that is quite useful. I’m looking at Boost.Date to see if that can help, the next thing to do is to be able to say how old a post is and filter results by time. Got any tips for doing this sort of thing?
I’m going to keep playing with this and see if I can come up with something useful for a proper app.
I’m working on a little C++ project at the moment. As part of it I’m trying to learn more about the Boost libraries and do clever things with the STL. I have found myself writing a lot of simple loops that iterate over containers of objects and do simple things like call a method on each object. I get tired of writing iterator code like this, it’s something we end up doing a lot as C++ programmers. The syntax for defining and using iterators can be a little long and it can be a right pain to debug if you make a simple typo.
Enter the BOOST_FOREACH macro. I am aware of the STL for_each() function that uses some fancy binding objects to do all sorts of clever stuff. Boost Bindings extend this and create a really powerful set of tools. The problem with this stuff is that it looks a bit weird and isn’t that easy to understand. While I was reading tutorials I came across BOOST_FOREACH. This simple macro gives us something very similar to the foreach keyword in C#.
Imagine you have a container of objects of class Foo, and you want to call the Bar method on each instance of Foo. Usually I’d write something like this:-
for( vector<Foo>::iterator f = container.begin();
f != container.end(); f++ )
{
f->Bar();
}
The BOOST_FOREACH macro can cut out some of the donkey work by doing all the iterator stuff for us. So here’s the above example using BOOST_FOREACH.
BOOST_FOREACH( Foo &f, container )
{
f.Bar();
}
I like this alternative because it’s a lot cleaner than the classic version and it will work the same with both STL containers and standard C arrays. You also end up typing less and so inevitably will make less mistakes. It’s not going to work in all circumstances, any code where you want to manipulate the container itself is probably a bit risky, but it makes doing simple stuff a bit easier and leads to better code without having to get bogged down with templates and std::for_each.