diff options
author | David Robillard <d@drobilla.net> | 2019-03-10 22:44:50 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-16 19:08:37 -0400 |
commit | abce3c01977c4a56952dac20de14076f86d58d74 (patch) | |
tree | bca5188eefe33a3a396e97004d6ea3a63b1f7632 /bindings | |
parent | 50bbbafd8cb7cac121e7fd37458a1975dd818611 (diff) | |
download | sratom-abce3c01977c4a56952dac20de14076f86d58d74.tar.gz sratom-abce3c01977c4a56952dac20de14076f86d58d74.tar.bz2 sratom-abce3c01977c4a56952dac20de14076f86d58d74.zip |
WIP: Add C++ bindings
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/cpp/include/sratom/sratom.hpp | 185 | ||||
-rw-r--r-- | bindings/cpp/meson.build | 76 | ||||
-rw-r--r-- | bindings/cpp/test/test_sratom_hpp.cpp | 25 |
3 files changed, 286 insertions, 0 deletions
diff --git a/bindings/cpp/include/sratom/sratom.hpp b/bindings/cpp/include/sratom/sratom.hpp new file mode 100644 index 0000000..5cd337e --- /dev/null +++ b/bindings/cpp/include/sratom/sratom.hpp @@ -0,0 +1,185 @@ +/* + Copyright 2019-2021 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/// @file sratom.hpp C++ API for Sratom + +#ifndef SRATOM_SRATOM_HPP +#define SRATOM_SRATOM_HPP + +#include "lv2/urid/urid.h" +#include "serd/serd.hpp" +#include "sratom/sratom.h" + +#include <cstdint> +#include <sstream> + +/** + @defgroup sratomxx Sratomxx + C++ bindings for Sratom, a library for serialising LV2 atoms. + @{ +*/ + +namespace sratom { + +namespace detail { + +struct Deleter { + void operator()(void* ptr) { sratom_free(ptr); } +}; + +} // namespace detail + +/// Flags to control how atoms are written and read +typedef enum { + /** + Write the main subject with a label. + + If set, the main subject will be written using its blank node label, + instead of as an anonymous node. + */ + named_subject = SRATOM_NAMED_SUBJECT, + + /** + Write pretty numeric literals. + + If set, numbers may be written as pretty literals instead of string + literals with explicit data types. Note that enabling this means that + types will not survive a round trip. + */ + pretty_numbers = SRATOM_PRETTY_NUMBERS, + + /** + Write terse output without newlines. + + If set, top level atoms will be written as a single long line. + */ + terse = SRATOM_TERSE, +} Flag; + +using Flags = serd::detail::Flags<Flag>; + +class Dumper + : public serd::detail::BasicWrapper<SratomDumper, sratom_dumper_free> +{ +public: + Dumper(serd::World& world, LV2_URID_Map& map, LV2_URID_Unmap& unmap) + : BasicWrapper(sratom_dumper_new(world.cobj(), &map, &unmap)) + {} + + int write(const serd::Env& env, + serd::SinkView sink, + const LV2_Atom& atom, + const Flags flags = {}) + { + return sratom_dump_atom( + cobj(), env.cobj(), sink.cobj(), nullptr, nullptr, &atom, flags); + } + + int write(const serd::Env& env, + serd::SinkView sink, + const serd::Node& subject, + const serd::Node& predicate, + const LV2_Atom& atom, + const Flags flags = {}) + { + return sratom_dump_atom(cobj(), + env.cobj(), + sink.cobj(), + subject.cobj(), + predicate.cobj(), + &atom, + flags); + } + + int write(const serd::Env& env, + serd::SinkView sink, + const serd::Node& subject, + const serd::Node& predicate, + LV2_URID type, + uint32_t size, + const void* body, + const Flags flags = {}) + { + return sratom_dump(cobj(), + env.cobj(), + sink.cobj(), + subject.cobj(), + predicate.cobj(), + type, + size, + body, + flags); + } + + std::string to_string(const serd::Env& env, + const LV2_Atom& atom, + const Flags flags = {}) + { + char* c_str = sratom_to_string(cobj(), env.cobj(), &atom, flags); + std::string result = c_str; + sratom_free(c_str); + return result; + } + +private: + static size_t string_sink(const void* buf, size_t, size_t nmemb, void* stream) + { + std::string& string = *(std::string*)stream; + string.insert(string.size(), static_cast<const char*>(buf), nmemb); + return nmemb; + } +}; + +class Loader + : public serd::detail::BasicWrapper<SratomLoader, sratom_loader_free> +{ +public: + using AtomPtr = std::unique_ptr<LV2_Atom, detail::Deleter>; + + Loader(serd::World& world, LV2_URID_Map& map) + : BasicWrapper(sratom_loader_new(world.cobj(), &map)) + {} + + int read(const serd::Optional<serd::Node>& base_uri, + LV2_Atom_Forge& forge, + const serd::Model& model, + const serd::Node& node) + { + return sratom_load( + cobj(), base_uri.cobj(), &forge, model.cobj(), node.cobj()); + } + + AtomPtr read_string(serd::Env& env, const char* str) + { + return AtomPtr{sratom_from_string(cobj(), env.cobj(), str)}; + } + + AtomPtr read_model(const serd::Optional<serd::Node>& base_uri, + const serd::Model& model, + const serd::Node& subject) + { + return AtomPtr{ + sratom_from_model(cobj(), base_uri.cobj(), model.cobj(), subject.cobj())}; + } +}; + +} // namespace sratom + +/** + @} +*/ + +#endif // SRATOM_SRATOM_HPP diff --git a/bindings/cpp/meson.build b/bindings/cpp/meson.build new file mode 100644 index 0000000..ee1dc4f --- /dev/null +++ b/bindings/cpp/meson.build @@ -0,0 +1,76 @@ +versioned_cpp_name = 'sratompp' + version_suffix + +# Set ultra strict warnings for developers, if requested +cpp_suppressions = [] +if get_option('strict') + if cpp.get_id() == 'clang' + cpp_suppressions += [ + '-Wno-documentation-unknown-command', + '-Wno-format-nonliteral', + '-Wno-nullability-extension', + '-Wno-padded', + ] + elif cpp.get_id() == 'gcc' + cpp_suppressions += [ + '-Wno-abi-tag', + '-Wno-float-equal', + '-Wno-inline', + '-Wno-multiple-inheritance', + '-Wno-padded', + '-Wno-suggest-attribute=pure', + '-Wno-switch-default', + '-Wno-unused-const-variable', + ] + elif cpp.get_id() == 'msvc' + cpp_suppressions += [ + '/wd4355', # 'this' used in base member initializer list + '/wd4571', # structured exceptions are no longer caught + '/wd4623', # default constructor implicitly deleted + '/wd4625', # copy constructor implicitly deleted + '/wd4626', # assignment operator implicitly deleted + '/wd4710', # function not inlined + '/wd4868', # may not enforce left-to-right evaluation order + '/wd5026', # move constructor implicitly deleted + '/wd5027', # move assignment operator implicitly deleted + ] + endif +else + if cpp.get_id() == 'clang' + cpp_suppressions += [ + '-Wno-nullability-extension', + ] + endif +endif + +exess_cpp_args = cpp.get_supported_arguments(cpp_suppressions) + +cpp_headers = [ + 'include/sratom/sratom.hpp', +] + +serdpp_dep = dependency('serdxx-1', + version: '>= 1.0.0') + +sratompp_dep = declare_dependency( + include_directories: include_directories(['include']), + link_with: libsratom) + +pkg.generate( + description: 'C++ bindings for sratom', + filebase: versioned_cpp_name, + name: 'Sratompp', + subdirs: [versioned_cpp_name], + version: meson.project_version()) + +# Install headers to a versioned include directory +install_headers(cpp_headers, subdir: versioned_cpp_name / 'sratom') + +cpp_test_args = cpp.get_supported_arguments(['-Wno-float-equal']) + +test('bindings', + executable('test_sratom_hpp', + 'test/test_sratom_hpp.cpp', + include_directories: include_directories(['include']), + cpp_args: exess_cpp_args + cpp_test_args, + dependencies: [sratom_dep, serdpp_dep, sratompp_dep]), + suite: 'cpp') diff --git a/bindings/cpp/test/test_sratom_hpp.cpp b/bindings/cpp/test/test_sratom_hpp.cpp new file mode 100644 index 0000000..2ada056 --- /dev/null +++ b/bindings/cpp/test/test_sratom_hpp.cpp @@ -0,0 +1,25 @@ +/* + Copyright 2018-2021 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "sratom/sratom.hpp" + +int +main() +{ + return 0; +} |