ObjFW  Check-in [2e13035431]

Overview
Comment:Preliminary OFPlugin implementation.
Win32 isn't supported yet due to the problems libobjc + DLLs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2e130354311c7f518449b04d289c45f8bd2e17fa08d6804d0bd38b8db7f9e32a
User & Date: js on 2009-03-14 17:17:15
Other Links: manifest | tags
Context
2009-03-15
13:46
Preliminary OFConstString implementation and support for @"" literals. check-in: 2ff44fe8d7 user: js tags: trunk
2009-03-14
17:17
Preliminary OFPlugin implementation.
Win32 isn't supported yet due to the problems libobjc + DLLs.
check-in: 2e13035431 user: js tags: trunk
15:39
Update buildsys. check-in: 57d5566e38 user: js tags: trunk
Changes

Modified TODO from [b1093a5a98] to [9efbe0a9f9].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Test if autorelease pool releases everything correctly when thread is ended

Serialization
Tests for OFFile.
Tests for OFNumber.

OFBase64

OFDirectory
OFDictionary
OFSortedArray
OFThread

OFStack
OFQueue

OFPlugin
OFXMLParser

OFSortedQueue

OFTLSSocket
OFXMPPClient









<






<






1
2
3
4
5
6
7
8
9

10
11
12
13
14
15

16
17
18
19
20
21
Test if autorelease pool releases everything correctly when thread is ended

Serialization
Tests for OFFile.
Tests for OFNumber.

OFBase64

OFDirectory

OFSortedArray
OFThread

OFStack
OFQueue


OFXMLParser

OFSortedQueue

OFTLSSocket
OFXMPPClient

Modified configure.ac from [b86ef585ee] to [6672b28534].

13
14
15
16
17
18
19







20
21
22
23
24
25
26
CFLAGS="$CFLAGS -Wall"
OBJCFLAGS="$OBJCFLAGS -Wall -fobjc-exceptions"
LIBS="$LIBS -lobjc"

AC_DEFINE(OF_CONFIG_H, 1, [Define so that we know we got our config.h])

BUILDSYS_LIB







AC_C_BIGENDIAN([AC_DEFINE(OF_BIG_ENDIAN, 1, [Whether we are big endian])])

AC_MSG_CHECKING(for SIZE_MAX)
AC_EGREP_CPP(yes, [
	#include <stdint.h>
	#include <limits.h>








>
>
>
>
>
>
>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
CFLAGS="$CFLAGS -Wall"
OBJCFLAGS="$OBJCFLAGS -Wall -fobjc-exceptions"
LIBS="$LIBS -lobjc"

AC_DEFINE(OF_CONFIG_H, 1, [Define so that we know we got our config.h])

BUILDSYS_LIB
AC_DEFINE_UNQUOTED(PLUGIN_SUFFIX, "$PLUGIN_SUFFIX", [Suffix for plugins])

if test x"$PLUGIN_SUFFIX" != "x"; then
	AC_SUBST(OFPLUGIN_M, "OFPlugin.m")
	AC_SUBST(OFPLUGIN, "OFPlugin")
fi

AC_C_BIGENDIAN([AC_DEFINE(OF_BIG_ENDIAN, 1, [Whether we are big endian])])

AC_MSG_CHECKING(for SIZE_MAX)
AC_EGREP_CPP(yes, [
	#include <stdint.h>
	#include <limits.h>

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
		#endif], [
		AC_MSG_RESULT(yes)
		size_max="SIZE_T_MAX"], [
		AC_MSG_RESULT(no)
		size_max="((size_t)-1)"])
	AC_DEFINE_UNQUOTED(SIZE_MAX, $size_max, [Maximum value for size_t])])



AC_CHECK_HEADER(objc/runtime.h,
	[AC_DEFINE(HAVE_OBJC_RUNTIME_H, 1, [Whether we have objc/runtime.h])])
AC_CHECK_LIB(objc, sel_get_name,
	[AC_DEFINE(HAVE_SEL_GET_NAME, 1, [Whether we have sel_get_name])])
AC_CHECK_LIB(objc, sel_getName,
	[AC_DEFINE(HAVE_SEL_GETNAME, 1, [Whether we have sel_getName])])

AC_CHECK_FUNC(asprintf, [
	have_asprintf="yes"
	AC_DEFINE(HAVE_ASPRINTF, 1, "Whether we have asprintf")], [
	have_asprintf="no"
	AC_SUBST(ASPRINTF, "asprintf.c")])

AC_MSG_CHECKING(whether snprintf returns something useful)
AC_CACHE_VAL(ac_cv_snprintf_useful_ret, [
	AC_TRY_RUN([
		#include <stdio.h>

		int







>
>











|







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
		#endif], [
		AC_MSG_RESULT(yes)
		size_max="SIZE_T_MAX"], [
		AC_MSG_RESULT(no)
		size_max="((size_t)-1)"])
	AC_DEFINE_UNQUOTED(SIZE_MAX, $size_max, [Maximum value for size_t])])

AC_CHECK_LIB(dl, dlopen, LIBS="$LIBS -ldl")

AC_CHECK_HEADER(objc/runtime.h,
	[AC_DEFINE(HAVE_OBJC_RUNTIME_H, 1, [Whether we have objc/runtime.h])])
AC_CHECK_LIB(objc, sel_get_name,
	[AC_DEFINE(HAVE_SEL_GET_NAME, 1, [Whether we have sel_get_name])])
AC_CHECK_LIB(objc, sel_getName,
	[AC_DEFINE(HAVE_SEL_GETNAME, 1, [Whether we have sel_getName])])

AC_CHECK_FUNC(asprintf, [
	have_asprintf="yes"
	AC_DEFINE(HAVE_ASPRINTF, 1, "Whether we have asprintf")], [
	have_asprintf="no"
	AC_SUBST(ASPRINTF_C, "asprintf.c")])

AC_MSG_CHECKING(whether snprintf returns something useful)
AC_CACHE_VAL(ac_cv_snprintf_useful_ret, [
	AC_TRY_RUN([
		#include <stdio.h>

		int

Modified extra.mk.in from [0946b120c4] to [cf1b4ce92e].

1


2
3
4
ASPRINTF = @ASPRINTF@


WS2_LIBS = @WS2_LIBS@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@
|
>
>



1
2
3
4
5
6
ASPRINTF_C = @ASPRINTF_C@
OFPLUGIN = @OFPLUGIN@
OFPLUGIN_M = @OFPLUGIN_M@
WS2_LIBS = @WS2_LIBS@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@

Modified src/Makefile from [a36117ea93] to [325a4e921c].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
       OFDictionary.m		\
       OFExceptions.m		\
       OFHashes.m		\
       OFFile.m			\
       OFList.m			\
       OFNumber.m		\
       OFObject.m		\

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

INCLUDESTMP = ${SRCS:.c=.h}
INCLUDES = ${INCLUDESTMP:.m=.h}	\
	   OFMacros.h		\
	   OFStream.h

include ../buildsys.mk

CPPFLAGS += -I..
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}







>



|













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
       OFDictionary.m		\
       OFExceptions.m		\
       OFHashes.m		\
       OFFile.m			\
       OFList.m			\
       OFNumber.m		\
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFString.m		\
       OFTCPSocket.m		\
       OFXMLFactory.m		\
       ${ASPRINTF_C}

INCLUDESTMP = ${SRCS:.c=.h}
INCLUDES = ${INCLUDESTMP:.m=.h}	\
	   OFMacros.h		\
	   OFStream.h

include ../buildsys.mk

CPPFLAGS += -I..
CFLAGS += ${LIB_CFLAGS}
OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}

Added src/OFPlugin.h version [c1caa01830].













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 OFPlugin class provides a system for loading plugins at runtime.
 */
@interface OFPlugin: OFObject
{
	void *handle;
	id   plugin;
}

/**
 * Loads an OFPlugin from a file.
 *
 * \param path Path to the OFPlugin file. The suffix is appended automatically.
 * \return A new autoreleased OFPlugin
 */
+ pluginFromFile: (const char*)path;

/**
 * Initializes an already allocated OFPlugin from a file.
 *
 * \param path Path to the OFPlugin file. The suffix is appended automatically.
 * \return An initialized OFPlugin
 */
- initFromFile: (const char*)path;
@end

Added src/OFPlugin.m version [496529e854].























































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
91
/*
 * 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"

#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#import "OFPlugin.h"
#import "OFExceptions.h"

@implementation OFPlugin
+ pluginFromFile: (const char*)path
{
	return [[[OFPlugin alloc] initFromFile: path] autorelease];
}

- initFromFile: (const char*)path
{
	char *file;
	size_t pathlen, suffixlen;
	id (*init_plugin)();
	Class c;

	if ((self = [super init])) {
		pathlen = strlen(path);
		suffixlen = strlen(PLUGIN_SUFFIX);

		if ((file = malloc(pathlen + suffixlen + 1)) == NULL) {
			c = [self class];
			[super free];
			@throw [OFNoMemException newWithClass: c
						      andSize: pathlen +
							       suffixlen + 1];
		}
		memcpy(file, path, pathlen);
		memcpy(file + pathlen, PLUGIN_SUFFIX, suffixlen);
		file[pathlen + suffixlen] = 0;

		if ((handle = dlopen(file, RTLD_NOW)) == NULL ||
		    (init_plugin = dlsym(handle, "init_plugin")) == NULL ||
		    (plugin = init_plugin()) == nil) {
			free(file);
			c = [self class];
			[super free];
			@throw [OFInitializationFailedException
			    newWithClass: c];
		}
		free(file);
	}

	return self;
}

- free
{
	[plugin free];
	dlclose(handle);

	return [super free];
}

#ifdef __objc_INCLUDE_GNU
- (retval_t)forward: (SEL)selector
		   : (arglist_t)args
#else
- (id)forward: (SEL)selector
	     : (marg_list)args
#endif
{
	return [plugin performv: selector
			       : args];
}

- (IMP)methodFor: (SEL)selector
{
	if ([self respondsTo: selector])
		return [self methodFor: selector];
	else
		return [plugin methodFor: selector];
}
@end

Modified tests/Makefile from [bfacede22f] to [da80e99a87].



1
2
3
4
5

6
7
8
9
10
11


SUBDIRS = OFObject		\
	  OFAutoreleasePool	\
	  OFArray		\
	  OFDictionary		\
	  OFHashes		\

	  OFString		\
	  OFTCPSocket		\
	  OFList		\
	  OFXMLFactory

include ../buildsys.mk
>
>





>






1
2
3
4
5
6
7
8
9
10
11
12
13
14
include ../extra.mk

SUBDIRS = OFObject		\
	  OFAutoreleasePool	\
	  OFArray		\
	  OFDictionary		\
	  OFHashes		\
	  ${OFPLUGIN}		\
	  OFString		\
	  OFTCPSocket		\
	  OFList		\
	  OFXMLFactory

include ../buildsys.mk

Added tests/OFPlugin/Makefile version [dab086716a].



















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
SUBDIRS = TestPlugin

PROG_NOINST = ofplugin${PROG_SUFFIX}
SRCS = OFPlugin.m

include ../../buildsys.mk
include ../../extra.mk

CPPFLAGS += -I../../src -I../..
LIBS := -L../../src -lobjfw ${LIBS}

.PHONY: run

all: run
run: subdirs ${PROG_NOINST}
	rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dll libobjfw.dylib
	ln -s ../../src/libobjfw.so libobjfw.so.1
	ln -s ../../src/libobjfw.so libobjfw.so.1.0
	ln -s ../../src/libobjfw.dll libobjfw.dll
	ln -s ../../src/libobjfw.dylib libobjfw.dylib
	LD_LIBRARY_PATH=.$${LD_LIBRARY_PATH+:}$$LD_LIBRARY_PATH \
	DYLD_LIBRARY_PATH=.$${DYLD_LIBRARY_PATH+:}$$DYLD_LIBRARY_PATH \
	${TEST_LAUNCHER} ./${PROG_NOINST}; EXIT=$$?; \
	rm -f libobjfw.so.1 libobjfw.so.1.0 libobjfw.dll libobjfw.dylib; \
	exit $$EXIT

Added tests/OFPlugin/OFPlugin.m version [0bbeec06e6].





















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "OFPlugin.h"
#import "TestPlugin/TestPlugin.h"

int
main()
{
	OFPlugin <TestPlugin> *plugin;

	plugin = [OFPlugin pluginFromFile: "TestPlugin/TestPlugin"];
	[plugin test];

	return 0;
}

Added tests/OFPlugin/TestPlugin/Makefile version [b512e9a984].



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
PLUGIN_NOINST = TestPlugin${PLUGIN_SUFFIX}
SRCS = TestPlugin.m

include ../../../buildsys.mk
include ../../../extra.mk

CPPFLAGS += ${PLUGIN_CPPFLAGS} -I../../../src
OBJCFLAGS += ${PLUGIN_CFLAGS}
LDFLAGS += ${PLUGIN_LDFLAGS}

Added tests/OFPlugin/TestPlugin/TestPlugin.h version [464f9220e6].







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * 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"

@protocol TestPlugin
- (void)test;
@end

@interface TestPlugin: OFObject <TestPlugin>
@end

Added tests/OFPlugin/TestPlugin/TestPlugin.m version [6f671f6355].























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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.
 */

#include <stdio.h>

#import "TestPlugin.h"

@implementation TestPlugin
- (void)test
{
	puts("Test successfull!");
}
@end

id
init_plugin()
{
	return [TestPlugin new];
}