dynamic_property_map.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
  7. <title>Boost Dynamic Property Maps</title>
  8. <link rel="stylesheet" href="../../parameter/doc/html/rst.css" type="text/css" />
  9. </head>
  10. <body>
  11. <div class="document" id="logo-dynamic-property-maps">
  12. <h1 class="title"><a class="reference external" href="../../../index.htm"><img align="middle" alt="Boost" class="align-middle" src="../../../boost.png" /></a> Dynamic Property Maps</h1>
  13. <!-- Copyright 2004-5 The Trustees of Indiana University.
  14. Use, modification and distribution is subject to the Boost Software
  15. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  16. http://www.boost.org/LICENSE_1_0.txt) -->
  17. <div class="section" id="summary">
  18. <h1><a class="toc-backref" href="#id2">Summary</a></h1>
  19. <p>The dynamic property map interfaces provides access to a collection of
  20. property maps through a dynamically-typed interface. An algorithm can
  21. use it to manipulate property maps without knowing their key or
  22. value types at compile-time. Type-safe codes can use dynamic property
  23. maps to interface more easily and completely with scripting languages
  24. and other text-based representations of key-value data.</p>
  25. <div class="contents topic" id="contents">
  26. <p class="topic-title first">Contents</p>
  27. <ul class="simple">
  28. <li><a class="reference internal" href="#summary" id="id2">Summary</a></li>
  29. <li><a class="reference internal" href="#introduction" id="id3">Introduction</a><ul>
  30. <li><a class="reference internal" href="#fred-s-info-revisited" id="id4">&quot;Fred's Info&quot; Revisited</a></li>
  31. </ul>
  32. </li>
  33. <li><a class="reference internal" href="#reference" id="id5">Reference</a><ul>
  34. <li><a class="reference internal" href="#member-functions" id="id6">Member Functions</a></li>
  35. <li><a class="reference internal" href="#free-functions" id="id7">Free functions</a></li>
  36. <li><a class="reference internal" href="#exceptions" id="id8">Exceptions</a></li>
  37. </ul>
  38. </li>
  39. </ul>
  40. </div>
  41. </div>
  42. <div class="section" id="introduction">
  43. <h1><a class="toc-backref" href="#id3">Introduction</a></h1>
  44. <p>The Boost Property Map library specifies statically type-safe
  45. interfaces through which key-value pairs can be manipulated by
  46. generic algorithms. Typically, an algorithm that uses property maps is
  47. parameterized on the types of the property maps it uses, and it
  48. manipulates them using the interfaces specified by the
  49. Boost Property Map Library.</p>
  50. <p>The following generic function illustrates property map basics.</p>
  51. <pre class="literal-block">
  52. template &lt;typename AgeMap, typename GPAMap&gt;
  53. void
  54. manipulate_freds_info(AgeMap ages, GPAMap gpas) {
  55. typedef typename boost::property_traits&lt;AgeMap&gt;::key_type name_type;
  56. typedef typename boost::property_traits&lt;AgeMap&gt;::value_type age_type;
  57. typedef typename boost::property_traits&lt;GPAMap&gt;::value_type gpa_type;
  58. name_type fred = &quot;Fred&quot;;
  59. age_type old_age = get(ages, fred);
  60. gpa_type old_gpa = get(gpas, fred);
  61. std::cout &lt;&lt; &quot;Fred's old age: &quot; &lt;&lt; old_age &lt;&lt; &quot;\n&quot;
  62. &lt;&lt; &quot;Fred's old gpa: &quot; &lt;&lt; old_gpa &lt;&lt; &quot;\n&quot;;
  63. age_type new_age = 18;
  64. gpa_type new_gpa = 3.9;
  65. put(ages, fred, new_age);
  66. put(gpas, fred, new_gpa);
  67. }
  68. </pre>
  69. <p>The function is parameterized on two property map types, <tt class="docutils literal"><span class="pre">AgeMap</span></tt> and
  70. <tt class="docutils literal"><span class="pre">GPAMap</span></tt>, and takes a value parameter for each of those types. The
  71. function uses the <tt class="docutils literal"><span class="pre">property_traits</span></tt> interface to ascertain, at
  72. compile-time, the value and key types of the property maps. The code
  73. then retrieves Fred's old information, using the <tt class="docutils literal"><span class="pre">get</span></tt> function, and
  74. updates it using the <tt class="docutils literal"><span class="pre">put</span></tt> function. The <tt class="docutils literal"><span class="pre">get</span></tt> function is required by the
  75. Readable Property Map concept and both <tt class="docutils literal"><span class="pre">get</span></tt> and <tt class="docutils literal"><span class="pre">put</span></tt> are required by the
  76. Read/Write Property Map concept.</p>
  77. <p>The above function not only requires the two type parameters to model
  78. property map concepts, but also makes some extra assumptions.
  79. <tt class="docutils literal"><span class="pre">AgeMap</span></tt> and <tt class="docutils literal"><span class="pre">GPAMap</span></tt> must have the same key type, and that type must be
  80. constructable from a string. Furthermore, <tt class="docutils literal"><span class="pre">AgeMap</span></tt>'s value type must be
  81. constructable from an <tt class="docutils literal"><span class="pre">int</span></tt>. Although these requirements are not
  82. explicitly stated, they are statically checked during compilation and
  83. failure to meet them yields compile-time errors.</p>
  84. <p>Although the static typing of property map interfaces usually provides
  85. desirable compile-time safety, some algorithms require a more dynamic
  86. interface to property maps. For example, the Boost Graph Library (BGL)
  87. provides functions that can initialize a graph by interpreting the
  88. contents of a textual graph description (i.e. a GraphML file). Such
  89. general-purpose graph description languages can specify an arbitrary
  90. number of edge and vertex properties, using strings to represent the
  91. key-value pairs. A graph reader function should capture these
  92. arbitrary properties, but since function templates can only be
  93. parameterized on a fixed number of property maps, the traditional
  94. techniques for handling property maps do not suffice to implement them.</p>
  95. <p>Dynamic property maps specifically address the need for an interface
  96. to property maps whose checking is delayed to runtime. Several
  97. components combine to provide support for dynamic property maps. The
  98. <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> class collects a
  99. group of heterogenous objects that model concepts from
  100. the Boost Property Map library. Each property map is assigned a
  101. string-based key when it is added to the collection, and it can be
  102. addressed using that key. Internally, <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> adapts
  103. each contained property map with the dynamic property map interface,
  104. which provides <tt class="docutils literal"><span class="pre">get</span></tt> and <tt class="docutils literal"><span class="pre">put</span></tt> functions that
  105. can be called using values of any type that meets a few requirements.
  106. Internally, the dynamic property map converts key and value pairs to
  107. meet the requirements of the underlying property map or signals a
  108. runtime exception if it cannot.</p>
  109. <div class="section" id="fred-s-info-revisited">
  110. <h2><a class="toc-backref" href="#id4">&quot;Fred's Info&quot; Revisited</a></h2>
  111. <p>Here's what the example above looks like using the
  112. <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> interface:</p>
  113. <pre class="literal-block">
  114. void manipulate_freds_info(boost::dynamic_properties&amp; properties)
  115. {
  116. using boost::get;
  117. std::string fred = &quot;Fred&quot;;
  118. int old_age = get&lt;int&gt;(&quot;age&quot;, properties, fred);
  119. std::string old_gpa = get(&quot;gpa&quot;, properties, fred);
  120. std::cout &lt;&lt; &quot;Fred's old age: &quot; &lt;&lt; old_age &lt;&lt; &quot;\n&quot;
  121. &lt;&lt; &quot;Fred's old gpa: &quot; &lt;&lt; old_gpa &lt;&lt; &quot;\n&quot;;
  122. std::string new_age = &quot;18&quot;;
  123. double new_gpa = 3.9;
  124. put(&quot;age&quot;,properties,fred,new_age);
  125. put(&quot;gpa&quot;,properties,fred,new_gpa);
  126. }
  127. </pre>
  128. <p>The new function is not a template parameterized on the property map
  129. types but instead a concrete function that takes a <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt>
  130. object. Furthermore, the code no longer makes reference to key or
  131. value types: keys and values are represented with strings.
  132. Nonetheless the function still uses non-string types where they are
  133. useful. For instance, Fred's old age is represented using an <tt class="docutils literal"><span class="pre">int</span></tt>.
  134. It's value is retreived by calling <tt class="docutils literal"><span class="pre">get</span></tt> with a
  135. type parameter, which determines its return type. Finally, the
  136. <tt class="docutils literal"><span class="pre">get</span></tt> and <tt class="docutils literal"><span class="pre">put</span></tt> functions are each supplied a string-based key that
  137. differs depending on the property of concern.</p>
  138. <p>Here's an example of how the above function might be called.</p>
  139. <pre class="literal-block">
  140. int main()
  141. {
  142. using boost::get;
  143. // build property maps using associative_property_map
  144. std::map&lt;std::string, int&gt; name2age;
  145. std::map&lt;std::string, double&gt; name2gpa;
  146. boost::associative_property_map&lt; std::map&lt;std::string, int&gt; &gt;
  147. age_map(name2age);
  148. boost::associative_property_map&lt; std::map&lt;std::string, double&gt; &gt;
  149. gpa_map(name2gpa);
  150. std::string fred(&quot;Fred&quot;);
  151. // add key-value information
  152. name2age.insert(make_pair(fred,17));
  153. name2gpa.insert(make_pair(fred,2.7));
  154. // build and populate dynamic interface
  155. boost::dynamic_properties properties;
  156. properties.property(&quot;age&quot;,age_map);
  157. properties.property(&quot;gpa&quot;,gpa_map);
  158. manipulate_freds_info(properties);
  159. std::cout &lt;&lt; &quot;Fred's age: &quot; &lt;&lt; get(age_map,fred) &lt;&lt; &quot;\n&quot;
  160. &lt;&lt; &quot;Fred's gpa: &quot; &lt;&lt; get(gpa_map,fred) &lt;&lt; &quot;\n&quot;;
  161. }
  162. </pre>
  163. <p>The code first creates two property maps using <tt class="docutils literal"><span class="pre">std::map</span></tt> and the
  164. <tt class="docutils literal"><span class="pre">associative_property_map</span></tt> adaptor. After initializing the
  165. property maps with key-value data, it constructs a
  166. <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object and adds to it both property maps,
  167. keyed on the strings &quot;age&quot; and &quot;gpa&quot;. Finally <tt class="docutils literal"><span class="pre">manipulate_freds_info</span></tt>
  168. is passed the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object and the results of its changes are
  169. displayed.</p>
  170. <p>As shown above, the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object provides, where needed, a
  171. dynamically-typed interface to property maps yet preserves the static
  172. typing of property map uses elsewhere in an application.</p>
  173. </div>
  174. </div>
  175. <div class="section" id="reference">
  176. <h1><a class="toc-backref" href="#id5">Reference</a></h1>
  177. <pre class="literal-block">
  178. class dynamic_properties
  179. </pre>
  180. <p>The <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> class provides a dynamically-typed interface to
  181. a set of property maps. To use it, one must populate
  182. an object of this class with property maps using the <tt class="docutils literal"><span class="pre">property</span></tt> member
  183. function.</p>
  184. <div class="section" id="member-functions">
  185. <h2><a class="toc-backref" href="#id6">Member Functions</a></h2>
  186. <pre class="literal-block">
  187. dynamic_properties()
  188. dynamic_properties(
  189. const boost::function&lt;
  190. boost::shared_ptr&lt;dynamic_property_map&gt; (
  191. const std::string&amp;, const boost::any&amp;, const boost::any&amp;)
  192. &gt;&amp; fn)
  193. </pre>
  194. <p>A <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object can be constructed with a function object
  195. that, when called, creates a new property map. The library provides the
  196. <tt class="docutils literal"><span class="pre">ignore_other_properties</span></tt> function object, which lets the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object ignore any properties that it hasn't been prepared to record.
  197. If an attempt is made
  198. to <tt class="docutils literal"><span class="pre">put</span></tt> a key-value pair to a nonexistent <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> key,
  199. then this function is called with the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> key and the
  200. intended property key and value . If <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> is
  201. default-constructed, such a <tt class="docutils literal"><span class="pre">put</span></tt> attempt throws
  202. <tt class="docutils literal"><span class="pre">property_not_found</span></tt>.</p>
  203. <pre class="literal-block">
  204. template&lt;typename PropertyMap&gt;
  205. dynamic_properties&amp;
  206. property(const std::string&amp; name, PropertyMap property_map)
  207. </pre>
  208. <p>This member function adds a property map to the set of maps contained,
  209. using <tt class="docutils literal"><span class="pre">name</span></tt> as its key.</p>
  210. <p>Requirements: <tt class="docutils literal"><span class="pre">PropertyMap</span></tt> must model Readable Property Map or
  211. Read/Write Property Map.</p>
  212. <pre class="literal-block">
  213. void insert(const std::string&amp; name, boost::shared_ptr&lt;dynamic_property_map&gt; pm)
  214. </pre>
  215. <p>This member function directly adds a <tt class="docutils literal"><span class="pre">dynamic_property_map</span></tt>
  216. to the collection, using <tt class="docutils literal"><span class="pre">name</span></tt> as its key.</p>
  217. <pre class="literal-block">
  218. iterator begin()
  219. const_iterator begin() const
  220. </pre>
  221. <p>This member function returns an iterator over the set of property maps
  222. held by the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object.</p>
  223. <pre class="literal-block">
  224. iterator end()
  225. const_iterator end() const
  226. </pre>
  227. <p>This member function returns a terminal iterator over the set of
  228. dynamic property maps held by the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object. It is used to
  229. terminate traversals over the set of dynamic property maps</p>
  230. <pre class="literal-block">
  231. iterator lower_bound(const std::string&amp; name)
  232. </pre>
  233. <p>This member function returns an iterator that points to the first
  234. property map whose <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> key is <tt class="docutils literal"><span class="pre">name</span></tt>.
  235. Bear in mind that multiple property maps may have the same
  236. <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> key, so long as their property map key types differ.</p>
  237. <p>Invariant: The range [ lower_bound(name), end() ) contains every
  238. property map that has name for its <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> key.</p>
  239. </div>
  240. <div class="section" id="free-functions">
  241. <h2><a class="toc-backref" href="#id7">Free functions</a></h2>
  242. <pre class="literal-block">
  243. boost::shared_ptr&lt;boost::dynamic_property_map&gt;
  244. ignore_other_properties(const std::string&amp;,
  245. const boost::any&amp;,
  246. const boost::any&amp;)
  247. </pre>
  248. <p>When passed to the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> constructor, this function
  249. allows the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object to disregard attempts to put
  250. values to unknown keys without signaling an error.</p>
  251. <pre class="literal-block">
  252. template&lt;typename Key, typename Value&gt;
  253. bool put(const std::string&amp; name, dynamic_properties&amp; dp, const Key&amp; key,
  254. const Value&amp; value)
  255. </pre>
  256. <p>This function adds a key-value pair to the property map with the
  257. matching name and key type. If no matching property map is found,
  258. behavior depends on the availability of a property map generator. If
  259. a property map generator was supplied when the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt>
  260. object was constructed, then that function is used to create a new
  261. property map. If the generator fails to generate a property map
  262. (returns a null <tt class="docutils literal"><span class="pre">shared_ptr</span></tt>), then the <tt class="docutils literal"><span class="pre">put</span></tt> function returns
  263. <tt class="docutils literal"><span class="pre">false</span></tt>. If, on the other hand, the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object
  264. has no property map generator (meaning it was default-constructed),
  265. then <tt class="docutils literal"><span class="pre">property_not_found</span></tt> is thrown. If a candidate property map is
  266. found but it does not support <tt class="docutils literal"><span class="pre">put</span></tt>, <tt class="docutils literal"><span class="pre">dynamic_const_put_error</span></tt> is
  267. thrown.</p>
  268. <pre class="literal-block">
  269. template&lt;typename Value, typename Key&gt;
  270. Value get(const std::string&amp; name, const dynamic_properties&amp; dp,
  271. const Key&amp; key)
  272. </pre>
  273. <p>This function gets the value from the property-map whose namee is
  274. given and whose key type matches. If <tt class="docutils literal"><span class="pre">Value</span></tt> is <tt class="docutils literal"><span class="pre">std::string</span></tt>, then the
  275. property map's value type must either be <tt class="docutils literal"><span class="pre">std::string</span></tt> or model
  276. OutputStreamable. In the latter case, the <tt class="docutils literal"><span class="pre">get</span></tt> function converts the
  277. value to a string. If no matching property map is found,
  278. <tt class="docutils literal"><span class="pre">dynamic_get_failure</span></tt> is thrown.</p>
  279. <hr class="docutils" />
  280. <pre class="literal-block">
  281. class dynamic_property_map
  282. </pre>
  283. <p>This class describes the interface used by <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> to
  284. interact with a user's property maps polymorphically.</p>
  285. <pre class="literal-block">
  286. boost::any get(const any&amp; key)
  287. </pre>
  288. <p>Given a representation of a key, return the value associated with that key.</p>
  289. <p>Requirement:
  290. 1) The object passed as the key must be convertible to a value of the
  291. map's key type. Details of that conversion are unspecified.
  292. 2) For this expression to be valid, the key must be
  293. associated with some value, otherwise the result is undefined.</p>
  294. <pre class="literal-block">
  295. std::string get_string(const any&amp; key)
  296. </pre>
  297. <p>Given a representation of a key, return the string representation
  298. of the value associated with that key.</p>
  299. <p>Requirements:
  300. 1) The object passed as the key must be convertible to the
  301. property map's key type. Details of that conversion are unspecified.
  302. 2) For this expression to be valid, the key must be
  303. associated with some value, otherwise the result is undefined.
  304. 3) The value type of the property map must model Output Streamable.</p>
  305. <pre class="literal-block">
  306. void put(const any&amp; key, const any&amp; value)
  307. </pre>
  308. <p>Given a representation of a key and a representation of a value, the
  309. key and value are associated in the property map.</p>
  310. <p>Requirements:
  311. 1) The object passed as the key must be convertible to the
  312. property map's key type. Details of that conversion are unspecified.
  313. 2) The object passed as the value must be convertible to the
  314. property map's value type. Details of that conversion are unspecified.
  315. 3) The property map need not support this member function, in which
  316. case an error will be signaled. This is the runtime analogue of the
  317. Readable Property Map concept.</p>
  318. <pre class="literal-block">
  319. const std::type_info&amp; key() const
  320. </pre>
  321. <p>Returns a <tt class="docutils literal"><span class="pre">type_info</span></tt> object that represents the property map's key type.</p>
  322. <pre class="literal-block">
  323. const std::type_info&amp; value() const
  324. </pre>
  325. <p>Returns a <tt class="docutils literal"><span class="pre">type_info</span></tt> object that represents the property map's value type.</p>
  326. </div>
  327. <div class="section" id="exceptions">
  328. <h2><a class="toc-backref" href="#id8">Exceptions</a></h2>
  329. <pre class="literal-block">
  330. struct dynamic_property_exception : public std::exception {
  331. virtual ~dynamic_property_exception() throw() {}
  332. };
  333. struct property_not_found : public std::exception {
  334. std::string property;
  335. property_not_found(const std::string&amp; property);
  336. virtual ~property_not_found() throw();
  337. const char* what() const throw();
  338. };
  339. struct dynamic_get_failure : public std::exception {
  340. std::string property;
  341. dynamic_get_failure(const std::string&amp; property);
  342. virtual ~dynamic_get_failure() throw();
  343. const char* what() const throw();
  344. };
  345. struct dynamic_const_put_error : public std::exception {
  346. virtual ~dynamic_const_put_error() throw();
  347. const char* what() const throw();
  348. };
  349. </pre>
  350. <p>Under certain circumstances, calls to <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> member
  351. functions will throw one of the above exceptions. The three concrete
  352. exceptions can all be caught using the general
  353. <tt class="docutils literal"><span class="pre">dynamic_property_exception</span></tt> moniker when greater precision is not
  354. needed. In addition, all of the above exceptions derive from the
  355. standard <tt class="docutils literal"><span class="pre">std::exception</span></tt> for even more generalized error handling.
  356. The specific circumstances that result in these exceptions are
  357. described above.</p>
  358. </div>
  359. </div>
  360. </div>
  361. <div class="footer">
  362. <hr class="footer" />
  363. Generated on: 2010-03-29 18:04 UTC.
  364. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  365. </div>
  366. </body>
  367. </html>