Monthly Archives: September 2005

Out to lunch

Windows XP Task managerYou know what really bugs me about Windows and Office sometimes? Sometimes a process will just decided to grab all the CPU and go out to lunch for minutes at a time. I don’t know what it thinks it’s doing — re-indexing its data, re-compiling itself, contacting Mars, something like that. Whatever it is, it’s not paying much attention to what I want it to do.

Outlook just did it. I was in the middle of writing an email. Voom, out to lunch. I managed to save it and exit. And the process kept running till I killed it. WTF? And before you ask, yes, my antivirus and firewall are primed, up-to-date and running. Latest releases, latest service packs of everything. This stuff should be stable.

Annoying, that’s what I call it.

C++: Pointers to member functions

Now, C++ programmers can all program C, to varying degrees of competency. And some know that in C you can store an entire function in a pointer, like so:

int DoTheThingThisWay(double count)
{
...
}

int DoTheThingThatWay(double count)
{
...
}

int (*DoTheThing)( double );  // define the variable
DoTheThing = &DoTheThingThatWay; // set the variable
result = DoTheThing( 2.0 );  // call the function, whatever it is now

Giving you a nifty kinda polymorphism. People who have programmed in C, and needed something like this (say for a state machine) have gone “neat!” and remembered it for later.

So, here we are, later. I’m working in C++ and going to do a state machine. Objects and all. I recall that you can do that polymorphic thing with function pointers, and quickly reconstruct the syntax. Now, with objects I want to point at a function on an object rather than a dumb, lying around knowing nothing about objects function. Easy, let’s just whip that up, I’ve proven how pretty the syntax for calling an arbitary function is, now just to call an arbitary member function.

Half an hour of wrestling with the compiler later, an observation is made along the lines of “everyone does this, just look it up on the web”. Top hit on a Google search for C++ “member function” pointer is http://www.goingware.com/tips/member-pointers.html – which is the right page, and you end up with the call:

result = (objectInstance.*DoTheThing)( 2.0 );

which ain’t so pretty. But you need to do that to provide the hidden this pointer, because you’re working with member functions here, not boring old functions. Add some parameters, store the pointers in an array and before you know it you’ve got

result = (objectInstance.*DoTheThing[FunctionIndex])( 2.0, InitalStringSize, HuffalumpFactor );

which is a long way from DoTheThing(2.0), and I’m not sure what you just got for all that effort. Certainly not readability. Try a pointer to a polymorphic type instead (maybe even a functiod?), and flip the pointer to different objects as you go. Much nicer:

class CThingDoer
{
public:
	virtual int operator(double count);
...
};

class CDoTheThingThisWay : public CThingDoer
{
public:
	virtual int operator(double count);
...
} DoTheThingThisWay

class CDoTheThingThatWay : public CThingDoer
{
public:
	virtual int operator(double count);
...
} DoTheThingThatWay

CThingDoer* DoTheThing;  // define the variable
DoTheThing = &DoTheThingThatWay; // set the variable
result = DoTheThing( 2.0 );  // call the function, whatever it is now

Documenting Oracle databases

We all know what a right PITA it is keeping database documentation up to date. You’ll get a column added to the schema, and make a mental note to update the docs with the intricacies of how it works, then you’ll get distracted by something, never get around to it, then six months later you’ll be trying to remember the details.

Fortunately, the newer versions of Oracle have a rather marvellous commenting feature for tables and columns, so you can document it at the same time as you build it.

To put a comment on a table:

COMMENT ON TABLE xyz IS ‘This is a table for recording xyz usage’

Or on a column

COMMENT ON COLUMN xyz.frequency IS ‘Frequency of xyz usage’

Now, this might be of limited use if you couldn’t get the information out easily. Fortunately, you can. Apart from turning on the comments options in schema browsers such as TOAD, you can get a list of table names with their comments like this:

SELECT t.table_name, t.comments
FROM all_tab_comments t
WHERE t.owner = ‘tableowner’
AND t.comments IS NOT NULL
ORDER BY t.table_name

…and the following will generate a nice list of tables, columns, column types and their comments:

SELECT c.table_name, c.column_name, ac.data_type, ac.data_length, ac.nullable, c.comments
FROM all_col_comments c, all_tab_columns ac
WHERE c.owner = ‘tableowner’ and c.column_name = ac.column_name and c.table_name = ac.table_name
ORDER BY c.table_name, ac.column_id

Mind you, the data length field comes out a bit funny for CLOBs and Numbers. Still, with a little imagination you can write up a quick program to format this output nicely in HTML or WikiText or whatever, for your database documentation.