Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef CARTOGRAPHER_ROS_METRICS_INTERNAL_GAUGE_H
00018 #define CARTOGRAPHER_ROS_METRICS_INTERNAL_GAUGE_H
00019
00020 #include <map>
00021 #include <string>
00022
00023 #include "absl/synchronization/mutex.h"
00024 #include "cartographer/metrics/gauge.h"
00025 #include "cartographer_ros_msgs/Metric.h"
00026
00027 namespace cartographer_ros {
00028 namespace metrics {
00029
00030 class Gauge : public ::cartographer::metrics::Gauge {
00031 public:
00032 explicit Gauge(const std::map<std::string, std::string>& labels)
00033 : labels_(labels), value_(0.) {}
00034
00035 void Decrement(const double value) override { Add(-1. * value); }
00036
00037 void Decrement() override { Decrement(1.); }
00038
00039 void Increment(const double value) override { Add(value); }
00040
00041 void Increment() override { Increment(1.); }
00042
00043 void Set(double value) override {
00044 absl::MutexLock lock(&mutex_);
00045 value_ = value;
00046 }
00047
00048 double Value() {
00049 absl::MutexLock lock(&mutex_);
00050 return value_;
00051 }
00052
00053 cartographer_ros_msgs::Metric ToRosMessage() {
00054 cartographer_ros_msgs::Metric msg;
00055 msg.type = cartographer_ros_msgs::Metric::TYPE_GAUGE;
00056 for (const auto& label : labels_) {
00057 cartographer_ros_msgs::MetricLabel label_msg;
00058 label_msg.key = label.first;
00059 label_msg.value = label.second;
00060 msg.labels.push_back(label_msg);
00061 }
00062 msg.value = Value();
00063 return msg;
00064 }
00065
00066 private:
00067 void Add(const double value) {
00068 absl::MutexLock lock(&mutex_);
00069 value_ += value;
00070 }
00071
00072 absl::Mutex mutex_;
00073 const std::map<std::string, std::string> labels_;
00074 double value_ GUARDED_BY(mutex_);
00075 };
00076
00077 }
00078 }
00079
00080 #endif // CARTOGRAPHER_ROS_METRICS_INTERNAL_GAUGE_H