diff options
-rw-r--r-- | bindings/cpp/include/sratom/sratom.hpp | 28 | ||||
-rw-r--r-- | bindings/cpp/test/test_sratom_hpp.cpp | 34 |
2 files changed, 48 insertions, 14 deletions
diff --git a/bindings/cpp/include/sratom/sratom.hpp b/bindings/cpp/include/sratom/sratom.hpp index 5cd337e..b12d5e1 100644 --- a/bindings/cpp/include/sratom/sratom.hpp +++ b/bindings/cpp/include/sratom/sratom.hpp @@ -27,8 +27,8 @@ #include <sstream> /** - @defgroup sratomxx Sratomxx - C++ bindings for Sratom, a library for serialising LV2 atoms. + @defgroup sratompp Sratompp + C++ bindings for Sratom, a library for reading and writing LV2 atoms. @{ */ @@ -37,13 +37,13 @@ namespace sratom { namespace detail { struct Deleter { - void operator()(void* ptr) { sratom_free(ptr); } + void operator()(void* const ptr) { sratom_free(ptr); } }; } // namespace detail /// Flags to control how atoms are written and read -typedef enum { +enum class Flag { /** Write the main subject with a label. @@ -67,7 +67,7 @@ typedef enum { If set, top level atoms will be written as a single long line. */ terse = SRATOM_TERSE, -} Flag; +}; using Flags = serd::detail::Flags<Flag>; @@ -82,7 +82,7 @@ public: int write(const serd::Env& env, serd::SinkView sink, const LV2_Atom& atom, - const Flags flags = {}) + const Flags flags) { return sratom_dump_atom( cobj(), env.cobj(), sink.cobj(), nullptr, nullptr, &atom, flags); @@ -93,7 +93,7 @@ public: const serd::Node& subject, const serd::Node& predicate, const LV2_Atom& atom, - const Flags flags = {}) + const Flags flags) { return sratom_dump_atom(cobj(), env.cobj(), @@ -111,7 +111,7 @@ public: LV2_URID type, uint32_t size, const void* body, - const Flags flags = {}) + const Flags flags) { return sratom_dump(cobj(), env.cobj(), @@ -126,9 +126,9 @@ public: std::string to_string(const serd::Env& env, const LV2_Atom& atom, - const Flags flags = {}) + const Flags flags) { - char* c_str = sratom_to_string(cobj(), env.cobj(), &atom, flags); + char* const c_str = sratom_to_string(cobj(), env.cobj(), &atom, flags); std::string result = c_str; sratom_free(c_str); return result; @@ -137,7 +137,7 @@ public: private: static size_t string_sink(const void* buf, size_t, size_t nmemb, void* stream) { - std::string& string = *(std::string*)stream; + std::string& string = *static_cast<std::string*>(stream); string.insert(string.size(), static_cast<const char*>(buf), nmemb); return nmemb; } @@ -153,7 +153,7 @@ public: : BasicWrapper(sratom_loader_new(world.cobj(), &map)) {} - int read(const serd::Optional<serd::Node>& base_uri, + int load(const serd::Optional<serd::Node>& base_uri, LV2_Atom_Forge& forge, const serd::Model& model, const serd::Node& node) @@ -162,12 +162,12 @@ public: cobj(), base_uri.cobj(), &forge, model.cobj(), node.cobj()); } - AtomPtr read_string(serd::Env& env, const char* str) + AtomPtr from_string(serd::Env& env, const char* const str) { return AtomPtr{sratom_from_string(cobj(), env.cobj(), str)}; } - AtomPtr read_model(const serd::Optional<serd::Node>& base_uri, + AtomPtr from_model(const serd::Optional<serd::Node>& base_uri, const serd::Model& model, const serd::Node& subject) { diff --git a/bindings/cpp/test/test_sratom_hpp.cpp b/bindings/cpp/test/test_sratom_hpp.cpp index 2ada056..4825f8d 100644 --- a/bindings/cpp/test/test_sratom_hpp.cpp +++ b/bindings/cpp/test/test_sratom_hpp.cpp @@ -16,10 +16,44 @@ #undef NDEBUG +#include "test_utils.h" + +#include "lv2/urid/urid.h" +#include "serd/serd.hpp" #include "sratom/sratom.hpp" +namespace sratom { +namespace test { +namespace { + +void +test_dumper() +{ + serd::World world; + serd::Env env; + Uris uris{nullptr, 0}; + LV2_URID_Map map{&uris, urid_map}; + LV2_URID_Unmap unmap{&uris, urid_unmap}; + Dumper dumper{world, map, unmap}; + + const LV2_Atom_Int a_int = {{sizeof(int32_t), urid_map(&uris, LV2_ATOM__Int)}, + 42}; + + assert(dumper.to_string(env, a_int.atom, {}) == + "( \"42\"^^<http://www.w3.org/2001/XMLSchema#int> ) .\n"); + + assert(dumper.to_string(env, a_int.atom, sratom::Flag::pretty_numbers) == + "( 42 ) .\n"); +} + +} // namespace +} // namespace test +} // namespace sratom + int main() { + sratom::test::test_dumper(); + return 0; } |