add a PropertyList class hierarchy

This is an imperfect revision of history.
This commit is contained in:
Bill Currie 2004-11-13 01:55:00 +00:00 committed by Jeff Teunissen
parent 5f34f87b58
commit 7a217b81cd
4 changed files with 265 additions and 3 deletions

View file

@ -8,7 +8,8 @@ nobase_pkginclude_HEADERS= \
draw.h key.h \
\
cbuf.h cmd.h cvar.h file.h gib.h hash.h plist.h \
Object.h AutoreleasePool.h Array.h Entity.h List.h ListNode.h Stack.h \
AutoreleasePool.h Array.h Entity.h List.h ListNode.h Object.h \
PropertyList.h Protocol.h Stack.h \
\
gui/Group.h gui/InputLine.h gui/Pic.h gui/Point.h gui/Rect.h gui/Size.h \
gui/Slider.h gui/Text.h gui/View.h

View file

@ -0,0 +1,54 @@
#ifndef __ruamoko_PropertyList_h
#define __ruamoko_PropertyList_h
#include "plist.h"
#include "Object.h"
@interface PLItem: Object
{
plitem_t item;
integer own;
}
+ (PLItem) newDictionary;
+ (PLItem) newArray;
+ (PLItem) newData:(void[]) data size:(integer) len;
+ (PLItem) newString:(string) str;
+ (PLItem) newFromString:(string) str;
-initWithItem:(plitem_t) item;
-initWithOwnItem:(plitem_t) item;
- (string) write;
- (pltype_t) type;
@end
@interface PLDictionary: PLItem
+ (PLDictionary) new;
- (integer) count;
- (integer) numKeys;
- (PLItem) getObjectForKey:(string) key;
- (PLItem) allKeys;
- addKey:(PLItem) key value:(PLItem) value;
@end
@interface PLArray: PLItem
+ (PLArray) new;
- (integer) count;
- (integer) numObjects;
- (PLItem) getObjectAtIndex:(integer) index;
- addObject:(PLItem) object;
- insertObject:(PLItem) object atIndex:(integer) index;
@end
@interface PLData: PLItem
+ (PLData) new:(void[]) data size:(integer) len;
@end
@interface PLString: PLItem
+ (PLString) new:(string) str;
- (string) string;
@end
#endif//__ruamoko_PropertyList_h

View file

@ -27,8 +27,9 @@ EXTRA_LIBRARIES= $(ruamoko_libs)
$(QFCC) $(QCFLAGS) $(QCPPFLAGS) -c -o $@ $<
libr_a_SOURCES=\
cbuf.r cmd.r cvar.r file.r hash.r plist.r qfile.r qfs.r string.r \
Object.r AutoreleasePool.r Array.r Entity.r List.r ListNode.r Stack.r
cbuf.r cmd.r cvar.r file.r hash.r plist.r qfile.r qfs.r script.r string.r \
AutoreleasePool.r Array.r Entity.r List.r ListNode.r Object.r \
PropertyList.r Protocol.r Stack.r
libr_a_AR=$(PAK) -cf
libqw_a_SOURCES=\

206
ruamoko/lib/PropertyList.r Normal file
View file

@ -0,0 +1,206 @@
#include "PropertyList.h"
@implementation PLItem
+ (PLItem) newDictionary
{
return [PLDictionary new];
}
+ (PLItem) newArray
{
return [PLArray new];
}
+ (PLItem) newData:(void[]) data size:(integer) len
{
return [PLData new:data size:len];
}
+ (PLItem) newString:(string) str
{
return [PLString new:str];
}
+ (PLItem) newFromString:(string) str
{
return [PLItem itemClass: PL_GetPropertyList (str)];
}
+itemClass:(plitem_t) item
{
local string classname = NIL;
local Class class;
if (!PL_TEST (item))
return NIL;
switch (PL_Type (item)) {
case QFDictionary:
classname = "PLDictionary";
break;
case QFArray:
classname = "PLArray";
break;
case QFBinary:
classname = "PLData";
break;
case QFString:
classname = "PLString";
break;
default:
return NIL;
}
class = obj_lookup_class (classname);
return [[class alloc] initWithItem: item];
}
-initWithItem:(plitem_t) item
{
if (!(self = [super init]))
return self;
self.item = item;
own = 0;
return self;
}
-initWithOwnItem:(plitem_t) item
{
if (!(self = [super init]))
return self;
self.item = item;
own = 1;
return self;
}
-dealloc
{
if (own)
PL_Free (item);
[super dealloc];
}
- (string) write
{
return PL_WritePropertyList (item);
}
- (pltype_t) type
{
return PL_Type (item);
}
@end
@implementation PLDictionary
+ (PLDictionary) new
{
return [[PLDictionary alloc] initWithOwnItem: PL_NewDictionary ()];
}
- (integer) count
{
return PL_D_NumKeys (item);
}
- (integer) numKeys
{
return PL_D_NumKeys (item);
}
- (PLItem) getObjectForKey:(string) key
{
return [PLItem itemClass: PL_ObjectForKey (item, key)];
}
- (PLItem) allKeys
{
return [PLItem itemClass: PL_D_AllKeys (item)];
}
- addKey:(PLItem) key value:(PLItem) value
{
if (!key.own || !value.own) {
obj_error (self, 0, "add of unowned key/value to PLDictionary");
return self;
}
PL_D_AddObject (item, key.item, value.item);
key.own = value.own = 0;
[key release];
[value release];
return self;
}
@end
@implementation PLArray
+ (PLArray) new
{
return [[PLArray alloc] initWithOwnItem: PL_NewArray ()];
}
- (integer) count
{
return PL_A_NumObjects (item);
}
- (integer) numObjects
{
return PL_A_NumObjects (item);
}
- (PLItem) getObjectAtIndex:(integer) index
{
return [PLItem itemClass: PL_ObjectAtIndex (item, index)];
}
- addObject:(PLItem) object
{
if (!object.own) {
obj_error (self, 0, "add of unowned object to PLArray");
return self;
}
PL_A_AddObject (item, object.item);
object.own = 0;
[object release];
return self;
}
- insertObject:(PLItem) object atIndex:(integer) index
{
if (!object.own) {
obj_error (self, 0, "add of unowned object to PLArray");
return self;
}
PL_A_InsertObjectAtIndex (item, object.item, index);
object.own = 0;
[object release];
return self;
}
@end
@implementation PLData
+ (PLData) new:(void[]) data size:(integer) len
{
return [[PLData alloc] initWithOwnItem: PL_NewData (data, len)];
}
@end
@implementation PLString
+ (PLString) new: (string) str
{
return [[PLString alloc] initWithOwnItem: PL_NewString (str)];
}
- (string) string
{
return PL_String (item);
}
@end