summaryrefslogtreecommitdiffstats
path: root/src/server/UndoStack.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/UndoStack.cpp')
-rw-r--r--src/server/UndoStack.cpp140
1 files changed, 74 insertions, 66 deletions
diff --git a/src/server/UndoStack.cpp b/src/server/UndoStack.cpp
index a94617a5..c6555123 100644
--- a/src/server/UndoStack.cpp
+++ b/src/server/UndoStack.cpp
@@ -16,33 +16,31 @@
#include "UndoStack.hpp"
-#include "ingen/URIMap.hpp"
-#include "ingen/URIs.hpp"
-#include "lv2/atom/atom.h"
-#include "lv2/atom/util.h"
-#include "lv2/patch/patch.h"
-#include "lv2/urid/urid.h"
-#include "serd/serd.h"
-#include "sratom/sratom.h"
+#include <ingen/URIMap.hpp>
+#include <ingen/URIs.hpp>
+#include <ingen/ingen.h>
+#include <lv2/atom/atom.h>
+#include <lv2/atom/util.h>
+#include <lv2/patch/patch.h>
+#include <serd/serd.h>
+#include <sratom/sratom.h>
#include <ctime>
#include <iterator>
-#include <memory>
-#define NS_RDF (const uint8_t*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+#define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
-#define USTR(s) ((const uint8_t*)(s))
+#define USTR(s) reinterpret_cast<const uint8_t*>(s)
-namespace ingen {
-namespace server {
+namespace ingen::server {
int
UndoStack::start_entry()
{
if (_depth == 0) {
- time_t now;
+ time_t now = {};
time(&now);
- _stack.emplace_back(Entry(now));
+ _stack.emplace_back(now);
}
return ++_depth;
}
@@ -62,22 +60,22 @@ UndoStack::ignore_later_event(const LV2_Atom* first,
return false;
}
- const auto* f = (const LV2_Atom_Object*)first;
- const auto* s = (const LV2_Atom_Object*)second;
+ const auto* f = reinterpret_cast<const LV2_Atom_Object*>(first);
+ const auto* s = reinterpret_cast<const LV2_Atom_Object*>(second);
if (f->body.otype == _uris.patch_Set && f->body.otype == s->body.otype) {
const LV2_Atom* f_subject = nullptr;
const LV2_Atom* f_property = nullptr;
const LV2_Atom* s_subject = nullptr;
const LV2_Atom* s_property = nullptr;
lv2_atom_object_get(f,
- (LV2_URID)_uris.patch_subject, &f_subject,
- (LV2_URID)_uris.patch_property, &f_property,
+ _uris.patch_subject.urid(), &f_subject,
+ _uris.patch_property.urid(), &f_property,
0);
lv2_atom_object_get(s,
- (LV2_URID)_uris.patch_subject, &s_subject,
- (LV2_URID)_uris.patch_property, &s_property,
+ _uris.patch_subject.urid(), &s_subject,
+ _uris.patch_property.urid(), &s_property,
0);
- return (lv2_atom_equals(f_subject, s_subject) &&
+ return (lv2_atom_equals(f_subject, s_subject) &&
lv2_atom_equals(f_property, s_property));
}
@@ -87,18 +85,18 @@ UndoStack::ignore_later_event(const LV2_Atom* first,
int
UndoStack::finish_entry()
{
- if (--_depth > 0) {
- return _depth;
- } else if (_stack.back().events.empty()) {
- // Disregard empty entry
- _stack.pop_back();
- } else if (_stack.size() > 1 && _stack.back().events.size() == 1) {
- // This entry and the previous one have one event, attempt to merge
- auto i = _stack.rbegin();
- ++i;
- if (i->events.size() == 1) {
- if (ignore_later_event(i->events[0], _stack.back().events[0])) {
- _stack.pop_back();
+ if (--_depth == 0) {
+ if (_stack.back().events.empty()) {
+ // Disregard empty entry
+ _stack.pop_back();
+ } else if (_stack.size() > 1 && _stack.back().events.size() == 1) {
+ // This entry and the previous one have one event, attempt to merge
+ auto i = _stack.rbegin();
+ ++i;
+ if (i->events.size() == 1) {
+ if (ignore_later_event(i->events[0], _stack.back().events[0])) {
+ _stack.pop_back();
+ }
}
}
}
@@ -118,7 +116,7 @@ UndoStack::pop()
}
struct BlankIDs {
- explicit BlankIDs(char c='b') : c(c) {}
+ explicit BlankIDs(const char prefix = 'b') noexcept : c{prefix} {}
SerdNode get() {
snprintf(buf, sizeof(buf), "%c%u", c, n++);
@@ -127,15 +125,18 @@ struct BlankIDs {
char buf[16]{};
unsigned n{0};
- const char c{'b'};
+ const char c;
};
struct ListContext {
- explicit ListContext(BlankIDs& ids, unsigned flags, const SerdNode* s, const SerdNode* p)
- : ids(ids)
- , s(*s)
- , p(*p)
- , flags(flags | SERD_LIST_O_BEGIN)
+ explicit ListContext(BlankIDs& blank_ids,
+ unsigned statement_flags,
+ const SerdNode* subject,
+ const SerdNode* predicate)
+ : ids(blank_ids)
+ , s(*subject)
+ , p(*predicate)
+ , flags(statement_flags | SERD_LIST_O_BEGIN)
{}
SerdNode start_node(SerdWriter* writer) {
@@ -149,7 +150,7 @@ struct ListContext {
const SerdNode node = start_node(writer);
// node rdf:first value
- p = serd_node_from_string(SERD_URI, NS_RDF "first");
+ p = serd_node_from_string(SERD_URI, USTR(NS_RDF "first"));
flags = SERD_LIST_CONT;
serd_writer_write_statement(writer, flags|oflags, nullptr, &node, &p, value, nullptr, nullptr);
@@ -159,12 +160,15 @@ struct ListContext {
void end_node(SerdWriter*, const SerdNode* node) {
// Prepare for next call: node rdf:rest ...
s = *node;
- p = serd_node_from_string(SERD_URI, NS_RDF "rest");
+ p = serd_node_from_string(SERD_URI, USTR(NS_RDF "rest"));
}
void end(SerdWriter* writer) {
- const SerdNode nil = serd_node_from_string(SERD_URI, NS_RDF "nil");
- serd_writer_write_statement(writer, flags, nullptr, &s, &p, &nil, nullptr, nullptr);
+ const SerdNode nil =
+ serd_node_from_string(SERD_URI, USTR(NS_RDF "nil"));
+
+ serd_writer_write_statement(
+ writer, flags, nullptr, &s, &p, &nil, nullptr, nullptr);
}
BlankIDs& ids;
@@ -183,8 +187,8 @@ UndoStack::write_entry(Sratom* sratom,
strftime(time_str, sizeof(time_str), "%FT%T", gmtime(&entry.time));
// entry rdf:type ingen:UndoEntry
- SerdNode p = serd_node_from_string(SERD_URI, USTR(INGEN_NS "time"));
- SerdNode o = serd_node_from_string(SERD_LITERAL, USTR(time_str));
+ SerdNode p = serd_node_from_string(SERD_URI, USTR(INGEN_NS "time"));
+ const SerdNode o = serd_node_from_string(SERD_LITERAL, USTR(time_str));
serd_writer_write_statement(writer, SERD_ANON_CONT, nullptr, subject, &p, &o, nullptr, nullptr);
p = serd_node_from_string(SERD_URI, USTR(INGEN_NS "events"));
@@ -195,9 +199,12 @@ UndoStack::write_entry(Sratom* sratom,
for (const LV2_Atom* atom : entry.events) {
const SerdNode node = ctx.start_node(writer);
- p = serd_node_from_string(SERD_URI, NS_RDF "first");
+ p = serd_node_from_string(SERD_URI,
+ reinterpret_cast<const uint8_t*>(NS_RDF
+ "first"));
+
ctx.flags = SERD_LIST_CONT;
- sratom_write(sratom, &_map.urid_unmap_feature()->urid_unmap, SERD_LIST_CONT,
+ sratom_write(sratom, &_map.urid_unmap(), SERD_LIST_CONT,
&node, &p,
atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
@@ -217,26 +224,28 @@ UndoStack::save(FILE* stream, const char* name)
const SerdNode base = serd_node_from_string(SERD_URI, USTR("ingen:/"));
SerdURI base_uri;
- serd_uri_parse(base.buf, &base_uri);
-
- SerdWriter* writer = serd_writer_new(
- SERD_TURTLE,
- (SerdStyle)(SERD_STYLE_RESOLVED|SERD_STYLE_ABBREVIATED|SERD_STYLE_CURIED),
- env,
- &base_uri,
- serd_file_sink,
- stream);
+ serd_uri_parse(USTR("ingen:/"), &base_uri);
+
+ SerdWriter* writer =
+ serd_writer_new(SERD_TURTLE,
+ static_cast<SerdStyle>(SERD_STYLE_RESOLVED |
+ SERD_STYLE_ABBREVIATED |
+ SERD_STYLE_CURIED),
+ env,
+ &base_uri,
+ serd_file_sink,
+ stream);
// Configure sratom to write directly to the writer (and thus the socket)
- Sratom* sratom = sratom_new(&_map.urid_map_feature()->urid_map);
+ Sratom* sratom = sratom_new(&_map.urid_map());
sratom_set_sink(sratom,
- (const char*)base.buf,
- (SerdStatementSink)serd_writer_write_statement,
- (SerdEndSink)serd_writer_end_anon,
+ reinterpret_cast<const char*>(base.buf),
+ reinterpret_cast<SerdStatementSink>(serd_writer_write_statement),
+ reinterpret_cast<SerdEndSink>(serd_writer_end_anon),
writer);
- SerdNode s = serd_node_from_string(SERD_BLANK, (const uint8_t*)name);
- SerdNode p = serd_node_from_string(SERD_URI, USTR(INGEN_NS "entries"));
+ const SerdNode s = serd_node_from_string(SERD_BLANK, USTR(name));
+ const SerdNode p = serd_node_from_string(SERD_URI, USTR(INGEN_NS "entries"));
BlankIDs ids('u');
ListContext ctx(ids, 0, &s, &p);
@@ -253,5 +262,4 @@ UndoStack::save(FILE* stream, const char* name)
serd_writer_free(writer);
}
-} // namespace server
-} // namespace ingen
+} // namespace ingen::server