Skip to content
Browse files

merging memory fixes and minor additions from external iOS project.

  • Loading branch information...
1 parent fa78cee commit e1ff0ed2ffaa2dbefccd58f0d9d398310966bfed @timburks committed Jan 1, 2011
Showing with 31 additions and 10 deletions.
  1. +6 −2 objc/NuBSON.h
  2. +25 −8 objc/NuBSON.m
View
8 objc/NuBSON.h
@@ -1,7 +1,7 @@
/*!
@header NuBSON.h
@discussion Declarations for the NuBSON component.
-@copyright Copyright (c) 2010 Neon Design Technology, Inc.
+@copyright Copyright (c) 2010 Radtastical, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -71,8 +71,10 @@ limitations under the License.
/*! Create a new and unique object ID. */
+ (NuBSONObjectID *) objectID;
-/*! Create an object ID from a 12-byte data representation */
+/*! Create an object ID from a 12-byte data representation. */
+ (NuBSONObjectID *) objectIDWithData:(NSData *) data;
+/*! Create an object ID wrapper from a bson_oid_t native structure. */
++ (NuBSONObjectID *) objectIDWithObjectIDPointer:(const bson_oid_t *) objectIDPointer;
/*! Create an object ID from a hex string. */
- (id) initWithString:(NSString *) s;
/*! Get the hex string value of an object ID. */
@@ -85,6 +87,8 @@ limitations under the License.
- (NSComparisonResult)compare:(NuBSONObjectID *) other;
/*! Test for equality with another object ID. */
- (BOOL)isEqual:(id)other;
+/*! Raw object id */
+- (bson_oid_t) oid;
@end
@interface NuBSONBuffer : NSObject
View
33 objc/NuBSON.m
@@ -10,6 +10,10 @@ @protocol NuSymbolProtocol
- (NSString *) labelName;
@end
+@interface NuBSONObjectID (Private)
+- (const bson_oid_t *) objectIDPointer;
+@end
+
@interface NuBSON (Private)
- (NuBSON *) initWithBSON:(bson) b;
- (id) initWithObjectIDPointer:(const bson_oid_t *) objectIDPointer;
@@ -113,7 +117,7 @@ void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
}
}
else if ([object respondsToSelector:@selector(cStringUsingEncoding:)]) {
- bson_append_string(bb, name,[object cStringUsingEncoding:NSUTF8StringEncoding]);
+ bson_append_string(bb, name, [object cStringUsingEncoding:NSUTF8StringEncoding]);
}
else {
NSLog(@"We have a problem. %@ cannot be serialized to bson", object);
@@ -134,6 +138,11 @@ + (NuBSONObjectID *) objectIDWithData:(NSData *) data
return [[[NuBSONObjectID alloc] initWithData:data] autorelease];
}
++ (NuBSONObjectID *) objectIDWithObjectIDPointer:(const bson_oid_t *) objectIDPointer
+{
+ return [[[NuBSONObjectID alloc] initWithObjectIDPointer:objectIDPointer] autorelease];
+}
+
- (id) initWithString:(NSString *) s
{
if (self = [super init]) {
@@ -152,6 +161,8 @@ - (id) initWithObjectIDPointer:(const bson_oid_t *) objectIDPointer
- (const bson_oid_t *) objectIDPointer {return &oid;}
+- (bson_oid_t) oid {return oid;}
+
- (id) initWithData:(NSData *) data
{
if (self = [super init]) {
@@ -229,6 +240,7 @@ + (NSMutableArray *) bsonArrayWithData:(NSData *) data
NuBSON *bsonObject = [[[NuBSON alloc] initWithBSON:bsonValue] autorelease];
[results addObject:bsonObject];
}
+ bson_destroy(&bsonBuffer);
return results;
}
@@ -242,7 +254,7 @@ + (NuBSON *) bsonWithList:(id) list
return [[[NuBSON alloc] initWithList:list] autorelease];
}
-// internal
+// internal, takes ownership of argument
- (NuBSON *) initWithBSON:(bson) b
{
if (self = [super init]) {
@@ -256,9 +268,9 @@ - (NuBSON *) initWithData:(NSData *) data
bson bsonBuffer;
bsonBuffer.data = (char *) [data bytes];
bsonBuffer.owned = NO;
- bson bsonValue;
bson_copy(&bsonValue, &bsonBuffer);
- return [[NuBSON alloc] initWithBSON:bsonValue];
+ bson_destroy(&bsonBuffer);
+ return self;
}
- (NSData *) dataRepresentation
@@ -298,6 +310,11 @@ - (NuBSON *) initWithList:(id) cell
return [self initWithBSON:b];
}
+- (void) dealloc {
+ bson_destroy(&bsonValue);
+ [super dealloc];
+}
+
void dump_bson_iterator(bson_iterator it, const char *indent)
{
bson_iterator it2;
@@ -504,7 +521,7 @@ - (id) objectForKey:(NSString *) key
{
bson_iterator it;
bson_iterator_init(&it, bsonValue.data);
- bson_type bt = bson_find(&it, &bsonValue, [key cStringUsingEncoding:NSUTF8StringEncoding]);
+ bson_find(&it, &bsonValue, [key cStringUsingEncoding:NSUTF8StringEncoding]);
id value = object_for_bson_iterator(it, NO);
return value;
}
@@ -575,12 +592,12 @@ - (id) handleUnknownMessage:(id) method withContext:(NSMutableDictionary *) cont
if (NuSymbol && [key isKindOfClass:[NuSymbol class]] && [key isLabel]) {
id evaluated_key = [key labelName];
id evaluated_value = [value evalWithContext:context];
- [self addObject:evaluated_value forKey:evaluated_key];
+ [self addObject:evaluated_value withKey:evaluated_key];
}
else {
id evaluated_key = [key evalWithContext:context];
id evaluated_value = [value evalWithContext:context];
- [self addObject:evaluated_value forKey:evaluated_key];
+ [self addObject:evaluated_value withKey:evaluated_key];
}
cursor = [[cursor cdr] cdr];
}
@@ -615,7 +632,7 @@ + (NuBSONComparator *) comparatorWithBSONSpecification:(NuBSON *) s
return comparator;
}
-- (int) compareDataAtAddress:(const void *) aptr withSize:(int) asiz withDataAtAddress:(const void *) bptr withSize:(int) bsiz
+- (int) compareDataAtAddress:(void *) aptr withSize:(int) asiz withDataAtAddress:(void *) bptr withSize:(int) bsiz
{
bson bsonA;
bsonA.data = aptr;

0 comments on commit e1ff0ed

Please sign in to comment.
Something went wrong with that request. Please try again.