• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.TypeErasure library
2 //
3 // Copyright 2011 Steven Watanabe
4 //
5 // Distributed under the Boost Software License Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // $Id$
10 
11 #include <boost/type_erasure/any.hpp>
12 #include <boost/type_erasure/any_cast.hpp>
13 #include <boost/type_erasure/builtin.hpp>
14 #include <boost/type_erasure/operators.hpp>
15 #include <boost/type_erasure/tuple.hpp>
16 #include <boost/mpl/vector.hpp>
17 #include <iostream>
18 
19 namespace mpl = boost::mpl;
20 using namespace boost::type_erasure;
21 
multi1()22 void multi1() {
23     //[multi1
24     /*`
25         Operations can have more than one __any argument.
26         Let's use binary addition as an example.
27     */
28     typedef any<
29         mpl::vector<
30             copy_constructible<>,
31             typeid_<>,
32             addable<>,
33             ostreamable<>
34         >
35     > any_type;
36     any_type x(10);
37     any_type y(7);
38     any_type z(x + y);
39     std::cout << z << std::endl; // prints 17
40     /*`
41         This is /not/ a multimethod.  The underlying types of the
42         arguments of `+` must be the same or the behavior is undefined.
43         This example is correct because the arguments both hold
44         `int`'s.
45 
46         [note Adding __relaxed leads an exception rather than undefined
47         behavior if the argument types are wrong.]
48     */
49     //]
50 }
51 
multi2()52 void multi2() {
53     //[multi2
54     /*`
55         __addable`<>` requires the types of the arguments to be exactly
56         the same.  This doesn't cover all uses of addition though.  For
57         example, pointer arithmetic takes a pointer and an integer and
58         returns a pointer.  We can capture this kind of relationship among
59         several types by identifying each type involved with a placeholder.
60         We'll let the placeholder `_a` represent the pointer and the
61         placeholder `_b` represent the integer.
62     */
63 
64     int array[5];
65 
66     typedef mpl::vector<
67         copy_constructible<_a>,
68         copy_constructible<_b>,
69         typeid_<_a>,
70         addable<_a, _b, _a>
71     > requirements;
72 
73     /*`
74         Our new concept, `addable<_a, _b, _a>` captures the
75         rules of pointer addition: `_a + _b -> _a`.
76 
77         Also, we can no longer capture the variables
78         independently.
79         ``
80             any<requirements, _a> ptr(&array[0]); // illegal
81         ``
82         This doesn't work because the library needs
83         to know the type that _b binds to when it
84         captures the concept bindings.  We need to
85         specify the bindings of both placeholders
86         when we construct the __any.
87      */
88 
89     typedef mpl::map<mpl::pair<_a, int*>, mpl::pair<_b, int> > types;
90     any<requirements, _a> ptr(&array[0], make_binding<types>());
91     any<requirements, _b> idx(2, make_binding<types>());
92     any<requirements, _a> x(ptr + idx);
93     // x now holds array + 2
94 
95     /*`
96         Now that the arguments of `+` aren't the same type,
97         we require that both arguments agree that `_a` maps
98         to `int*` and that `_b` maps to `int`.
99 
100         We can also use __tuple to avoid having to
101         write out the map out explicitly.  __tuple is
102         just a convenience class that combines the
103         placeholder bindings it gets from all its arguments.
104      */
105     tuple<requirements, _a, _b> t(&array[0], 2);
106     any<requirements, _a> y(get<0>(t) + get<1>(t));
107     //]
108 }
109 
110 
111 //[multi
112 //` (For the source of the examples in this section see
113 //` [@boost:/libs/type_erasure/example/multi.cpp multi.cpp])
114 //` [multi1]
115 //` [multi2]
116 //]
117