From 10c706a040abeaf5c82db54086d4edb03a995cf3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 13 Jan 2021 19:30:45 +0100 Subject: Add SerdWorld for shared library state --- include/serd/serd.h | 72 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 26 deletions(-) (limited to 'include/serd/serd.h') diff --git a/include/serd/serd.h b/include/serd/serd.h index 2f349e90..d57f1470 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -38,9 +38,11 @@ #ifdef __GNUC__ # define SERD_PURE_FUNC __attribute__((pure)) # define SERD_CONST_FUNC __attribute__((const)) +# define SERD_MALLOC_FUNC __attribute__((malloc)) #else # define SERD_PURE_FUNC # define SERD_CONST_FUNC +# define SERD_MALLOC_FUNC #endif #if defined(__clang__) && __clang_major__ >= 7 @@ -61,6 +63,10 @@ SERD_API \ SERD_CONST_FUNC +#define SERD_MALLOC_API \ + SERD_API \ + SERD_MALLOC_FUNC + #ifdef __cplusplus extern "C" { # if defined(__GNUC__) @@ -75,6 +81,9 @@ extern "C" { @{ */ +/// Global library state +typedef struct SerdWorldImpl SerdWorld; + /// Lexical environment for relative URIs or CURIEs (base URI and namespaces) typedef struct SerdEnvImpl SerdEnv; @@ -840,6 +849,39 @@ typedef SerdStatus (*SerdStatementFunc)(void* SERD_NULLABLE handle, typedef SerdStatus (*SerdEndFunc)(void* SERD_NULLABLE handle, const SerdNode* SERD_NONNULL node); +/** + @} + @defgroup serd_world World + @{ +*/ + +/** + Create a new Serd World. + + It is safe to use multiple worlds in one process, though no objects can be + shared between worlds. +*/ +SERD_MALLOC_API +SerdWorld* SERD_ALLOCATED +serd_world_new(void); + +/// Free `world` +SERD_API +void +serd_world_free(SerdWorld* SERD_NULLABLE world); + +/** + Set a function to be called when errors occur. + + The `error_func` will be called with `handle` as its first argument. If + no error function is set, errors are printed to stderr. +*/ +SERD_API +void +serd_world_set_error_func(SerdWorld* SERD_NONNULL world, + SerdErrorFunc SERD_NULLABLE error_func, + void* SERD_NULLABLE handle); + /** @} @defgroup serd_env Environment @@ -1011,7 +1053,8 @@ serd_sink_write_end(const SerdSink* SERD_NONNULL sink, /// Create a new RDF reader SERD_API SerdReader* SERD_ALLOCATED -serd_reader_new(SerdSyntax syntax, +serd_reader_new(SerdWorld* SERD_NONNULL world, + SerdSyntax syntax, const SerdSink* SERD_NONNULL sink, size_t stack_size); @@ -1026,18 +1069,6 @@ SERD_API void serd_reader_set_strict(SerdReader* SERD_NONNULL reader, bool strict); -/** - Set a function to be called when errors occur during reading. - - The `error_func` will be called with `handle` as its first argument. If - no error function is set, errors are printed to stderr in GCC style. -*/ -SERD_API -void -serd_reader_set_error_sink(SerdReader* SERD_NONNULL reader, - SerdErrorFunc SERD_NULLABLE error_func, - void* SERD_NULLABLE error_handle); - /** Set a prefix to be added to all blank node identifiers. @@ -1142,7 +1173,8 @@ serd_reader_free(SerdReader* SERD_NULLABLE reader); /// Create a new RDF writer SERD_API SerdWriter* SERD_ALLOCATED -serd_writer_new(SerdSyntax syntax, +serd_writer_new(SerdWorld* SERD_NONNULL world, + SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* SERD_NONNULL env, SerdWriteFunc SERD_NONNULL ssink, @@ -1188,18 +1220,6 @@ SERD_API char* SERD_NULLABLE serd_buffer_sink_finish(SerdBuffer* SERD_NONNULL stream); -/** - Set a function to be called when errors occur during writing. - - The `error_func` will be called with `handle` as its first argument. If - no error function is set, errors are printed to stderr. -*/ -SERD_API -void -serd_writer_set_error_sink(SerdWriter* SERD_NONNULL writer, - SerdErrorFunc SERD_NONNULL error_func, - void* SERD_NULLABLE error_handle); - /** Set a prefix to be removed from matching blank node identifiers -- cgit v1.2.1