File: mocks.h

package info (click to toggle)
intrusive-shared-ptr 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: cpp: 2,852; ansic: 173; makefile: 3
file content (63 lines) | stat: -rw-r--r-- 1,330 bytes parent folder | download
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


template<int Tag = 1>
struct instrumented_counted
{
    mutable int count = 1;

    virtual ~instrumented_counted()
    {
        CHECK(this->count == -1); 
    }
};

template<int Tag = 1>
struct derived_instrumented_counted : instrumented_counted<Tag>
{};

struct non_counted 
{};


template<int TraitsTag = 1>
struct mock_traits
{
    template<int Tag>
    static void add_ref(const instrumented_counted<Tag> * c) noexcept
    {
        REQUIRE(c->count > 0); 
        ++c->count; 
    }

    template<int Tag>
    static void sub_ref(const instrumented_counted<Tag> * c) noexcept
    { 
        CHECK(c->count > 0);
        if (--c->count == 0)
            c->count = -1;
    }
};

template<class T>
using mock_ptr = isptr::intrusive_shared_ptr<T, mock_traits<>>;

template<class T>
using mock_ptr_different_traits = isptr::intrusive_shared_ptr<T, mock_traits<2>>;

template<class T> 
mock_ptr<T> mock_ref(T * ptr) {
    return mock_ptr<T>::ref(ptr);
}
template<class T> 
mock_ptr<T> mock_noref(T * ptr) {
    return mock_ptr<T>::noref(ptr);
}

template<class T> 
mock_ptr_different_traits<T> mock_ref_different_traits(T * ptr) {
    return mock_ptr_different_traits<T>::ref(ptr);
}
template<class T> 
mock_ptr_different_traits<T> mock_noref_different_traits(T * ptr) {
    return mock_ptr_different_traits<T>::noref(ptr);
}