/*
 *  objcpp.h
 *  ========
 *
 *  Created by John Holdsworth on 01/04/2009.
 *  Copyright 2009 John Holdsworth.
 *
 *  V1.0 - 09/04/2009.
 *
 *  C++ classes to wrap XCode classes for operator overload of
 *  useful operations such as access to NSArrays and NSDictionary
 *  subscript or NSString operators such as + for concatenation.
 *
 *  This works as the Apple Objective-C compiler supports source
 *  which mixes C++ with objective C. To enable this: for each
 *  source file which will include/import this header file, select
 *  it in Xcode and open it's "Info". To enable mixed compilation,
 *  for the file's "File Type" select: "sourcecode.cpp.objcpp".
 *
 *  For bugs or ommisions please email objcpp@johnholdsworth.com
 *
 *  This software is shareware. You may use but not sell this software
 *  nor can you remove this notice from this source if you redistrubute.
 *  If you find it useful please send a donation via paypal to account
 *  objcpp@johnholdsworth.com. It would be much appreciated. Thanks.
 *
 *  Home page for updates and docs: http://objcpp.johnholdsworth.com
 *
 */

static int _retained, _constructed;

/*==================================== Overrides =========================================*/

/**
 For detailed debugging
 */

#ifndef OOTrace
#define OOTrace // NSLog 
#endif

/**
 Function to log warning messages
 */

#ifndef OOWarn
#define OOWarn NSLog
#endif

/*===================================== Basic reference managment ========================*/

template <typename RTYPE>
class OORef;

/**
 A class managing basic reference counting using retain/release mechansim for use in
 instance variables which will not allow constructors or destructors. Use the & operator
 to get a pointer with "autorelease" scope for use in the rest of your program.
 
 To free the reference either assign nil using the "=" operator or call use the ~ operator
 which returns a transient reference. Use &~var to free the reference with autorelrease scope.
 */

template <typename RTYPE>
class OORefBase {
	RTYPE reference;
protected:
	inline RTYPE set( RTYPE newValue ) {
		if ( newValue ) {
			[newValue retain];
			OOTrace( @"0x%08x %@#%d: %@\n", this, @"RETAIN", [newValue retainCount], newValue );
			_retained++;
		}
		if ( reference ) {
			OOTrace( @"0x%08x %@#%d: %@\n", this, @"RELEASE", [reference retainCount], reference );
			[reference release];
			_retained--;
		}
		return reference = newValue;
	}
	inline RTYPE init( RTYPE initialValue ) {
		OOTrace( @"0x%08x %@: %@\n", this, @"INIT", initialValue );
		reference = nil;
		return set( initialValue );
	}
	inline void dealloc() {
		set( (RTYPE)nil );
	}
	inline RTYPE autoget() {
		return [[get() retain] autorelease];
	}
public:
	inline RTYPE ref() const {
		return reference;
	}
	inline RTYPE get() {
		return reference;
	}
	inline RTYPE alloc() {
		if ( !reference ) {
			OOTrace( @"0x%08x %@\n", this, @"ALLOC" );
			[set( [[typeof *(RTYPE)0 alloc] init] ) release];
		}
		return reference;
	}

	inline OORefBase & operator = ( RTYPE val ) { set( val ); return *this; }
	inline OORefBase & operator = ( OORefBase &val ) { return operator = ( val.ref() );	}
	inline OORefBase & operator = ( OORef<RTYPE> &val ) { return operator = ( val.ref() ); }

	inline OORefBase & operator <<= ( RTYPE val ) { [set( [val mutableCopy] ) release]; return *this; }
	inline OORefBase & operator <<= ( OORefBase &val ) { return operator = ( val.ref() );	}
	inline OORefBase & operator <<= ( OORef<RTYPE> &val ) { return operator = ( val.ref() ); }

	inline id operator [] ( NSString *key ) { return [reference valueForKey:key]; }

	inline operator RTYPE () { return get(); }
	inline RTYPE operator * () { return get(); }
	inline RTYPE operator & () { return autoget(); }
	inline OORef<RTYPE> operator ~ ();
};

/**
 Adds constructors and destructors to OORefBase for use in automatic variables.
 Assignment copies only the reference. Use the "<<=" operator to take a copy.
 */

template <typename RTYPE>
class OORef : public OORefBase<RTYPE> {
public:
	inline OORef() { init( (RTYPE)nil ); };
	inline OORef( RTYPE obj ) { init( obj ); }
	inline OORef( const OORef &val ) { init( val.ref() ); }
	inline OORef( const OORefBase<RTYPE> &val ) { init( val.ref() ); }

	inline OORef & operator = ( RTYPE val ) { set( val ); return *this; }
	inline OORef & operator = ( OORef &val ) { return operator = ( val.ref() ); }
	inline OORef & operator = ( OORefBase<RTYPE> &val ) { return operator = ( val.ref() ); }

	inline ~OORef() { this->dealloc(); }
};

template <typename RTYPE>
inline OORef<RTYPE> OORefBase<RTYPE>::operator ~ () {
	OORef<RTYPE> save = get();
	dealloc();
	return save;
}

/*================================== Array classes ===================================*/

template <typename ETYPE> class OOArray;
template <typename ETYPE> class OOArrayRef;
template <typename ETYPE> class OODictBase;
template <typename ETYPE> class OODictRef;
#define EETYPE ETYPE

/**
 NSMutableArray wrapper allowing subscript syntax by index and various other operators.
 */

template <typename ETYPE>
class OOArrayBase : public OORefBase<NSMutableArray *> {
public:
	inline OOArrayBase & operator = ( NSMutableArray *val ) { set( val ); return *this; }
	inline OOArrayBase & operator = ( OOArrayBase &val ) { return operator = ( val.ref() ); }
	inline OOArrayBase & operator = ( OOArray<ETYPE> &val ) { return operator = ( val.ref() ); }
	inline OOArrayBase & operator <<= ( NSMutableArray *val ) {
		[this->alloc() removeAllObjects];
		[get() addObjectsFromArray:val];
		return *this;
	}

	inline BOOL operator == ( NSMutableArray *val ) {
		return [get() isEqualToArray:val];
	}
	inline BOOL operator != ( NSMutableArray *val ) {
		return !operator == ( val );
	}

	inline OOArrayBase & operator += ( ETYPE val ) {
		[alloc() addObject:val];
		return *this;
	}
	inline OOArrayBase & operator += ( OOArray<ETYPE> &val ) {
		[alloc() addObjectsFromArray:val];
		return *this;
	}
	inline OOArrayBase & operator -= ( int sub ) {
		[get() removeObjectAtIndex:sub < 0 ? [get() count]+sub : sub];
		return *this;
	}
	inline OOArrayBase & operator -= ( ETYPE val ) {
		[get() removeObject:val];
		return *this;
	}
	inline OOArrayBase & operator -= ( OOArray<ETYPE> &val ) {
#if 0
		for ( id obj in *val )
			[alloc() removeObject:obj];
#else
		for ( int i=0 ; i<[*val count] ; i++ )
			[alloc() removeObject:val[i]];
#endif
		return *this;
	}
	
	inline OOArray<ETYPE> operator - ();
	inline OOArray<ETYPE> operator + ( ETYPE val );
	inline OOArray<ETYPE> operator - ( ETYPE val );
	inline OOArray<ETYPE> operator + ( OOArray<ETYPE> val );
	inline OOArray<ETYPE> operator - ( OOArray<ETYPE> val );

	inline OOArrayRef<ETYPE> operator [] ( int sub ) {
		return OOArrayRef<ETYPE>( this, sub );
	}
	inline ETYPE operator -- () {
		return ~(*this)[0];
	}
	inline ETYPE operator -- ( int ) {
		return ~(*this)[-1];
	}
};

/**
 OOArrayBase subclass for use in automatic variables. Capacity can be specified in the constructor.
 */

template <typename ETYPE>
class OOArray : public OOArrayBase<ETYPE> {
public:
	inline OOArray() { this->init( nil ); }
	inline OOArray( NSMutableArray *arr ) { this->init( arr ); }
	inline OOArray( NSUInteger capacity ) { [this->init( [[NSMutableArray alloc] initWithCapacity:capacity] ) release]; }
	inline OOArray( const OOArray &val ) { this->init( val.ref() ); }
	inline OOArray( const OOArrayBase<ETYPE> &val ) { this->init( val.ref() ); }
	
	inline OOArray & operator = ( NSMutableArray *val ) { this->set( val ); return *this; }
	inline OOArray & operator = ( OOArray &val ) { return operator = ( val.ref() ); }
	inline OOArray & operator = ( OOArrayBase<ETYPE> &val ) { return operator = ( val.ref() ); }
	
	inline ~OOArray() { this->dealloc(); }
};

template <typename ETYPE>
inline OOArray<ETYPE> OOArrayBase<ETYPE>::operator - () {
	return [[this reverseObjectEnumerator] allObjects];
}
template <typename ETYPE>
inline OOArray<ETYPE> OOArrayBase<ETYPE>::operator + ( ETYPE val ) {
	OOArray<ETYPE> arr; arr <<= *this; arr += val; return arr;
}
template <typename ETYPE>
inline OOArray<ETYPE> OOArrayBase<ETYPE>::operator - ( ETYPE val ) { 
	OOArray<ETYPE> arr; arr <<= *this; arr -= val; return arr;
}
template <typename ETYPE>
inline OOArray<ETYPE> OOArrayBase<ETYPE>::operator + ( OOArray<ETYPE> val ) {
	OOArray<ETYPE> arr; arr <<= *this; arr += val; return arr;
}
template <typename ETYPE>
inline OOArray<ETYPE> OOArrayBase<ETYPE>::operator - ( OOArray<ETYPE> val ) {
	OOArray<ETYPE> arr; arr <<= *this; arr -= val; return arr;
}

/**
 Internal class to represent a subscript operation in an expression so it can
 be assigned to. You can also use the ~val[i] operation to remove the value
 from the array and return it with either expression or autorelease scope.
 */

template <typename ETYPE>
class OOArrayRef {
	friend class OOArrayBase<ETYPE>;
	friend class OODictBase<ETYPE>;
	friend class OODictRef<ETYPE>;
	OOArrayBase<ETYPE> *array;
	OOArrayRef<NSMutableArray *> *aref;
	OODictRef<NSMutableArray *> *dref;
	int idx;
	inline NSMutableArray *parent( BOOL allocate );
protected:
	inline OOArrayRef( OOArrayBase<ETYPE> *ref, int sub ) {
		array = ref;
		idx = sub < 0 ? [parent( NO ) count]+sub : sub;
	}
	inline OOArrayRef( OOArrayRef<NSMutableArray *> *ref, int sub ) {
		array = NULL; aref = ref; dref = NULL;
		idx = sub < 0 ? [parent( NO ) count]+sub : sub;
	}
	inline OOArrayRef( OODictRef<NSMutableArray *> *ref, int sub ) {
		array = NULL; aref = NULL; dref = ref;
		idx = sub < 0 ? [parent( NO ) count]+sub : sub;
	}
public:
	inline id get( BOOL warn ) {
		NSMutableArray *arr = parent( NO );
		id ret = nil;
		if ( idx < 0 )
			OOWarn( @"0x%08x Excess negative index (%d) beyond size of array (%d)", this, idx-[arr count], [arr count] );
		else if ( idx < [arr count] )
			ret = [arr objectAtIndex:idx];
		else if ( warn )
			OOWarn( @"0x%08x Array reference (%d) beyond end of array (%d)", this, idx, [arr count] );
		return ret;
	}
	inline id get() {
		return get( YES );
	}
	inline id autoget() {
		return [[get() retain] autorelease];
	}

	inline operator id () { return get(); }
	inline operator ETYPE () { return get(); }
	inline ETYPE operator * () { return get(); }
	inline id operator & () { return autoget(); }

	inline id set ( id val ) {
		NSMutableArray *arr = parent( YES );
		NSUInteger count = [arr count];
		if ( val == nil ) val = (id)kCFNull;

		if ( idx < count )
			[arr replaceObjectAtIndex:idx withObject:val];
		else {
			while ( count++ < idx )
				[arr addObject:(id)kCFNull];
			[arr addObject:val];
		}
		return val;
	}
	inline OOArrayRef<ETYPE> & operator = ( ETYPE val ) {
		set( val );
		return *this;
	}

	inline OOArrayRef & operator += ( ETYPE val ) {	**this += val; return *this; }
	inline OOArrayRef & operator -= ( ETYPE val ) {	**this -= val; return *this; }
	inline OOArrayRef & operator *= ( ETYPE val ) {	**this *= val; return *this; }
	inline OOArrayRef & operator /= ( ETYPE val ) {	**this /= val; return *this; }
	inline BOOL operator == ( ETYPE val ) {	return **this == val; }
	inline BOOL operator != ( ETYPE val ) {	return **this != val; }
	inline ETYPE operator + ( ETYPE val ) {	return **this + val; }
	inline ETYPE operator - ( ETYPE val ) {	return **this - val; }
	inline ETYPE operator * ( ETYPE val ) {	return **this * val; }
	inline ETYPE operator / ( ETYPE val ) {	return **this / val; }

	inline OOArrayRef<EETYPE> operator [] ( int sub ) {
		return OOArrayRef<EETYPE>( (OOArrayRef<NSMutableArray *> *)this, sub );
	}
	inline OODictRef<EETYPE> operator [] ( id sub );
	inline id alloc( Class c ) {
		if ( !get( NO ) )
			[set( [[c alloc] init] ) release];
		return *this;
	}
	inline ETYPE operator ~ () {
		id obj = get();
		int rc = [obj retainCount];
		ETYPE save = obj;
		if ( rc == [obj retainCount] )
			autoget();
		[parent( NO ) removeObjectAtIndex:idx];
		return save;
	}
};

/*==================================== Dictionary classes ===============================*/

template <typename ETYPE>
class OODict;

/**
 NSMutableDictionary wrapper for use in instance variables which allows subscripting
 by the key value. Use &~dict[@"key"] to remove the entry and return it with expression
 or autorelease scope.
 */

template <typename ETYPE>
class OODictBase : public OORefBase<NSMutableDictionary *> {

public:
	inline OODictBase & operator = ( NSMutableDictionary *val ) { this->set( val );	return *this; }
	inline OODictBase & operator = ( OODictBase &val ) { return operator = ( val.ref() ); }
	inline OODictBase & operator = ( OODict<ETYPE> &val ) {	return operator = ( val.ref() ); }
	inline OODictBase & operator <<= ( NSMutableDictionary *val ) {
		[this->alloc() removeAllObjects];
		[get() addEntriesFromDictionary:val];
		return *this;
	}
	
	inline BOOL operator == ( NSDictionary *val ) {	return [get() isEqualToDictionary:val]; }
	inline BOOL operator != ( NSDictionary *val ) {	return !operator == ( val ); }

	inline OODictBase & operator += ( NSDictionary *val ) { [alloc() addEntriesFromDictionary:val];	return *this; }
	inline OODictBase & operator -= ( id val ) { [get() removeObjectForKey:val]; return *this; }
	inline OODictBase & operator -= ( NSArray *val ) { [get() removeObjectsForKeys:val]; return *this; }

	inline OODictRef<ETYPE> operator [] ( id sub ) { return OODictRef<ETYPE>( this, sub ); }
	inline OODictBase & operator ! () { [alloc() removeAllObjects]; return *this; }
};

/**
 OODictBase subclass for use in automatic variables. Make sue any dictionary object you
 assign or construct with is mutable if you want to be able to add entries.
 */

template <typename ETYPE>
class OODict : public OODictBase<ETYPE> {
public:
	inline OODict() { this->init( nil ); }
	inline OODict( NSMutableDictionary *dict ) { this->init( dict ); }
	inline OODict( NSUInteger capacity ) { [this->init( [[NSMutableDictionary alloc] initWithCapacity:capacity] ) release]; }
	inline OODict( const OODict &val ) { init( val.ref() ); }
	inline OODict( const OODictBase<ETYPE> &val ) { init( val.ref() ); }

	inline OODict & operator = ( NSMutableDictionary *val ) { this->set( val ); return *this; }
	inline OODict & operator = ( OODict &val ) { return operator = ( val.ref ); }
	inline OODict & operator = ( OODictBase<ETYPE> &val ) { return operator = ( val.ref ); }

	inline ~OODict() { this->dealloc(); }
};

/**
 Internal class representing subscript by key in an expression so it 
 can be assigned to. Subscripts can be applied recursively "viviifying"
 the required Dictionaries (or arrays) at each node as required.
 */

template <typename ETYPE>
class OODictRef {
	friend class OODictBase<ETYPE>;
	friend class OOArrayBase<ETYPE>;
	friend class OOArrayRef<ETYPE>;
	id key;
	OODictBase<ETYPE> *dict;
	OOArrayRef<NSMutableDictionary *> *aref;
	OODictRef<NSMutableDictionary *> *dref;
	inline NSMutableDictionary *parent( BOOL allocate ) {
		return allocate ? 
		dict ? dict->alloc() : aref ? aref->alloc( [NSMutableDictionary class] ) : dref->alloc( [NSMutableDictionary class] ) :
		dict ? dict->get() : aref ? aref->get( NO ) : dref->get();
	}
protected:
	inline OODictRef( OODictBase<ETYPE> *ref, id sub ) {
		dict = ref; key = sub;
	}
	inline OODictRef( OOArrayRef<NSMutableDictionary *> *ref, id sub ) {
		dict = NULL; aref = ref; dref = NULL; key = sub;
	}
	inline OODictRef( OODictRef<NSMutableDictionary *> *ref, id sub ) {
		dict = NULL; aref = NULL; dref = ref; key = sub;
	}
public:
	inline id get() {
		return [parent( NO ) objectForKey:key];
	}
	inline id autoget() {
		return [[get() retain] autorelease];
	}

	inline operator id () {	return get(); }
	inline operator ETYPE () { return get(); }
	inline ETYPE operator * () { return get(); }
	inline id operator & () { return autoget(); }
	
	inline id set( id val ) {
		if ( val == nil ) val = (id)kCFNull;
		[parent( YES ) setValue:val forKey:key];
		return val;
	}
	inline OODictRef & operator = ( ETYPE val ) {
		set( val );
		return *this;
	}

	inline OODictRef & operator += ( ETYPE val ) {	**this += val; return *this; }
	inline OODictRef & operator -= ( ETYPE val ) {	**this -= val; return *this; }
	inline OODictRef & operator *= ( ETYPE val ) {	**this *= val; return *this; }
	inline OODictRef & operator /= ( ETYPE val ) {	**this /= val; return *this; }
	inline BOOL operator == ( ETYPE val ) {	return **this == val; }
	inline BOOL operator != ( ETYPE val ) {	return **this != val; }
	inline ETYPE operator + ( ETYPE val ) {	return **this + val; }
	inline ETYPE operator - ( ETYPE val ) {	return **this - val; }
	inline ETYPE operator * ( ETYPE val ) {	return **this * val; }
	inline ETYPE operator / ( ETYPE val ) {	return **this / val; }

	inline OOArrayRef<EETYPE> operator [] ( int sub ) {
		return OOArrayRef<EETYPE>( (OODictRef<NSMutableArray *> *)this, sub );
	}
	inline OODictRef<EETYPE> operator [] ( id sub ) {
		return OODictRef<EETYPE>( (OODictRef<NSMutableDictionary *> *)this, sub );
	}
	inline ETYPE operator ~ () {
		id obj = get();
		int rc = [obj retainCount];
		ETYPE save = obj;
		if ( rc == [obj retainCount] )
			autoget();
		[parent( NO ) removeObjectForKey:key];
		return save;
	}
	inline id alloc( Class c ) {
		if ( !get() )
			[set( [[c alloc] init] ) release];
		return *this;
	}
};

template <typename ETYPE>
inline NSMutableArray *OOArrayRef<ETYPE>::parent( BOOL allocate ) {
	return allocate ? 
	array ? array->alloc() : aref ? aref->alloc( [NSMutableArray class] ) : dref->alloc( [NSMutableArray class] ) :
	array ? array->get() : aref ? aref->get( NO ) : dref->get();
}

template <typename ETYPE>
inline OODictRef<EETYPE> OOArrayRef<ETYPE>::operator [] ( id sub ) {
	return OODictRef<EETYPE>( (OOArrayRef<NSMutableDictionary *> *)this, sub );
}

/*=================================== String classes =======================================*/

/**
 Internal class representing a susbscript operation into a string to access or assign
 to individual characters.
 */

class OOStringRef {
	friend class OOStringBase;
	NSMutableString *str;
	int idx;
	inline OOStringRef( NSMutableString *ref, int sub ) {
		str = ref;
		idx = sub < 0 ? [str length]+sub : sub;
	}
public:
	inline operator unichar () {
		return [str characterAtIndex:idx];
	}
	inline OOStringRef & operator = ( unichar val ) {
		[str replaceCharactersInRange:NSMakeRange( idx, 1 )
						   withString:[NSString stringWithFormat:@"%c", val]];
		return *this;
	}
};

/**
 Internal class representing subscript by string which performs a search into the
 string. Assigning to for example str[@"BARRY"] = @"BAZ" will change all occurances
 of "BARRY" in the string to "BAZ".
 */

class OOStringSub {
	friend class OOStringBase;
	NSMutableString *str;
	NSString *idx;
	inline OOStringSub( NSMutableString *ref,  NSString *sub ) {
		str = ref;
		idx = sub;
	}
public:
	inline operator NSRange () {
		return [str rangeOfString:idx];
	}
	inline operator NSUInteger () {
		return [str rangeOfString:idx].location;
	}
	inline OOStringSub & operator = ( NSString *replacement ) {
		[str setString:[str stringByReplacingOccurrencesOfString:str withString:replacement]];
		return *this;
	}
};

class OOString;
class OOStringArray;

/**
 Yet another string class wrapping around NSString with all the usual operators including subscript.
 */

class OOStringBase : public OORefBase<NSMutableString *> {
public:
	inline OOStringBase &operator = ( NSMutableString *val ) { this->set( val ); return *this;	}
	inline OOStringBase &operator = ( OOStringBase &val ) { return operator = ( val.ref() ); }
	inline OOStringBase &operator = ( OOString &val );
	
	inline OOStringBase & operator <<= ( NSMutableString *val ) {
		[this->alloc() setString:val];
		return *this;
	}
	
	inline BOOL operator == ( NSString *str ) {	return [get() isEqualToString:str]; }
	inline BOOL operator != ( NSString *str ) { return !operator == ( str ); }
	inline BOOL operator <  ( NSString *str ) { return [get() compare:str] == NSOrderedAscending; }
	inline BOOL operator >= ( NSString *str ) {	return !operator < ( str ); }
	inline BOOL operator >  ( NSString *str ) { return [get() compare:str] == NSOrderedDescending; }
	inline BOOL operator <= ( NSString *str ) {	return !operator > ( str ); }
	
	inline OOString operator + ( int val );
	inline OOString operator + ( double val );
	inline OOString operator + ( NSString *str );
	inline OOString operator - ( NSString *str );
	inline OOString operator * ( int count );
	inline OOStringArray operator / ( NSString *str );
	inline operator const char * () { return [get() UTF8String]; }

	inline OOStringBase & operator += ( NSString *str ) { [alloc() appendString:str]; return *this;	}
	inline OOStringBase & operator -= ( NSString *str ) { alloc(); (*this)[str] = @""; return *this; }
	inline OOStringBase & operator *= ( NSUInteger count ) {
		NSString *str = [[get() copy] autorelease];
		for ( int i=1 ; i<count ; i++ )
			*this += str;
		return *this;
	}
	
	inline OOStringRef operator [] ( int sub ) { return OOStringRef( get(), sub ); }
	inline OOStringSub operator [] ( NSString *sub ) { return OOStringSub( get(), sub ); }
};

/**
 OOStringBase subclass with constructors and destructors to managme reference counting
 for use in automatic variables.
 */

class OOString : public OOStringBase {
public:
	inline OOString() { init( nil ); alloc(); }
	inline OOString( NSMutableString *str ) { init( str ); }
	inline OOString( const OOString &val ) { init( val.ref() );	}
	inline OOString( const OOStringBase &val ) { init( val.ref() );	}
	inline OOString( NSUInteger capacity ) {
		[this->init( [[NSMutableString alloc] initWithCapacity:capacity] ) release];
	}
	inline OOString( const char *str ) { init( [NSString stringWithUTF8String:str] ); }
	
	inline OOString & operator = ( NSMutableString *val ) { set( val ); return *this; }
	inline OOString & operator = ( OOString &val ) { set( val.ref() );	return *this; }
	inline OOString & operator = ( OOStringBase &val ) { set( val.ref() );	return *this; }
	
	inline ~OOString() { this->dealloc(); }
};

inline OOStringBase &OOStringBase::operator = ( OOString &val ) {
	return operator = ( val.ref() );
}

inline OOString OOStringBase::operator + ( int  val ) {
	return *this + [NSString stringWithFormat:@"%d", val];
}

inline OOString OOStringBase::operator + ( double val ) {
	return *this + [NSString stringWithFormat:@"%f", val];
}

inline OOString OOStringBase::operator + ( NSString *str ) {
	return [get() stringByAppendingString:str];
}

inline OOString OOStringBase::operator - ( NSString *str ) {
	return [get() stringByReplacingOccurrencesOfString:str withString:@""];
}

inline OOString OOStringBase::operator * ( int count ) {
	OOString out;
	for ( int i=0 ; i<count ; i++ )
		out += get();
	return out;
}

class OOStringArray;

/**
 NSMutableArray wrapper representing The NSString objects contained as OOString C++ class objects.
 */

class OOStringArrayBase : public OOArrayBase<OOString> {
public:
	inline OOStringArray operator + ();
	inline OOStringArrayBase &operator = ( NSMutableArray *val ) { set( val ); return *this; }
	inline OOStringArrayBase &operator = ( OOStringArray &val );
	inline OOStringArrayBase &operator = ( OOStringArrayBase &val ) {  return operator = ( val.ref() ); }
};

/**
 OOStringArrayBase subclass for use in automatic variables. Can be initialised with a 
 string of the form: "ONE TWO THREE" which will create an array containing strings
 "ONE", "TWO", "THREE".
 */

class OOStringArray : public OOStringArrayBase {
public:
	inline OOStringArray() { init( nil ); }
	inline OOStringArray( NSMutableArray *val ) { init( val ); }
	inline OOStringArray( OOString &val ) { init( val / @" " ); }
	inline OOStringArray( NSString *val ) { init( (OOString)val / @" " ); }
	inline OOStringArray( const OOStringArray &val ) { init( val.ref() ); }
	inline OOStringArray( const OOStringArrayBase &val ) { init( val.ref() ); }
	inline OOStringArray &operator = ( NSMutableArray *val ) { set( val ); return *this; }
	inline OOStringArray &operator = ( OOStringArray &val ) { return operator = ( val.ref() ); }
	inline OOStringArray &operator = ( OOStringArrayBase &val ) { return operator = ( val.ref() ); }
	inline ~OOStringArray() { this->dealloc(); }
};

inline OOStringArray OOStringArrayBase::operator + () {
	return [get() sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}

inline OOStringArrayBase &OOStringArrayBase::operator = ( OOStringArray &val ) {
	return operator = ( val.ref() );
}

inline OOStringArray OOStringBase::operator / ( NSString *str ) {
	return [get() componentsSeparatedByString:str];
}

class OOStringDict;

/**
 NSMutableDictionary wrapper representing The NSString entries as OOString C++ class objects
 */

class OOStringDictBase : public OODictBase<OOString> {
public:
	inline OOStringDictBase &operator = ( NSMutableDictionary *val ) { set( val ); return *this; }
	inline OOStringDictBase &operator = ( OOStringDictBase &val ) { set( val.ref() ); return *this; }
	inline OOStringDictBase &operator = ( OOStringDict &val );
};

/**
 OOStringDictBase subclass for use in instance variables.
 */

class OOStringDict : public OOStringDictBase {
public:
	inline OOStringDict() { init( nil ); }
	inline OOStringDict( NSMutableDictionary *val ) { init( val ); }
	inline OOStringDict( const OOStringDict &val ) { init( val.ref() ); }
	inline OOStringDict( const OOStringDictBase &val ) { init( val.ref() ); }
	inline OOStringDict &operator = ( NSMutableDictionary *val ) { set( val ); return *this; }
	inline OOStringDict &operator = ( OOStringDict &val ) { set( val.ref() ); return *this; }
	inline OOStringDict &operator = ( OOStringDictBase &val ) { set( val.ref() ); return *this; }
	inline ~OOStringDict() { this->dealloc(); }
};

inline OOStringDictBase &OOStringDictBase::operator = ( OOStringDict &val ) { 
	set( val.ref() ); return *this;
}

/*====================================== Number classes =================================*/

/**
 Internal class representing subscripted access to OONumArray class.
 */

class OONumArrayRef : public OOArrayRef<NSNumber *> {
	friend class OONumArrayBase;
	inline OONumArrayRef( OOArrayBase<NSNumber *> *ref, int sub ) : OOArrayRef<NSNumber *>( ref, sub ) { }
public:
	inline operator double() {
		return [get() doubleValue];
	}
	inline OONumArrayRef & operator = ( double val ) {
		NSNumber *obj = [[NSNumber alloc] initWithDouble:val];
		set( obj );
		[obj release];
		return *this;
	}
	inline OONumArrayRef & operator += ( double val ) {	return *this = *this + val; }
	inline OONumArrayRef & operator -= ( double val ) { return *this = *this - val;	}
	inline OONumArrayRef & operator *= ( double val ) { return *this = *this * val; }
	inline OONumArrayRef & operator /= ( double val ) { return *this = *this / val; }
	inline double operator ~ () {
		return [OOArrayRef<NSNumber *>::operator ~ () doubleValue];
	}
};

class OONumArray;

/**
 An NSMutableArray wrapper for numbers. Use the more efficient OOVector<TYPE> class in preference.
 */

class OONumArrayBase : public OOArrayBase<NSNumber *> {
public:
	inline OONumArrayBase &operator = ( OONumArrayBase &val ) { set( val.ref() ); return *this; }
	inline OONumArrayBase &operator = ( OONumArray &val );
	inline OONumArrayRef operator [] ( int sub ) {
		return OONumArrayRef( this, sub );
	}
};

/**
 An NSMutableArray wrapper for numbers. Use the more efficient OOVector<TYPE> class in preference.
 */

class OONumArray : public OONumArrayBase {
public:
	inline OONumArray() { init( nil ); }
	inline OONumArray( OONumArray &val ) { init( val.ref() ); }
	inline OONumArray( OONumArrayBase &val ) { init( val.ref() ); }
	inline OONumArray &operator = ( OONumArray &val ) { set( val.ref() ); return *this; }
	inline OONumArray &operator = ( OONumArrayBase &val ) { set( val.ref() ); return *this; }
	inline ~OONumArray() { dealloc(); }
};

inline OONumArrayBase &OONumArrayBase::operator = ( OONumArray &val ) { set( val.ref() ); return *this; }

/**
 Internal class representing keyed access to number values.
 */

class OONumDictRef : public OODictRef<NSNumber *> {
	friend class OONumDictBase;
	inline OONumDictRef( OODictBase<NSNumber *> *ref, id sub ) : OODictRef<NSNumber *>( ref, sub ) { }
public:
	inline operator double() {
		return [get() doubleValue];
	}
	inline OONumDictRef & operator = ( double val ) {
		NSNumber *obj = [[NSNumber alloc] initWithDouble:val];
		set( obj );
		[obj release];
		return *this;
	}
	inline OONumDictRef & operator += ( double val ) { return *this = *this + val; }
	inline OONumDictRef & operator -= ( double val ) { return *this = *this - val; }
	inline OONumDictRef & operator *= ( double val ) { return *this = *this * val; }
	inline OONumDictRef & operator /= ( double val ) { return *this = *this / val; }
	inline double operator ~ () {
		return [OODictRef<NSNumber *>::operator ~ () doubleValue];
	}
};

class OONumDict;

/**
 NSMutableDictionary wrapper for storing numbers by key for instance variables.
 */

class OONumDictBase : public OODictBase<NSNumber *> {
public:
	inline OONumDictBase &operator = ( OONumDictBase &val ) { set( val.ref() ); return *this; }
	inline OONumDictBase &operator = ( OONumDict &val );
	inline OONumDictRef operator [] ( id sub ) {
		return OONumDictRef( this, sub );
	}
};

/**
 NSMutableDictionary wrapper for storing numbers by key for automatic variables.
 */

class OONumDict : public OONumDictBase {
public:
	inline OONumDict() { init( nil ); }
	inline OONumDict( OONumDict &val ) { init( val.ref() ); }
	inline OONumDict( OONumDictBase &val ) { init( val.ref() ); }
	inline OONumDict &operator = ( OONumDict &val ) { set( val.ref() ); return *this; }
	inline OONumDict &operator = ( OONumDictBase &val ) { set( val.ref() ); return *this; }
	inline ~OONumDict() { dealloc(); }
};

inline OONumDictBase &OONumDictBase::operator = ( OONumDict &val ) { set( val.ref() ); return *this; }

/*==================================== Pointer classes =====================================*/

template <typename PTYPE>
class OOPtr;

/**
 A class to represent a C pointer inside an NSValue object for reference counting and
 so it can be stored inside the OSX containers NSArray, NSDictionary etc.
 */

template <typename PTYPE>
class OOPtrBase : public OORefBase<NSValue *> {
	PTYPE ptr;
protected:
	inline void init() {
		OORefBase<NSValue *>::init( nil );
		ptr = NULL;
	}
	inline PTYPE pget() {
		return ptr;
	}
	inline NSValue *pset( NSValue *val ) {
		set( val );
		ptr = (PTYPE)[val pointerValue];
		return val;
	}
	inline PTYPE pset( PTYPE ptr ) {
		[pset( [[NSValue alloc] initWithBytes:&ptr objCType:@encode(PTYPE)] ) release];
		return ptr;
	}
public:
	inline OOPtrBase &operator = ( PTYPE ptr ) { pset( ptr ); return *this; }
	inline OOPtrBase &operator = ( NSValue *val ) { pset( val ); return *this; }
	inline OOPtrBase &operator = ( OOPtrBase &val ) { pset( val.ref() ); return *this; }
	inline OOPtrBase &operator = ( OOPtr<PTYPE> &val ) { pset( val.ref() ); return *this; }
	inline operator PTYPE () { return pget(); }
	inline PTYPE operator * () { return pget(); }
};

/**
 OOPtrBase subclass for reference counted storage of a C pointer for use in automatic variables.
 */

template <typename PTYPE>
class OOPtr : public OOPtrBase<PTYPE> {
public:
	inline OOPtr() { this->init(); }
	inline OOPtr( PTYPE ptr ) { this->init(); *this = ptr; }
	inline OOPtr( NSValue *val ) { this->init(); *this = val; }
	inline OOPtr( const OOPtr &val ) { this->init(); *this = val; }
	inline OOPtr( const OOPtrBase<PTYPE> &val ) { this->init(); *this = val; }

	inline OOPtr &operator = ( PTYPE ptr ) { pset( ptr ); return *this;	}
	inline OOPtr &operator = ( NSValue *val ) { this->pset( val ); return *this; }
	inline OOPtr &operator = ( OOPtr &val ) { this->pset( val.ref() ); return *this; }
	inline OOPtr &operator = ( OOPtrBase<PTYPE> &val ) { this->pset( val.ref() ); return *this; }
	
	inline ~OOPtr() { this->dealloc(); }
};

template <typename CNAME>
class OOClassPtr;

/**
 OOPtrBase subclass to store a pointer to a heap allocated C++ class instance
 inside an NSValue object to it can be reference counted and stored in OSX
 containers such as NSMutableDictionary.
 */

template <typename CNAME>
class OOClassPtrBase : public OOPtrBase<CNAME *> {
protected:
	inline NSValue *cset( NSValue *val ) {
		if ( val != this->get() )
			dealloc();
		return this->pset( val );
	}
	inline CNAME &cset( CNAME *cptr ) {
		if ( cptr != this->pget() )
			dealloc();
		return *pset( cptr );
	}
	inline CNAME &alloc() {
		if ( !this->pget() ) {
			OOTrace( @"0x%08x %@", this, @"CONSTRUCT" );
			cset( new CNAME() );
		}
		return *this->pget();
	}
public:
	inline OOClassPtrBase &operator = ( CNAME *cptr ) {	cset( cptr ); return *this; }
	inline OOClassPtrBase &operator = ( CNAME &cptr ) {	cset( &cptr ); return *this; }
	inline OOClassPtrBase &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOClassPtrBase &operator = ( const OOClassPtrBase &val ) { cset( val.ref() ); return *this; }
	inline OOClassPtrBase &operator = ( const OOClassPtr<CNAME> &val ) { cset( val.ref() ); return *this; }

	inline CNAME &cget() { return alloc(); }
	inline CNAME &operator * () { return alloc(); }		

	inline void dealloc() {
		if ( this->pget() && [this->get() retainCount] == 1 ) {
			OOTrace( @"0x%08x %@", this, @"DESTRUCT" );
			delete this->pget();
		}
		OOPtrBase<CNAME *>dealloc();
	}
};

/**
 OOClassPtrBase subclass to maintain pointers to C++ class instances in
 automatic variable and delete them as they go out of scope.
 */

template <typename CNAME>
class OOClassPtr : public OOClassPtrBase<CNAME> {
public:
	inline OOClassPtr() { this->init(); }
	inline OOClassPtr( CNAME *cptr ) { this->init(); *this = cptr; }
	inline OOClassPtr( CNAME &cptr ) { this->init(); *this = &cptr; }
	inline OOClassPtr( NSValue *val ) { this->init(); *this = val; }
	inline OOClassPtr( const OOClassPtr &val ) { this->init(); *this = val.ref(); }
	inline OOClassPtr( const OOClassPtrBase<CNAME> &val ) { this->init(); *this = val.ref(); }

	inline OOClassPtr &operator = ( CNAME *cptr ) { cset( cptr ); return *this;	}
	inline OOClassPtr &operator = ( CNAME &cptr ) { cset( &cptr ); return *this;	}
	inline OOClassPtr &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOClassPtr &operator = ( const OOClassPtr &val ) { cset( val.ref() ); return *this; }
	inline OOClassPtr &operator = ( const OOClassPtrBase<CNAME> &val ) { cset( val.ref() ); return *this; }

	inline ~OOClassPtr() { this->dealloc(); }
};

/*===================================== Storage classes ===================================*/

template <typename ETYPE>
class OOBuffer;

template <typename ETYPE>
class OOVector;

/**
 A more efficient container class than NSArray for any element type (int, id, OORef<NSString> etc.).
 Memory is allocated on deamnd when referenced by the subscipt operator and if the type is a class
 its constructors and destructors will be called by the underlying OOBuffer class.
 */

template <typename ETYPE>
class OOVectorBase : public OOClassPtrBase< OOBuffer<ETYPE> > {
public:
	inline OOVectorBase &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOVectorBase &operator = ( const OOVectorBase &val ) { cset( val.ref() ); return *this; }
	inline OOVectorBase &operator = ( const OOVector<ETYPE> &val ) { cset( val.ref() ); return *this; }
	inline ETYPE &operator [] ( int sub ) {
		return this->cget()[ sub ];
	}
};

/**
 OOVectorBase subclass for memory recover when used in automatic variables. As the array grows
 it is reallocated to always contain 4/3 of the memeory required. If the element type is a 
 class the an assignment operator must be available to copy data to the new larger array 
 and handle reference counting (See OOBuffer)
 */

template <typename ETYPE>
class OOVector : public OOVectorBase<ETYPE> {
public:
	inline OOVector() { this->init(); }
	inline OOVector( int size ) { this->init(); cset( new OOBuffer<ETYPE>( size ) ); }
	inline OOVector &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOVector &operator = ( const OOVector &val ) { cset( val.ref() ); return *this; }
	inline OOVector &operator = ( const OOVectorBase<ETYPE> &val ) { cset( val.ref() ); return *this; }
	inline ~OOVector() { this->dealloc(); }
};

template <typename ETYPE>
class OOMatrix;

/**
 A two dimensional Container class of any size using in effect a OOVector of OOBuffer instances.
 */

template <typename ETYPE>
class OOMatrixBase : public OOVectorBase< OOBuffer<ETYPE> > {
public:
	inline OOMatrixBase &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOMatrixBase &operator = ( const OOMatrixBase &val ) { cset( val.ref() ); return *this; }
	inline OOMatrixBase &operator = ( const OOMatrix<ETYPE> &val ) { cset( val.ref() ); return *this; }
};

/**
 A two dimensional Container class of any size using in effect a OOVector of OOBuffer instances.
 */

template <typename ETYPE>
class OOMatrix : public OOMatrixBase<ETYPE> {
public:
	inline OOMatrix() { this->init(); }
	inline OOMatrix &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOMatrix &operator = ( const OOMatrix &val ) { cset( val.ref() ); return *this; }
	inline OOMatrix &operator = ( const OOMatrixBase<ETYPE> &val ) { cset( val.ref() ); return *this; }
	inline ~OOMatrix() { this->dealloc(); }
};

template <typename ETYPE>
class OOCube;

/**
 A three dimensional Container class of any size using a OOVector of OOBuffers of OOBuffers.
 */

template <typename ETYPE>
class OOCubeBase : public OOVectorBase< OOBuffer< OOBuffer<ETYPE> > > {
public:
	inline OOCubeBase &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOCubeBase &operator = ( const OOCubeBase &val ) { cset( val.ref() ); return *this; }
	inline OOCubeBase &operator = ( const OOCube<ETYPE> &val ) { cset( val.ref() ); return *this; }
};

/**
 A three dimensional Container class.
 */

template <typename ETYPE>
class OOCube : public OOCubeBase<ETYPE> {
public:
	inline OOCube() { this->init(); }
	inline OOCube &operator = ( NSValue *val ) { this->cset( val ); return *this; }
	inline OOCube &operator = ( const OOCube &val ) { cset( val.ref() ); return *this; }
	inline OOCube &operator = ( const OOCube<ETYPE> &val ) { cset( val.ref() ); return *this; }
	inline ~OOCube() { this->dealloc(); }
};

/**
 Internal class to allocate storage on demand as it is accessed by reference using the
 subscript operator. A memory is allocated and reallocated if the element type is a
 plain type it will be zeroed out otherwise if it is a class the constructor will
 be called for each element in the array as it is allocated. On reallocation a new
 oversized array is allocated then the "=" operator used to copy the old values
 to the new array. This operator should "steal" any deep references from the old
 instance before it is destroyed as does the OOBuffer class itself as it is used
 recursively.
 */

template <typename ETYPE>
class OOBuffer {
	ETYPE *buff;
public:
	inline OOBuffer() {
		buff = NULL;
		used = allocated = bounds = 0;
		OOTrace( @"0x%08x %@", this, @"BCONSTRUCT" );
	}
	OOBuffer( int size ) {
		buff = NULL;
		(*this)[(bounds = size)-1];
		used = 0;
		OOTrace( @"0x%08x %@ [%d] 0x%08x", this, @"BCONSTRUCT", size, buff );
	}
	int used, allocated, bounds;
	inline ETYPE &operator [] ( int sub ) {
		if ( !buff || sub+1 > allocated ) {
			int realloc = (sub+11)*4/3;
			ETYPE *next = new ETYPE[realloc];
			const char *info = @encode(ETYPE);
			if ( info[0] != '{' )
				memset( next, '\000', realloc * sizeof *next );
			OOTrace( @"0x%08x %@ [%d] 0x%08x = 0x%08x %s", this, @"REALLOC", sub, buff, next, info );
			if ( buff ) {
				for ( int i=0 ; i<allocated ; i++ )
					next[i] = buff[i];
				delete[] buff;
			}
			allocated = realloc;
			buff = next;
		}
		if ( used < sub+1 )
			used = sub+1;
		else if ( sub < 0 )
			sub += bounds ? bounds : used;
		else if ( bounds && sub >= bounds )
			OOWarn( @"0x%08x Subscript [%d] beyond bounds of array (%d)", this, sub, bounds );
		return buff[sub];
	}
	inline OOBuffer &operator = ( OOBuffer & from ) {
		OOTrace( @"0x%08x %@, 0x%08x = 0x%08x", this, @"BASSIGN", buff, from.buff );
		buff = from.buff;
		used = from.used;
		allocated = from.allocated;
		bounds = from.bounds;
		from.buff = NULL;
		return *this;
	}
	inline ~OOBuffer() {
		if( buff ) {
			OOTrace( @"0x%08x %@ 0x%08x", this, @"BDESTRUCT", buff );
			delete[] buff;
		}
	}
};

/*==================================== Class containers ===============================*/

#ifndef NO_CPP_CONTAINERS

/**
 A text class to check objects in C++ NSValue wrapper classes are recovered properly.
 */

class OOClassTest {
public:
	OOClassTest() { _constructed++; OOTrace( @"0x%08x %@", this, @"TCONSTRUCT" ); }
	~OOClassTest() { _constructed--; OOTrace( @"0x%08x %@", this, @"TDESTRUCT" ); }
};

template <typename CNAME>
class OOClassArray;

/**
 A wrapper for an array of heap allocated C++ class pointers stored as NSValue objects.
 */

template <typename CNAME>
class OOClassArrayBase : public OOArrayBase<OOClassPtr<CNAME> > {
protected:
	inline NSMutableArray *set( NSMutableArray *val ) {
		NSMutableArray *old = this->get();
		if ( val != old && [old retainCount] == 1 )
			while ( [old count] )
				~(*this)[-1];
		return OOArrayBase<OOClassPtr<CNAME> >::set( val );
	}
	inline void dealloc() { set( nil ); }
public:
	inline OOClassArrayBase &operator = ( NSMutableArray *val ) { set( val ); return *this; };
	inline OOClassArrayBase &operator = ( OOClassArrayBase *val ) { set( val.ref() ); return *this; };
	inline OOClassArrayBase &operator = ( OOClassArray<CNAME> *val ) { set( val.ref() ); return *this; };
	inline OOClassArray<CNAME> operator ~ () {
		OOClassArray<CNAME> save = this->get();
		dealloc();
		return save;
	}
};

/**
 OOClassArrayBase  subclass to correctly delete class instances when container goes out of scope.
 */

template <typename CNAME>
class OOClassArray : public OOClassArrayBase<CNAME> {
public:
	inline OOClassArray() { this->init( nil ); }
	inline OOClassArray( NSMutableArray *val ) { this->init( val ); }
	inline OOClassArray( const OOClassArray &val ) { this->init( val.ref() ); }
	inline OOClassArray( const OOClassArrayBase<CNAME> *val ) { this->init( val.ref() ); }
	inline OOClassArray &operator = ( NSMutableArray *val ) { this->set( val ); return *this; };
	inline OOClassArray &operator = ( OOClassArray *val ) { this->set( val.ref() ); return *this; };
	inline OOClassArray &operator = ( OOClassArrayBase<CNAME> *val ) { this->set( val.ref() ); return *this; };
	inline ~OOClassArray() { this->dealloc(); }
};

template <typename CNAME>
class OOClassDict;

/**
 NSMutable dictionary wrapper for storing heap allocated C++ instances by key.
 */

template <typename CNAME>
class OOClassDictBase : public OODictBase<OOClassPtr<CNAME> > {
protected:
	inline NSMutableDictionary *set( NSMutableDictionary *val ) {
		NSMutableDictionary *old = this->get();
		if ( val != old && [old retainCount] == 1 ) {
#if 0
			for ( id key in keys )
				~(*this)[key];
#else
			NSArray *keys = [old allKeys];
			for ( int i=0 ; i<[keys count] ; i++ )
				~(*this)[[keys objectAtIndex:i]];
#endif
		}
		return OODictBase<OOClassPtr<CNAME> >::set( val );
	}
	inline void dealloc() { set( nil ); }
public:
	inline OOClassDictBase &operator = ( NSMutableArray *val ) { set( val ); return *this; };
	inline OOClassDictBase &operator = ( OOClassDictBase *val ) { set( val.ref() ); return *this; };
	inline OOClassDictBase &operator = ( OOClassDict<CNAME> *val ) { set( val.ref() ); return *this; };
	inline OOClassDict<CNAME> operator ~ () {
		OOClassDict<CNAME> save = this->get();
		dealloc();
		return save;
	}
};

/**
 OOClassDictBase for use in automatic variables.
 */

template <typename CNAME>
class OOClassDict : public OOClassDictBase<CNAME> {
public:
	inline OOClassDict() { this->init( nil ); }
	inline OOClassDict( NSMutableDictionary *val ) { this->init( val ); }
	inline OOClassDict( const OOClassDict &val ) { this->init( val.ref() ); }
	inline OOClassDict( const OOClassDictBase<CNAME> *val ) { this->init( val.ref() ); }
	inline OOClassDict &operator = ( NSMutableArray *val ) { this->set( val ); return *this; };
	inline OOClassDict &operator = ( OOClassDict *val ) { this->set( val.ref() ); return *this; };
	inline OOClassDict &operator = ( OOClassDictBase<CNAME> *val ) { this->set( val.ref() ); return *this; };
	inline ~OOClassDict() { this->dealloc(); }
};

#endif // NO_CPP_CONTAINERS
