Sunday, May 18, 2008

Replacing GetKeys with CGEventSourceKeyState

Working through some of the ECMA (JavaScript) interpreter there is a line of code that tests for the escape key being held down in order to abort execution.
if ((::GetAsyncKeyState(VK_ESCAPE) & 0x8000))
{
  :
}
On the Mac the equivalent UI convention is the command-period. Looking at the world through old Carbon programmer eyes I would probably use GetKeys. In the brave new world of Cocoa replete with the Animal Farm bleat of "Cocoa good, Carbon bad" GetKeys is deprecated. I could use it but I'd rather not.

It took a little hunting but the replacement for GetKeys is CGEventSourceKeyState. This function will return if a given key (expressed as a virtual key code) is depressed. Virtual key codes are defined in HIToolbox/Events.h.

So the function drops out quite nicely:
bool IsCommandPeriodPressed()
{
  CGEventSourceStateID eventSource
    = kCGEventSourceStateCombinedSessionState;

  return ::CGEventSourceKeyState(eventSource, kVK_Command)
    && ::CGEventSourceKeyState(eventSource, kVK_ANSI_Period);
}
As a postscript, and for the avoidance of doubt, I have no beef with Cocoa; Cocoa is good and fun as well as being splendid bed-time beverage for children and adults alike. After all I will be using pure Cocoa for the Cello UI - it is just the Cocoa religion thing that is a bit scary.

Pointers to Methods in C++

Working through the ECMA script (JavaScript) interpreter I had some problems compiling a dispatch table that used function pointers. The function pointers were to non-static object methods. My gut reaction was that this must be wrong and a non-standard MSDN/Visual C++ extension. Having worked with C++ for a long time you sort of assume that you know the beast.

I briefly started converting the methods to static methods that passed in the object this in as the first parameter before I stepped back and looked at the problem properly. Function pointers to class methods are a part of the C++ standard. It required a very light touch to get the code to compile correctly. Here is a stolen code sample illustrating pointers to methods. I have modified it slightly to include a typedef for the method pointer.

class Adder
{
int x;
public:
Adder(int in) : x(in) { }
int addx(int y) { return x + y; }

// declare MethodPointer
typedef int (Adder::*MethodPointer)(int);
};

int main()
{
// pointer to the addx method
Adder::MethodPointer new_function
= &Adder::addx;

// Create an Adder instance
Adder foo = Adder(3);

// Call the method
std::cout << (foo.*new_function)(5)
<< std::endl; // prints "8"
return 0;
}
As ever I stand back staggered by what is out there on the internet. Wikipedia has an excellent section on function pointers here - and amazingly there is/was a site http://www.function-pointer.org/ dedicated to explanations of function pointers.

Saturday, May 17, 2008

JavaScript with YACC and LEX

Pressing on with Cello I have started compiling the files that are responsible for the implementation of ECMA script (JavaScript).

Inheriting a code base is a whole set of surprises one of which is that Cello has its own JavaScript interpreter built in. I have some experience of working with SpiderMonkey and have looked at the JavaScript interpreter that is built into WebKit. Implementing a JavaScript interpreter is not a small job - and as with most of the Cello code it is well written and easy to understand. Down the line when everything is working (quite a distant point) I may remove it and instead use the WebKit implementation of JavaScript - I don't have any immediate desire to do this - instead I am very much following the path of least resistance.

At the moment the easiest route is to get it to compile and build. Thus far, working through the code file by file, the code compiles with few errors - typically four or five per file and I have so many clean runs (files with no errors) that I look forward to the ones with problems to fix. The biggest issues I have had (so far) is the absence of _fpclass, but the equivalent fpclassify seems to fit the bill.

For fun and games down the line Cello links against Bumble-Bee Software's Pargen YACC and Lex libraries. The source of these libraries is available (though not included in my Cello sources). Building these seems quite possible.

Thursday, May 15, 2008

File & Related Classes

All of the file & related classes in Cello now compile. So I have a folder full of files 'ticked off'. This part of the project took longer and was more painful than I had imagined - simply because structured storage was an unexpected wrinkle.

POLE, the structured storage library I have deployed will only read structured storage files and won't write them. Looking at the code there seems to have been a reasonable start in getting the writing part of POLE completed but it is not close to being finished. It is something that I am reluctant to undertake. POLE amongst other things was the only non-GPL C/C++ library that I found.

The initial part of the Cello port is to get enough together to open files generated on the PC version and for the code to run to generate the output. So to deal with the issue of writing I have
'stubbed-out' all of Cello that butts onto the structured storage writing and to defer the issue. Though it would be nice to be able to write file in the existing format it is not essential.

Sunday, May 11, 2008

Boost - Core Foundation

I have started plumbing in some of Core Foundation into Cello. Core Foundation objects are owner-counted which leaves you to take care of calling CFRetain and CFRelease at appropriate moments. It is very straight forward except that adding all of the CFRetain and CFRelease clutters your code and (more importantly) can be tricky to get right - it is really easy to forget a release, especially if you consider exceptions. .

Things are eased a little if you have classes that wrap some of the Core Foundation objects - but the thing that really makes the difference is smart pointers.

In a previous life I used Apple Class Suites (ACS) which was a part of MacApp. ACS wrapped most of the underlying OS APIs in good C++ and introduced me to using smart pointers that would automatically call CFRetain and CFRelease. So if you wanted a pointer to a CFString that would automatically call CFRelease when it went out of scope there was an AutoCFString_AC smart pointer.

There is excellent support for smart pointers in boost. Declaring and implementing a smart pointer using intrusive_ptr to do the same as MacApp's AutoCFString_AC takes just three lines of code in boost:
 typedef boost::intrusive_ptr auto_CFString;
inline void intrusive_ptr_add_ref(CFStringRef p)
{ ::CFRetain(p); }
inline void intrusive_ptr_release(CFStringRef p)
{ ::CFRelease(p); }
For those curious in the MacApp annex of history, MacApp though 'dodo dead' has played an important part in the development of many application arameworks, there is an insightful article in wikipedia here .

Saturday, May 10, 2008

Boost - Standing on Giants' Shoulders

I have just downloaded the latest release of Boost and checked it into the CVS repository for the Cello project. Boost starts where STL leaves off - and allows you, as a programmer, to stand on the shoulders of giants.

Boost is huge - checking it in manually once took a friend of mine the best part of a day (CVS won't let you check in recursively). I was fortunate to find the following commands that will recursively add to CVS under OSX in Torsten Curdt's blog.
find . -type d -print | grep -v CVS | xargs -n1 cvs add
find . -type f -print | grep -v CVS | xargs -n1 cvs add
The first command adds the folders from the current directory - the second one adds the files. The standing on Torsten Curdt's shoulders saved a huge amount of time.

The other thing you can do is to just deploy the parts of boost you are using Alex Ott's blog details this.

The MFC version of Cello does not use Boost (the code is reasonably old) - I decided to add it to the project as I need to have some objects that are managed with owner counts and shared_ptr is ready rolled and works well.

Side by Wrinkled Side - VMWare Fusion

I have jut installed VMWare Fusion. This is an almost magical piece of software that means that you can run Windows at the same time as OSX. The s/w has some trickery that means that your XP (and I am sure Vista) Windows can be scarily interleaved with your Mac windows should you want it. The whole mish-mash is a little strange (and behaves a little strangely) but it means that you can run Visual Studio at the same time as XCode - and conveniently do things like copy/paste between them.

Later in the MFC->OSX port when I will be debugging I think that being able to run XCode and Visual Studio 'side by wrinkled side ... like two old kippers in a box' will save a huge amount of time.