Tuesday, February 23, 2010

It almost feels like cheating...

I've been noodling about in the last few days, with a view to eventually porting some 'C' code from last century to .net by incremental replacement; something I tried and gave up as too horrible some years (back when we only had Managed C++ on .net).

In the interim, of course, F# has arrived on the scene, so what I'm trying is to replace from the substrate up with that language; and unlike last time, I'm not trying a Big Bang approach to the translation.

Having renamed all the .c files as .cpp, getting a first building assembly was simple, and then making it refer to an initially empty F# library, no problem; and then setting up an (again, initially empty) unit test assembly depending on both (where vice-tests to match up new and old implementations, and their eventual unit test replacements can accumulate), just as normal.

And then the magic starts to happen. Code that looked like

can become

where

and accessed by replacing the CB_ prefix with cb_severity::; or

plus its other associated functions and header file become just

which is invoked in the old code as the simple drop-in replacement My::FSharp::Module::RandomByte().

And because it's C++/CLI I'm using, where I have 'C' structs, they can become .net structs accessed in the old code with stack semantics.

So far, the most painful bit -- which I've been skirting for the moment -- looks to be the transition from old multi-byte character set 'C' text to Unicode; which is a hit I'd have to take in moving to modern native C++ anyway.

Credit for some of the ease of transition is the Manning Press C++/CLI in Action book, especially for its treatment of mixed mode programming.

No comments :