ObjFW  Check-in [8ab6561840]

Overview
Comment:Reworked OFList.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8ab656184022c2a8ca9daf50d901684c3cfcefbd662dfcde217d94d9c9e608af
User & Date: js on 2009-01-05 22:18:45
Other Links: manifest | tags
Context
2009-01-06
22:38
Use OFList and OFArray in OFAutoreleasePool.
This reduces code duplication and looks far better.
check-in: dfc10dbeb8 user: js tags: trunk
2009-01-05
22:18
Reworked OFList. check-in: 8ab6561840 user: js tags: trunk
02:18
Fix three stupid warnings. check-in: 41095bf7a1 user: js tags: trunk
Changes

Modified src/Makefile from [a070773c11] to [97d6293a2a].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
include ../extra.mk

LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFArray.m		\
       OFAutoreleasePool.m	\
       OFExceptions.m		\
       OFHashes.m		\
       OFFile.m			\
       OFList.m			\
       OFListObject.m		\
       OFNumber.m		\
       OFObject.m		\
       OFString.m		\
       OFTCPSocket.m		\
       OFXMLFactory.m		\
       ${ASPRINTF}













<







1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
include ../extra.mk

LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFArray.m		\
       OFAutoreleasePool.m	\
       OFExceptions.m		\
       OFHashes.m		\
       OFFile.m			\
       OFList.m			\

       OFNumber.m		\
       OFObject.m		\
       OFString.m		\
       OFTCPSocket.m		\
       OFXMLFactory.m		\
       ${ASPRINTF}

Modified src/OFList.h from [e40d20478e] to [8e66167c8d].

1
2
3
4
5
6
7
8
9
10
11
12
13






14
15
16
17
18
19
20
21
22
23

24


25
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
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFListObject.h"







/**
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject
{
	OFListObject *first;
	OFListObject *last;
}


- init;



/**
 * Frees the OFList and all OFListObjects, but not the data they contian.

 */
- free;

/**
 * Frees the list and the data included in all OFListObjects it contains.





 */
- freeIncludingData;


/**
 * \returns The first OFListObject in the OFList





 */
- (OFListObject*)first;


/**




 * \returns The last OFListObject in the OFList


 */
- (OFListObject*)last;


/**

 * Adds a new OFListObject to the OFList.

 *


 * \param obj An OFListObject
 */
- add: (OFListObject*)obj;


/**
 * Creates a new OFListObject and adds it to the OFList.
 *
 * \param obj A data object for the OFListObject which will be added
 */
- addNew: (id)obj;
@end












|
>
>
>
>
>
>






|
|


>
|
>
>


<
>

|


|
>
>
>
>
>

<
>


|
>
>
>
>
>

<
>


>
>
>
>
|
>
>

|
>


>
|
>
|
>
>
|

|
>


|

|

|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"

typedef struct __of_list_object
{
	id			object;
	struct __of_list_object *next;
	struct __of_list_object *prev;
} of_list_object_t;

/**
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject
{
	of_list_object_t *first;
	of_list_object_t *last;
}

/**
 * \return The first object in the list
 */
- (of_list_object_t*)first;

/**

 * \return The last object in the list
 */
- (of_list_object_t*)last;

/**
 * Appends an object to the list.
 *
 * \param obj The object to append
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */

- (of_list_object_t*)append: (id)obj;

/**
 * Prepends an object to the list.
 *
 * \param obj The object to prepend
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */

- (of_list_object_t*)prepend: (id)obj;

/**
 * Inserts an object before another object.
 * \param obj The object to insert
 * \param listobj The of_list_object_t of the object before which it should be
 *	  inserted
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */
- (of_list_object_t*)insert: (id)obj
		     before: (of_list_object_t*)listobj;

/**
 * Inserts an object after another object.
 * \param obj The object to insert
 * \param listobj The of_list_object_t of the object after which it should be
 *	  inserted
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */
- (of_list_object_t*)insert: (id)obj
		      after: (of_list_object_t*)listobj;

/**
 * Removes an object from the list.
 *
 * \param listobj The list object returned by append / prepend
 */
- remove: (of_list_object_t*)listobj;
@end

Modified src/OFList.m from [2396e4ff81] to [55395f28ca].

13
14
15
16
17
18
19
20
21
22

23
24
25
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
















64




65
66
67




68



69


70
























71
72

73
74
75






76




77

78
79
80

#import "OFList.h"

@implementation OFList
- init
{
	if ((self = [super init])) {
		first = nil;
		last  = nil;
	}

	return self;
}

- free
{
	OFListObject *iter, *next;

	for (iter = first; iter != nil; iter = next) {
		next = [iter next];
		[iter free];
	}

	return [super free];
}

- freeIncludingData
{
	OFListObject *iter, *next;

	for (iter = first; iter != nil; iter = next) {
		next = [iter next];
		[iter freeIncludingData];
	}

	first = last = nil;
	return [super free];
}

- (OFListObject*)first
{
	return first;
}

- (OFListObject*)last
{
	return last;
}




- add: (OFListObject*)obj



{





	if (!first || !last) {
















		first = last = obj;




		return self;
	}





	[obj setPrev: last];



	[last setNext: obj];



























	last = obj;


	return self;
}







- addNew: (id)obj




{

	return [self add: [OFListObject newWithData: obj]];
}
@end







|
|

>





|

|
<
|
<




<
<
<
|
<
<
<
<
<
<
<
<
<
<




|




>
>
>
|
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
|

>
>
>
>
|
>
>
>
|
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
|


>
>
>
>
>
>
|
>
>
>
>
|
>
|


13
14
15
16
17
18
19
20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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

#import "OFList.h"

@implementation OFList
- init
{
	if ((self = [super init])) {
		first = NULL;
		last  = NULL;
	}

	return self;
}

- free
{
	of_list_object_t *iter;

	for (iter = first; iter != NULL; iter = iter->next)

		[iter->object release];


	return [super free];
}




- (of_list_object_t*)first










{
	return first;
}

- (of_list_object_t*)last
{
	return last;
}

- (of_list_object_t*)append: (id)obj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = NULL;
	o->prev = last;

	if (last != NULL)
		last->next = o;

	last = o;
	if (first == NULL)
		first = o;

	[obj retain];
	return o;
}

- (of_list_object_t*)prepend: (id)obj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = first;
	o->prev = NULL;

	if (first != NULL)
		first->prev = o;

	first = o;
	if (last == NULL)
		last = o;

	[obj retain];
	return o;
}

- (of_list_object_t*)insert: (id)obj
		     before: (of_list_object_t*)listobj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = listobj;
	o->prev = listobj->prev;

	if (listobj->prev != NULL)
		listobj->prev->next = o;

	listobj->prev = o;

	if (listobj == first)
		first = o;

	[obj retain];
	return o;
}

- (of_list_object_t*)insert: (id)obj
		      after: (of_list_object_t*)listobj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = listobj->next;
	o->prev = listobj;

	if (listobj->next != NULL)
		listobj->next->prev = o;

	listobj->next = o;

	if (listobj == last)
		last = o;

	[obj retain];
	return o;
}

- remove: (of_list_object_t*)listobj
{
	if (listobj->prev != NULL)
		listobj->prev->next = listobj->next;
	if (listobj->next != NULL)
		listobj->next->prev = listobj->prev;

	if (first == listobj)
		first = listobj->next;
	if (last == listobj)
		last = listobj->prev;

	[self freeMem: listobj];
	return self;
}
@end

Deleted src/OFListObject.h version [fae0419316].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"

/**
 * The OFListObject class is a class for objects to be stored in an OFObject.
 */
@interface OFListObject: OFObject
{
	id	     data;
	OFListObject *next;
	OFListObject *prev;
}

/**
 * \param obj The data object the OFListObject should contain
 * \return A new OFListObject.
 */
+ newWithData: (id)obj;

/**
 * Initializes an already allocated OFListObjeect.
 *
 * \param obj The data object the OFListObject should contain
 * \return An initialized OFListObject.
 */
- initWithData: (id)obj;

/**
 * Free the OFListObject and the data it contains.
 */
- freeIncludingData;

/**
 * \return The data object included in the OFListObject
 */
- (id)data;

/**
 * \return The next OFListObject in the OFList
 */
- (OFListObject*)next;

/**
 * \return The previous OFListObject in the OFList
 */
- (OFListObject*)prev;

/**
 * Sets the next OFListObject in the OFList.
 *
 * \param obj An OFListObject
 */
- setNext: (OFListObject*)obj;

/**
 * Sets the previous OFListObject in the OFList.
 *
 * \param obj An OFListObject
 */
- setPrev: (OFListObject*)obj;
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<














































































































































Deleted src/OFListObject.m version [3a84a0e1cb].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import <stdlib.h>

#import "OFListObject.h"

@implementation OFListObject
+ newWithData: (id)obj
{
	return [[self alloc] initWithData: obj];
}

- initWithData: (id)obj
{
	if ((self = [super init])) {
		next = nil;
		prev = nil;
		data = obj;
	}

	return self;
}

- freeIncludingData
{
	if (data != nil)
		[data free];

	return [super free];
}

- (id)data
{
	return data;
}

- (OFListObject*)next
{
	return next;
}

- (OFListObject*)prev
{
	return prev;
}

- setNext: (OFListObject*)obj
{
	next = obj;

	return self;
}

- setPrev: (OFListObject*)obj
{
	prev = obj;

	return self;
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<














































































































































Modified tests/OFList/OFList.m from [ba45bed53f] to [147b8c86d0].

20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
72
73















74
75
76
77
78
79
80

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 5
#define SUCCESS								\
{									\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);							\
}
#define FAIL								\
{									\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\
	return 1;							\
}
#define CHECK(cond)							\

	if (cond)							\
		SUCCESS							\
	else								\
		FAIL							\
	i++;


const char *strings[] = {
	"First String Object",
	"Second String Object",
	"Third String Object"
};

int
main()
{
	size_t	     i;
	OFList	     *list;
	OFListObject *iter;

	list = [OFList new];

	[list addNew: [OFString newFromCString: strings[0]]];
	[list addNew: [OFString newFromCString: strings[1]]];
	[list addNew: [OFString newFromCString: strings[2]]];

	for (iter = [list first], i = 0; iter != nil; iter = [iter next], i++)
		if (!strcmp([[iter data] cString], strings[i]))
			SUCCESS
		else
			FAIL

	CHECK(!strcmp([[[list first] data] cString], strings[0]))
	CHECK(!strcmp([[[list last] data] cString], strings[2]))
















	puts("");

	[list freeIncludingData];

	return 0;
}







|













>




|
>










|
|
|



|
|
|

|
|




|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
<


20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93


94
95

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 10
#define SUCCESS								\
{									\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);							\
}
#define FAIL								\
{									\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\
	return 1;							\
}
#define CHECK(cond)							\
{									\
	if (cond)							\
		SUCCESS							\
	else								\
		FAIL							\
	i++;								\
}

const char *strings[] = {
	"First String Object",
	"Second String Object",
	"Third String Object"
};

int
main()
{
	size_t i, j;
	OFList *list;
	of_list_object_t *iter;

	list = [OFList new];

	[list append: [OFString newFromCString: strings[0]]];
	[list append: [OFString newFromCString: strings[1]]];
	[list append: [OFString newFromCString: strings[2]]];

	for (iter = [list first], i = 0; iter != NULL; iter = iter->next, i++)
		if (!strcmp([iter->object cString], strings[i]))
			SUCCESS
		else
			FAIL

	CHECK(!strcmp([[list first]->object cString], strings[0]))
	CHECK(!strcmp([[list last]->object cString], strings[2]))

	[list remove: [list last]];
	CHECK(!strcmp([[list last]->object cString], strings[1]))

	[list remove: [list first]];
	CHECK(!strcmp([[list first]->object cString],
	    [[list last]->object cString]))

	[list insert: [OFString newFromCString: strings[0]]
	      before: [list last]];
	[list insert: [OFString newFromCString: strings[2]]
	       after: [list first]->next];

	for (iter = [list first], j = 0; iter != NULL; iter = iter->next, j++)
		CHECK(!strcmp([iter->object cString], strings[j]))

	puts("");



	return 0;
}