Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/any/s02.html @ 25

Last change on this file since 25 was 12, checked in by landauf, 17 years ago

added boost

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