root/hydranode/hnbase/test/test-unchainptr.cpp

Revision 2613, 3.3 kB (checked in by madcat, 3 years ago)

Copyright notice update for 2006.

Line 
1 /* Copyright 2005-2006 Alo Sarv
2  * Distributed under the Boost Software Licence, Version 1.0
3  * (See accompanying file LICENCE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  */
6
7 #include <boost/mpl/assert.hpp>
8 #include <hnbase/unchain_ptr.h>
9 #include <iostream>
10 using namespace Utils;
11
12 // compile-time checks for detail::get_type template
13 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int>::type> ));
14 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int*>::type> ));
15 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int**>::type> ));
16 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int***>::type> ));
17 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int****>::type> ));
18 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int&>::type> ));
19 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int*&>::type> ));
20 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int**&>::type> ));
21 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int***&>::type> ));
22 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int****&>::type> ));
23 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int *const>::type> ));
24 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int *const&>::type> ));
25 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<int **const&>::type> ));
26 BOOST_MPL_ASSERT(( boost::is_same<const int, detail::get_type<const int*>::type> ));
27 BOOST_MPL_ASSERT(( boost::is_same<const int, detail::get_type<const int&>::type> ));
28 BOOST_MPL_ASSERT(( boost::is_same<const int, detail::get_type<const int*&>::type> ));
29 BOOST_MPL_ASSERT(( boost::is_same<const int, detail::get_type<const int *const>::type> ));
30 BOOST_MPL_ASSERT(( boost::is_same<const int, detail::get_type<const int *const&>::type> ));
31 BOOST_MPL_ASSERT(( boost::is_same<const int, detail::get_type<const int **const&>::type> ));
32 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::shared_ptr<int> >::type> ));
33 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::shared_ptr<int>&>::type> ));
34 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::shared_ptr<int>*>::type> ));
35 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::shared_ptr<int>*&>::type> ));
36 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::shared_ptr<int*> >::type> ));
37 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::shared_ptr<int*&> >::type> ));
38 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::weak_ptr<int> >::type> ));
39 BOOST_MPL_ASSERT(( boost::is_same<int, detail::get_type<boost::intrusive_ptr<int> >::type> ));
40
41 // calls const function on type
42 template<typename T>
43 void process1(T t) {
44         unchain_ptr(t).foo();
45 }
46
47 // calls non-const function on type
48 template<typename T>
49 void process2(T t) {
50         unchain_ptr(t).bar();
51 }
52
53 struct X {
54         void foo() const { std::cerr << "foo()" << std::endl; }
55         void bar() { std::cerr << "bar()" << std::endl; }
56 };
57
58 struct nullDeleter {
59         template<typename T>
60         void operator()(T t) {}
61 };
62
63 int main() {
64         X t1;
65         process1(t1);
66         process2(t1);
67         X *t2 = new X;
68         process1(t2);
69         process2(t2);
70         X **t3 = &t2;
71         process1(t3);
72         process2(t3);
73         boost::shared_ptr<X> t4(new X);
74         process1(t4);
75         process2(t4);
76         boost::shared_ptr<const X> t5(new X);
77         process1(t5);
78         boost::shared_ptr<X*> t6(&t2, nullDeleter());
79         process1(t6);
80         process2(t6);
81 }
Note: See TracBrowser for help on using the browser.