ObjFW  Check-in [7107bd9906]

Overview
Comment:Add one more convenience method to OFArray.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7107bd99060600e9cb0b5ee17003287a01ccaaec00203e5025559b411cb57c27
User & Date: js on 2009-05-18 20:41:47
Other Links: manifest | tags
Context
2009-05-18
20:53
Split OFDictionary into OFDictionary and OFMutableDictionary. check-in: 71abb030af user: js tags: trunk
20:41
Add one more convenience method to OFArray. check-in: 7107bd9906 user: js tags: trunk
19:38
Add test for OFArray. check-in: 9c435fddfc user: js tags: trunk
Changes

Modified src/OFArray.h from [0cd7239c8e] to [c8ae639320].

35
36
37
38
39
40
41






42
43
44
45
46
47
48

/**
 * \param first The first object in the array
 * \return A new autoreleased OFArray
 */
+ arrayWithObjects: (OFObject*)first, ...;







/**
 * Initializes an OFArray with the specified object.
 *
 * \param obj An object
 * \return An initialized OFArray
 */
- initWithObject: (OFObject*)obj;







>
>
>
>
>
>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

/**
 * \param first The first object in the array
 * \return A new autoreleased OFArray
 */
+ arrayWithObjects: (OFObject*)first, ...;

/**
 * \param objs A C array of objects.
 * \return A new autoreleased OFArray
 */
+ arrayWithCArray: (OFObject**)objs;

/**
 * Initializes an OFArray with the specified object.
 *
 * \param obj An object
 * \return An initialized OFArray
 */
- initWithObject: (OFObject*)obj;
61
62
63
64
65
66
67








68
69
70
71
72
73
74
 * \param first The first object
 * \param args A va_list
 * \return An initialized OFArray
 */
- initWithObject: (OFObject*)first
      andArgList: (va_list)args;









/**
 * \return The number of objects in the OFArray
 */
- (size_t)count;

/**
 * \return The objects of the array as a C array







>
>
>
>
>
>
>
>







67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 * \param first The first object
 * \param args A va_list
 * \return An initialized OFArray
 */
- initWithObject: (OFObject*)first
      andArgList: (va_list)args;

/**
 * Initializes an OFArray with the objects from the specified C array.
 *
 * \param objs A C array of objects
 * \return An initialized OFArray
 */
- initWithCArray: (OFObject**)objs;

/**
 * \return The number of objects in the OFArray
 */
- (size_t)count;

/**
 * \return The objects of the array as a C array

Modified src/OFArray.m from [30af0ffa87] to [9f48848034].

35
36
37
38
39
40
41





42
43
44
45
46
47
48
	va_start(args, first);
	ret = [[[self alloc] initWithObject: first
				 andArgList: args] autorelease];
	va_end(args);

	return ret;
}






- init
{
	self = [super init];

	@try {
		array = [[OFDataArray alloc]







>
>
>
>
>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	va_start(args, first);
	ret = [[[self alloc] initWithObject: first
				 andArgList: args] autorelease];
	va_end(args);

	return ret;
}

+ arrayWithCArray: (OFObject**)objs
{
	return [[[self alloc] initWithCArray: objs] autorelease];
}

- init
{
	self = [super init];

	@try {
		array = [[OFDataArray alloc]
110
111
112
113
114
115
116





















117
118
119
120
121
122
123
	objs = [array data];
	len = [array count];

	[first retain];
	for (i = 0; i < len; i++)
		[objs[i] retain];






















	return self;
}

- (size_t)count
{
	return [array count];
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	objs = [array data];
	len = [array count];

	[first retain];
	for (i = 0; i < len; i++)
		[objs[i] retain];

	return self;
}

- initWithCArray: (OFObject**)objs
{
	id *obj;

	self = [self init];

	@try {
		for (obj = objs; *obj != nil; obj++)
			[array add: obj];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	/* Retain objects after adding them for the case adding failed */
	for (obj = objs; *obj != nil; obj++)
		[*obj retain];

	return self;
}

- (size_t)count
{
	return [array count];
}

Modified tests/OFArray/OFArray.m from [d1f0787995] to [693a970af9].

26
27
28
29
30
31
32







33
34
35
36
37
38

39
40
41
42
43
44
45

46

47
48
49
50
51
52
53
		puts("NOT CAUGHT!");			\
		return 1;				\
	} @catch (exception *e) {			\
		puts("CAUGHT! Error string was:");	\
		puts([[e string] cString]);		\
		puts("Resuming...");			\
	}








int
main()
{
	OFArray *a = [OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil];
	OFArray *b = [OFMutableArray array];


	[b add: @"Foo"];
	[b add: @"Bar"];
	[b add: @"Baz"];

	assert([a count] == 3);
	assert([b count] == 3);

	assert([a isEqual: b]);


	[b removeNObjects: 1];
	[b add: @"Baz"];
	assert([a isEqual: b]);

	[b removeNObjects: 1];
	[b add: @"Qux"];







>
>
>
>
>
>
>






>







>

>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		puts("NOT CAUGHT!");			\
		return 1;				\
	} @catch (exception *e) {			\
		puts("CAUGHT! Error string was:");	\
		puts([[e string] cString]);		\
		puts("Resuming...");			\
	}

id c_array[] = {
	@"Foo",
	@"Bar",
	@"Baz",
	nil
};

int
main()
{
	OFArray *a = [OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil];
	OFArray *b = [OFMutableArray array];
	OFArray *c = [OFArray arrayWithCArray: c_array];

	[b add: @"Foo"];
	[b add: @"Bar"];
	[b add: @"Baz"];

	assert([a count] == 3);
	assert([b count] == 3);
	assert([c count] == 3);
	assert([a isEqual: b]);
	assert([a isEqual: c]);

	[b removeNObjects: 1];
	[b add: @"Baz"];
	assert([a isEqual: b]);

	[b removeNObjects: 1];
	[b add: @"Qux"];