blob: b5102afe8107d7bfc57a1caec65d0bf736c9bec1 [file] [log] [blame]
Marco Polettibdf5dc02016-12-21 17:37:54 +00001/*
2 * Copyright 2014 Google Inc. All rights reserved.
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 FRUIT_CLASS_CONSTRUCTION_TRACKER_H
18#define FRUIT_CLASS_CONSTRUCTION_TRACKER_H
19
20/**
21 * This class is useful to keep track of how many instances of a given type are created during the entire program
22 * execution.
23 *
24 * Example use:
25 * class Foo : public ConstructionTracker<Foo> {
26 * ...
27 * };
28 *
29 * int main() {
30 * ...
31 * assert(Foo::num_objects_constructed == 3);
32 * }
33 */
34template <typename T>
35struct ConstructionTracker {
36 static std::size_t num_objects_constructed;
37
38 ConstructionTracker() {
39 ++num_objects_constructed;
40 }
41};
42
43template <typename T>
44std::size_t ConstructionTracker<T>::num_objects_constructed = 0;
45
46
47#endif // FRUIT_CLASS_CONSTRUCTION_TRACKER_H