From 6ac4f3bcc0bd1306bf1a8dd1bebae1a00081c6b7 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 8 Jan 2019 15:30:40 +0300 Subject: Add support for filtering during manifest parsing and serialization --- libbutl/manifest-serializer.mxx | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'libbutl/manifest-serializer.mxx') diff --git a/libbutl/manifest-serializer.mxx b/libbutl/manifest-serializer.mxx index 66ca398..6a87656 100644 --- a/libbutl/manifest-serializer.mxx +++ b/libbutl/manifest-serializer.mxx @@ -11,8 +11,9 @@ #ifndef __cpp_lib_modules #include #include -#include // size_t -#include // runtime_error +#include // size_t +#include // runtime_error +#include #endif // Other includes. @@ -42,8 +43,23 @@ LIBBUTL_MODEXPORT namespace butl class LIBBUTL_SYMEXPORT manifest_serializer { public: - manifest_serializer (std::ostream& os, const std::string& name) - : os_ (os), name_ (name) {} + // The filter, if specified, is called by next() prior to serializing the + // pair into the stream. If the filter returns false, then the pair is + // discarded. + // + // Note that currently there is no way for the filter to modify the name + // or value. If we ever need this functionality, then we can add an + // "extended" filter alternative with two "receiving" arguments: + // + // bool (..., optional& n, optional& v); + // + using filter_function = bool (const std::string& name, + const std::string& value); + + manifest_serializer (std::ostream& os, + const std::string& name, + std::function filter = {}) + : os_ (os), name_ (name), filter_ (std::move (filter)) {} const std::string& name () const {return name_;} @@ -77,6 +93,9 @@ LIBBUTL_MODEXPORT namespace butl private: friend class manifest_rewriter; + void + write_next (const std::string& name, const std::string& value); + // Validate and write a name. // void @@ -105,5 +124,8 @@ LIBBUTL_MODEXPORT namespace butl private: std::ostream& os_; const std::string name_; + const std::function filter_; }; } + +#include -- cgit v1.1