using pimpl to isolate boost::regex headers - refs #2439

This commit is contained in:
Dane Springmeyer 2014-09-29 16:48:19 -07:00
parent aaec8058fa
commit 3ee609112a
6 changed files with 156 additions and 154 deletions

View file

@ -34,12 +34,6 @@
#include <mapnik/function_call.hpp> #include <mapnik/function_call.hpp>
#include <mapnik/util/variant.hpp> #include <mapnik/util/variant.hpp>
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp>
#else
#include <boost/regex.hpp>
#endif
namespace mapnik { namespace mapnik {
namespace { namespace {
@ -107,24 +101,13 @@ struct evaluate_expression : util::static_visitor<T>
value_type operator() (regex_match_node const& x) const value_type operator() (regex_match_node const& x) const
{ {
value_type v = util::apply_visitor(*this, x.expr); value_type v = util::apply_visitor(*this, x.expr);
#if defined(BOOST_REGEX_HAS_ICU) return x.apply(v);
return boost::u32regex_match(v.to_unicode(),x.pattern);
#else
return boost::regex_match(v.to_string(),x.pattern);
#endif
} }
value_type operator() (regex_replace_node const& x) const value_type operator() (regex_replace_node const& x) const
{ {
value_type v = util::apply_visitor(*this, x.expr); value_type v = util::apply_visitor(*this, x.expr);
#if defined(BOOST_REGEX_HAS_ICU) return x.apply(v);
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
} }
value_type operator() (unary_function_call const& call) const value_type operator() (unary_function_call const& call) const
@ -206,24 +189,13 @@ struct evaluate_expression<T, boost::none_t> : util::static_visitor<T>
value_type operator() (regex_match_node const& x) const value_type operator() (regex_match_node const& x) const
{ {
value_type v = util::apply_visitor(*this, x.expr); value_type v = util::apply_visitor(*this, x.expr);
#if defined(BOOST_REGEX_HAS_ICU) return x.apply(v);
return boost::u32regex_match(v.to_unicode(),x.pattern);
#else
return boost::regex_match(v.to_string(),x.pattern);
#endif
} }
value_type operator() (regex_replace_node const& x) const value_type operator() (regex_replace_node const& x) const
{ {
value_type v = util::apply_visitor(*this, x.expr); value_type v = util::apply_visitor(*this, x.expr);
#if defined(BOOST_REGEX_HAS_ICU) return x.apply(v);
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
} }
value_type operator() (unary_function_call const& call) const value_type operator() (unary_function_call const& call) const

View file

@ -29,12 +29,6 @@
#include <mapnik/expression_node.hpp> #include <mapnik/expression_node.hpp>
#include <mapnik/function_call.hpp> #include <mapnik/function_call.hpp>
#include <mapnik/util/variant.hpp> #include <mapnik/util/variant.hpp>
// boost
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp>
#else
#include <boost/regex.hpp>
#endif
namespace mapnik namespace mapnik
{ {
@ -130,24 +124,13 @@ struct evaluate : util::static_visitor<T1>
value_type operator() (regex_match_node const& x) const value_type operator() (regex_match_node const& x) const
{ {
value_type v = util::apply_visitor(*this, x.expr); value_type v = util::apply_visitor(*this, x.expr);
#if defined(BOOST_REGEX_HAS_ICU) return x.apply(v);
return boost::u32regex_match(v.to_unicode(),x.pattern);
#else
return boost::regex_match(v.to_string(),x.pattern);
#endif
} }
value_type operator() (regex_replace_node const& x) const value_type operator() (regex_replace_node const& x) const
{ {
value_type v = util::apply_visitor(*this, x.expr); value_type v = util::apply_visitor(*this, x.expr);
#if defined(BOOST_REGEX_HAS_ICU) return x.apply(v);
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
} }
value_type operator() (unary_function_call const& call) const value_type operator() (unary_function_call const& call) const

View file

@ -69,21 +69,13 @@ binary_function_types::binary_function_types()
template <typename T0,typename T1> template <typename T0,typename T1>
expr_node regex_match_impl::operator() (T0 & node, T1 const& pattern) const expr_node regex_match_impl::operator() (T0 & node, T1 const& pattern) const
{ {
#if defined(BOOST_REGEX_HAS_ICU) return regex_match_node(tr_,node,pattern);
return regex_match_node(node,tr_.transcode(pattern.c_str()));
#else
return regex_match_node(node,pattern);
#endif
} }
template <typename T0,typename T1,typename T2> template <typename T0,typename T1,typename T2>
expr_node regex_replace_impl::operator() (T0 & node, T1 const& pattern, T2 const& format) const 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(tr_,node,pattern,format);
return regex_replace_node(node,tr_.transcode(pattern.c_str()),tr_.transcode(format.c_str()));
#else
return regex_replace_node(node,pattern,format);
#endif
} }
template <typename Iterator> template <typename Iterator>

View file

@ -30,13 +30,6 @@
#include <mapnik/function_call.hpp> #include <mapnik/function_call.hpp>
#include <mapnik/expression_node_types.hpp> #include <mapnik/expression_node_types.hpp>
// boost
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp>
#else
#include <boost/regex.hpp>
#endif
namespace mapnik namespace mapnik
{ {
@ -87,43 +80,6 @@ struct binary_node
expr_node left,right; 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 struct unary_function_call
{ {
using argument_type = expr_node; using argument_type = expr_node;
@ -146,6 +102,29 @@ struct binary_function_call
argument_type arg2; 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) inline expr_node & operator- (expr_node& expr)
{ {

View file

@ -22,31 +22,135 @@
#include <mapnik/expression_node.hpp> #include <mapnik/expression_node.hpp>
#include <mapnik/value_types.hpp> #include <mapnik/value_types.hpp>
#include <mapnik/noncopyable.hpp>
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp>
#else
#include <boost/regex.hpp>
#endif
namespace mapnik namespace mapnik
{ {
struct _regex_match_impl : noncopyable {
#if defined(BOOST_REGEX_HAS_ICU) #if defined(BOOST_REGEX_HAS_ICU)
_regex_match_impl(value_unicode_string const& ustr) :
regex_match_node::regex_match_node (expr_node const& a, mapnik::value_unicode_string const& ustr) pattern_(boost::make_u32regex(ustr)) {}
: expr(a), boost::u32regex pattern_;
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) {}
#else #else
_regex_match_impl(std::string const& ustr) :
regex_match_node::regex_match_node (expr_node const& a, std::string const& str) pattern_(ustr) {}
: expr(a), boost::regex pattern_;
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) {}
#endif #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_;
}
}

View file

@ -29,10 +29,6 @@
#include <mapnik/attribute.hpp> #include <mapnik/attribute.hpp>
#include <mapnik/value_types.hpp> #include <mapnik/value_types.hpp>
#include <mapnik/value.hpp> #include <mapnik/value.hpp>
// boost
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp> // for u32regex
#endif
namespace mapnik namespace mapnik
{ {
@ -94,37 +90,13 @@ struct expression_string : util::static_visitor<void>
void operator() (regex_match_node const & x) const void operator() (regex_match_node const & x) const
{ {
util::apply_visitor(*this,x.expr); util::apply_visitor(*this,x.expr);
str_ +=".match('"; str_ += x.to_string();
#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_ +="')";
} }
void operator() (regex_replace_node const & x) const void operator() (regex_replace_node const & x) const
{ {
util::apply_visitor(*this,x.expr); util::apply_visitor(*this,x.expr);
str_ +=".replace("; str_ += x.to_string();
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_ +="')";
} }
void operator() (unary_function_call const& call) const void operator() (unary_function_call const& call) const