epoc32/include/stdapis/boost/dynamic_property_map.hpp
branchSymbian2
changeset 2 2fe1408b6811
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/boost/dynamic_property_map.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,362 @@
     1.4 +#ifndef DYNAMIC_PROPERTY_MAP_RG09302004_HPP
     1.5 +#define DYNAMIC_PROPERTY_MAP_RG09302004_HPP
     1.6 +
     1.7 +// Copyright 2004-5 The Trustees of Indiana University.
     1.8 +
     1.9 +// Use, modification and distribution is subject to the Boost Software
    1.10 +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    1.11 +// http://www.boost.org/LICENSE_1_0.txt)
    1.12 +
    1.13 +//  dynamic_property_map.hpp -
    1.14 +//    Support for runtime-polymorphic property maps.  This header is factored
    1.15 +//  out of Doug Gregor's routines for reading GraphML files for use in reading
    1.16 +//  GraphViz graph files.
    1.17 +
    1.18 +//  Authors: Doug Gregor
    1.19 +//           Ronald Garcia
    1.20 +//
    1.21 +
    1.22 +
    1.23 +#include <boost/config.hpp>
    1.24 +#include <boost/property_map.hpp>
    1.25 +#include <boost/lexical_cast.hpp>
    1.26 +#include <boost/any.hpp>
    1.27 +#include <boost/function/function3.hpp>
    1.28 +#include <boost/type_traits/is_convertible.hpp>
    1.29 +#include <typeinfo>
    1.30 +#include <boost/mpl/bool.hpp>
    1.31 +#include <stdexcept>
    1.32 +#include <sstream>
    1.33 +#include <map>
    1.34 +#include <boost/type.hpp>
    1.35 +
    1.36 +namespace boost {
    1.37 +
    1.38 +namespace detail {
    1.39 +
    1.40 +  // read_value -
    1.41 +  //   A wrapper around lexical_cast, which does not behave as
    1.42 +  //   desired for std::string types.
    1.43 +  template<typename Value>
    1.44 +  inline Value read_value(const std::string& value)
    1.45 +  { return boost::lexical_cast<Value>(value); }
    1.46 +
    1.47 +  template<>
    1.48 +  inline std::string read_value<std::string>(const std::string& value)
    1.49 +  { return value; }
    1.50 +
    1.51 +}
    1.52 +
    1.53 +
    1.54 +// dynamic_property_map -
    1.55 +//  This interface supports polymorphic manipulation of property maps.
    1.56 +class dynamic_property_map
    1.57 +{
    1.58 +public:
    1.59 +  virtual ~dynamic_property_map() { }
    1.60 +
    1.61 +  virtual boost::any get(const any& key) = 0;
    1.62 +  virtual std::string get_string(const any& key) = 0;
    1.63 +  virtual void put(const any& key, const any& value) = 0;
    1.64 +  virtual const std::type_info& key() const = 0;
    1.65 +  virtual const std::type_info& value() const = 0;
    1.66 +};
    1.67 +
    1.68 +
    1.69 +//////////////////////////////////////////////////////////////////////
    1.70 +// Property map exceptions
    1.71 +//////////////////////////////////////////////////////////////////////
    1.72 +
    1.73 +struct dynamic_property_exception : public std::exception {
    1.74 +  virtual ~dynamic_property_exception() throw() {}
    1.75 +  virtual const char* what() const throw() = 0;
    1.76 +};
    1.77 +
    1.78 +struct property_not_found : public dynamic_property_exception {
    1.79 +  std::string property;
    1.80 +  mutable std::string statement;
    1.81 +  property_not_found(const std::string& property) : property(property) {}
    1.82 +  virtual ~property_not_found() throw() {}
    1.83 +
    1.84 +  const char* what() const throw() {
    1.85 +    if(statement.empty())
    1.86 +      statement =
    1.87 +        std::string("Property not found: ") + property + ".";
    1.88 +
    1.89 +    return statement.c_str();
    1.90 +  }
    1.91 +};
    1.92 +
    1.93 +struct dynamic_get_failure : public dynamic_property_exception {
    1.94 +  std::string property;
    1.95 +  mutable std::string statement;
    1.96 +  dynamic_get_failure(const std::string& property) : property(property) {}
    1.97 +  virtual ~dynamic_get_failure() throw() {}
    1.98 +
    1.99 +  const char* what() const throw() {
   1.100 +    if(statement.empty())
   1.101 +      statement =
   1.102 +        std::string(
   1.103 +         "dynamic property get cannot retrieve value for  property: ")
   1.104 +        + property + ".";
   1.105 +
   1.106 +    return statement.c_str();
   1.107 +  }
   1.108 +};
   1.109 +
   1.110 +struct dynamic_const_put_error  : public dynamic_property_exception {
   1.111 +  virtual ~dynamic_const_put_error() throw() {}
   1.112 +
   1.113 +  const char* what() const throw() {
   1.114 +    return "Attempt to put a value into a const property map: ";
   1.115 +  }
   1.116 +};
   1.117 +
   1.118 +
   1.119 +namespace detail {
   1.120 +
   1.121 +//
   1.122 +// dynamic_property_map_adaptor -
   1.123 +//   property-map adaptor to support runtime polymorphism.
   1.124 +template<typename PropertyMap>
   1.125 +class dynamic_property_map_adaptor : public dynamic_property_map
   1.126 +{
   1.127 +  typedef typename property_traits<PropertyMap>::key_type key_type;
   1.128 +  typedef typename property_traits<PropertyMap>::value_type value_type;
   1.129 +  typedef typename property_traits<PropertyMap>::category category;
   1.130 +
   1.131 +  // do_put - overloaded dispatches from the put() member function.
   1.132 +  //   Attempts to "put" to a property map that does not model
   1.133 +  //   WritablePropertyMap result in a runtime exception.
   1.134 +
   1.135 +  //   in_value must either hold an object of value_type or a string that
   1.136 +  //   can be converted to value_type via iostreams.
   1.137 +  void do_put(const any& in_key, const any& in_value, mpl::bool_<true>)
   1.138 +  {
   1.139 +#if !(defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95))
   1.140 +    using boost::put;
   1.141 +#endif
   1.142 +
   1.143 +    key_type key = any_cast<key_type>(in_key);
   1.144 +    if (in_value.type() == typeid(value_type)) {
   1.145 +#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
   1.146 +      boost::put(property_map, key, any_cast<value_type>(in_value));
   1.147 +#else
   1.148 +      put(property_map, key, any_cast<value_type>(in_value));
   1.149 +#endif
   1.150 +    } else {
   1.151 +      //  if in_value is an empty string, put a default constructed value_type.
   1.152 +      std::string v = any_cast<std::string>(in_value);
   1.153 +      if (v.empty()) {
   1.154 +#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
   1.155 +        boost::put(property_map, key, value_type());
   1.156 +#else
   1.157 +        put(property_map, key, value_type());
   1.158 +#endif
   1.159 +      } else {
   1.160 +#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
   1.161 +        boost::put(property_map, key, detail::read_value<value_type>(v));
   1.162 +#else
   1.163 +        put(property_map, key, detail::read_value<value_type>(v));
   1.164 +#endif
   1.165 +      }
   1.166 +    }
   1.167 +  }
   1.168 +
   1.169 +  void do_put(const any&, const any&, mpl::bool_<false>)
   1.170 +  {
   1.171 +    throw dynamic_const_put_error();
   1.172 +  }
   1.173 +
   1.174 +public:
   1.175 +  explicit dynamic_property_map_adaptor(const PropertyMap& property_map)
   1.176 +    : property_map(property_map) { }
   1.177 +
   1.178 +  virtual boost::any get(const any& key)
   1.179 +  {
   1.180 +#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
   1.181 +    return boost::get(property_map, any_cast<key_type>(key));
   1.182 +#else
   1.183 +    using boost::get;
   1.184 +
   1.185 +    return get(property_map, any_cast<key_type>(key));
   1.186 +#endif
   1.187 +  }
   1.188 +
   1.189 +  virtual std::string get_string(const any& key)
   1.190 +  {
   1.191 +#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 95)
   1.192 +    std::ostringstream out;
   1.193 +    out << boost::get(property_map, any_cast<key_type>(key));
   1.194 +    return out.str();
   1.195 +#else
   1.196 +    using boost::get;
   1.197 +
   1.198 +    std::ostringstream out;
   1.199 +    out << get(property_map, any_cast<key_type>(key));
   1.200 +    return out.str();
   1.201 +#endif
   1.202 +  }
   1.203 +
   1.204 +  virtual void put(const any& in_key, const any& in_value)
   1.205 +  {
   1.206 +    do_put(in_key, in_value,
   1.207 +           mpl::bool_<(is_convertible<category*,
   1.208 +                                      writable_property_map_tag*>::value)>());
   1.209 +  }
   1.210 +
   1.211 +  virtual const std::type_info& key()   const { return typeid(key_type); }
   1.212 +  virtual const std::type_info& value() const { return typeid(value_type); }
   1.213 +
   1.214 +  PropertyMap&       base()       { return property_map; }
   1.215 +  const PropertyMap& base() const { return property_map; }
   1.216 +
   1.217 +private:
   1.218 +  PropertyMap property_map;
   1.219 +};
   1.220 +
   1.221 +} // namespace detail
   1.222 +
   1.223 +//
   1.224 +// dynamic_properties -
   1.225 +//   container for dynamic property maps
   1.226 +//
   1.227 +struct dynamic_properties
   1.228 +{
   1.229 +  typedef std::multimap<std::string, dynamic_property_map*>
   1.230 +    property_maps_type;
   1.231 +  typedef boost::function3<std::auto_ptr<dynamic_property_map>,
   1.232 +                           const std::string&,
   1.233 +                           const boost::any&,
   1.234 +                           const boost::any&> generate_fn_type;
   1.235 +public:
   1.236 +
   1.237 +  typedef property_maps_type::iterator iterator;
   1.238 +  typedef property_maps_type::const_iterator const_iterator;
   1.239 +
   1.240 +  dynamic_properties() : generate_fn() { }
   1.241 +  dynamic_properties(const generate_fn_type& g) : generate_fn(g) {}
   1.242 +
   1.243 +  ~dynamic_properties()
   1.244 +  {
   1.245 +    for (property_maps_type::iterator i = property_maps.begin();
   1.246 +         i != property_maps.end(); ++i) {
   1.247 +      delete i->second;
   1.248 +    }
   1.249 +  }
   1.250 +
   1.251 +  template<typename PropertyMap>
   1.252 +  dynamic_properties&
   1.253 +  property(const std::string& name, PropertyMap property_map)
   1.254 +  {
   1.255 +    // Tbd: exception safety
   1.256 +    std::auto_ptr<dynamic_property_map> pm(
   1.257 +      new detail::dynamic_property_map_adaptor<PropertyMap>(property_map));
   1.258 +    property_maps_type::iterator i =
   1.259 +      property_maps.insert(property_maps_type::value_type(name, 0));
   1.260 +    i->second = pm.release();
   1.261 +
   1.262 +    return *this;
   1.263 +  }
   1.264 +
   1.265 +  iterator       begin()       { return property_maps.begin(); }
   1.266 +  const_iterator begin() const { return property_maps.begin(); }
   1.267 +  iterator       end()         { return property_maps.end(); }
   1.268 +  const_iterator end() const   { return property_maps.end(); }
   1.269 +
   1.270 +  iterator lower_bound(const std::string& name)
   1.271 +  { return property_maps.lower_bound(name); }
   1.272 +
   1.273 +  const_iterator lower_bound(const std::string& name) const
   1.274 +  { return property_maps.lower_bound(name); }
   1.275 +
   1.276 +  void
   1.277 +  insert(const std::string& name, std::auto_ptr<dynamic_property_map> pm)
   1.278 +  {
   1.279 +    property_maps.insert(property_maps_type::value_type(name, pm.release()));
   1.280 +  }
   1.281 +
   1.282 +  template<typename Key, typename Value>
   1.283 +  std::auto_ptr<dynamic_property_map>
   1.284 +  generate(const std::string& name, const Key& key, const Value& value)
   1.285 +  {
   1.286 +    if(!generate_fn) {
   1.287 +      throw property_not_found(name);
   1.288 +    } else {
   1.289 +      return generate_fn(name,key,value);
   1.290 +    }
   1.291 +  }
   1.292 +
   1.293 +private:
   1.294 +  property_maps_type property_maps;
   1.295 +  generate_fn_type generate_fn;
   1.296 +};
   1.297 +
   1.298 +template<typename Key, typename Value>
   1.299 +bool
   1.300 +put(const std::string& name, dynamic_properties& dp, const Key& key,
   1.301 +    const Value& value)
   1.302 +{
   1.303 +  for (dynamic_properties::iterator i = dp.lower_bound(name);
   1.304 +       i != dp.end() && i->first == name; ++i) {
   1.305 +    if (i->second->key() == typeid(key)) {
   1.306 +      i->second->put(key, value);
   1.307 +      return true;
   1.308 +    }
   1.309 +  }
   1.310 +
   1.311 +  std::auto_ptr<dynamic_property_map> new_map = dp.generate(name, key, value);
   1.312 +  if (new_map.get()) {
   1.313 +    new_map->put(key, value);
   1.314 +    dp.insert(name, new_map);
   1.315 +    return true;
   1.316 +  } else {
   1.317 +    return false;
   1.318 +  }
   1.319 +}
   1.320 +
   1.321 +#ifndef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS 
   1.322 +template<typename Value, typename Key>
   1.323 +Value
   1.324 +get(const std::string& name, const dynamic_properties& dp, const Key& key)
   1.325 +{
   1.326 +  for (dynamic_properties::const_iterator i = dp.lower_bound(name);
   1.327 +       i != dp.end() && i->first == name; ++i) {
   1.328 +    if (i->second->key() == typeid(key))
   1.329 +      return any_cast<Value>(i->second->get(key));
   1.330 +  }
   1.331 +
   1.332 +  throw dynamic_get_failure(name);
   1.333 +}
   1.334 +#endif
   1.335 +
   1.336 +template<typename Value, typename Key>
   1.337 +Value
   1.338 +get(const std::string& name, const dynamic_properties& dp, const Key& key, type<Value>)
   1.339 +{
   1.340 +  for (dynamic_properties::const_iterator i = dp.lower_bound(name);
   1.341 +       i != dp.end() && i->first == name; ++i) {
   1.342 +    if (i->second->key() == typeid(key))
   1.343 +      return any_cast<Value>(i->second->get(key));
   1.344 +  }
   1.345 +
   1.346 +  throw dynamic_get_failure(name);
   1.347 +}
   1.348 +
   1.349 +template<typename Key>
   1.350 +std::string
   1.351 +get(const std::string& name, const dynamic_properties& dp, const Key& key)
   1.352 +{
   1.353 +  for (dynamic_properties::const_iterator i = dp.lower_bound(name);
   1.354 +       i != dp.end() && i->first == name; ++i) {
   1.355 +    if (i->second->key() == typeid(key))
   1.356 +      return i->second->get_string(key);
   1.357 +  }
   1.358 +
   1.359 +  throw dynamic_get_failure(name);
   1.360 +}
   1.361 +
   1.362 +
   1.363 +}
   1.364 +
   1.365 +#endif // DYNAMIC_PROPERTY_MAP_RG09302004_HPP