Nitrogen, Lisa Lippincott's class library for doing Carbon programming,
<http://nitric.sourceforge.net/>, is wonderfully powerful and easy to
use.
Metrowerk's C++ Mach-O Carbon Nib stationery has a SimpleHello.cp
program that is 117 lines. Here is what an equivalent program looks
like in Nitrogen (40 lines).
#include <Carbon/Carbon.h>
#include "Nitrogen/CFBundle.h"
#include "Nitrogen/IBCarbonRuntime.h"
#include "Nitrogen/OSStatus.h"
#include "Nitrogen/Shared.h"
namespace N = Nitrogen;
const CFStringRef kMainNibFileName = CFSTR("SimpleHello");
const CFStringRef kMenuBarNibName = CFSTR("MenuBar");
const CFStringRef kMyAppNibName = CFSTR("MainWindow");
class MyApp {
private:
CFBundleRef mBundle;
N::Shared<IBNibRef> mNibs;
public:
MyApp();
};
MyApp::MyApp(){
N::Owned<WindowRef> theWindow;
mBundle = N::CFBundleGetMainBundle();
mNibs = N::CreateNibReferenceWithCFBundle(mBundle, kMainNibFileName);
InitCursor();
N::SetMenuBarFromNib(mNibs, kMenuBarNibName);
theWindow = N::CreateWindowFromNib(mNibs, kMyAppNibName);
ShowWindow(theWindow);
theWindow.Release();
}
int main(){
try{
MyApp theApplication;
RunApplicationEventLoop();
}catch(...){
// write error handling code here.
}
return 0;
}
Some advantages of Nitrogen are clear from this simple example:
The Nitrogen wrappers for system calls throw on error, so we don't
need to explicitly check everywhere.
Since the wrappers throw, they can return useful values, making the
intent of the code more clear.
The wrappers for data structures have well defined ownership semantics,
that interact well with exceptions, so data gets cleaned up when it is
supposed to. For example, in the MyApp constructor above, we allocate
a WindowRef object that will be automatically cleaned up if an error
occurs, but will survive if the constructor executes all the way to
the end.
And to you experienced Nitrogen programmers out there, forgive me. I'm
new to Nitrogen, this is the first thing I've actually written and
run, and it was only after I wrote it that I discovered the sample code
in the CVS repository on <http://nitric.sourceforge.net/>.
David Phillip Oster
>> Stay informed about: Example of using Nitrogen, the C++ class library for Carbo..