any.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <library name="Any" dirname="any" xmlns:xi="http://www.w3.org/2001/XInclude"
  5. id="any" last-revision="$Date$">
  6. <libraryinfo>
  7. <author>
  8. <firstname>Kevlin</firstname>
  9. <surname>Henney</surname>
  10. </author>
  11. <copyright>
  12. <year>2001</year>
  13. <holder>Kevlin Henney</holder>
  14. </copyright>
  15. <copyright>
  16. <year>2013-2019</year>
  17. <holder>Antony Polukhin</holder>
  18. </copyright>
  19. <librarypurpose>
  20. Safe, generic container for single values of different value types
  21. </librarypurpose>
  22. <librarycategory name="category:data-structures"/>
  23. <legalnotice>
  24. <para>Distributed under the Boost Software License, Version 1.0.
  25. (See accompanying file <filename>LICENSE_1_0.txt</filename> or copy at
  26. <ulink
  27. url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)
  28. </para>
  29. </legalnotice>
  30. </libraryinfo>
  31. <title>Boost.Any</title>
  32. <section>
  33. <title>Introduction</title>
  34. <para>There are times when a generic (in the sense of
  35. <emphasis>general</emphasis> as opposed to
  36. <emphasis>template-based programming</emphasis>) type is needed:
  37. variables that are truly variable, accommodating values of many
  38. other more specific types rather than C++'s normal strict and
  39. static types. We can distinguish three basic kinds of generic
  40. type:</para>
  41. <itemizedlist>
  42. <listitem>
  43. <para>Converting types that can hold one of a number of
  44. possible value types, e.g. <code>int</code> and
  45. <code>string</code>, and freely convert between them, for
  46. instance interpreting <code>5</code> as <code>"5"</code> or
  47. vice-versa. Such types are common in scripting and other
  48. interpreted
  49. languages.
  50. <code><functionname>boost::lexical_cast</functionname></code>
  51. supports such conversion functionality.</para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. Discriminated types that contain values of different types but
  56. do not attempt conversion between them, i.e. <code>5</code> is
  57. held strictly as an <code>int</code> and is not implicitly
  58. convertible either to <code>"5"</code> or to
  59. <code>5.0</code>. Their indifference to interpretation but
  60. awareness of type effectively makes them safe, generic
  61. containers of single values, with no scope for surprises from
  62. ambiguous conversions.</para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. Indiscriminate types that can refer to anything but are
  67. oblivious to the actual underlying type, entrusting all forms
  68. of access and interpretation to the programmer. This niche is
  69. dominated by <code>void *</code>, which offers plenty of scope
  70. for surprising, undefined behavior.</para>
  71. </listitem>
  72. </itemizedlist>
  73. <para>The <code><classname>boost::any</classname></code> class
  74. (based on the class of the same name described in <ulink
  75. url="http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf">"Valued
  76. Conversions"</ulink> by Kevlin Henney, <emphasis>C++
  77. Report</emphasis> 12(7), July/August 2000) is a variant value type
  78. based on the second category. It supports copying of any value
  79. type and safe checked extraction of that value strictly against
  80. its type. A similar design, offering more appropriate operators,
  81. can be used for a generalized function adaptor,
  82. <code>any_function</code>, a generalized iterator adaptor,
  83. <code>any_iterator</code>, and other object types that need
  84. uniform runtime treatment but support only compile-time template
  85. parameter conformance.</para>
  86. </section>
  87. <section>
  88. <title>Examples</title>
  89. <using-namespace name="boost"/>
  90. <using-class name="boost::any"/>
  91. <para>The following code demonstrates the syntax for using
  92. implicit conversions to and copying of any objects:</para>
  93. <programlisting name="any.example.first">
  94. #include &lt;list&gt;
  95. #include &lt;boost/any.hpp&gt;
  96. using <functionname>boost::any_cast</functionname>;
  97. typedef std::list&lt;<classname>boost::any</classname>&gt; many;
  98. void append_int(many &amp; values, int value)
  99. {
  100. <classname>boost::any</classname> to_append = value;
  101. values.push_back(to_append);
  102. }
  103. void append_string(many &amp; values, const std::string &amp; value)
  104. {
  105. values.push_back(value);
  106. }
  107. void append_char_ptr(many &amp; values, const char * value)
  108. {
  109. values.push_back(value);
  110. }
  111. void append_any(many &amp; values, const <classname>boost::any</classname> &amp; value)
  112. {
  113. values.push_back(value);
  114. }
  115. void append_nothing(many &amp; values)
  116. {
  117. values.push_back(<classname>boost::any</classname>());
  118. }
  119. </programlisting>
  120. <para>The following predicates follow on from the previous
  121. definitions and demonstrate the use of queries on any
  122. objects:</para>
  123. <programlisting name="any.example.second">
  124. bool is_empty(const <classname>boost::any</classname> &amp; operand)
  125. {
  126. return operand.<methodname>empty</methodname>();
  127. }
  128. bool is_int(const <classname>boost::any</classname> &amp; operand)
  129. {
  130. return operand.<methodname>type</methodname>() == typeid(int);
  131. }
  132. bool is_char_ptr(const <classname>boost::any</classname> &amp; operand)
  133. {
  134. try
  135. {
  136. <functionname>any_cast</functionname>&lt;const char *&gt;(operand);
  137. return true;
  138. }
  139. catch(const <classname>boost::bad_any_cast</classname> &amp;)
  140. {
  141. return false;
  142. }
  143. }
  144. bool is_string(const <classname>boost::any</classname> &amp; operand)
  145. {
  146. return <functionname>any_cast</functionname>&lt;std::string&gt;(&amp;operand);
  147. }
  148. void count_all(many &amp; values, std::ostream &amp; out)
  149. {
  150. out &lt;&lt; "#empty == "
  151. &lt;&lt; std::count_if(values.begin(), values.end(), is_empty) &lt;&lt; std::endl;
  152. out &lt;&lt; "#int == "
  153. &lt;&lt; std::count_if(values.begin(), values.end(), is_int) &lt;&lt; std::endl;
  154. out &lt;&lt; "#const char * == "
  155. &lt;&lt; std::count_if(values.begin(), values.end(), is_char_ptr) &lt;&lt; std::endl;
  156. out &lt;&lt; "#string == "
  157. &lt;&lt; std::count_if(values.begin(), values.end(), is_string) &lt;&lt; std::endl;
  158. }
  159. </programlisting>
  160. <para>The following type, patterned after the OMG's Property Service, defines name-value pairs for arbitrary value types:</para>
  161. <programlisting>
  162. struct property
  163. {
  164. property();
  165. property(const std::string &amp;, const <classname>boost::any</classname> &amp;);
  166. std::string name;
  167. <classname>boost::any</classname> value;
  168. };
  169. typedef std::list&lt;property&gt; properties;
  170. </programlisting>
  171. <para>The following base class demonstrates one approach to
  172. runtime polymorphism based callbacks that also require arbitrary
  173. argument types. The absence of virtual member templates requires
  174. that different solutions have different trade-offs in terms of
  175. efficiency, safety, and generality. Using a checked variant type
  176. offers one approach:</para>
  177. <programlisting>
  178. class consumer
  179. {
  180. public:
  181. virtual void notify(const <classname>any</classname> &amp;) = 0;
  182. ...
  183. };
  184. </programlisting>
  185. </section>
  186. <library-reference>
  187. <section id="any.ValueType">
  188. <title><emphasis>ValueType</emphasis> requirements</title>
  189. <para>Values are strongly informational objects for which
  190. identity is not significant, i.e. the focus is principally on
  191. their state content and any behavior organized around
  192. that. Another distinguishing feature of values is their
  193. granularity: normally fine-grained objects representing simple
  194. concepts in the system such as quantities.</para>
  195. <para>As the emphasis of a value lies in its state not its
  196. identity, values can be copied and typically assigned one to
  197. another, requiring the explicit or implicit definition of a
  198. public copy constructor and public assignment operator. Values
  199. typically live within other scopes, i.e. within objects or
  200. blocks, rather than on the heap. Values are therefore normally
  201. passed around and manipulated directly as variables or through
  202. references, but not as pointers that emphasize identity and
  203. indirection.</para>
  204. <para>The specific requirements on value types to be used in an
  205. <code><classname alt="boost::any">any</classname></code>
  206. are:</para>
  207. <itemizedlist spacing="compact">
  208. <listitem><simpara>A <emphasis>ValueType</emphasis> is
  209. <emphasis>CopyConstructible</emphasis> [20.1.3].</simpara>
  210. </listitem>
  211. <listitem><simpara>The destructor for a
  212. <emphasis>ValueType</emphasis> upholds the no-throw
  213. exception-safety guarantee.</simpara>
  214. </listitem>
  215. </itemizedlist>
  216. </section>
  217. <header name="boost/any.hpp">
  218. <namespace name="boost">
  219. <class name="bad_any_cast">
  220. <inherit access="public">
  221. <classname>std::bad_cast</classname>
  222. </inherit>
  223. <purpose>The exception thrown in the event of a failed
  224. <code><functionname>any_cast</functionname></code> of an
  225. <code><classname>any</classname></code> value.</purpose>
  226. <method name="what" specifiers="virtual" cv="const">
  227. <type>const char *</type>
  228. </method>
  229. </class>
  230. <class name="any">
  231. <purpose>A class whose instances can hold instances of any
  232. type that satisfies <link
  233. linkend="any.ValueType">ValueType</link>
  234. requirements.</purpose>
  235. <constructor>
  236. <postconditions><simpara><code>this-&gt;<methodname>empty</methodname>()</code></simpara></postconditions>
  237. </constructor>
  238. <constructor>
  239. <parameter name="other">
  240. <paramtype>const <classname>any</classname> &amp;</paramtype>
  241. </parameter>
  242. <effects><simpara> Copy constructor that copies content of
  243. <code>other</code> into new instance, so that any content
  244. is equivalent in both type and value to the content of
  245. <code>other</code>, or empty if <code>other</code> is
  246. empty. </simpara></effects>
  247. <throws><simpara>May fail with a
  248. <code><classname>std::bad_alloc</classname></code>
  249. exception or any exceptions arising from the copy
  250. constructor of the contained type.</simpara></throws>
  251. </constructor>
  252. <constructor>
  253. <parameter name="other">
  254. <paramtype><classname>any</classname> &amp;&amp;</paramtype>
  255. </parameter>
  256. <effects><simpara> Move constructor that moves content of
  257. <code>other</code> into new instance and leaves <code>other</code>
  258. empty. </simpara></effects>
  259. <precondition>C++11 compatible compiler.</precondition>
  260. <postconditions><simpara><code>other-&gt;<methodname>empty</methodname>()</code></simpara></postconditions>
  261. <throws><simpara>Nothing.</simpara></throws>
  262. </constructor>
  263. <constructor>
  264. <template>
  265. <template-type-parameter name="ValueType"/>
  266. </template>
  267. <parameter name="value">
  268. <paramtype>const ValueType &amp;</paramtype>
  269. </parameter>
  270. <effects><simpara>Makes a copy of <code>value</code>, so
  271. that the initial content of the new instance is equivalent
  272. in both type and value to
  273. <code>value</code>.</simpara></effects>
  274. <throws><simpara><code><classname>std::bad_alloc</classname></code>
  275. or any exceptions arising from the copy constructor of the
  276. contained type.</simpara></throws>
  277. </constructor>
  278. <constructor>
  279. <template>
  280. <template-type-parameter name="ValueType"/>
  281. </template>
  282. <parameter name="value">
  283. <paramtype>ValueType &amp;&amp;</paramtype>
  284. </parameter>
  285. <effects><simpara>Forwards <code>value</code>, so
  286. that the initial content of the new instance is equivalent
  287. in both type and value to
  288. <code>value</code> before the forward.</simpara></effects>
  289. <precondition>C++11 compatible compiler.</precondition>
  290. <throws><simpara><code><classname>std::bad_alloc</classname></code>
  291. or any exceptions arising from the copy constructor of the
  292. contained type.</simpara></throws>
  293. </constructor>
  294. <destructor>
  295. <effects><simpara>Releases any and all resources used in
  296. management of instance.</simpara></effects>
  297. <throws><simpara>Nothing.</simpara></throws>
  298. </destructor>
  299. <copy-assignment>
  300. <type><classname>any</classname> &amp;</type>
  301. <parameter name="rhs">
  302. <paramtype>const <classname>any</classname> &amp;</paramtype>
  303. </parameter>
  304. <effects><simpara>Copies content of <code>rhs</code> into
  305. current instance, discarding previous content, so that the
  306. new content is equivalent in both type and value to the
  307. content of <code>rhs</code>, or empty if
  308. <code>rhs.<methodname>empty</methodname>()</code>.</simpara></effects>
  309. <throws><simpara><code><classname>std::bad_alloc</classname></code>
  310. or any exceptions arising from the copy constructor of the
  311. contained type. Assignment satisfies the strong guarantee
  312. of exception safety.</simpara></throws>
  313. </copy-assignment>
  314. <copy-assignment>
  315. <type><classname>any</classname> &amp;</type>
  316. <parameter name="rhs">
  317. <paramtype><classname>any</classname> &amp;&amp;</paramtype>
  318. </parameter>
  319. <effects><simpara>Moves content of <code>rhs</code> into
  320. current instance, discarding previous content, so that the
  321. new content is equivalent in both type and value to the
  322. content of <code>rhs</code> before move, or empty if
  323. <code>rhs.<methodname>empty</methodname>()</code>.</simpara></effects>
  324. <precondition>C++11 compatible compiler.</precondition>
  325. <postconditions><simpara><code>rhs-&gt;<methodname>empty</methodname>()</code></simpara></postconditions>
  326. <throws><simpara>Nothing.</simpara></throws>
  327. </copy-assignment>
  328. <copy-assignment>
  329. <template>
  330. <template-type-parameter name="ValueType"/>
  331. </template>
  332. <type><classname>any</classname> &amp;</type>
  333. <parameter name="rhs">
  334. <paramtype>const ValueType &amp;</paramtype>
  335. </parameter>
  336. <effects><simpara>Makes a copy of <code>rhs</code>,
  337. discarding previous content, so that the new content of is
  338. equivalent in both type and value to
  339. <code>rhs</code>.</simpara></effects>
  340. <throws><simpara><code><classname>std::bad_alloc</classname></code>
  341. or any exceptions arising from the copy constructor of the
  342. contained type. Assignment satisfies the strong guarantee
  343. of exception safety.</simpara></throws>
  344. </copy-assignment>
  345. <copy-assignment>
  346. <template>
  347. <template-type-parameter name="ValueType"/>
  348. </template>
  349. <type><classname>any</classname> &amp;</type>
  350. <parameter name="rhs">
  351. <paramtype>ValueType &amp;&amp;</paramtype>
  352. </parameter>
  353. <effects><simpara>Forwards <code>rhs</code>,
  354. discarding previous content, so that the new content of is
  355. equivalent in both type and value to
  356. <code>rhs</code> before forward.</simpara></effects>
  357. <precondition>C++11 compatible compiler.</precondition>
  358. <throws><simpara><code><classname>std::bad_alloc</classname></code>
  359. or any exceptions arising from the move or copy constructor of the
  360. contained type. Assignment satisfies the strong guarantee
  361. of exception safety.</simpara></throws>
  362. </copy-assignment>
  363. <method-group name="modifiers">
  364. <method name="swap">
  365. <type><classname>any</classname> &amp;</type>
  366. <parameter name="rhs">
  367. <paramtype><classname>any</classname> &amp;</paramtype>
  368. </parameter>
  369. <effects><simpara>Exchange of the contents of
  370. <code>*this</code> and
  371. <code>rhs</code>.</simpara></effects>
  372. <returns><simpara><code>*this</code></simpara></returns>
  373. <throws><simpara>Nothing.</simpara></throws>
  374. </method>
  375. </method-group>
  376. <method-group name="queries">
  377. <method name="empty" cv="const">
  378. <type>bool</type>
  379. <returns><simpara><code>true</code> if instance is
  380. empty, otherwise <code>false</code>.</simpara></returns>
  381. <throws><simpara>Nothing.</simpara></throws>
  382. </method>
  383. <method name="type" cv="const">
  384. <type>const <classname>std::type_info</classname> &amp;</type>
  385. <returns><simpara>the <code>typeid</code> of the
  386. contained value if instance is non-empty, otherwise
  387. <code>typeid(void)</code>.</simpara></returns>
  388. <notes><simpara>Useful for querying against types known
  389. either at compile time or only at
  390. runtime.</simpara></notes>
  391. </method>
  392. </method-group>
  393. </class>
  394. <function name="swap">
  395. <type>void</type>
  396. <parameter name="lhs">
  397. <paramtype><classname>any</classname> &amp;</paramtype>
  398. </parameter>
  399. <parameter name="rhs">
  400. <paramtype><classname>any</classname> &amp;</paramtype>
  401. </parameter>
  402. <effects><simpara>Exchange of the contents of
  403. <code>lhs</code> and
  404. <code>rhs</code>.</simpara></effects>
  405. <throws><simpara>Nothing.</simpara></throws>
  406. </function>
  407. <overloaded-function name="any_cast">
  408. <signature>
  409. <template>
  410. <template-type-parameter name="T"/>
  411. </template>
  412. <type>T</type>
  413. <parameter name="operand">
  414. <paramtype><classname>any</classname> &amp;</paramtype>
  415. </parameter>
  416. </signature>
  417. <signature>
  418. <template>
  419. <template-type-parameter name="T"/>
  420. </template>
  421. <type>T</type>
  422. <parameter name="operand">
  423. <paramtype><classname>any</classname> &amp;&amp;</paramtype>
  424. </parameter>
  425. </signature>
  426. <signature>
  427. <template>
  428. <template-type-parameter name="T"/>
  429. </template>
  430. <type>T</type>
  431. <parameter name="operand">
  432. <paramtype>const <classname>any</classname> &amp;</paramtype>
  433. </parameter>
  434. </signature>
  435. <signature>
  436. <template>
  437. <template-type-parameter name="ValueType"/>
  438. </template>
  439. <type>const ValueType *</type>
  440. <parameter name="operand">
  441. <paramtype>const <classname>any</classname> *</paramtype>
  442. </parameter>
  443. </signature>
  444. <signature>
  445. <template>
  446. <template-type-parameter name="ValueType"/>
  447. </template>
  448. <type>ValueType *</type>
  449. <parameter name="operand">
  450. <paramtype><classname>any</classname> *</paramtype>
  451. </parameter>
  452. </signature>
  453. <purpose><simpara>Custom keyword cast for extracting a value
  454. of a given type from an
  455. <code><classname>any</classname></code>.</simpara></purpose>
  456. <returns><simpara> If passed a pointer, it returns a
  457. similarly qualified pointer to the value content if
  458. successful, otherwise null is returned.
  459. If T is ValueType, it returns a copy of the held value, otherwise, if T is a reference
  460. to (possibly const qualified) ValueType, it returns a reference to the held
  461. value.</simpara></returns>
  462. <throws><simpara>Overloads taking an
  463. <code><classname>any</classname></code> pointer do not
  464. throw; overloads taking an
  465. <code><classname>any</classname></code> value or reference
  466. throws <code><classname>bad_any_cast</classname></code> if
  467. unsuccessful.</simpara></throws>
  468. </overloaded-function>
  469. </namespace>
  470. </header>
  471. </library-reference>
  472. <section>
  473. <title>Acknowledgements</title>
  474. <para>Doug Gregor ported the documentation to the BoostBook format.</para>
  475. </section>
  476. </library>