From 7026fb72f85e349eea64a62bd924358c608520cc Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 30 May 2020 21:32:37 +0200 Subject: Replace multiple stream callbacks with SerdEvent This makes plumbing easier since everything goes through the same "stream" and only one callback is required to handling everything. It's also more easily extensible in case more event types need to be added in the future. --- serd/serd.h | 88 ++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 30 deletions(-) (limited to 'serd') diff --git a/serd/serd.h b/serd/serd.h index 5a6815f8..4dd7e7bd 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -849,39 +849,77 @@ serd_node_compare(const SerdNode* a, const SerdNode* b); @{ */ +/// Type of a SerdEvent +typedef enum { + SERD_BASE = 1, ///< Base URI changed + SERD_PREFIX = 2, ///< New URI prefix + SERD_STATEMENT = 3, ///< Statement + SERD_END = 4 ///< End of anonymous node +} SerdEventType; + /** - Sink (callback) for base URI changes + Event for base URI changes - Called whenever the base URI of the serialisation changes. + Emitted whenever the base URI of the serialisation changes. */ -typedef SerdStatus (*SerdBaseFunc)(void* handle, const SerdNode* uri); +typedef struct { + SerdEventType type; ///< #SERD_BASE + const SerdNode* uri; ///< Base URI +} SerdBaseEvent; /** - Sink function for namespace definitions + Event for namespace definitions - Called whenever a prefix is defined in the serialisation. + Emitted whenever a prefix is defined in the serialisation. */ -typedef SerdStatus (*SerdPrefixFunc)(void* handle, - const SerdNode* name, - const SerdNode* uri); +typedef struct { + SerdEventType type; ///< #SERD_PREFIX + const SerdNode* name; ///< Prefix name + const SerdNode* uri; ///< Namespace URI +} SerdPrefixEvent; /** - Sink function for statements + Event for statements - Called for every RDF statement in the serialisation. + Emitted for every RDF statement in the serialisation. */ -typedef SerdStatus (*SerdStatementFunc)(void* handle, - SerdStatementFlags flags, - const SerdStatement* statement); +typedef struct { + SerdEventType type; ///< #SERD_STATEMENT + SerdStatementFlags flags; ///< Flags for streaming pretty-printing + const SerdStatement* statement; ///< Statement +} SerdStatementEvent; /** - Sink function for anonymous node end markers + Event for the end of anonymous node descriptions - This is called to indicate that the anonymous node with the given + This is emitted to indicate that the anonymous node with the given `value` will no longer be referred to by any future statements (i.e. the anonymous serialisation of the node is finished). */ -typedef SerdStatus (*SerdEndFunc)(void* handle, const SerdNode* node); +typedef struct { + SerdEventType type; ///< #SERD_END + const SerdNode* node; ///< Anonymous blank node that is finished +} SerdEndEvent; + +/** + An event in a data stream + + Streams of data are represented as an ordered series of events. Events + represent everything that can occur in an RDF document, and are used to plumb + together different components. For example, when parsing a document, a + reader emits a stream of events which can be sent to a writer to serialise a + document, or to an inserter to build a model in memory. +*/ +typedef union { + SerdEventType type; ///< Event type (always set) + SerdBaseEvent base; ///< Base URI changed + SerdPrefixEvent prefix; ///< New namespace prefix + SerdStatementEvent statement; ///< Statement + SerdEndEvent end; ///< End of anonymous node +} SerdEvent; + +/// Function for handling events +typedef SerdStatus (*SerdEventFunc)(void* handle, const SerdEvent* event); /** @} @@ -1127,25 +1165,15 @@ SERD_API void serd_sink_free(SerdSink* sink); -/// Set a function to be called when the base URI changes -SERD_API -SerdStatus -serd_sink_set_base_func(SerdSink* sink, SerdBaseFunc base_func); - -/// Set a function to be called when a namespace prefix is defined -SERD_API -SerdStatus -serd_sink_set_prefix_func(SerdSink* sink, SerdPrefixFunc prefix_func); - -/// Set a function to be called when a statement is emitted +/// Set a function to be called for every event SERD_API SerdStatus -serd_sink_set_statement_func(SerdSink* sink, SerdStatementFunc statement_func); +serd_sink_set_event_func(SerdSink* sink, SerdEventFunc event_func); -/// Set a function to be called when an anonymous node ends +/// Send an event to the sink SERD_API SerdStatus -serd_sink_set_end_func(SerdSink* sink, SerdEndFunc end_func); +serd_sink_write_event(const SerdSink* sink, const SerdEvent* event); /// Set the base URI SERD_API -- cgit v1.2.1