From 86e095df809f8d15b54ee62801f3deb829adfae9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 28 Apr 2011 04:45:59 +0000 Subject: Const-correct node API. git-svn-id: http://svn.drobilla.net/sord/trunk@98 3d64ff67-21c5-427c-a301-fe4f08042e5a --- sord/sord.h | 4 ++-- sord/sordmm.hpp | 4 ++-- src/sord.c | 33 +++++++++++++++++---------------- src/syntax.c | 24 +++++++++++++----------- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/sord/sord.h b/sord/sord.h index 587456b..8d31085 100644 --- a/sord/sord.h +++ b/sord/sord.h @@ -91,7 +91,7 @@ typedef struct SordNodeImpl SordNode; Nodes are ordered (S P O G). The ID of the default graph is 0. */ -typedef SordNode* SordQuad[4]; +typedef const SordNode* SordQuad[4]; /** Index into a SordQuad. @@ -220,7 +220,7 @@ sord_new_literal_counted(SordWorld* world, */ SORD_API SordNode* -sord_node_copy(SordNode* node); +sord_node_copy(const SordNode* node); /** Free a node (i.e. drop a reference). diff --git a/sord/sordmm.hpp b/sord/sordmm.hpp index 294f38f..dc80cb3 100644 --- a/sord/sordmm.hpp +++ b/sord/sordmm.hpp @@ -149,7 +149,7 @@ public: inline Node(World& world, Type t, const std::string& s); inline Node(World& world); - inline Node(World& world, SordNode* node); + inline Node(World& world, const SordNode* node); inline Node(const Node& other); inline ~Node(); @@ -269,7 +269,7 @@ Node::Node(World& world) } inline -Node::Node(World& world, SordNode* node) +Node::Node(World& world, const SordNode* node) : _world(&world) { _c_obj = node ? sord_node_copy(node) : NULL; diff --git a/src/sord.c b/src/sord.c index ff449ee..75e9287 100644 --- a/src/sord.c +++ b/src/sord.c @@ -314,7 +314,7 @@ sord_iter_seek_match(SordIter* iter) for (iter->end = true; !g_sequence_iter_is_end(iter->cur); sord_iter_forward(iter)) { - SordNode** const key = (SordNode**)g_sequence_get(iter->cur); + const SordNode** const key = (const SordNode**)g_sequence_get(iter->cur); if (sord_quad_match_inline(key, iter->pat)) return (iter->end = false); } @@ -332,7 +332,7 @@ sord_iter_seek_match_range(SordIter* iter) return true; do { - SordNode** key = (SordNode**)g_sequence_get(iter->cur); + const SordNode** key = (const SordNode**)g_sequence_get(iter->cur); if (sord_quad_match_inline(key, iter->pat)) return false; // Found match @@ -371,7 +371,7 @@ sord_iter_new(const SordModel* sord, GSequenceIter* cur, const SordQuad pat, case SINGLE: case RANGE: assert( - sord_quad_match_inline((SordNode**)g_sequence_get(iter->cur), + sord_quad_match_inline((const SordNode**)g_sequence_get(iter->cur), iter->pat)); break; case FILTER_RANGE: @@ -588,18 +588,18 @@ sord_new(SordWorld* world, unsigned indices, bool graphs) } static void -sord_add_quad_ref(SordModel* sord, SordNode* node) +sord_add_quad_ref(SordModel* sord, const SordNode* node) { if (node) { assert(node->refs > 0); - ++node->refs; + ++((SordNode*)node)->refs; } } static void -sord_drop_quad_ref(SordModel* sord, SordNode* node) +sord_drop_quad_ref(SordModel* sord, const SordNode* node) { - sord_node_free(sord_get_world(sord), node); + sord_node_free(sord_get_world(sord), (SordNode*)node); } void @@ -731,10 +731,10 @@ sord_find(SordModel* sord, const SordQuad pat) // It's easiest to think about this algorithm in terms of (S P O) ordering, // assuming (A B C) == (S P O). For other orderings this is not actually // the case, but it works the same way. - SordNode* a = pat[ordering[0]]; // Most Significant Node (MSN) - SordNode* b = pat[ordering[1]]; // ... - SordNode* c = pat[ordering[2]]; // ... - SordNode* d = pat[ordering[3]]; // Least Significant Node (LSN) + const SordNode* a = pat[ordering[0]]; // Most Significant Node (MSN) + const SordNode* b = pat[ordering[1]]; // ... + const SordNode* c = pat[ordering[2]]; // ... + const SordNode* d = pat[ordering[3]]; // Least Significant Node (LSN) if (a && b && c && d) mode = SINGLE; // No duplicate quads (Sord is a set) @@ -746,7 +746,7 @@ sord_find(SordModel* sord, const SordQuad pat) SORD_FIND_LOG("No match found\n"); return NULL; } - SordNode** const key = (SordNode**)g_sequence_get(cur); + const SordNode** const key = (const SordNode**)g_sequence_get(cur); if (!key || ( (mode == RANGE || mode == SINGLE) && !sord_quad_match_inline(search_key, key) )) { SORD_FIND_LOG("No match found\n"); @@ -957,12 +957,13 @@ sord_node_free(SordWorld* world, SordNode* node) } SordNode* -sord_node_copy(SordNode* node) +sord_node_copy(const SordNode* node) { - if (node) { - ++node->refs; + SordNode* copy = (SordNode*)node; + if (copy) { + ++copy->refs; } - return node; + return copy; } static inline bool diff --git a/src/syntax.c b/src/syntax.c index daeec69..a0198c3 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -151,26 +151,28 @@ event_statement(void* handle, { ReadState* const state = (ReadState*)handle; - SordQuad tup; - tup[0] = sord_node_from_serd_node(state, subject, NULL, NULL); - tup[1] = sord_node_from_serd_node(state, predicate, NULL, NULL); - tup[2] = sord_node_from_serd_node(state, object, - object_datatype, object_lang); + SordNode* s = sord_node_from_serd_node(state, subject, NULL, NULL); + SordNode* p = sord_node_from_serd_node(state, predicate, NULL, NULL); + SordNode* o = sord_node_from_serd_node(state, object, + object_datatype, object_lang); + SordNode* g = NULL; if (state->graph_uri_node) { assert(graph->type == SERD_NOTHING); - tup[3] = sord_node_copy(state->graph_uri_node); + g = sord_node_copy(state->graph_uri_node); } else { - tup[3] = (graph && graph->buf) + g = (graph && graph->buf) ? sord_node_from_serd_node(state, graph, NULL, NULL) : NULL; } + const SordQuad tup = { s, p, o, g }; sord_add(state->sord, tup); - sord_node_free(state->world, tup[0]); - sord_node_free(state->world, tup[1]); - sord_node_free(state->world, tup[2]); - sord_node_free(state->world, tup[3]); + + sord_node_free(state->world, s); + sord_node_free(state->world, p); + sord_node_free(state->world, o); + sord_node_free(state->world, g); return true; } -- cgit v1.2.1