File: ref_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (101 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright David Abrahams and Aleksey Gurtovoy
// 2002-2004. Distributed under the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// run-time test for "boost/core/ref.hpp" header content
// see 'ref_ct_test.cpp' for compile-time part

#include <boost/core/ref.hpp>
#include <boost/core/lightweight_test.hpp>

namespace {
using namespace boost;

template <class T>
struct ref_wrapper
{
    // Used to verify implicit conversion
    static T* get_pointer(T& x)
    {
        return &x;
    }

    static T const* get_const_pointer(T const& x)
    {
        return &x;
    }

    template <class Arg>
    static T* passthru(Arg x)
    {
        return get_pointer(x);
    }

    template <class Arg>
    static T const* cref_passthru(Arg x)
    {
        return get_const_pointer(x);
    }

    static void test(T x)
    {
        BOOST_TEST(passthru(ref(x)) == &x);
        BOOST_TEST(&ref(x).get() == &x);

        BOOST_TEST(cref_passthru(cref(x)) == &x);
        BOOST_TEST(&cref(x).get() == &x);
    }
};

struct copy_counter {
  static int count_;
  copy_counter(copy_counter const& /*other*/) {
    ++count_;
  }
  copy_counter() {}
  static void reset() { count_ = 0; }
  static int count() { return copy_counter::count_;  }
};

int copy_counter::count_ = 0;

} // namespace unnamed

template <class T>
void do_unwrap(T t) {

  /* typename unwrap_reference<T>::type& lt = */
  unwrap_ref(t);

}

void unwrap_test() {

  int i = 3;
  const int ci = 2;

  do_unwrap(i);
  do_unwrap(ci);
  do_unwrap(ref(i));
  do_unwrap(cref(ci));
  do_unwrap(ref(ci));

  copy_counter cc;
  BOOST_TEST(cc.count() == 0);

  do_unwrap(cc);
  do_unwrap(ref(cc));
  do_unwrap(cref(cc));

  BOOST_TEST(cc.count() == 1);
  BOOST_TEST(unwrap_ref(ref(cc)).count() == 1); 
}

int main()
{
    ref_wrapper<int>::test(1);
    ref_wrapper<int const>::test(1);
    unwrap_test();
    return boost::report_errors();
}