1/*
2 Copyright (c) 2005-2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifndef tbb_tests_harness_checktype_H
18#define tbb_tests_harness_checktype_H
19
20// type that checks construction and destruction.
21
22#ifndef __HARNESS_CHECKTYPE_DEFAULT_CTOR
23 #define __HARNESS_CHECKTYPE_DEFAULT_CTOR 1
24#endif
25
26template<class Counter>
27class check_type : Harness::NoAfterlife {
28 Counter id;
29 bool am_ready;
30public:
31 static tbb::atomic<int> check_type_counter;
32 // if only non-default constructors are desired, set __HARNESS_CHECKTYPE_NODEFAULT_CTOR
33 check_type(Counter _n
34#if __HARNESS_CHECKTYPE_DEFAULT_CTOR
35 = 0
36#endif
37 ) : id(_n), am_ready(false) {
38 ++check_type_counter;
39 }
40
41 check_type(const check_type& other) : Harness::NoAfterlife(other) {
42 other.AssertLive();
43 AssertLive();
44 id = other.id;
45 am_ready = other.am_ready;
46 ++check_type_counter;
47 }
48
49 operator int() const { return (int)my_id(); }
50 check_type& operator++() { ++id; return *this;; }
51
52 ~check_type() {
53 AssertLive();
54 --check_type_counter;
55 ASSERT(check_type_counter >= 0, "too many destructions");
56 }
57
58 check_type &operator=(const check_type &other) {
59 other.AssertLive();
60 AssertLive();
61 id = other.id;
62 am_ready = other.am_ready;
63 return *this;
64 }
65
66 Counter my_id() const { AssertLive(); return id; }
67 bool is_ready() { AssertLive(); return am_ready; }
68 void function() {
69 AssertLive();
70 if( id == (Counter)0 ) {
71 id = (Counter)1;
72 am_ready = true;
73 }
74 }
75
76};
77
78template<class Counter>
79tbb::atomic<int> check_type<Counter>::check_type_counter;
80
81// provide a class that for a check_type will initialize the counter on creation, and on
82// destruction will check that the constructions and destructions of check_type match.
83template<class MyClass>
84struct Check {
85 Check() {} // creation does nothing
86 ~Check() {} // destruction checks nothing
87};
88
89template<class Counttype>
90struct Check<check_type< Counttype > > {
91 Check() { check_type<Counttype>::check_type_counter = 0; }
92 ~Check() { ASSERT(check_type<Counttype>::check_type_counter == 0, "check_type constructions and destructions don't match"); }
93};
94
95#endif // tbb_tests_harness_checktype_H
96