gauge.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 }  // namespace metrics
00078 }  // namespace cartographer_ros
00079 
00080 #endif  // CARTOGRAPHER_ROS_METRICS_INTERNAL_GAUGE_H


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Wed Jul 10 2019 04:10:28