The Standard Portable Library (SPL) is a smart pointer based cross platform application framework. SPL is intended to be used instead of STL and has an API that should be familiar to dot net and java programmers. It includes basic types such as Date and DateTime, runtime pointer debugging, collection templates, file IO, networking, database access, threading, and XML parsing. Sourceforge site
There are a few steps to setup and teardown the library. The skeleton below is for command line applications. #include <spl/Debug.h> #include <spl/configuration/CommandLine.h> #include <spl/Log.h> #include <spl/UnitTest.h> #include <spl/net/Socket.h> int main(int argc, char **argv) { try { CommandLine args(argc, (const char **)argv); // // Do stuff. // ASSERT_MEM_FREE(); Socket::ShutdownService(); DEBUG_TEAR_DOWN(true); return (Log::SMessageCount() > 0) ? 20 : 0; } catch ( Exception *ex ) { Log::SWrite( ex ); delete ex; } catch ( OutOfMemoryException mex ) { Log::SWrite( mex ); } return 20; } |
If you're doing windozes development, be sure to include stdafx, windows top level headers, and #import's prior to including SPL headers. SPL uses an abrievated set of windows headers to get networking to work. If you susequently include <windows.h>, you'll get a lot of errors.
There are three templates used to implement smart pointers: RefCountPtr, RefCountPtrCast, and WeakReference. RefCountPointer overloads "->", so variables declared with it can be used just like orginary pointers. The raw pointer is also available if needed.
RefCountPtr<String> pstr(new String("Hi")); // Call String member function. int len = pstr->Length(); // Use the raw String pointer. len = pstr.Get()->Length();
When you need to cast from pointer type to another, define a RefCountPtrCast. For example, let's say that you have a class SuperString that inherits from String. Putting the following definitions in SuperString's header will allow the compliler to automaticly convert the pointers.
#include <spl/String.h> class SuperString : public String { }; typedef RefCountPtrCast<SuperString, String, StringPtr> SuperStringPtr;
To avoid circular references that would prevent the pointer from being released, use WeakReference. Let's say the A creates an instance of B and B holds a back pointer to A. A should use RefCountPtr<B> and B should use WeakReference<A> You'll also need to use an interface to break the compiler dependancy loop.
|
|
specific attributions and modification notices).