File: Monitor.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (86 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download | duplicates (2)
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
#pragma once

#include <type_traits>

#include "tracing/categories.h"

namespace tracing {

class MonitorBase {
 protected:
	const char* _name;
	Category _tracing_cat;

	void valueChanged(float newVal);

 public:
	MonitorBase(const char* name);

	// Disallow any copy or movement
	MonitorBase(const MonitorBase&) = delete;
	MonitorBase& operator=(const MonitorBase&) = delete;

	MonitorBase(MonitorBase&&) = delete;
	MonitorBase& operator=(MonitorBase&&) = delete;
};

template<typename T>
class Monitor: public MonitorBase {
	T _value;

	static_assert(std::is_convertible<T, float>::value, "Monitor values must be convertible to float!");
 public:
	Monitor(const char* name, const T& defaultVal) : MonitorBase(name), _value(defaultVal) {
	}

	void changeValue(const T& val) {
		_value = val;
		valueChanged(_value);
	}

	Monitor<T>& operator=(const T& val) {
		changeValue(val);
		return *this;
	}

	Monitor<T>& operator+=(const T& val) {
		_value += val;
		valueChanged((float)_value);

		return *this;
	}

	Monitor<T>& operator-=(const T& val) {
		_value -= val;
		valueChanged((float)_value);

		return *this;
	}
};

/**
 * @brief Class that keeps track of how many operations are currently running
 */
class RunningCounter {
	Monitor<int>& _monitor;

  public:
	explicit RunningCounter(Monitor<int>& monitor);
	~RunningCounter();

	RunningCounter(const RunningCounter&);
	RunningCounter& operator=(const RunningCounter&);

	RunningCounter(RunningCounter&&) noexcept;
	RunningCounter& operator=(RunningCounter&&) noexcept;
};

} // namespace tracing

// Creates a monitor variable
#define MONITOR(function_name)				static ::tracing::Monitor<int> mon_##function_name(#function_name, 0);

// Increments a monitor variable
#define MONITOR_INC(function_name, inc)		do { mon_##function_name += (inc); } while(false)