Post dinner notes

One value that this comment has served along with my compiler, is to teach me that default parameters on a template *function* are actually a C++0x feature, and apparently not part of C++03 as I have assumed for ages.



template <typename Num=int> Num square(Num n) { return n * n; }

int main() { return square(5); }

// g++ 4.5.1 reports:
//
//      error: default template arguments may not be used in function
//             templates without -std=c++0x or -std=gnu++0x


Somehow, I always figured if you could do it on template classes it would be legal on template functions as well. Guess that’s what I get for never reading the ISO C++ 2003 standard, and it having been a number of years since I last RTFM so to speak o/. About 95% of the times I use templates, it’s at the class level anyway, and most queries I have on C++ are easily solved by reading a header file. To be honest, while the drafts for C99 make for a very nice read, I’ve always worried that digging into the C++ standards might melt my brain or put me soundly to sleep. Frankly, C standards are helpful along side SUS, but the standards for C++, well all I can say is that I’ll be happy when 0x becomes as omnipresent as C89 capable C compilers. I’m just not sure I will live that long though :-(.

What I’ve done is implemented Engine::Call() as a sequence of template functions that use the vaguely normal method overloading semantics to cope with the arguments through Push(). Nothing else has been able to maintain the desired level of type safety. Basically if Call gets 0 arguments = use the current class state, and successive versions of Call(funcname, …) doing the obvious manipulations to generate the correct state and than run it. I would rather a recursive variadic template but life sucks and C++0x isn’t every where just yet. Having to supporting late model GCC 3.x is a real possibility for Cassius to bear fruit without thinning a few hedges.

Push() is just done as a set of virtual member functions for logistical reasons. Since each Engine instance is tagged with it’s language and implementation details (as an insurance policy), the same effect could be done through a set of template  specializations and a little static_cast magic to forward it to the correct subclass without any inheritance tricks. At the price of a more stringent contract with subclasses, that would also allow for getting rid of other virtual members in the base class. I’m not what instructions a C++ compiler is likely to generate for a static_cast from parent to child class compared to what C++ virtual methods and casting things in C land will produce, but I could really care less about the cost of using virtual Push()’s versus templated Push()’s here: Call() is the one that matters at the template level. Why virtuals are used for Push() in point of fact, is to avoid a build time dependency against the backends (to be loaded at runtime), which obviously would be a circular dependency. So templated Call(), virtual Push() works more convineantly than a purely templated driven solution.

Being sublimely lazy, rather than write out a set of similar template functions for each version of Call() that’s needed to handle these successive amounts of arguments, I just wrote a Lua script to do it for me: then shoved that into the projects build scripts and adjusted the headers to include that auto generated file as needed. One of my favourite Boost libraries actually does something similar for handling a variable number arguments for a functor type class, but I’ll be damned if I’m writing them by hand!

Lately I’ve been relying on a mixture of rake (Ruby) and premake (Lua) based build systems, so it is rather nice to be able to do such things: and not kick myself later (make, vcbuild).

I love it when someone nudges my brain back into programmer mode 🙂

Spent yesterday trying to get back into the swing of things, first time I’ve actually logged on comms in a few days lol.

Last night I was watching The Others and Surrogates. The others might make some people feel creeped out but I only found its plot a bit overly clichéd. If the ending isn’t obvious by first leg of the  movie, it is by the half way point o/. It does pose an interessting thought though. What if “Disturbances” that people feel are ghosts and hauntings and all that sort of stuff, is really just a conflict of matter? Where the living and the dead have crossed paths but nether know one is dead, or perhaps it’s not death but something whacko in the space time continuum. Who knows, I still maintain that if ghosts exist, you’re more likely to die in the bath tub than experience something like Poltergeist. I don’t rule out the possibilities of ghosts, I just don’t expect to ever see one ^_^.

The surrogates on the other hand, is an interesting sci fi flick. Basically people no longer “Go out” but instead control an avatar via remote. It follows a detectives hunt for a weapon that is able to kill the human operators behind their “Surrogates”.  While not exactly a spectacular movie, it explores an interesting concept, well interesting in light of the modern Internet. Being able to do anything without risk of life and limb, is an interesting idea, but I would argue that’s part of what makes life worth living. After all, if you’re going to go sky diving , what good is it, if you can’t *really* experience it? It’s only a facsimile of the real thing. For something like a fire fighter, IMHO it would be perfect but the ideal of living every day life through a surrogate, just, doesn’t feel right to me.

Currently Lua and Python are in the works, because Lua’s API is different from what I’m used to, and Python is well, not fun to embed but simple enough. Conceptually there is no major difference between a parameter list and a stack, it’s just a sequence of data at heart, and Python functions basically use sequence objects.

What would be awesome, is if the calls are defined in terms of stack manipulation, is to create a template method called Push, that users template specialisation in order to wrangle plain old data types and callables to the right scripting language types, so we would have something like this:

e->Run(SourceString("function f(a, b) print(a); return b * b end")
e->Push(/* instance of some type representing f() */);
e->Push("header message");
e->Push(2);
e->Call();

and rely on the compiler to Get It Right in figuring out the relevant overloads, e.g. Push. Somewhat probmatic is the issuance that Push needs to be part of the abstract base class but can only be defined by a concrete implementation of the interface; normally it would just be defined as a virtual method. That means it works like you would expect a class method to work by default o/. Only thing with that is templates cannot be virtual, so we would have to sidestep the whole vtable thing.

That’s a piece of cake thanks to C++ allowing the abuse of inheritance and casting:

/* An example */

#include <iostream>

template<class Impl> class Base {
public:
template <typename T> void Push(T arg)
{
static_cast<Impl *>(this)->Push(arg);
}
};

// implementation
class Impl : Base<Impl>
{
public:
template <typename T> void Push(T arg);

};

template <> void Impl::Push (int arg)
{
std::cout << arg << std::endl;
}
template <> void Impl::Push (const char *arg)
{
std::cout << arg << std::endl;
}

// subclass that just overrides one method
class X : Impl {
public:
template <typename T> void Push(T arg);
};

template <> void X::Push (int arg)
{
std::cout << arg * arg << std::endl;
}
template <> void X::Push (const char *arg)
{
static_cast<Impl *>(this)->Push(arg);
}

int main()
{
{
Impl test;
test.Push(2); // 2
test.Push("string");
}
{
X test;
test.Push(2); // 4
test.Push("string");
}
return 0;
}

Because the parent class is templated against the derived class, it’s possible to get jiggy with it at compile time. Namely enough is known by the parent about the child to invoke the correct method. Where it becomes somewhat annoying is when you want to continue with subclasses, like X in the above example.

A year or two ago I learned that people call this the “Continually Reoccurring Template Pattern (CRTP)”. Being lazy, I just think of it as the poor mans way of doing something similar to what “virtual template <…> …” would logically imply, if only the effing compiler was that smart. For what I need, just mating instance method overloading with virtual method calls is good enough.

Now all that is trivial, the real gripe however is how do you properly make a “Convenience” method, let’s say one we can do like e->Call(/* variable number of parameters */); and have it do the appropriate magic for us based on the type.

Well, sadly we can’t so easily. To use a va_list, there has to be a way to access the type of the argument. Normally this is done the same way that printf() and scanf() work in C, taking a format string saying what type to cast each parameter to. Pythons embedding API actually does this to convert from C data types to Python objects. Someday I need to open up a C library and look at how va_arg() actually works, I’ve always assumed it’s some sort of hack around a block of memory and type casting. It’s trivial to implement that kind of thing, already have done it for testing purposes (rather than templated Push()’s) but using a format string to describe the specifiers breaks down on type safety, where as at least with the template thing, the compiler can help some.

We can’t rely on Push() overloads to do the right thing because va_arg() is needed to access the arguments if Call() takes a variable number of arguments in the C++ 2003 compliant way. Obviously the easist solution is to find a smarter way of doing va_arg(a_va_list, a_type). Life would be a piece of cake with templates that can take a variable number of parameters, right? Well there’s few  vendors out there who seem to know how to do that <_>.

So how the fuck do you do a smart va_arg() like behaviour? The only thing I can think of at the moment is to make them all the same templated type, so it’s known how to cast them; then try and work some sort of char_traits<> like magic to figure out which Push() is appropriate but binding the necessary info creates more of the same. That and looking up in the C++ standard how many template parameters (if any) compilers are required to allow, and generating every possible permutation of arguments using a script to make the necessary template code before calling the compiler.

Either way, I’m just taking a break for a few minutes to enjoy how peaceful the quiet has been for the last twenty minutes lol.

Despite being interrupted almost every five to fifteen minutes, I managed to get the backends for embedding Lua and Python sorted. Today I would like to start getting into making it useful for something besides evaluating scripts.

Cassius needs to allow two things in order to be useful to me: invoking the embedded languages procedures from C++, and a way to export code to the embedded language. My interest, is whether or not it’s actually possible to accomplish that using fairly standard C++.  I’m kind of hoping, to see just how far that can be pushed.

From experience, I’ve learned that you can expect something vaguely C89 compliant anywhere in the world but expecting C++ compilers to agree on all things template related, can be like asking a goldfish to walk on air – a bad idea! That’s why I rarely do more with templates than I have to. With how much compilers have changed in the last four years, I reckon it’s time.

Out of everything traditional C++ offers, most of it is just sub standard compared to newer languages. A lot of the code I’ve read over the years, I would hardly even count as C++ so much as C with classes, but people have developed reasons I guess. IMHO how C++ can interface with C code is a killer feature, that could be just as readily solved by adapting a C compiler to generate JNI glue code or some shit like that. Throwing on inheritance based OOP isn’t that killer in my books, when you look at languages like Ruby and Python. The real killer feature of C++ is what you can do with templates. While supporting simple generics are part of it, that could be done in C by (abusing) the pre processor and adjusting your Makefile. It’s the opportunities to get creative at compile time that make its it worth while, someday I really should see if any good books have been written on TMP in C++.

The way I look at it, macros make Common Lisp stand out from it’s younger peers, C++ templates make you drool, or curse compilers more frequently lol. Leveraging languages is why more than one programming language should exist.

Relaxation time

One thing spending almost my entire life around computers has taught me, is that rarely is anything impossible, so much as it may just be a pain in the ass to get done. For R&R, my interest is in exploring whether or not a wrapper around scripting languages can really work without heavy introspection or SWIG style code generation.

Principally, embedding a scripting language amounts to initializing it, feeding it with code, and stitching together an interface between it and the desired parts of your C/C++ code. In my experience most time is spent on writing code to bridge C with the script language. It’s kind of like an adaptor for calling conventions, but in C rather than native code.

The question that interests me, is whether or not C++’s standard issue functors and binders, are good enough that it could be done without having to to cuddling up to much to a specific scripting engines embedding API, for each one being embedded. In most dynamic languages the manipulations needed are pretty trivial, but C++ is rather, more traditional. Because of that curiosity, I’ve had an idea on my mind for ages, which I dub “Cassius”. The idea is, to have an interface that knows how to embed several scripting languages, and use that to interface with the scripting languages, in a way more agnostic to which scripting language you’re using.

I thought of the name as a reference both to the Roman name and to Cassius Clay, better known as Muhammad Ali—because after adding support for a embedding a few scripting languages, it might very well knock me out ^_^. The part that I’m not sure, is whether or not, it is technically possible to do it in straight C++, or if the APIs would require more than is possible without using something like SWIG.

Sometimes for a change, it’s fun to ride an idea to the end of a tunnel without trying to speculate what’s waiting there.

I don’t really feel like sleeping, so I guess it’s time for a little L4D or RvS. Tomorrow, it’s time to go back to coding. Big question, is whether or not my family will let me get anything done, instead of trying to pump me for information :-/.

Headphones, here I come!

Well it has been an interesting day. I was called to an interview for a programming job, how shocking is that? Whether or not knowing my arse from a hole in the ground is sufficient for an entry level job, where as most places I’ve seen, the requirements all CS degree and check list of X years experience in Y, still remains to be seen. I don’t have infinitum work experience, I have what experience I’ve been able to get while following my interests. That’s kind of a price of doing things on my own, more practical knowledge than working experience.

The good news, is now that interview is done, now I can refocus my attention back on coding instead of worrying about getting there on time. I’ve been wanting to push on with an idea I dubbed Cassius, which includes my first shot using Lua’s C API instead of Lua itself. Embedding Lua is rather different from Python and the SpiderMonkey JS engine, so it helps test a little theory further. Most of my time of late, has been split between helping someone with an ASP.NET project and absorbing information. I would rather spend time getting stuff done.

Since hearing about all this, my mother has had her strings of being “Fore” and “Against”. Mostly fore when she realises my getting this job would mean she would have less to worry about; mostly against when faced the fact, that would mean an end I would be working on my own accord.  My mother couldn’t be expected to make up her mind about anything, if my life depended on it 8=).

From what I’ve been able to gather about the company via my own poking about and through the interviewers, both their questions and their answers to mine, suggests to me that this company would be an excellent business to work for. At least, they passed through my gaze without any raising any red flags. They seemed to be mostly interested in two areas of focus, one of which I’m particularly interested in,and the second one seems to lead towards a technology that I have always wanted to dig deeper into, but never have been able to on my own. The only bad thing that I can say about the company, is that it’s about an hour and a half drive from home, but I don’t mind that. I’ll never quite understand why so many people seem to hate commuting. You just get in the car and go to work, doesn’t matter if it’s across the street or across the metro area.

Prep work included plotting the trip, getting a hair cut, and ripping open the closet for a suitable shirt. Being me, of course I wore blue jeans and army boots lol. Since I spend most of my free time in front of a computer rather than people, I rarely bother much about hair. As expected, my mother hated the barbers handy work because it wasn’t “Short” enough among other things.  I don’t even like my hair short, which of course infuriates my mother, and she has been cursing barbers and the like for as long as I can remember. For the shirt, the only one more formal than a plain T-shirt, that didn’t need washing was one marked “Technology Specialist”. Ma found that most appropriate; I consider it the less than ideal but being unable to dry the others faster, thus shutting her up is a small price to pay. With my luck, getting hired would likely take a miracle anyway, so why worry about the shirt? Brains are more important than appearance when it comes to problem solving.

Ma decided that she wanted to come along, since she had never been out there before; the business being located in Duluth. That’s basically a 70 mile / 112 kilometre trip, or a nice hour and a half on the highway. Heck, I remember when you had to drive that far from Newnan, just to go shopping somewhere besides Wallmart! Honestly, I would rather not have my mother along, it’s not like I’m five ****ing years old, but try and get that through any mothers head let alone mine. Hmm, that reminds me of once telling my best friend that mothers are mothers, everywhere, when his was driving him batty over packing.

Hit the Interstate early, because while I know what the traffic on the I-85 is like, it has been a veerrry long time since I’ve been on the I-285. The Interstate is easy to get around, it’s just a matter of paying attention to where you are going, and I have a rather long attention span compared to most people lol. The main problem was on the cities main drag, where it seems just like Peachtree city, relying on the street signs is generally a bad idea for getting around. After trying to get there early, I ended up an hour late, courtesy of going past it. Ironically, I marked the last three roads leading up to it as a safe guard, and guess what? Two of them were only marked by the names on the map, when coming from the opposite direction and thus they had different names when coming than going. Which isn’t good when it’s your first time in the area. On the way home, I noticed several other instances of that with signs, which is just like parts of Peachtree city really. I guess that is also Google Maps in all it’s glory o/.

Getting lost for me, is very annoying. I’ve got no problems about asking someone for directions, I’ve just got problems with being late. I intend to go over the map and the directions with a fine tooth comb because either there was an off by one error finding it, or the street sign didn’t match what I had seen on the map. Once guided onto the needed road, finding the place was the same process  what Google map had given me for the “Missing road” in question. Next time I need to find some place for the first time, I’m printing the map out several times, at different scales; as opposed to just taking a shorthand note. That way I can at least stop somewhere to check printed maps against the written directions. Interviewing for a programming gig, I would find it sickly ironic, if debriefing myself showed it was an off by one error. On the upside though, now that I know how to get there, I can find it easily… which is a little late to help me much now. In my eyes, being late is a failure and being late for an interview is even worse! For me personally, being punctual is the goal you live by and the only excuse I’ll tolerate of myself as a valid one for being late, is something involving an “Act of GOD, dog, or dismemberment”, anything else, I’ll kick myself over.

Sigh, the one thing I actually wanted to do was make a good first impression. You only get one of those. After how much time I’ve spent wondering if this opportunity might be a much needed break, some how it figures that such a slip would happen. I seemed to take being late much more seriously than they did, perhaps they cut more slack for an out of towner than I allow myself.

The man that called me in for the interview had me speak to three of the people that worked there. The first one seemed fairly focused on programming stuff and the ability to express it verbally. He basically got me talking about a few bits of specific to coding and some of the projects that I’ve done. Mostly talking about boring stuff in my book but I can understand the reasoning behind it. When I was in [SAS], I often subjected potential recruits and young instructors to such things. The second person to interview me, seemed more focused on checking my experience, as if he wanted to find where my stronger focuses were. My experience is fairly spread out between languages, so that’s likely a lost cause: because I pick the language for the task, rather than e.g. doing everything in XYZ.  Accumulating so many languages, has to be good for something, eh? Like my studies with lisp, I love lisp but it is rarely the ideal tool for getting crap done; particularly in a world written in C. The third person, piqued my interest when he mentioned the dawf and stabs formats, but I can’t really say that it was much deeper than that.

I don’t particularly like interviews, it’s just not my thing. But that’s how the world works. Guess things went fair enough with the first person, not so sure about the other two. Don’t know the third man well enough to read his body language assuredly. The thing that mostly concerns me, is how much such queries can tell you about someone’s practical ability. Going into the interview, I was curious whether or not I would be asked anything relevant to coding, but I would say code had very little to do with it. There’s a rather big difference between knowing technologies, and having used it for enough years that you dream in it. Nuts, how many times has perlishness slipped out? :-/.

Modern programming languages are trivial to learn, at least for me. Languages are pretty much like how a knife is in cooking: you use the big one to butcher the hog and the small one to make French fries with. If you know how to use a knives, it’s not hard to become proficient with a knife. The cooking techniques dictate the application of the knife to the problem, same exact thing in programming. Only you rarely can eat the results.

One thing they seemed fairly interested in was the distance involved, which is probably a good sign that they noted it. In my book though, it’s just a matter of gas money lol. If I got the job, I would just save up and move closer anyway, in order to spend less a month on petrol.

It felt as if my meagre resume got a thorough review, even though it’s not really worth much more than the stuff I’ve managed to learn. Most of my experience comes from my own projects or volunteer work, because no one will hire me without the degree. The only good thing about that, is because I learn things quickly and frequently enough, that there is a fair bit stuff on my resume. In three years, I slurped up like nine languages in the time a friend of mine picked up one language, and he hardly knew enough C++ to even count as knowing part of C. Seriously, sometimes I’m glad that I wasn’t the one to go to college.

Going home was fairly easy, just a matter of getting back to the interstate, and looking for the right merge onto the I-85, i.e. going south. Since she was holding the directions, I asked my mother to double check the exit number before reaching the exit in question. Instead of her reading the printed page as directed, I ended up having to do it and having to quickly juggle onto the I-85 North going back towards Atlanta. Bloody hell, someday’s you can’t get rid of an exit!

I had wanted to get there and back out early enough that we would be on the Interstate before dark, but we actually made it to local roads before dark, and I can practically drive those blind folded, night or day. So now I’ve just got to put up with my mother dissemination information, which she can’t even  manage to quote correctly.

At least my family is consistent….

Somehow it figures, that for a rare change where I’m feeling good about a piece of news…. Friends and peers have congratulations and wishes for good luck, my mother on the other hand treats me like an ass and resorts to the evil eye!

I learned a long time ago, no matter what it is, if I want to be happy about something, it is better if my mother hears it last rather than first but when will I learn to implement that?

Somehow it fits, that I was having a rather pleasant yet unpleasant sort of dream this morning, got up with an uneasy feeling, and was soon vindicated in having one, lol.

In going to uncover the car this morning, I noticed something plastic in front of the car, gave my usual sigh about how everyone here but me seems to litter o/. In pulling the cover off the car, the bloody thing wouldn’t come off, for some reason it was stuck on the drivers side view mirror. I saw it had ripped open the car cover, that’s happened before (the covers are shit) but I wasn’t pulling that hard, and usually that only happens after a good freeze.

Then I saw the whole fucking side view mirror was hanging by it’s electrical wire with the mirror plate missing! My first thought was how the hell can a car cover break a side view mirror into pieces? Seriously, the material the car cover is made out of, is almost paper thin, lol. Car covers ready to break a hole at the bounce of a dime don’t break Side view mirrors, side view mirrors break miserable excuses for car covers, not the other way ’round!   The damage to the body of the side view mirror was ludicrous, like it got a hard mashing the other corner, not like it got pulled off by the cover. If it had been done by the cover, I would also have *heard* it quite loudly, there was that much plastic shattered off of it.

What made me suspicious however was every thing about the mirror. Under the car were several shards of plastic from the mirrors support and frame; sufficiently covered in water and sediment, that I’ll eat my hat if those things had just landed there. They looked as if they had been there for hours. The real clincher however was the mirror itself: after putting the cover away in the boot, on a hunch, I walked around to the front end and picked up the plastic thing I saw coming down the walk way. Sure enough, it was the side view mirrors plate with the mirror, broken into several pieces, face down, and equally dampened with mud.

All evidence points to someone knocking it off, and conveniently it was the parking space right next to it that was empty this morning.  Don’t you just love Georgia drivers?