/* This file is part of Mapnik (c++ mapping toolkit) * Copyright (C) 2005 Artem Pavlenko * * Mapnik is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ //$Id$ #ifndef ATTRIBUTE_HH #define ATTRIBUTE_HH #include #include #include namespace mapnik { template struct attribute_traits { static std::string to_string(const T& value) { std::stringstream ss; ss << value; return ss.str(); } }; template <> struct attribute_traits { static std::string to_string(const std::string& value) { return value; } }; class attribute { public: attribute() : base_(0) {} template attribute(const T& value) : base_(new attribute_impl(value)) {} attribute(const attribute& rhs) : base_(rhs.base_ ? rhs.base_->clone() : 0) {} ~attribute() { delete base_; } template attribute& operator=(const T& rhs) { attribute(rhs).swap(*this); return *this; } attribute& operator=(const attribute& rhs) { attribute(rhs).swap(*this); return *this; } bool empty() const { return !base_; } const std::type_info & type() const { return base_ ? base_->type() : typeid(void); } const std::string to_string() const { return base_ ? base_->to_string() : ""; } private: attribute& swap(attribute& rhs) { std::swap(base_,rhs.base_); return *this; } struct attribute_base { virtual ~attribute_base() {} virtual attribute_base* clone() const=0; virtual std::string to_string() const=0; virtual const std::type_info& type() const=0; }; template > class attribute_impl : public attribute_base { public: typedef T value_type; attribute_impl(const value_type& value) : value_(value) {} std::string to_string() const { return ATraits::to_string(value_); } attribute_base* clone() const { return new attribute_impl(value_); } const std::type_info& type() const { return typeid(value_); } value_type value_; }; template friend value_type* attribute_cast(attribute*); public: attribute_base* base_; }; template struct bad_attribute_cast : public std::bad_cast { virtual const char* what() const throw() { return "attribute::failed conversion"; } }; template bool is_type(const attribute& attr) { return attr.type()==typeid(T); } template T* attribute_cast(attribute* attr) { return attr && attr->type() == typeid(T) ? &static_cast*>(attr->base_)->value_ : 0; } template T* attribute_cast(const attribute* attr) { return attribute_cast(const_cast(attr)); } template T attribute_cast(const attribute& attr) { const T* result=attribute_cast(&attr); if (!result) throw bad_attribute_cast(); return *result; } template T attribute_cast(attribute& attr) { T* result=attribute_cast(&attr); if (!result) throw bad_attribute_cast(); return *result; } typedef std::map attributes; template attribute attribute_from_string(const std::string& val) { std::istringstream is(val); T t; is >> t; return attribute(t); } } #endif //ATTRIBUTE_HH