scenario_simulator_v2 C++ API
stop_watch.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TRAFFIC_SIMULATOR__HELPER__STOP_WATCH_HPP_
16 #define TRAFFIC_SIMULATOR__HELPER__STOP_WATCH_HPP_
17 
18 #include <chrono>
19 #include <iostream>
20 #include <optional>
21 #include <rclcpp/rclcpp.hpp>
22 #include <string>
23 
24 namespace traffic_simulator
25 {
26 namespace helper
27 {
28 template <typename T>
29 class StopWatch
30 {
31 public:
32  explicit StopWatch(const std::string & name, bool verbose = false, bool autostart = true)
34  {
35  if (autostart) {
36  start();
37  }
38  }
39 
40  void start()
41  {
42  end_time = std::nullopt;
43  start_time = std::chrono::system_clock::now();
44  }
45  void stop() { end_time = std::chrono::system_clock::now(); }
46  void print()
47  {
48  if (verbose) {
49  if (start_time && end_time) {
50  double elapsed =
51  std::chrono::duration_cast<T>(end_time.value() - start_time.value()).count();
52  if (typeid(T) == typeid(std::chrono::microseconds)) {
53  std::cout << "elapsed time in stop watch " << name << " : " << elapsed << " microseconds"
54  << std::endl;
55  }
56  if (typeid(T) == typeid(std::chrono::milliseconds)) {
57  std::cout << "elapsed time in stop watch " << name << " : " << elapsed << " milliseconds"
58  << std::endl;
59  }
60  if (typeid(T) == typeid(std::chrono::seconds)) {
61  std::cout << "elapsed time in stop watch " << name << " : " << elapsed << " seconds"
62  << std::endl;
63  }
64  }
65  } else {
66  if (start_time) {
67  RCLCPP_ERROR_STREAM(
68  rclcpp::get_logger(name), "Stop watch : " << name << " did not started.");
69  }
70  if (end_time) {
71  RCLCPP_ERROR_STREAM(
72  rclcpp::get_logger(name), "Stop watch : " << name << " did not stopped.");
73  }
74  }
75  }
76 
77 public:
79  const bool verbose;
80 
81 private:
82  std::optional<std::chrono::system_clock::time_point> start_time, end_time;
83 };
84 } // namespace helper
85 } // namespace traffic_simulator
86 
87 #endif // TRAFFIC_SIMULATOR__HELPER__STOP_WATCH_HPP_
Definition: stop_watch.hpp:30
const bool verbose
Definition: stop_watch.hpp:79
void start()
Definition: stop_watch.hpp:40
void stop()
Definition: stop_watch.hpp:45
StopWatch(const std::string &name, bool verbose=false, bool autostart=true)
Definition: stop_watch.hpp:32
void print()
Definition: stop_watch.hpp:46
const std::string name
Definition: stop_watch.hpp:78
Definition: api.hpp:48
std::string string
Definition: junit5.hpp:26