1 #ifndef DYNAMIC_PROPERTY_MAP_RG09302004_HPP
2 #define DYNAMIC_PROPERTY_MAP_RG09302004_HPP
4 // Copyright 2004-5 The Trustees of Indiana University.
6 // Use, modification and distribution is subject to the Boost Software
7 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // dynamic_property_map.hpp -
11 // Support for runtime-polymorphic property maps. This header is factored
12 // out of Doug Gregor's routines for reading GraphML files for use in reading
13 // GraphViz graph files.
15 // Authors: Doug Gregor
20 #include <boost/config.hpp>
21 #include <boost/throw_exception.hpp>
22 #include <boost/property_map/property_map.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/any.hpp>
25 #include <boost/function/function3.hpp>
26 #include <boost/type_traits/is_convertible.hpp>
28 #include <boost/mpl/bool.hpp>
32 #include <boost/type.hpp>
33 #include <boost/smart_ptr.hpp>
40 // A wrapper around lexical_cast, which does not behave as
41 // desired for std::string types.
42 template<typename Value>
43 inline Value read_value(const std::string& value)
44 { return boost::lexical_cast<Value>(value); }
47 inline std::string read_value<std::string>(const std::string& value)
53 // dynamic_property_map -
54 // This interface supports polymorphic manipulation of property maps.
55 class dynamic_property_map
58 virtual ~dynamic_property_map() { }
60 virtual boost::any get(const any& key) = 0;
61 virtual std::string get_string(const any& key) = 0;
62 virtual void put(const any& key, const any& value) = 0;
63 virtual const std::type_info& key() const = 0;
64 virtual const std::type_info& value() const = 0;
68 //////////////////////////////////////////////////////////////////////
69 // Property map exceptions
70 //////////////////////////////////////////////////////////////////////
72 struct dynamic_property_exception : public std::exception {
73 virtual ~dynamic_property_exception() throw() {}
74 virtual const char* what() const throw() = 0;
77 struct property_not_found : public dynamic_property_exception {
79 mutable std::string statement;
80 property_not_found(const std::string& property) : property(property) {}
81 virtual ~property_not_found() throw() {}
83 const char* what() const throw() {
86 std::string("Property not found: ") + property + ".";
88 return statement.c_str();
92 struct dynamic_get_failure : public dynamic_property_exception {
94 mutable std::string statement;
95 dynamic_get_failure(const std::string& property) : property(property) {}
96 virtual ~dynamic_get_failure() throw() {}
98 const char* what() const throw() {
102 "dynamic property get cannot retrieve value for property: ")
105 return statement.c_str();
109 struct dynamic_const_put_error : public dynamic_property_exception {
110 virtual ~dynamic_const_put_error() throw() {}
112 const char* what() const throw() {
113 return "Attempt to put a value into a const property map: ";
120 // Trying to work around VC++ problem that seems to relate to having too many
121 // functions named "get"
122 template <typename PMap, typename Key>
123 typename boost::property_traits<PMap>::reference
124 get_wrapper_xxx(const PMap& pmap, const Key& key) {
126 return get(pmap, key);
130 // dynamic_property_map_adaptor -
131 // property-map adaptor to support runtime polymorphism.
132 template<typename PropertyMap>
133 class dynamic_property_map_adaptor : public dynamic_property_map
135 typedef typename property_traits<PropertyMap>::key_type key_type;
136 typedef typename property_traits<PropertyMap>::value_type value_type;
137 typedef typename property_traits<PropertyMap>::category category;
139 // do_put - overloaded dispatches from the put() member function.
140 // Attempts to "put" to a property map that does not model
141 // WritablePropertyMap result in a runtime exception.
143 // in_value must either hold an object of value_type or a string that
144 // can be converted to value_type via iostreams.
145 void do_put(const any& in_key, const any& in_value, mpl::bool_<true>)
149 key_type key_ = any_cast<key_type>(in_key);
150 if (in_value.type() == typeid(value_type)) {
151 put(property_map_, key_, any_cast<value_type>(in_value));
153 // if in_value is an empty string, put a default constructed value_type.
154 std::string v = any_cast<std::string>(in_value);
156 put(property_map_, key_, value_type());
158 put(property_map_, key_, detail::read_value<value_type>(v));
163 void do_put(const any&, const any&, mpl::bool_<false>)
165 BOOST_THROW_EXCEPTION(dynamic_const_put_error());
169 explicit dynamic_property_map_adaptor(const PropertyMap& property_map_)
170 : property_map_(property_map_) { }
172 virtual boost::any get(const any& key_)
174 return get_wrapper_xxx(property_map_, any_cast<typename boost::property_traits<PropertyMap>::key_type>(key_));
177 virtual std::string get_string(const any& key_)
179 std::ostringstream out;
180 out << get_wrapper_xxx(property_map_, any_cast<typename boost::property_traits<PropertyMap>::key_type>(key_));
184 virtual void put(const any& in_key, const any& in_value)
186 do_put(in_key, in_value,
187 mpl::bool_<(is_convertible<category*,
188 writable_property_map_tag*>::value)>());
191 virtual const std::type_info& key() const { return typeid(key_type); }
192 virtual const std::type_info& value() const { return typeid(value_type); }
194 PropertyMap& base() { return property_map_; }
195 const PropertyMap& base() const { return property_map_; }
198 PropertyMap property_map_;
201 } // namespace detail
204 // dynamic_properties -
205 // container for dynamic property maps
207 struct dynamic_properties
209 typedef std::multimap<std::string, boost::shared_ptr<dynamic_property_map> >
211 typedef boost::function3<boost::shared_ptr<dynamic_property_map>,
214 const boost::any&> generate_fn_type;
217 typedef property_maps_type::iterator iterator;
218 typedef property_maps_type::const_iterator const_iterator;
220 dynamic_properties() : generate_fn() { }
221 dynamic_properties(const generate_fn_type& g) : generate_fn(g) {}
223 ~dynamic_properties() {}
225 template<typename PropertyMap>
227 property(const std::string& name, PropertyMap property_map_)
229 boost::shared_ptr<dynamic_property_map> pm(
230 boost::static_pointer_cast<dynamic_property_map>(
231 boost::make_shared<detail::dynamic_property_map_adaptor<PropertyMap> >(property_map_)));
232 property_maps.insert(property_maps_type::value_type(name, pm));
237 template<typename PropertyMap>
239 property(const std::string& name, PropertyMap property_map_) const
241 dynamic_properties result = *this;
242 result.property(name, property_map_);
246 iterator begin() { return property_maps.begin(); }
247 const_iterator begin() const { return property_maps.begin(); }
248 iterator end() { return property_maps.end(); }
249 const_iterator end() const { return property_maps.end(); }
251 iterator lower_bound(const std::string& name)
252 { return property_maps.lower_bound(name); }
254 const_iterator lower_bound(const std::string& name) const
255 { return property_maps.lower_bound(name); }
258 insert(const std::string& name, boost::shared_ptr<dynamic_property_map> pm)
260 property_maps.insert(property_maps_type::value_type(name, pm));
263 template<typename Key, typename Value>
264 boost::shared_ptr<dynamic_property_map>
265 generate(const std::string& name, const Key& key, const Value& value)
268 BOOST_THROW_EXCEPTION(property_not_found(name));
270 return generate_fn(name,key,value);
275 property_maps_type property_maps;
276 generate_fn_type generate_fn;
279 template<typename Key, typename Value>
281 put(const std::string& name, dynamic_properties& dp, const Key& key,
284 for (dynamic_properties::iterator i = dp.lower_bound(name);
285 i != dp.end() && i->first == name; ++i) {
286 if (i->second->key() == typeid(key)) {
287 i->second->put(key, value);
292 boost::shared_ptr<dynamic_property_map> new_map = dp.generate(name, key, value);
294 new_map->put(key, value);
295 dp.insert(name, new_map);
302 template<typename Value, typename Key>
304 get(const std::string& name, const dynamic_properties& dp, const Key& key)
306 for (dynamic_properties::const_iterator i = dp.lower_bound(name);
307 i != dp.end() && i->first == name; ++i) {
308 if (i->second->key() == typeid(key))
309 return any_cast<Value>(i->second->get(key));
312 BOOST_THROW_EXCEPTION(dynamic_get_failure(name));
315 template<typename Value, typename Key>
317 get(const std::string& name, const dynamic_properties& dp, const Key& key, type<Value>)
319 for (dynamic_properties::const_iterator i = dp.lower_bound(name);
320 i != dp.end() && i->first == name; ++i) {
321 if (i->second->key() == typeid(key))
322 return any_cast<Value>(i->second->get(key));
325 BOOST_THROW_EXCEPTION(dynamic_get_failure(name));
328 template<typename Key>
330 get(const std::string& name, const dynamic_properties& dp, const Key& key)
332 for (dynamic_properties::const_iterator i = dp.lower_bound(name);
333 i != dp.end() && i->first == name; ++i) {
334 if (i->second->key() == typeid(key))
335 return i->second->get_string(key);
338 BOOST_THROW_EXCEPTION(dynamic_get_failure(name));
341 // The easy way to ignore properties.
343 boost::shared_ptr<boost::dynamic_property_map>
344 ignore_other_properties(const std::string&,
347 return boost::shared_ptr<boost::dynamic_property_map>();
352 #endif // DYNAMIC_PROPERTY_MAP_RG09302004_HPP