diff --git a/include/mapnik/boolean.hpp b/include/mapnik/boolean.hpp new file mode 100644 index 000000000..dc74d3d8b --- /dev/null +++ b/include/mapnik/boolean.hpp @@ -0,0 +1,90 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2011 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ +#ifndef MAPNIK_BOOLEAN_HPP +#define MAPNIK_BOOLEAN_HPP +#include + +namespace mapnik +{ +/** Helper for class bool */ +class boolean { +public: + boolean() {} + boolean(bool b) : b_(b) {} + boolean(boolean const& b) : b_(b.b_) {} + + operator bool() const + { + return b_; + } + boolean & operator = (boolean const& other) + { + b_ = other.b_; + return * this; + } + boolean & operator = (bool other) + { + b_ = other; + return * this; + } +private: + bool b_; +}; + +/** Special stream input operator for boolean values */ +template +std::basic_istream & +operator >> ( std::basic_istream & s, boolean & b ) +{ + std::string word; + s >> word; + if ( s ) + { + if ( word == "true" || word == "yes" || word == "on" || + word == "1") + { + b = true; + } + else if ( word == "false" || word == "no" || word == "off" || + word == "0") + { + b = false; + } + else + { + s.setstate( std::ios::failbit ); + } + } + return s; +} + +template +std::basic_ostream & +operator << ( std::basic_ostream & s, boolean const& b ) +{ + s << ( b ? "true" : "false" ); + return s; +} + +} + +#endif // MAPNIK_BOOLEAN_HPP diff --git a/include/mapnik/color.hpp b/include/mapnik/color.hpp index c7f2de610..ad9806276 100644 --- a/include/mapnik/color.hpp +++ b/include/mapnik/color.hpp @@ -137,6 +137,14 @@ public: std::string to_hex_string() const; }; +template +std::basic_ostream & +operator << ( std::basic_ostream & s, mapnik::color const& c ) +{ + std::string hex_string( c.to_string() ); + s << hex_string; + return s; +} } diff --git a/include/mapnik/ptree_helpers.hpp b/include/mapnik/ptree_helpers.hpp index 811317312..64167b590 100644 --- a/include/mapnik/ptree_helpers.hpp +++ b/include/mapnik/ptree_helpers.hpp @@ -23,442 +23,17 @@ #ifndef MAPNIK_PTREE_HELPERS_HPP #define MAPNIK_PTREE_HELPERS_HPP -// mapnik -#include -#include -#include -#include // boost #include -#include -#include - -// stl -#include -#include namespace mapnik { -template -inline boost::optional fast_cast(std::string const& value); - -template -T get(boost::property_tree::ptree const& node, std::string const& name, bool is_attribute, - T const& default_value); -template -T get(boost::property_tree::ptree const& node, std::string const& name, bool is_attribute); -template -T get_value(boost::property_tree::ptree const& node, std::string const& name); -template -boost::optional get_optional(boost::property_tree::ptree const& node, std::string const& name, - bool is_attribute); - -template -boost::optional get_opt_attr( boost::property_tree::ptree const& node, - std::string const& name) -{ - return get_optional( node, name, true); -} - -template -boost::optional get_opt_child( boost::property_tree::ptree const& node, - std::string const& name) -{ - return get_optional( node, name, false); -} - -template -T get_attr( boost::property_tree::ptree const& node, std::string const& name, - T const& default_value ) -{ - return get( node, name, true, default_value); -} - -template -T get_attr( boost::property_tree::ptree const& node, std::string const& name ) -{ - return get( node, name, true ); -} - - -template -std::basic_ostream & -operator << ( std::basic_ostream & s, mapnik::color const& c ) -{ - std::string hex_string( c.to_string() ); - s << hex_string; - return s; -} - -/** Helper for class bool */ -class boolean { -public: - boolean() {} - boolean(bool b) : b_(b) {} - boolean(boolean const& b) : b_(b.b_) {} - - operator bool() const - { - return b_; - } - boolean & operator = (boolean const& other) - { - b_ = other.b_; - return * this; - } - boolean & operator = (bool other) - { - b_ = other; - return * this; - } -private: - bool b_; -}; - -/** Special stream input operator for boolean values */ -template -std::basic_istream & -operator >> ( std::basic_istream & s, boolean & b ) -{ - std::string word; - s >> word; - if ( s ) - { - if ( word == "true" || word == "yes" || word == "on" || - word == "1") - { - b = true; - } - else if ( word == "false" || word == "no" || word == "off" || - word == "0") - { - b = false; - } - else - { - s.setstate( std::ios::failbit ); - } - } - return s; -} - -template -std::basic_ostream & -operator << ( std::basic_ostream & s, boolean const& b ) -{ - s << ( b ? "true" : "false" ); - return s; -} - template void set_attr(boost::property_tree::ptree & pt, std::string const& name, T const& v) { pt.put("." + name, v); } - -class boolean; - -template -struct name_trait -{ - static std::string name() - { - return ""; - } - // missing name_trait for type ... - // if you get here you are probably using a new type - // in the XML file. Just add a name trait for the new - // type below. - BOOST_STATIC_ASSERT( sizeof(T) == 0 ); -}; - -#define DEFINE_NAME_TRAIT( type, type_name ) \ - template <> \ - struct name_trait \ - { \ - static std::string name() { return std::string("type ") + type_name; } \ - }; - - -DEFINE_NAME_TRAIT( double, "double") -DEFINE_NAME_TRAIT( float, "float") -DEFINE_NAME_TRAIT( unsigned, "unsigned") -DEFINE_NAME_TRAIT( boolean, "boolean") -DEFINE_NAME_TRAIT( int, "integer" ) -DEFINE_NAME_TRAIT( std::string, "string" ) -DEFINE_NAME_TRAIT( color, "color" ) - -template -struct name_trait< mapnik::enumeration > -{ - typedef enumeration Enum; - - static std::string name() - { - std::string value_list("one of ["); - for (unsigned i = 0; i < Enum::MAX; ++i) - { - value_list += Enum::get_string( i ); - if ( i + 1 < Enum::MAX ) value_list += ", "; - } - value_list += "]"; - - return value_list; - } -}; - -template -inline boost::optional fast_cast(std::string const& value) -{ - return boost::lexical_cast( value ); -} - -template <> -inline boost::optional fast_cast(std::string const& value) -{ - int result; - if (mapnik::conversions::string2int(value,result)) - return boost::optional(result); - return boost::optional(); -} - -template <> -inline boost::optional fast_cast(std::string const& value) -{ - double result; - if (mapnik::conversions::string2double(value,result)) - return boost::optional(result); - return boost::optional(); -} - -template <> -inline boost::optional fast_cast(std::string const& value) -{ - float result; - if (mapnik::conversions::string2float(value,result)) - return boost::optional(result); - return boost::optional(); -} - -template -T get(boost::property_tree::ptree const& node, - std::string const& name, - bool is_attribute, - T const& default_value) -{ - boost::optional str; - if (is_attribute) - { - str = node.get_optional( std::string(".") + name ); - } - else - { - str = node.get_optional(name + "."); - } - - if ( str ) - { - boost::optional result = fast_cast(*str); - if (result) - { - return *result; - } - else - { - throw config_error(std::string("Failed to parse ") + - (is_attribute ? "attribute" : "child node") + " '" + - name + "'. Expected " + name_trait::name() + - " but got '" + *str + "'"); - } - } - else - { - return default_value; - } -} - -template <> -inline color get(boost::property_tree::ptree const& node, - std::string const& name, - bool is_attribute, - color const& default_value) -{ - boost::optional str; - if (is_attribute) - { - str = node.get_optional( std::string(".") + name ); - } - else - { - str = node.get_optional(name + "."); - } - - if ( str ) - { - try - { - return mapnik::color_factory::from_string((*str).c_str()); - } - catch (...) - { - throw config_error(std::string("Failed to parse ") + - (is_attribute ? "attribute" : "child node") + " '" + - name + "'. Expected " + name_trait::name() + - " but got '" + *str + "'"); - } - } - else - { - return default_value; - } -} - -template -T get(boost::property_tree::ptree const& node, std::string const& name, bool is_attribute) -{ - boost::optional str; - if (is_attribute) - { - str = node.get_optional( std::string(".") + name); - } - else - { - str = node.get_optional(name + "."); - } - - if ( ! str ) - { - throw config_error(std::string("Required ") + - (is_attribute ? "attribute " : "child node ") + - "'" + name + "' is missing"); - } - boost::optional result = fast_cast(*str); - if (result) - { - return *result; - } - else - { - throw config_error(std::string("Failed to parse ") + - (is_attribute ? "attribute" : "child node") + " '" + - name + "'. Expected " + name_trait::name() + - " but got '" + *str + "'"); - } -} - -template -T get_value(boost::property_tree::ptree const& node, std::string const& name) -{ - try - { - /* NOTE: get_child works as long as there is only one child with that name. - If this function is used this used this condition must always be satisfied. - */ - return node.get_child("").get_value(); - } - catch (boost::property_tree::ptree_bad_path) - { - /* If the XML parser did not find any non-empty data element the is no - node. But we don't want to fail here but simply return a - default constructed value of the requested type. - */ - return T(); - } - catch (...) - { - throw config_error(std::string("Failed to parse ") + - name + ". Expected " + name_trait::name() + - " but got '" + node.data() + "'"); - } -} - -template -boost::optional get_optional(boost::property_tree::ptree const& node, - std::string const& name, - bool is_attribute) -{ - boost::optional str; - if (is_attribute) - { - str = node.get_optional( std::string(".") + name); - } - else - { - str = node.get_optional(name + "."); - } - - boost::optional result; - if ( str ) - { - result = fast_cast(*str); - if (!result) - { - throw config_error(std::string("Failed to parse ") + - (is_attribute ? "attribute" : "child node") + " '" + - name + "'. Expected " + name_trait::name() + - " but got '" + *str + "'"); - } - } - - return result; -} - -template <> -inline boost::optional get_optional(boost::property_tree::ptree const& node, - std::string const& name, - bool is_attribute) -{ - if (is_attribute) - { - return node.get_optional( std::string(".") + name); - } - else - { - return node.get_optional(name + "."); - } -} - -template <> -inline boost::optional get_optional(boost::property_tree::ptree const& node, - std::string const& name, - bool is_attribute) -{ - boost::optional str; - if (is_attribute) - { - str = node.get_optional( std::string(".") + name); - } - else - { - str = node.get_optional(name + "."); - } - - boost::optional result; - if ( str ) - { - try - { - result = mapnik::color_factory::from_string((*str).c_str()); - } - catch (...) - { - throw config_error(std::string("Failed to parse ") + - (is_attribute ? "attribute" : "child node") + " '" + - name + "'. Expected " + name_trait::name() + - " but got '" + *str + "'"); - } - } - - return result; -} - -static inline bool has_child(boost::property_tree::ptree const& node, std::string const& name) -{ - boost::optional str = node.get_optional(name); - return str; -} - } // end of namespace mapnik #endif // MAPNIK_PTREE_HELPERS_HPP diff --git a/include/mapnik/xml_tree.hpp b/include/mapnik/xml_tree.hpp index 534228e79..40406382c 100644 --- a/include/mapnik/xml_tree.hpp +++ b/include/mapnik/xml_tree.hpp @@ -23,6 +23,10 @@ #ifndef MAPNIK_XML_TREE_H #define MAPNIK_XML_TREE_H +//mapnik +#include + + //boost #include diff --git a/plugins/input/csv/csv_datasource.cpp b/plugins/input/csv/csv_datasource.cpp index b2848509f..8aa04b667 100644 --- a/plugins/input/csv/csv_datasource.cpp +++ b/plugins/input/csv/csv_datasource.cpp @@ -14,7 +14,8 @@ #include #include #include -#include // mapnik::boolean +#include +#include // stl #include diff --git a/plugins/input/gdal/gdal_datasource.cpp b/plugins/input/gdal/gdal_datasource.cpp index a19842413..1bdf85ce1 100644 --- a/plugins/input/gdal/gdal_datasource.cpp +++ b/plugins/input/gdal/gdal_datasource.cpp @@ -25,7 +25,7 @@ #include "gdal_featureset.hpp" // mapnik -#include +#include #include #include diff --git a/plugins/input/geos/geos_datasource.cpp b/plugins/input/geos/geos_datasource.cpp index 9fcc8c4ab..ddf649ede 100644 --- a/plugins/input/geos/geos_datasource.cpp +++ b/plugins/input/geos/geos_datasource.cpp @@ -30,7 +30,7 @@ #include // mapnik -#include +#include #include // boost diff --git a/plugins/input/kismet/kismet_datasource.cpp b/plugins/input/kismet/kismet_datasource.cpp index 7780b078d..00f604789 100644 --- a/plugins/input/kismet/kismet_datasource.cpp +++ b/plugins/input/kismet/kismet_datasource.cpp @@ -33,7 +33,7 @@ #include // mapnik -#include +#include // boost #include diff --git a/plugins/input/occi/occi_datasource.cpp b/plugins/input/occi/occi_datasource.cpp index b99698d22..e4d3fa4a3 100644 --- a/plugins/input/occi/occi_datasource.cpp +++ b/plugins/input/occi/occi_datasource.cpp @@ -25,7 +25,7 @@ #include "occi_featureset.hpp" // mapnik -#include +#include #include // boost diff --git a/plugins/input/ogr/ogr_datasource.cpp b/plugins/input/ogr/ogr_datasource.cpp index 0e6035a3c..da404cbb9 100644 --- a/plugins/input/ogr/ogr_datasource.cpp +++ b/plugins/input/ogr/ogr_datasource.cpp @@ -33,7 +33,7 @@ #include // mapnik -#include +#include #include // boost diff --git a/plugins/input/postgis/postgis_datasource.cpp b/plugins/input/postgis/postgis_datasource.cpp index 0c5f083e8..26c2b9074 100644 --- a/plugins/input/postgis/postgis_datasource.cpp +++ b/plugins/input/postgis/postgis_datasource.cpp @@ -26,7 +26,7 @@ // mapnik #include -#include +#include #include #include diff --git a/plugins/input/rasterlite/rasterlite_datasource.cpp b/plugins/input/rasterlite/rasterlite_datasource.cpp index e9cd0bf69..ac609f357 100644 --- a/plugins/input/rasterlite/rasterlite_datasource.cpp +++ b/plugins/input/rasterlite/rasterlite_datasource.cpp @@ -29,7 +29,7 @@ #include // mapnik -#include +#include #include using mapnik::datasource; diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index 02e184d3a..77015ef82 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -26,7 +26,7 @@ #include "sqlite_utils.hpp" // mapnik -#include +#include #include #include #include diff --git a/src/formatting/text.cpp b/src/formatting/text.cpp index 0be312501..a65285200 100644 --- a/src/formatting/text.cpp +++ b/src/formatting/text.cpp @@ -45,7 +45,7 @@ void text_node::to_xml(ptree &xml) const node_ptr text_node::from_xml(xml_node const& xml) { - std::string data = xml.get_text(); + std::string data = xml.text(); if (data.empty()) return node_ptr(); //No text return node_ptr(new text_node(parse_expression(data, "utf8"))); } diff --git a/src/xml_tree.cpp b/src/xml_tree.cpp index 415107da8..2ba5ff709 100644 --- a/src/xml_tree.cpp +++ b/src/xml_tree.cpp @@ -27,6 +27,8 @@ #include #include #include +#include +#include //boost #include @@ -79,6 +81,11 @@ inline boost::optional fast_cast(xml_tree const& tree, std::string const& return mapnik::color_factory::from_string(value); } +template <> +inline boost::optional fast_cast(xml_tree const& tree, std::string const& value) +{ + return parse_expression(value); +} /****************************************************************************/ @@ -112,6 +119,7 @@ DEFINE_NAME_TRAIT( boolean, "boolean") DEFINE_NAME_TRAIT( int, "integer" ) DEFINE_NAME_TRAIT( std::string, "string" ) DEFINE_NAME_TRAIT( color, "color" ) +DEFINE_NAME_TRAIT(expression_ptr, "expression_ptr" ) template struct name_trait< mapnik::enumeration > @@ -376,19 +384,21 @@ std::string xml_node::get_text() const template T xml_node::get_value() const { - boost::optional result = fast_cast(get_text()); + boost::optional result = fast_cast(tree_, get_text()); if (!result) { throw config_error(std::string("Failed to parse value in node '") + name_ + "'. Expected " + name_trait::name() + " but got '" + get_text() + "'"); } - return result; + return *result; } #define compile_get_opt_attr(T) template boost::optional xml_node::get_opt_attr(std::string const&) const +#define compile_get_attr(T) template T xml_node::get_attr(std::string const&) const; template T xml_node::get_attr(std::string const&, T const&) const +#define compile_get_value(T) template T xml_node::get_value() const -//compile_get_opt_attr(boolean); +compile_get_opt_attr(boolean); compile_get_opt_attr(std::string); compile_get_opt_attr(unsigned); compile_get_opt_attr(float); @@ -396,4 +406,23 @@ compile_get_opt_attr(double); compile_get_opt_attr(color); compile_get_opt_attr(gamma_method_e); compile_get_opt_attr(line_rasterizer_e); +compile_get_opt_attr(line_join_e); +compile_get_opt_attr(line_cap_e); +compile_get_opt_attr(text_transform_e); +compile_get_opt_attr(label_placement_e); +compile_get_opt_attr(vertical_alignment_e); +compile_get_opt_attr(horizontal_alignment_e); +compile_get_opt_attr(justify_alignment_e); +compile_get_attr(std::string); +compile_get_attr(filter_mode_e); +compile_get_attr(point_placement_e); +compile_get_attr(marker_placement_e); +compile_get_attr(marker_type_e); +compile_get_attr(pattern_alignment_e); +compile_get_attr(line_rasterizer_e); +compile_get_attr(colorizer_mode); +compile_get_attr(double); +compile_get_value(int); +compile_get_value(double); +compile_get_value(expression_ptr); } //ns mapnik