aboutsummaryrefslogtreecommitdiffstats
path: root/include/serd
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-03 19:24:59 +0200
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commit40534e6c42daabb6cc8ec2d49785bec088d1d3a8 (patch)
tree593e37de3e9bce98e291ec098a7081e6e26240b0 /include/serd
parent38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3 (diff)
downloadserd-40534e6c42daabb6cc8ec2d49785bec088d1d3a8.tar.gz
serd-40534e6c42daabb6cc8ec2d49785bec088d1d3a8.tar.bz2
serd-40534e6c42daabb6cc8ec2d49785bec088d1d3a8.zip
Add SerdStatement
Diffstat (limited to 'include/serd')
-rw-r--r--include/serd/sink.h16
-rw-r--r--include/serd/statement.h83
2 files changed, 93 insertions, 6 deletions
diff --git a/include/serd/sink.h b/include/serd/sink.h
index f24db683..95979b50 100644
--- a/include/serd/sink.h
+++ b/include/serd/sink.h
@@ -39,12 +39,10 @@ typedef SerdStatus (*SerdPrefixFunc)(void* SERD_NULLABLE handle,
Called for every RDF statement in the serialisation.
*/
-typedef SerdStatus (*SerdStatementFunc)(void* SERD_NULLABLE handle,
- SerdStatementFlags flags,
- const SerdNode* SERD_NULLABLE graph,
- const SerdNode* SERD_NONNULL subject,
- const SerdNode* SERD_NONNULL predicate,
- const SerdNode* SERD_NONNULL object);
+typedef SerdStatus (*SerdStatementFunc)(void* SERD_NULLABLE handle,
+ SerdStatementFlags flags,
+ const SerdStatement* SERD_NONNULL
+ statement);
/**
Sink function for anonymous node end markers.
@@ -110,6 +108,12 @@ serd_sink_write_prefix(const SerdSink* SERD_NONNULL sink,
const SerdNode* SERD_NONNULL name,
const SerdNode* SERD_NONNULL uri);
+/// Write a statement
+SERD_API SerdStatus
+serd_sink_write_statement(const SerdSink* SERD_NONNULL sink,
+ SerdStatementFlags flags,
+ const SerdStatement* SERD_NONNULL statement);
+
/// Write a statement from individual nodes
SERD_API SerdStatus
serd_sink_write(const SerdSink* SERD_NONNULL sink,
diff --git a/include/serd/statement.h b/include/serd/statement.h
index 33911c41..eff7b796 100644
--- a/include/serd/statement.h
+++ b/include/serd/statement.h
@@ -5,7 +5,10 @@
#define SERD_STATEMENT_H
#include "serd/attributes.h"
+#include "serd/caret.h"
+#include "serd/node.h"
+#include <stdbool.h>
#include <stdint.h>
SERD_BEGIN_DECLS
@@ -39,6 +42,86 @@ typedef enum {
/// Bitwise OR of SerdStatementFlag values
typedef uint32_t SerdStatementFlags;
+/// A subject, predicate, and object, with optional graph context
+typedef struct SerdStatementImpl SerdStatement;
+
+/**
+ Create a new statement.
+
+ Note that, to minimise model overhead, statements do not own their nodes, so
+ they must have a longer lifetime than the statement for it to be valid. For
+ statements in models, this is the lifetime of the model. For user-created
+ statements, the simplest way to handle this is to use `SerdNodes`.
+
+ @param s The subject
+ @param p The predicate ("key")
+ @param o The object ("value")
+ @param g The graph ("context")
+ @param caret Optional caret at the origin of this statement
+ @return A new statement that must be freed with serd_statement_free()
+*/
+SERD_API SerdStatement* SERD_ALLOCATED
+serd_statement_new(const SerdNode* SERD_NONNULL s,
+ const SerdNode* SERD_NONNULL p,
+ const SerdNode* SERD_NONNULL o,
+ const SerdNode* SERD_NULLABLE g,
+ const SerdCaret* SERD_NULLABLE caret);
+
+/// Return a copy of `statement`
+SERD_API SerdStatement* SERD_ALLOCATED
+serd_statement_copy(const SerdStatement* SERD_NULLABLE statement);
+
+/// Free `statement`
+SERD_API void
+serd_statement_free(SerdStatement* SERD_NULLABLE statement);
+
+/// Return the given node of the statement
+SERD_PURE_API const SerdNode* SERD_NULLABLE
+serd_statement_node(const SerdStatement* SERD_NONNULL statement,
+ SerdField field);
+
+/// Return the subject of the statement
+SERD_PURE_API const SerdNode* SERD_NONNULL
+serd_statement_subject(const SerdStatement* SERD_NONNULL statement);
+
+/// Return the predicate of the statement
+SERD_PURE_API const SerdNode* SERD_NONNULL
+serd_statement_predicate(const SerdStatement* SERD_NONNULL statement);
+
+/// Return the object of the statement
+SERD_PURE_API const SerdNode* SERD_NONNULL
+serd_statement_object(const SerdStatement* SERD_NONNULL statement);
+
+/// Return the graph of the statement
+SERD_PURE_API const SerdNode* SERD_NULLABLE
+serd_statement_graph(const SerdStatement* SERD_NONNULL statement);
+
+/// Return the source location where the statement originated, or NULL
+SERD_PURE_API const SerdCaret* SERD_NULLABLE
+serd_statement_caret(const SerdStatement* SERD_NONNULL statement);
+
+/**
+ Return true iff `a` is equal to `b`, ignoring statement caret metadata.
+
+ Only returns true if nodes are equivalent, does not perform wildcard
+ matching.
+*/
+SERD_PURE_API bool
+serd_statement_equals(const SerdStatement* SERD_NULLABLE a,
+ const SerdStatement* SERD_NULLABLE b);
+
+/**
+ Return true iff the statement matches the given pattern.
+
+ Nodes match if they are equivalent, or if one of them is NULL. The
+ statement matches if every node matches.
+*/
+SERD_PURE_API bool
+serd_statement_matches(const SerdStatement* SERD_NONNULL statement,
+ const SerdNode* SERD_NULLABLE subject,
+ const SerdNode* SERD_NULLABLE predicate,
+ const SerdNode* SERD_NULLABLE object,
+ const SerdNode* SERD_NULLABLE graph);
/**
@}
*/