From 3ee609112a8de80edf87df6098899120b7711661 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Mon, 29 Sep 2014 16:48:19 -0700 Subject: [PATCH] using pimpl to isolate boost::regex headers - refs #2439 --- include/mapnik/evaluate_global_attributes.hpp | 36 +---- include/mapnik/expression_evaluator.hpp | 21 +-- include/mapnik/expression_grammar_impl.hpp | 12 +- include/mapnik/expression_node.hpp | 67 +++------ src/expression_node.cpp | 142 +++++++++++++++--- src/expression_string.cpp | 32 +--- 6 files changed, 156 insertions(+), 154 deletions(-) diff --git a/include/mapnik/evaluate_global_attributes.hpp b/include/mapnik/evaluate_global_attributes.hpp index d3e3626df..f131ee7c1 100644 --- a/include/mapnik/evaluate_global_attributes.hpp +++ b/include/mapnik/evaluate_global_attributes.hpp @@ -34,12 +34,6 @@ #include #include -#if defined(BOOST_REGEX_HAS_ICU) -#include -#else -#include -#endif - namespace mapnik { namespace { @@ -107,24 +101,13 @@ struct evaluate_expression : util::static_visitor value_type operator() (regex_match_node const& x) const { value_type v = util::apply_visitor(*this, x.expr); -#if defined(BOOST_REGEX_HAS_ICU) - return boost::u32regex_match(v.to_unicode(),x.pattern); -#else - return boost::regex_match(v.to_string(),x.pattern); -#endif - + return x.apply(v); } value_type operator() (regex_replace_node const& x) const { value_type v = util::apply_visitor(*this, x.expr); -#if defined(BOOST_REGEX_HAS_ICU) - return boost::u32regex_replace(v.to_unicode(),x.pattern,x.format); -#else - std::string repl = boost::regex_replace(v.to_string(),x.pattern,x.format); - mapnik::transcoder tr_("utf8"); - return tr_.transcode(repl.c_str()); -#endif + return x.apply(v); } value_type operator() (unary_function_call const& call) const @@ -206,24 +189,13 @@ struct evaluate_expression : util::static_visitor value_type operator() (regex_match_node const& x) const { value_type v = util::apply_visitor(*this, x.expr); -#if defined(BOOST_REGEX_HAS_ICU) - return boost::u32regex_match(v.to_unicode(),x.pattern); -#else - return boost::regex_match(v.to_string(),x.pattern); -#endif - + return x.apply(v); } value_type operator() (regex_replace_node const& x) const { value_type v = util::apply_visitor(*this, x.expr); -#if defined(BOOST_REGEX_HAS_ICU) - return boost::u32regex_replace(v.to_unicode(),x.pattern,x.format); -#else - std::string repl = boost::regex_replace(v.to_string(),x.pattern,x.format); - mapnik::transcoder tr_("utf8"); - return tr_.transcode(repl.c_str()); -#endif + return x.apply(v); } value_type operator() (unary_function_call const& call) const diff --git a/include/mapnik/expression_evaluator.hpp b/include/mapnik/expression_evaluator.hpp index 01f9d7815..c8b94a95e 100644 --- a/include/mapnik/expression_evaluator.hpp +++ b/include/mapnik/expression_evaluator.hpp @@ -29,12 +29,6 @@ #include #include #include -// boost -#if defined(BOOST_REGEX_HAS_ICU) -#include -#else -#include -#endif namespace mapnik { @@ -130,24 +124,13 @@ struct evaluate : util::static_visitor value_type operator() (regex_match_node const& x) const { value_type v = util::apply_visitor(*this, x.expr); -#if defined(BOOST_REGEX_HAS_ICU) - return boost::u32regex_match(v.to_unicode(),x.pattern); -#else - return boost::regex_match(v.to_string(),x.pattern); -#endif - + return x.apply(v); } value_type operator() (regex_replace_node const& x) const { value_type v = util::apply_visitor(*this, x.expr); -#if defined(BOOST_REGEX_HAS_ICU) - return boost::u32regex_replace(v.to_unicode(),x.pattern,x.format); -#else - std::string repl = boost::regex_replace(v.to_string(),x.pattern,x.format); - mapnik::transcoder tr_("utf8"); - return tr_.transcode(repl.c_str()); -#endif + return x.apply(v); } value_type operator() (unary_function_call const& call) const diff --git a/include/mapnik/expression_grammar_impl.hpp b/include/mapnik/expression_grammar_impl.hpp index 8edb01313..b56c2cf79 100644 --- a/include/mapnik/expression_grammar_impl.hpp +++ b/include/mapnik/expression_grammar_impl.hpp @@ -69,21 +69,13 @@ binary_function_types::binary_function_types() template expr_node regex_match_impl::operator() (T0 & node, T1 const& pattern) const { -#if defined(BOOST_REGEX_HAS_ICU) - return regex_match_node(node,tr_.transcode(pattern.c_str())); -#else - return regex_match_node(node,pattern); -#endif + return regex_match_node(tr_,node,pattern); } template expr_node regex_replace_impl::operator() (T0 & node, T1 const& pattern, T2 const& format) const { -#if defined(BOOST_REGEX_HAS_ICU) - return regex_replace_node(node,tr_.transcode(pattern.c_str()),tr_.transcode(format.c_str())); -#else - return regex_replace_node(node,pattern,format); -#endif + return regex_replace_node(tr_,node,pattern,format); } template diff --git a/include/mapnik/expression_node.hpp b/include/mapnik/expression_node.hpp index 95e5fc1fc..0eb093c5c 100644 --- a/include/mapnik/expression_node.hpp +++ b/include/mapnik/expression_node.hpp @@ -30,13 +30,6 @@ #include #include -// boost -#if defined(BOOST_REGEX_HAS_ICU) -#include -#else -#include -#endif - namespace mapnik { @@ -87,43 +80,6 @@ struct binary_node expr_node left,right; }; -#if defined(BOOST_REGEX_HAS_ICU) - -struct regex_match_node -{ - regex_match_node (expr_node const& a, mapnik::value_unicode_string const& ustr); - expr_node expr; - boost::u32regex pattern; -}; - - -struct regex_replace_node -{ - regex_replace_node (expr_node const& a, mapnik::value_unicode_string const& ustr, mapnik::value_unicode_string const& f); - expr_node expr; - boost::u32regex pattern; - mapnik::value_unicode_string format; -}; - -#else - -struct regex_match_node -{ - regex_match_node (expr_node const& a, std::string const& str); - expr_node expr; - boost::regex pattern; -}; - - -struct regex_replace_node -{ - regex_replace_node (expr_node const& a, std::string const& str, std::string const& f); - expr_node expr; - boost::regex pattern; - std::string format; -}; -#endif - struct unary_function_call { using argument_type = expr_node; @@ -146,6 +102,29 @@ struct binary_function_call argument_type arg2; }; +// pimpl +struct _regex_match_impl; +struct _regex_replace_impl; + +struct regex_match_node +{ + regex_match_node(transcoder const& tr, expr_node const& a, std::string const& ustr); + mapnik::value apply(mapnik::value const& v) const; + std::string to_string() const; + expr_node expr; + // TODO - use unique_ptr once https://github.com/mapnik/mapnik/issues/2457 is fixed + std::shared_ptr<_regex_match_impl> impl_; +}; + +struct regex_replace_node +{ + regex_replace_node(transcoder const& tr, expr_node const& a, std::string const& ustr, std::string const& f); + mapnik::value apply(mapnik::value const& v) const; + std::string to_string() const; + expr_node expr; + // TODO - use unique_ptr once https://github.com/mapnik/mapnik/issues/2457 is fixed + std::shared_ptr<_regex_replace_impl> impl_; +}; inline expr_node & operator- (expr_node& expr) { diff --git a/src/expression_node.cpp b/src/expression_node.cpp index aeae3d745..69cb53074 100644 --- a/src/expression_node.cpp +++ b/src/expression_node.cpp @@ -22,31 +22,135 @@ #include #include +#include + +#if defined(BOOST_REGEX_HAS_ICU) +#include +#else +#include +#endif namespace mapnik { +struct _regex_match_impl : noncopyable { #if defined(BOOST_REGEX_HAS_ICU) - -regex_match_node::regex_match_node (expr_node const& a, mapnik::value_unicode_string const& ustr) - : expr(a), - pattern(boost::make_u32regex(ustr)) {} - -regex_replace_node::regex_replace_node (expr_node const& a, mapnik::value_unicode_string const& ustr, mapnik::value_unicode_string const& f) - : expr(a), - pattern(boost::make_u32regex(ustr)), - format(f) {} - + _regex_match_impl(value_unicode_string const& ustr) : + pattern_(boost::make_u32regex(ustr)) {} + boost::u32regex pattern_; #else - -regex_match_node::regex_match_node (expr_node const& a, std::string const& str) - : expr(a), - pattern(str) {} - -regex_replace_node::regex_replace_node (expr_node const& a, std::string const& str, std::string const& f) - : expr(a), - pattern(str), - format(f) {} + _regex_match_impl(std::string const& ustr) : + pattern_(ustr) {} + boost::regex pattern_; #endif +}; +struct _regex_replace_impl : noncopyable { +#if defined(BOOST_REGEX_HAS_ICU) + _regex_replace_impl(value_unicode_string const& ustr, value_unicode_string const& f) : + pattern_(boost::make_u32regex(ustr)), + format_(f) {} + boost::u32regex pattern_; + value_unicode_string format_; +#else + _regex_replace_impl(std::string const& ustr,std::string const& f) : + pattern_(ustr), + format_(f) {} + boost::regex pattern_; + std::string format_; +#endif +}; + + +regex_match_node::regex_match_node(transcoder const& tr, + expr_node const& a, + std::string const& ustr) + : expr(a), + impl_(new _regex_match_impl( +#if defined(BOOST_REGEX_HAS_ICU) + tr.transcode(ustr.c_str()) +#else + ustr +#endif + )) {} + +value regex_match_node::apply(value const& v) const +{ + auto const& pattern = impl_.get()->pattern_; +#if defined(BOOST_REGEX_HAS_ICU) + return boost::u32regex_match(v.to_unicode(),pattern); +#else + return boost::regex_match(v.to_string(),pattern); +#endif } + +std::string regex_match_node::to_string() const +{ + std::string str_; + str_ +=".match('"; + auto const& pattern = impl_.get()->pattern_; +#if defined(BOOST_REGEX_HAS_ICU) + std::string utf8; + value_unicode_string ustr = value_unicode_string::fromUTF32( &pattern.str()[0], pattern.str().length()); + to_utf8(ustr,utf8); + str_ += utf8; +#else + str_ += pattern.str(); +#endif + str_ +="')"; + return str_; +} + +regex_replace_node::regex_replace_node(transcoder const& tr, + expr_node const& a, + std::string const& ustr, + std::string const& f) + : expr(a), + impl_(new _regex_replace_impl( +#if defined(BOOST_REGEX_HAS_ICU) + tr.transcode(ustr.c_str()), + tr.transcode(f.c_str()) +#else + ustr, + f +#endif + )) {} + +value regex_replace_node::apply(value const& v) const +{ + auto const& pattern = impl_.get()->pattern_; + auto const& format = impl_.get()->format_; +#if defined(BOOST_REGEX_HAS_ICU) + return boost::u32regex_replace(v.to_unicode(),pattern,format); +#else + std::string repl = boost::regex_replace(v.to_string(),pattern,format); + transcoder tr_("utf8"); + return tr_.transcode(repl.c_str()); +#endif +} + +std::string regex_replace_node::to_string() const +{ + std::string str_; + str_ +=".replace("; + str_ += "'"; + auto const& pattern = impl_.get()->pattern_; + auto const& format = impl_.get()->format_; +#if defined(BOOST_REGEX_HAS_ICU) + std::string utf8; + value_unicode_string ustr = value_unicode_string::fromUTF32( &pattern.str()[0], pattern.str().length()); + to_utf8(ustr,utf8); + str_ += utf8; + str_ +="','"; + to_utf8(format ,utf8); + str_ += utf8; +#else + str_ += pattern.str(); + str_ +="','"; + str_ += format; +#endif + str_ +="')"; + return str_; +} + +} \ No newline at end of file diff --git a/src/expression_string.cpp b/src/expression_string.cpp index 568c79ed4..a5d1386b7 100644 --- a/src/expression_string.cpp +++ b/src/expression_string.cpp @@ -29,10 +29,6 @@ #include #include #include -// boost -#if defined(BOOST_REGEX_HAS_ICU) -#include // for u32regex -#endif namespace mapnik { @@ -94,37 +90,13 @@ struct expression_string : util::static_visitor void operator() (regex_match_node const & x) const { util::apply_visitor(*this,x.expr); - str_ +=".match('"; -#if defined(BOOST_REGEX_HAS_ICU) - std::string utf8; - mapnik::value_unicode_string ustr = mapnik::value_unicode_string::fromUTF32( &x.pattern.str()[0] ,x.pattern.str().length()); - to_utf8(ustr,utf8); - str_ += utf8; -#else - str_ += x.pattern.str(); -#endif - str_ +="')"; + str_ += x.to_string(); } void operator() (regex_replace_node const & x) const { util::apply_visitor(*this,x.expr); - str_ +=".replace("; - str_ += "'"; -#if defined(BOOST_REGEX_HAS_ICU) - std::string utf8; - mapnik::value_unicode_string ustr = mapnik::value_unicode_string::fromUTF32( &x.pattern.str()[0] ,x.pattern.str().length()); - to_utf8(ustr,utf8); - str_ += utf8; - str_ +="','"; - to_utf8(x.format ,utf8); - str_ += utf8; -#else - str_ += x.pattern.str(); - str_ +="','"; - str_ += x.format; -#endif - str_ +="')"; + str_ += x.to_string(); } void operator() (unary_function_call const& call) const