blob: beb865035394f65b4a28bee9d26a7f2c8aa6bb96 [file] [log] [blame]
hbos74e1a4f2016-09-15 23:33:01 -07001/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_API_STATS_RTCSTATSREPORT_H_
12#define WEBRTC_API_STATS_RTCSTATSREPORT_H_
13
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
19#include "webrtc/api/stats/rtcstats.h"
20#include "webrtc/base/refcount.h"
21#include "webrtc/base/scoped_ref_ptr.h"
22
23namespace webrtc {
24
25// A collection of stats.
26// This is accessible as a map from |RTCStats::id| to |RTCStats|.
27class RTCStatsReport : public rtc::RefCountInterface {
28 public:
29 typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
30
31 class ConstIterator {
32 public:
33 ConstIterator(const ConstIterator&& other);
34 ~ConstIterator();
35
36 ConstIterator& operator++();
37 ConstIterator& operator++(int);
38 const RTCStats& operator*() const;
39 bool operator==(const ConstIterator& other) const;
40 bool operator!=(const ConstIterator& other) const;
41
42 private:
43 friend class RTCStatsReport;
44 ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report,
45 StatsMap::const_iterator it);
46
47 // Reference report to make sure it is kept alive.
48 rtc::scoped_refptr<const RTCStatsReport> report_;
49 StatsMap::const_iterator it_;
50 };
51
perkj7eaa8362016-10-31 23:52:25 -070052 static rtc::scoped_refptr<RTCStatsReport> Create();
hbos74e1a4f2016-09-15 23:33:01 -070053
perkj7eaa8362016-10-31 23:52:25 -070054 RTCStatsReport();
hbos74e1a4f2016-09-15 23:33:01 -070055 RTCStatsReport(const RTCStatsReport& other) = delete;
56
57 bool AddStats(std::unique_ptr<const RTCStats> stats);
58 const RTCStats* Get(const std::string& id) const;
59 size_t size() const { return stats_.size(); }
60
61 // Takes ownership of all the stats in |victim|, leaving it empty.
62 void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim);
63
64 // Stats iterators. Stats are ordered lexicographically on |RTCStats::id|.
65 ConstIterator begin() const;
66 ConstIterator end() const;
67
68 // Gets the subset of stats that are of type |T|, where |T| is any class
69 // descending from |RTCStats|.
70 template<typename T>
71 std::vector<const T*> GetStatsOfType() const {
72 std::vector<const T*> stats_of_type;
73 for (const RTCStats& stats : *this) {
74 if (stats.type() == T::kType)
75 stats_of_type.push_back(&stats.cast_to<const T>());
76 }
77 return stats_of_type;
78 }
79
80 friend class rtc::RefCountedObject<RTCStatsReport>;
81
82 private:
83 ~RTCStatsReport() override;
84
hbos74e1a4f2016-09-15 23:33:01 -070085 StatsMap stats_;
86};
87
88} // namespace webrtc
89
90#endif // WEBRTC_API_STATS_RTCSTATSREPORT_H_