summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/contract/test/detail/counter.hpp
blob: ab40dfcdbf21e5cd6cb55d390bc7db1914128e92 (plain)
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
#ifndef BOOST_CONTRACT_TEST_DETAIL_COUNTER_HPP_
#define BOOST_CONTRACT_TEST_DETAIL_COUNTER_HPP_

// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html

namespace boost { namespace contract { namespace test { namespace detail {

// Helper to count copies and evaluations of type (e.g., for old values).
template<class Tag, typename T>
struct counter {
    T value;

    counter() : value() { ++ctors_; }
    static unsigned ctors() { return ctors_; }

    ~counter() { ++dtors_; }
    static unsigned dtors() { return dtors_; }

    /* implicit */ counter(counter const& other) : value(other.value) {
        ++ctor_copies_;
        ++ctors_;
    }

    counter& operator=(counter const& other) {
        value = other.value;
        ++op_copies_;
        return *this;
    }
    
    static unsigned copies() { return ctor_copies_ + op_copies_; }

    static counter const& eval(counter const& me) { ++me.evals_; return me; }
    static unsigned evals() { return evals_; }

private:
    static unsigned ctors_; // Total constructions (including copies).
    static unsigned dtors_;
    static unsigned ctor_copies_;
    static unsigned op_copies_;
    static unsigned evals_;
};

template<class Tag, typename T> unsigned counter<Tag, T>::ctors_ = 0;
template<class Tag, typename T> unsigned counter<Tag, T>::dtors_ = 0;
template<class Tag, typename T> unsigned counter<Tag, T>::ctor_copies_ = 0;
template<class Tag, typename T> unsigned counter<Tag, T>::op_copies_ = 0;
template<class Tag, typename T> unsigned counter<Tag, T>::evals_ = 0;
    
} } } } // namespace

#endif // #include guard