Automatic Storage

September 19th, 2008

One of the things I like about C++ is the ability to have the compiler create code for me that does actual work.

What do I mean? I am thinking about implicit conversions (wrapping) of data types and constructing/destructing data types when they go in/out of scope.

I will focus on the latter in this blog post, show how it can be used with Objective-C and how it can track leaks in C++ code.

Read the rest of this entry »

Objective-C++ Tips

May 22nd, 2008

C++ Objects as Instance Data

Say you create a custom view with arbitrary many tracking rectangles (i.e. dynamically added).

Each time you add a rectangle you get back an identifier for this rectangle which can’t be stored in an NSArray as-is since it is of the primitive type NSTrackingRectTag (an integer).

If you use Objective-C++ then you can use a std::vector<NSTrackingRectTag> to avoid having to box/unbox your identifiers but if you have tried to put non-POD in the interface declaration of your Objective-C class you have probably seen that gcc does not like that.

Well, starting with 10.4 (so actually, some time ago) Apple added a switch to gcc which allows C++ objects as part of the instance data, and it will call both constructor and destructor for your C++ objects when allocating/deallocating the Objective-C object.

The flag you need to set is -fobjc-call-cxx-cdtors.

C++ Objects as Method Arguments

Occasionally it is convenient to pass a C++ object to an Objective-C method. For example I have an NSString initializer that takes a std::string as argument.

This works as long as you pass the object as a reference (i.e. pass a pointer), but you can use the “reference of” operator in the method signature rather than at the call-site. By using a const reference it will work for temporary/implicit objects.

So with the following method:

+ (NSString*)stringWithCxxString:(std::string const&)cxxString
{
   return [[[NSString alloc] initWithBytes:cxxString.data()
                                    length:cxxString.size()
                                  encoding:NSUTF8StringEncoding] autorelease];
}

We can have code like this:

std::string dir  = get_some_dir();
std::string file = get_some_file();

NSString* str    = [NSString stringWithCxxString:dir + file];

Message Catalogs on Darwin

April 20th, 2006

I wanted to localize a shell command to give danish output and decided to look into the message catalog functions described in/by XPG4.

Read the rest of this entry »

Clipboard access from shell (utf-8)

October 11th, 2005

Two very nice shell commands that Apple has given us are the pbcopy and pbpaste commands. These allow stdin to go to the clipboard and the clipboard to be written to stdout.

Unfortunately the commands seem to use a combination of MacRoman and question marks for non-ASCII characters, which often makes them unusable for me, since I work with non-ASCII characters.

So today I decided to write a replacement for the two commands (yes, I did also file an enhancement report). You can download them here.

There's just one source, it compiles to a command which works as pbcopy, when called under that name, otherwise pbpaste.

What I've done is place the command in ~/bin and added a symbolic link from pbpaste to pbcopy, like this:

  ln -s pbcopy ~/bin/pbpaste

And in addition ensured that my PATH contains ~/bin before anything else, i.e. by placing the following in my ~/.bash_profile (well, actually ~/.zshrc):

  export PATH="$HOME/bin:/opt/local/bin:$PATH:/Developer/Tools"

The source is included in the archive, and it's very simple. No usage instructions etc., and it links with the Application Kit, since NSPasteboard is under that and not Foundation Kit.

Determinate progress indicator for uncompressing a tar + bzip2 archive

October 2nd, 2005

I added a software updater to my application, and one of the steps was uncompressing the archive (after downloading it). Since the archive size is a few megabytes, and I use bzip2 as compression, this step takes a few seconds, and thus I want to show a determinate progress indicator while it is working on this.

Read the rest of this entry »

Fighting comment spam

September 25th, 2005

Update 2007-07-17: Since I installed the JS challenge almost two years ago it has blocked 83,837 POSTs. Roughly a dozen spam POSTs did defeat the challenge. Looking at the access log for these they do seem to be from actual humans (based on the initial hit having a google referrer, all resources (CSS and images) being fetched, and the delay from last GET to the POST), but it could also be a cleverly scripted browser (not sure of the “economy” of either though).

Recently I've received a lot of comment spam, which is fake comments posted to a blog or wiki (for me once every hour) with the purpose of increasing the page rank for a website.

Looking at the comment spam I have received, I see that more than 90% of the IP addresses are unique (infected Windows machines used as proxies?) so for the challenge I decided to run sha-1 on the visitors IP (plus a constant) and ask for that back when he submits the form.

Read the rest of this entry »

Deciphering an NSEvent

September 24th, 2005

Since I need to handle key equivalents myself, I need to mimic how the system does it.

After having spent some time investigating this, it seems to be impossible to do with the info provided by NSEvent, but I ended up creating a heuristic which works fairly well (but isn't perfect).

Read the rest of this entry »

Shell calculator

July 26th, 2005

They say that old habits die hard, and the following is probably an example of such.

Read the rest of this entry »

Metadata workarounds on Tiger

July 20th, 2005

Tiger introduced functions for file metadata in the BSD layer (see man setxattr for more info).

File metadata is something most programmers have wet dreams about, and naturally I started to use these new functions in TextMate 1.1b12. After this I got several reports of kernel panics when saving files to AFP mounted volumes (personal file sharing).

Read the rest of this entry »

Oniguruma C++ wrapper

May 19th, 2005

I've recently partially switched to the Oniguruma regular expression library.

Since I also use regular expressions in my source code I've created a simple C++ wrapper which makes the API more friendly to my tasks. I generally work with iterators, and there are 4 tasks I often do.

Read the rest of this entry »

String List Value Transformer

December 10th, 2004

It's been a while since I last wrote something — the reason for this is that I've been busy working on TextMate, but now that I have a free moment, let me tell you about this general tag value to string transformer I use when I bind radio or popup buttons to my user defaults (to get a string stored in the user defaults instead of an integer).

Read the rest of this entry »

Iterating an array

September 13th, 2004

Over at CocoaDev they are discussing how to efficiently iterate an NSArray.

I would really never ever think that the way you iterate an NSArray has any impact on the perceived performance of your program (granted you do not change the time complexity or do other stupid things), I would however think that it affects the perceived complexity of the source.

Read the rest of this entry »

Increment build number for deployment builds

September 12th, 2004

If we want to distribute "deployment builds" regularly (e.g. to our betatesters) it pays to automate the process. A deployment build can be done from the command line using:

xcodebuild clean
xcodebuild -buildstyle Deployment

But if we do this, we probably also want to bump the version number, or at least the build number, so that we can distinguish between the different builds.

Read the rest of this entry »

Disabled image buttons

September 12th, 2004

Some time ago Apple introduced these small whitish image buttons mostly for use with table views (for adding and removing items), an example is shown here:

Row of image buttons

The buttons have three states, and Apple use images for each of these three states since the border is part of the image, so if NSControl renders it pressed or disabled, it will affect the border. Unfortunately Interface Builder only lets us set a normal image and an alternate image (which can be used as the pressed-image by setting "behavior" to "momentary change").

Read the rest of this entry »

Using OpenSSL for license keys

September 5th, 2004

In this article I will give an example of how one can generate and verify license keys (also known as serial numbers) using the tools included with Mac OS X, and in a way which should make it difficult for the cracker to generate his own fake license key(s).

Read the rest of this entry »