DiSMEC++
tracked.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_STATS_TRACKED_H
7 #define DISMEC_STATS_TRACKED_H
8 
9 #include <memory>
10 #include "stat_id.h"
11 
12 namespace dismec::stats {
13  class StatisticsCollection;
14  class Statistics;
15  class ScopeTimer;
16 
42  class Tracked {
43  public:
45  Tracked();
46 
52  void register_stat(const std::string& name, std::unique_ptr<Statistics> stat);
53 
65  [[nodiscard]] std::shared_ptr<StatisticsCollection> get_stats() const {
66  return m_Collection;
67  }
68  private:
74  template<class T, class... Args>
75  T& make_dependent(T& t) {
76  return t;
77  }
78 
79  protected:
84 
89  template<class T>
90  void record(stat_id_t stat, T&& value) {
91  // make m_Collection dependent, so it only needs to be a complete type where we actually call this function
92  make_dependent<StatisticsCollection&, T>(*m_Collection).record(stat, std::forward<T>(value));
93  }
94 
100  void declare_stat(stat_id_t index, StatisticMetaData meta);
101 
107  void declare_tag(tag_id_t index, std::string name);
108 
109 
115  template<class... Args>
116  void set_tag(tag_id_t tag, long value) {
117  static_assert(sizeof...(Args) == 0, "Unsupported extra args");
118  // make m_Collection dependent, so it only needs to be a complete type where we actually call this function
119  make_dependent<StatisticsCollection&, Args...>(*m_Collection).set_tag(tag, value);
120  }
121 
129  template<class... Args>
130  auto make_timer(stat_id_t id, Args... args) {
131  // check that we don't accidentally use the dummy args in any way
132  static_assert(sizeof...(Args) == 0, "Unsupported extra args");
133  return record_scope_time(*m_Collection, id, args...);
134  }
135 
136  private:
138  std::shared_ptr<StatisticsCollection> m_Collection;
139  };
140 }
141 
142 #endif //DISMEC_STATS_TRACKED_H
This class manages a collection of named Statistics objects.
Definition: collection.h:47
void set_tag(tag_id_t tag, int value)
Sets the tag to the given integer value.
Definition: collection.h:209
A base class to be used for all types that implement some for of statistics tracking.
Definition: tracked.h:42
void register_stat(const std::string &name, std::unique_ptr< Statistics > stat)
Registers a tracker for the statistics name.
Definition: tracked.cpp:20
Tracked()
Default constructor, creates the internal stats::StatisticsCollection.
Definition: tracked.cpp:13
~Tracked()
Non-virtual destructor. Declared protected, so we don't accidentally try to do a polymorphic delete.
std::shared_ptr< StatisticsCollection > get_stats() const
Gets an ownership-sharing reference to the StatisticsCollection.
Definition: tracked.h:65
std::shared_ptr< StatisticsCollection > m_Collection
The internal collection. Filled with a new StatisticsCollection in the constructor.
Definition: tracked.h:138
void declare_tag(tag_id_t index, std::string name)
Declares a new tag. This function just forwards all its arguments to the internal StatisticsCollectio...
Definition: tracked.cpp:24
void record(stat_id_t stat, T &&value)
Record statistics. This function just forwards all its arguments to the internal StatisticsCollection...
Definition: tracked.h:90
T & make_dependent(T &t)
Given an object T, and some dummy template parameters Args, returns T unchanged.
Definition: tracked.h:75
auto make_timer(stat_id_t id, Args... args)
Creates a new ScopeTimer using stats::record_scope_time.
Definition: tracked.h:130
void declare_stat(stat_id_t index, StatisticMetaData meta)
Declares a new statistics. This function just forwards all its arguments to the internal StatisticsCo...
Definition: tracked.cpp:16
void set_tag(tag_id_t tag, long value)
Set value of tag. This function just forwards all its arguments to the internal StatisticsCollection.
Definition: tracked.h:116
ScopeTimer record_scope_time(StatisticsCollection &accumulator, stat_id_t id)
Definition: timer.h:53
Data that is associated with each declared statistics.
Definition: stat_id.h:33