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.68.1"> |
---|
7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
---|
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 C++ Libraries" 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><h2 class="title" style="clear: both"> |
---|
27 | <a name="id936841"></a>Examples</h2></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 <list> |
---|
32 | #include <boost/any.hpp> |
---|
33 | |
---|
34 | using <code class="computeroutput"><a href="../boost/any_cast.html" title="Function any_cast">boost::any_cast</a></code>; |
---|
35 | typedef std::list<<code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code>> many; |
---|
36 | |
---|
37 | void append_int(many & 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 | |
---|
43 | void append_string(many & values, const std::string & value) |
---|
44 | { |
---|
45 | values.push_back(value); |
---|
46 | } |
---|
47 | |
---|
48 | void append_char_ptr(many & values, const char * value) |
---|
49 | { |
---|
50 | values.push_back(value); |
---|
51 | } |
---|
52 | |
---|
53 | void append_any(many & values, const <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> & value) |
---|
54 | { |
---|
55 | values.push_back(value); |
---|
56 | } |
---|
57 | |
---|
58 | void append_nothing(many & 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"> |
---|
67 | bool is_empty(const <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> & operand) |
---|
68 | { |
---|
69 | return operand.<code class="computeroutput"><a href="../boost/any.html#id717434-bb">empty</a></code>(); |
---|
70 | } |
---|
71 | |
---|
72 | bool is_int(const <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> & operand) |
---|
73 | { |
---|
74 | return operand.<code class="computeroutput"><a href="../boost/any.html#id717459-bb">type</a></code>() == typeid(int); |
---|
75 | } |
---|
76 | |
---|
77 | bool is_char_ptr(const <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> & operand) |
---|
78 | { |
---|
79 | try |
---|
80 | { |
---|
81 | <code class="computeroutput"><a href="../boost/any_cast.html" title="Function any_cast">any_cast</a></code><const char *>(operand); |
---|
82 | return true; |
---|
83 | } |
---|
84 | catch(const <code class="computeroutput"><a href="../boost/bad_any_cast.html" title="Class bad_any_cast">boost::bad_any_cast</a></code> &) |
---|
85 | { |
---|
86 | return false; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | bool is_string(const <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> & operand) |
---|
91 | { |
---|
92 | return <code class="computeroutput"><a href="../boost/any_cast.html" title="Function any_cast">any_cast</a></code><std::string>(&operand); |
---|
93 | } |
---|
94 | |
---|
95 | void count_all(many & values, std::ostream & out) |
---|
96 | { |
---|
97 | out << "#empty == " |
---|
98 | << std::count_if(values.begin(), values.end(), is_empty) << std::endl; |
---|
99 | out << "#int == " |
---|
100 | << std::count_if(values.begin(), values.end(), is_int) << std::endl; |
---|
101 | out << "#const char * == " |
---|
102 | << std::count_if(values.begin(), values.end(), is_char_ptr) << std::endl; |
---|
103 | out << "#string == " |
---|
104 | << std::count_if(values.begin(), values.end(), is_string) << 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"> |
---|
109 | struct property |
---|
110 | { |
---|
111 | property(); |
---|
112 | property(const std::string &, const <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> &); |
---|
113 | |
---|
114 | std::string name; |
---|
115 | <code class="computeroutput"><a href="../boost/any.html" title="Class any">boost::any</a></code> value; |
---|
116 | }; |
---|
117 | |
---|
118 | typedef std::list<property> 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"> |
---|
127 | class consumer |
---|
128 | { |
---|
129 | public: |
---|
130 | virtual void notify(const <code class="computeroutput"><a href="../boost/any.html" title="Class any">any</a></code> &) = 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> |
---|