From e4bc9cab8dc5e67ad8e2985ded1d3c6f2ad70470 Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:05:40 +0000 Subject: [PATCH 1/6] + cleanup --- include/mapnik/boolean.hpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/include/mapnik/boolean.hpp b/include/mapnik/boolean.hpp index 8d7520bad..333152c9b 100644 --- a/include/mapnik/boolean.hpp +++ b/include/mapnik/boolean.hpp @@ -26,38 +26,39 @@ #include #include #include +#include namespace mapnik { -/** Helper for class bool */ + +// Helper for class bool class boolean { public: - boolean(): b_(false) {} - boolean(bool b) : b_(b) {} - boolean(boolean const& b) : b_(b.b_) {} + boolean() + : b_(false) {} + boolean(bool b) + : b_(b) {} + explicit boolean(boolean const& b) + : b_(b.b_) {} operator bool() const { return b_; } - boolean & operator = (boolean const& other) + boolean & operator =(boolean const& other) { + if (this == &other) + return *this; b_ = other.b_; - return * this; - } - - boolean & operator = (bool other) - { - b_ = other; - return * this; + return *this; } private: bool b_; }; -/** Special stream input operator for boolean values */ +// Special stream input operator for boolean values template std::basic_istream & operator >> ( std::basic_istream & s, boolean & b ) From f1d142a3c13c4a90abfaa280483983eb86196012 Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:06:15 +0000 Subject: [PATCH 2/6] + cleanup --- include/mapnik/enumeration.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/mapnik/enumeration.hpp b/include/mapnik/enumeration.hpp index b1132c5c4..229bc0906 100644 --- a/include/mapnik/enumeration.hpp +++ b/include/mapnik/enumeration.hpp @@ -135,19 +135,19 @@ protected: * @endcode */ -template +template class MAPNIK_DECL enumeration { public: typedef ENUM native_type; enumeration() - : value_() {} + : value_() {} enumeration( ENUM v ) - : value_(v) {} + : value_(v) {} - enumeration( const enumeration & other ) - : value_(other.value_) {} + enumeration( enumeration const& other ) + : value_(other.value_) {} /** Assignment operator for native enum values. */ void operator=(ENUM v) @@ -156,7 +156,7 @@ public: } /** Assignment operator. */ - void operator=(const enumeration & other) + void operator=(enumeration const& other) { value_ = other.value_; } @@ -220,7 +220,7 @@ public: { from_string( word ); } - catch (const illegal_enum_value &) + catch (illegal_enum_value const&) { is.setstate(std::ios::failbit); } @@ -256,8 +256,8 @@ public: if (our_strings_[i] == 0 ) { std::cerr << "### FATAL: Not enough strings for enum " - << our_name_ << " defined in file '" << filename - << "' at line " << line_no; + << our_name_ << " defined in file '" << filename + << "' at line " << line_no; } } if ( std::string("") != our_strings_[THE_MAX]) From 982aed825f20bb465a92118724b61c8a2092f7ff Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:12:43 +0000 Subject: [PATCH 3/6] + implement string2bool converters --- include/mapnik/util/conversions.hpp | 3 ++ src/conversions.cpp | 44 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/include/mapnik/util/conversions.hpp b/include/mapnik/util/conversions.hpp index 7fcddea73..522633d4a 100644 --- a/include/mapnik/util/conversions.hpp +++ b/include/mapnik/util/conversions.hpp @@ -32,6 +32,9 @@ namespace mapnik { namespace util { +MAPNIK_DECL bool string2bool(const char * value, bool & result); +MAPNIK_DECL bool string2bool(std::string const& value, bool & result); + MAPNIK_DECL bool string2int(const char * value, int & result); MAPNIK_DECL bool string2int(std::string const& value, int & result); diff --git a/src/conversions.cpp b/src/conversions.cpp index 63c95c74f..843599537 100644 --- a/src/conversions.cpp +++ b/src/conversions.cpp @@ -60,13 +60,53 @@ BOOST_SPIRIT_AUTO(qi, LONGLONG, qi::long_long) BOOST_SPIRIT_AUTO(qi, FLOAT, qi::float_) BOOST_SPIRIT_AUTO(qi, DOUBLE, qi::double_) + + +struct bool_symbols : qi::symbols +{ + bool_symbols() + { + add("true",true) + ("false",false) + ("yes",true) + ("no",false) + ("on",true) + ("off",false) + ("1",true) + ("0",false); + } +}; + +bool string2bool(const char * value, bool & result) +{ + using boost::spirit::qi::no_case; + size_t length = strlen(value); + if (length < 1 || value == NULL) + return false; + const char *iter = value; + const char *end = value + length + bool r = qi::phrase_parse(iter,end, no_case[bool_symbols()] ,ascii::space,result); + return r && (iter == end); +} + +bool string2bool(std::string const& value, bool & result) +{ + using boost::spirit::qi::no_case; + if (value.empty()) + return false; + std::string::const_iterator str_beg = value.begin(); + std::string::const_iterator str_end = value.end(); + bool r = qi::phrase_parse(str_beg,str_end,no_case[bool_symbols()],ascii::space,result); + return r && (str_beg == str_end); +} + bool string2int(const char * value, int & result) { size_t length = strlen(value); if (length < 1 || value == NULL) return false; - const char *iter = value; - const char *end = value + length; + const char *iter = value; + const char *end = value + length; bool r = qi::phrase_parse(iter,end,INTEGER,ascii::space,result); return r && (iter == end); } From e8cc64a9a7944087aabc60c844dd64e5a7318268 Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:14:08 +0000 Subject: [PATCH 4/6] + fast_cast to mapnik::boolean specialization --- src/xml_tree.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/xml_tree.cpp b/src/xml_tree.cpp index 5729a9ea9..651a3d147 100644 --- a/src/xml_tree.cpp +++ b/src/xml_tree.cpp @@ -56,6 +56,15 @@ inline boost::optional fast_cast(xml_tree const& tree, std::string const& val } } +template <> +inline boost::optional fast_cast(xml_tree const& tree, std::string const& value) +{ + bool result; + if (mapnik::util::string2bool(value, result)) + return boost::optional(result); + return boost::optional(); +} + template <> inline boost::optional fast_cast(xml_tree const& tree, std::string const& value) { From 96857ca291611e5a822d631b71d5170ff065a5fc Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:14:47 +0000 Subject: [PATCH 5/6] + wrap xml_attribute.value into std::string to avoid crashes when linking to libc++ on os x (TODO) --- src/xml_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xml_tree.cpp b/src/xml_tree.cpp index 651a3d147..fe3d36ade 100644 --- a/src/xml_tree.cpp +++ b/src/xml_tree.cpp @@ -402,7 +402,7 @@ boost::optional xml_node::get_opt_attr(std::string const& name) const std::map::const_iterator itr = attributes_.find(name); if (itr == attributes_.end()) return boost::optional(); itr->second.processed = true; - boost::optional result = fast_cast(tree_, itr->second.value); + boost::optional result = fast_cast(tree_, std::string(itr->second.value)); if (!result) { throw config_error(std::string("Failed to parse attribute '") + From b4723b6dd16615acf2ce15250940431084aeed4e Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 15 Jan 2013 14:34:38 +0000 Subject: [PATCH 6/6] + fix typo --- src/conversions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conversions.cpp b/src/conversions.cpp index 843599537..8b85440cd 100644 --- a/src/conversions.cpp +++ b/src/conversions.cpp @@ -84,7 +84,7 @@ bool string2bool(const char * value, bool & result) if (length < 1 || value == NULL) return false; const char *iter = value; - const char *end = value + length + const char *end = value + length; bool r = qi::phrase_parse(iter,end, no_case[bool_symbols()] ,ascii::space,result); return r && (iter == end); }