Archive for February, 2005

Synchronicity

Monday, February 14th, 2005

It’s all been quiet on the synchronicity front recently; last night’s was a doozy, though.

On my way to meet Naomi for the Juliet Turner gig; I’m sat on the bus, iPod on shuffle and apropos of absolutely nothing at all it’s playing a couple of songs by a band called Booley, who were a fairly obscure Northern Irish band who did the church hall circuit in the late 90s; they were also one of Naomi’s favourite bands ever.

We get into Life Cafe, and Naomi notices a guy hovering around the back of the bar who looks a bit familiar. “I’m sure he used to be in Booley, you know. Go on, ask him“, she presses. Cut to the chase; I ask him and not only was it The Bloke What Used To Be In Booley, but he was actually the support act for Juliet Turner, now going under the name Duke Special.

So, that was good.

Less good was the bit where Naomi got her cash card eaten by the Sainsbury’s Bank machine just opposite Cornerhouse, although we did get a good laugh at the guy who used the machine after us, despite repeated recommendations from ourselves that he didn’t:

“I wouldn’t use that, mate. It just ate my girlfriend’s card.”
“Yeah? You sure?”
“Um, yeah. She’s on the phone to her bank to cancel the card now. Really, I wouldn’t use it.”
“I think I’ll risk it.”
“I really don’t think you should. I hope you’ve got your bank’s phone number handy.”
[time passes]
“Uh, I guess that was a mistake, then.”
“Is it stuck on the ‘We’re processing your request’ screen?”
“Er, yeah.”
“I think you’d better phone your bank.”

Oh, how we laughed. Plus, the taxi driver on the way home was pure dead brilliant and made me laugh lots.

Well

Sunday, February 13th, 2005

All things considered, I think yesterday went okay. I mean, Julian emerged from the day having married Sally, no-one got hurt and I figure that, having burned through 8 rolls of film, there’s got to be at least one or two good pictures in there. I’ll be dropping them in to Jessops for Vicky to develop, print and stick onto a couple of CDs tomorrow.

Children, though? Right pain in the arse. And also, what’s the deal with having a no-alcohol rule at a wedding reception? I mean, okay, the Bride’s family requested it, but still. Thankfully, there was a bottle of wine in the fridge at home which the missus and I polished off when we got home.

I lieu of doing something properly romantic, I’m taking Naomi to see Juliet Turner tonight at the Life Cafe. It’s slightly surreal taking your girlfriend to see the sister of someone she knows from her hometown.

Today I are mostly

Saturday, February 12th, 2005

I’ve been asked to do the wedding photography for a friend. Today is the day. It’s persisting it down with rain and blowing a gale. I’ve never done any wedding photography before. Portrait photography is possibly my weakest point. It’s going to be an interesting day.

Cashing in on the narcissism of blogging

Friday, February 11th, 2005

Now you can get your blog printed in book form! The levels of self-aggrandisation necessary to even consider such a thing must be through the roof – but also, does printing your blog not kind of miss the point? Weren’t blogs supposed to be the publishing revolution, largely because they weren’t printed, didn’t have to have costly reprints when things went out of date and permitted instant feedback and discussion on the topics covered?

More C++ fun

Thursday, February 10th, 2005

I’m loathe to turn this into a Fun Things To Make And Do with C++ blog, but this one is kind of neat. One of the first things all programmers do when they first get their hands on a graphics library is draw a Mandelbrot Set as defined by the equation z → z2 + c. I’ve been toying around with templates and stuff recently, and someone suggested that I should give writing a mandelbrot set generator using template metaprogramming a go. So I did:

#include <iostream>

template <int iter, int zx, int zy, int cx, int cy>
class Mand
{
public:
  static const int zxPrime = (int) (
    (
      (
        ((float)(Mand<iter-1, zx, zy, cx, cy>::zxPrime)/10000.0f) *
        ((float)(Mand<iter-1, zx, zy, cx, cy>::zxPrime)/10000.0f)
        –
        ((float)(Mand<iter-1, zx, zy, cx, cy>::zyPrime)/10000.0f) *
        ((float)(Mand<iter-1, zx, zy, cx, cy>::zyPrime)/10000.0f)
      ) +
      (cx/10000.0f)
    ) * 10000.0f
  );

  static const int zyPrime = (int) (
    (
      (
        2.0f *
        ((float)(Mand<iter-1, zx, zy, cx, cy>::zxPrime)/10000.0f) *
        ((float)(Mand<iter-1, zx, zy, cx, cy>::zyPrime)/10000.0f)
      ) –
      (cy/10000.0f)
    ) * 10000.0f
  );
};

template<int cx, int cy>
class Mand<0, 0, 0, cx, cy>
{
public:
  static const int zxPrime = cx;
  static const int zyPrime = -cy;
};

int x = Mand<100, 0, 0, 1000, 1000>::zxPrime;
int y = Mand<100, 0, 0, 1000, 1000>::zyPrime;

int main(void)
{
  std::cout << “Mandlebrot metaprogramming” << std::endl;
  std::cout << (float)x/10000.0f << “, ” << (float)y/10000.0f << std::endl;
}

This will calculate 100 iterations of the Mandlebrot function for the point c=(0.1, 0.1) and print out the result. The neat trick is that it’s actually the compiler that does the calculation, not the executable (however, as it requires float-casts in a constant expression, some compilers [including gcc 3.4] will barf on it – gcc 3.3.3 seems fine). Because you can’t have floats as parameters to templates, I’ve had to pass all the values around as ints, hence all the /10000.0f and *10000.0f all over the place.

I discovered this whilst I was looking for ideas; it actually prints out the set in ASCII as well, which is very nice indeed. He uses enums rather than static consts, which may break fewer compilers; I don’t know.

Yesterday

Thursday, February 10th, 2005

Bleh. Yesterday was a bit of a write off. First, Naomi goes to the doctor to get her sore throat checked out and ends up being sent to the hospital to get checked for glandular fever. Then, in the evening, I go round and we set off to go to the cinema, but the brakes on her car fail and I have to drive it home using the handbrake to stop. Then, every taxi company in Manchester is utterly slack and can’t get a taxi from her house to mine in a reasonable amount of time, so I end up staying round her house. And then I completely fail to get any sleep at all because the spare room in her house has a streetlight outside the window, is across the landing from the bathroom with an extractor fan that stays on all night, and has broken central heating so is completely freezing.

I am so very, very tired today.

Bah

Wednesday, February 9th, 2005

Actually, that code below turns out to be less useful than I’d hoped, as the singleton constraint is only enforced through the getInstance() member function and not through the constructor. I’m sure there must be a way of getting round it, but I can’t work out what it is; the central issue is that the base class is constructing the derived class through a template parameter. Setting the base class constructor to private means that the derived class cannot be constructed at all; setting it to protected means that the derived class can be constructed by normal means as well as the getInstance() member. Setting the derived class constructor to either private or protected means that the base class cannot access it, as so far as SingletonPolicy is concerned, it’s a separate class.

I think there’s some icky solution workable by deriving from both classes, but I’ll have to go away and think about it a bit.

Urgh

Wednesday, February 9th, 2005

I was going to write about Ireland, but then I got a clean compile on the following piece of code and felt so dirty that I’ve decided to put it off for a while:

#include <iostream>

template <typename T>
class SingletonPolicy
{
private:
  static T* instance;

protected:
  SingletonPolicy() {}

public:
  static T* getInstance()
  {
    if (instance)
      return instance;
    else
      return (instance = new T());
  }
};

template <typename T>
class MultipleInstancePolicy
{
public:
  static T* getInstance()
  {
    return new T();
  }
};

template< template <class> class InstancePolicy>
class MyClass : public InstancePolicy< MyClass<InstancePolicy> >
{
public:
  void aMethod() { std::cout << “BaseMyClass::aMethod – ” << this << std::endl; }
};

typedef MyClass<SingletonPolicy> MySingleton;
MyClass<SingletonPolicy>* SingletonPolicy<MyClass<SingletonPolicy> >::instance = 0;

typedef MyClass<MultipleInstancePolicy> MyMultiple;

int main(void)
{
  MySingleton *myS = MySingleton::getInstance();
  MySingleton *myOtherS = MySingleton::getInstance();
  MyMultiple *myM = MyMultiple::getInstance();
  MyMultiple *myOtherM = MyMultiple::getInstance();
  myS->aMethod();
  myOtherS->aMethod();
  myM->aMethod();
  myOtherM->aMethod();
}

The Curiously Recurring Template idiom is quite well known (defining a derived class in terms of a base class specialised on the derived class) as a way of providing the base class with full access to the derived class’s namespace, with static, compile-time checking on the access. What I’ve done above (in the MyClass definition) is take this one step further and combine this with template template parameters, making the derived class a template itself. This presents the additional complication that the templated base class is being specialised on a templated derived class, hence the wonderful class MyClass : public InstancePolicy< MyClass<InstancePolicy> > line – specialisations of the MyClass template are derived from a specialisation of the InstancePolicy template specialised on MyClass which is, of course, a template specialised on InstancePolicy. It all gets a bit recursive and will hurt your head if you think about it too much. Fortunately, the compiler is able to spot the co-dependency and work out what should happen here, rather than create an infinite chain of template specialisations.

The only thing I’m a bit unhappy about is the MyClass<SingletonPolicy&gt* SingletonPolicy<MyClass<SingletonPolicy> >::instance = 0; line, which just looks a bit inelegant.

Anyway, I’m sure Alexandrescu would be proud (and could probably write it much better, actually).

Back from Ireland

Wednesday, February 9th, 2005

Back now. Tired. BT Broadband actually worse than leprechaun post. Will post more in the morning.

I am a comedy genius

Friday, February 4th, 2005

This afternoon’s project: here. Now, if only CafePress did ultra-baggy hip-hop trousers, too.