atomic_fibonacci.cpp
Go to the documentation of this file.
2 
3 #include <ros/console.h>
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 
8 namespace ros1_cpptemplate
9 {
10 
11 AtomicFibonacci::AtomicFibonacci(const int& last_number, const int& current_number, const int& max_number,
12  const std::string& name)
13 {
14  last_number_ = last_number;
15  current_number_ = current_number;
16  max_number_ = max_number;
17 
18  if (name == "")
19  {
20  // don't show empty [] brackets
21  log_prefix_ = "";
22  }
23  else
24  {
25  std::stringstream log_prefix_string_stream;
26  log_prefix_string_stream << "[" << name << "] ";
27  log_prefix_ = log_prefix_string_stream.str();
28  }
29 }
30 
32 {
33 }
34 
35 int AtomicFibonacci::nextAndLog(const std::string& log_prefix)
36 {
37  std::lock_guard<std::mutex> lock(mutex_);
38  int next_number = next_();
39  ROS_INFO_STREAM(log_prefix_ << log_prefix << "Next fibonacci_number: " << next_number);
40  return next_number;
41 }
42 
44 {
45  std::lock_guard<std::mutex> lock(mutex_);
46  return next_();
47 }
48 
50 {
51  std::lock_guard<std::mutex> lock(mutex_);
52  next_();
53  return next_();
54 }
55 
57 {
58  int new_number = current_number_ + last_number_;
59  last_number_ = current_number_;
60 
61  if (new_number > max_number_)
62  {
63  // wrap around to avoid overflow
64  new_number = 1;
65  last_number_ = 0;
66  }
67 
68  current_number_ = new_number;
69 
70  return new_number;
71 }
72 
73 void AtomicFibonacci::setMax(const int& value)
74 {
75  max_number_ = value;
76 }
77 
78 } // namespace
int last_number_
Last Fibonacci number.
virtual int nextAndLog(const std::string &log_prefix="")
virtual void setMax(const int &value)
int current_number_
Current Fibonacci number.
std::mutex mutex_
Mutex for locking.
std::atomic< int > max_number_
Max Fibonacci number before reseting the sequence.
#define ROS_INFO_STREAM(args)
std::string log_prefix_
Log prefix.


ros1_cpptemplate
Author(s): Alexander Reimann
autogenerated on Sat Sep 2 2017 02:37:58