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/util/variant.hpp>
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp>
#else
#include <boost/regex.hpp>
#endif
namespace mapnik {
namespace {
@ -107,24 +101,13 @@ struct evaluate_expression : util::static_visitor<T>
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<T, boost::none_t> : util::static_visitor<T>
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

View file

@ -29,12 +29,6 @@
#include <mapnik/expression_node.hpp>
#include <mapnik/function_call.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
{
@ -130,24 +124,13 @@ struct evaluate : util::static_visitor<T1>
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

View file

@ -69,21 +69,13 @@ binary_function_types::binary_function_types()
template <typename T0,typename T1>
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 <typename T0,typename T1,typename T2>
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 <typename Iterator>

View file

@ -30,13 +30,6 @@
#include <mapnik/function_call.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
{
@ -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)
{

View file

@ -22,31 +22,135 @@
#include <mapnik/expression_node.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
{
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_;
}
}

View file

@ -29,10 +29,6 @@
#include <mapnik/attribute.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/value.hpp>
// boost
#if defined(BOOST_REGEX_HAS_ICU)
#include <boost/regex/icu.hpp> // for u32regex
#endif
namespace mapnik
{
@ -94,37 +90,13 @@ struct expression_string : util::static_visitor<void>
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