• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Examples</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../any.html" title="Chapter 4. Boost.Any">
10<link rel="prev" href="../any.html" title="Chapter 4. Boost.Any">
11<link rel="next" href="reference.html" title="Reference">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="../any.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../any.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="id-1.3.5.4"></a>Examples</h2></div></div></div>
29<p>The following code demonstrates the syntax for using
30    implicit conversions to and copying of any objects:</p>
31<pre class="programlisting">
32#include &lt;list&gt;
33#include &lt;boost/any.hpp&gt;
34
35using <code class="computeroutput"><a class="link" href="../boost/any_cast.html" title="Function any_cast">boost::any_cast</a></code>;
36typedef std::list&lt;<code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code>&gt; many;
37
38void append_int(many &amp; values, int value)
39{
40    <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> to_append = value;
41    values.push_back(to_append);
42}
43
44void append_string(many &amp; values, const std::string &amp; value)
45{
46    values.push_back(value);
47}
48
49void append_char_ptr(many &amp; values, const char * value)
50{
51    values.push_back(value);
52}
53
54void append_any(many &amp; values, const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> &amp; value)
55{
56    values.push_back(value);
57}
58
59void append_nothing(many &amp; values)
60{
61    values.push_back(<code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code>());
62}
63</pre>
64<p>The following predicates follow on from the previous
65    definitions and demonstrate the use of queries on any
66    objects:</p>
67<pre class="programlisting">
68bool is_empty(const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> &amp; operand)
69{
70    return operand.<code class="computeroutput"><a class="link" href="../boost/any.html#id-1_3_5_5_2_1_2_13_1-bb">empty</a></code>();
71}
72
73bool is_int(const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> &amp; operand)
74{
75    return operand.<code class="computeroutput"><a class="link" href="../boost/any.html#id-1_3_5_5_2_1_2_13_2-bb">type</a></code>() == typeid(int);
76}
77
78bool is_char_ptr(const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> &amp; operand)
79{
80    try
81    {
82        <code class="computeroutput"><a class="link" href="../boost/any_cast.html" title="Function any_cast">any_cast</a></code>&lt;const char *&gt;(operand);
83        return true;
84    }
85    catch(const <code class="computeroutput"><a class="link" href="../boost/bad_any_cast.html" title="Class bad_any_cast">boost::bad_any_cast</a></code> &amp;)
86    {
87        return false;
88    }
89}
90
91bool is_string(const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> &amp; operand)
92{
93    return <code class="computeroutput"><a class="link" href="../boost/any_cast.html" title="Function any_cast">any_cast</a></code>&lt;std::string&gt;(&amp;operand);
94}
95
96void count_all(many &amp; values, std::ostream &amp; out)
97{
98    out &lt;&lt; "#empty == "
99        &lt;&lt; std::count_if(values.begin(), values.end(), is_empty) &lt;&lt; std::endl;
100    out &lt;&lt; "#int == "
101        &lt;&lt; std::count_if(values.begin(), values.end(), is_int) &lt;&lt; std::endl;
102    out &lt;&lt; "#const char * == "
103        &lt;&lt; std::count_if(values.begin(), values.end(), is_char_ptr) &lt;&lt; std::endl;
104    out &lt;&lt; "#string == "
105        &lt;&lt; std::count_if(values.begin(), values.end(), is_string) &lt;&lt; std::endl;
106}
107</pre>
108<p>The following type, patterned after the OMG's Property Service, defines name-value pairs for arbitrary value types:</p>
109<pre class="programlisting">
110struct property
111{
112    property();
113    property(const std::string &amp;, const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> &amp;);
114
115    std::string name;
116    <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">boost::any</a></code> value;
117};
118
119typedef std::list&lt;property&gt; properties;
120</pre>
121<p>The following base class demonstrates one approach to
122    runtime polymorphism based callbacks that also require arbitrary
123    argument types. The absence of virtual member templates requires
124    that different solutions have different trade-offs in terms of
125    efficiency, safety, and generality. Using a checked variant type
126    offers one approach:</p>
127<pre class="programlisting">
128class consumer
129{
130public:
131    virtual void notify(const <code class="computeroutput"><a class="link" href="../boost/any.html" title="Class any">any</a></code> &amp;) = 0;
132    ...
133};
134</pre>
135</div>
136<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
137<td align="left"></td>
138<td align="right"><div class="copyright-footer">Copyright © 2001 Kevlin Henney<br>Copyright © 2013-2020 Antony Polukhin<p>Distributed under the Boost Software License, Version 1.0.
139      (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at
140      <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
141      </p>
142</div></td>
143</tr></table>
144<hr>
145<div class="spirit-nav">
146<a accesskey="p" href="../any.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../any.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
147</div>
148</body>
149</html>
150