[libr] Make PLItem more useful

I can't say that I like what's there even now, but at least PLItem can
be used without a lot of casting. Really, Ruamoko needs dictionary and
string classes so reading a property list can build more natural object
trees rather than this mess from when I knew too little.
This commit is contained in:
Bill Currie 2020-07-05 14:38:44 +09:00
parent f544a6f0b0
commit 7a478b4ce7
2 changed files with 101 additions and 18 deletions

View File

@ -4,7 +4,29 @@
#include <plist.h> #include <plist.h>
#include <Object.h> #include <Object.h>
@interface PLItem: Object @class PLItem;
@protocol PLDictionary
- (int) count;
- (int) numKeys;
- (PLItem *) getObjectForKey:(string) key;
- (PLItem *) allKeys;
- addKey:(string) key value:(PLItem *) value;
@end
@protocol PLArray
- (int) count;
- (int) numObjects;
- (PLItem *) getObjectAtIndex:(int) index;
- addObject:(PLItem *) object;
- insertObject:(PLItem *) object atIndex:(int) index;
@end
@protocol PLString
- (string) string;
@end
@interface PLItem: Object <PLDictionary, PLArray, PLString>
{ {
plitem_t item; plitem_t item;
int own; int own;
@ -22,34 +44,20 @@
- (pltype_t) type; - (pltype_t) type;
@end @end
@interface PLDictionary: PLItem @interface PLDictionary: PLItem <PLDictionary>
+ (PLDictionary *) new; + (PLDictionary *) new;
- (int) count;
- (int) numKeys;
- (PLItem *) getObjectForKey:(string) key;
- (PLItem *) allKeys;
- addKey:(string) key value:(PLItem *) value;
@end @end
@interface PLArray: PLItem @interface PLArray: PLItem <PLArray>
+ (PLArray *) new; + (PLArray *) new;
- (int) count;
- (int) numObjects;
- (PLItem *) getObjectAtIndex:(int) index;
- addObject:(PLItem *) object;
- insertObject:(PLItem *) object atIndex:(int) index;
@end @end
@interface PLData: PLItem @interface PLData: PLItem
+ (PLData *) new:(void*) data size:(int) len; + (PLData *) new:(void*) data size:(int) len;
@end @end
@interface PLString: PLItem @interface PLString: PLItem <PLString>
+ (PLString *) new:(string) str; + (PLString *) new:(string) str;
- (string) string;
@end @end
#endif//__ruamoko_PropertyList_h #endif//__ruamoko_PropertyList_h

View File

@ -94,6 +94,81 @@
return PL_Type (item); return PL_Type (item);
} }
- (int) count
{
if ([self class] == [PLDictionary class]) {
return PL_D_NumKeys (item);
} else {
return PL_A_NumObjects (item);
}
}
- (int) numKeys
{
return PL_D_NumKeys (item);
}
- (PLItem *) getObjectForKey:(string) key
{
return [[PLItem itemClass: PL_ObjectForKey (item, key)] autorelease];
}
- (PLItem *) allKeys
{
return [[PLItem itemClass: PL_D_AllKeys (item)] autorelease];
}
- addKey:(string) key value:(PLItem *) value
{
if (!value.own) {
obj_error (self, 0, "add of unowned key/value to PLDictionary");
return self;
}
PL_D_AddObject (item, key, value.item);
value.own = 0;
[value release];
return self;
}
- (int) numObjects
{
return PL_A_NumObjects (item);
}
- (PLItem *) getObjectAtIndex:(int) index
{
return [[PLItem itemClass: PL_ObjectAtIndex (item, index)] autorelease];
}
- 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:(int) 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;
}
- (string) string
{
return PL_String (item);
}
@end @end