abstract_logger.h
Go to the documentation of this file.
1 #ifndef ABSTRACT_LOGGER_H
2 #define ABSTRACT_LOGGER_H
3 
6 
7 namespace BT
8 {
9 enum class TimestampType
10 {
11  absolute,
12  relative
13 };
14 
16 {
17 public:
18  StatusChangeLogger(TreeNode* root_node);
19  virtual ~StatusChangeLogger() = default;
20 
21  StatusChangeLogger(const StatusChangeLogger& other) = delete;
22  StatusChangeLogger& operator=(const StatusChangeLogger& other) = delete;
23 
24  StatusChangeLogger(StatusChangeLogger&& other) = default;
26 
27  virtual void callback(BT::Duration timestamp, const TreeNode& node,
28  NodeStatus prev_status, NodeStatus status) = 0;
29 
30  virtual void flush() = 0;
31 
32  void setEnabled(bool enabled)
33  {
34  enabled_ = enabled;
35  }
36 
38  {
39  type_ = type;
40  }
41 
42  bool enabled() const
43  {
44  return enabled_;
45  }
46 
47  // false by default.
48  bool showsTransitionToIdle() const
49  {
51  }
52 
53  void enableTransitionToIdle(bool enable)
54  {
55  show_transition_to_idle_ = enable;
56  }
57 
58 private:
59  bool enabled_;
61  std::vector<TreeNode::StatusChangeSubscriber> subscribers_;
65 };
66 
67 //--------------------------------------------
68 
70  : enabled_(true), show_transition_to_idle_(true), type_(TimestampType::absolute)
71 {
72  first_timestamp_ = std::chrono::high_resolution_clock::now();
73 
74  auto subscribeCallback = [this](TimePoint timestamp, const TreeNode& node,
75  NodeStatus prev, NodeStatus status) {
76  std::unique_lock lk(callback_mutex_);
78  {
80  {
81  this->callback(timestamp.time_since_epoch(), node, prev, status);
82  }
83  else
84  {
85  this->callback(timestamp - first_timestamp_, node, prev, status);
86  }
87  }
88  };
89 
90  auto visitor = [this, subscribeCallback](TreeNode* node) {
91  subscribers_.push_back(node->subscribeToStatusChange(std::move(subscribeCallback)));
92  };
93 
94  applyRecursiveVisitor(root_node, visitor);
95 }
96 } // namespace BT
97 
98 #endif // ABSTRACT_LOGGER_H
BT
Definition: ex01_wrap_legacy.cpp:29
BT::StatusChangeLogger::showsTransitionToIdle
bool showsTransitionToIdle() const
Definition: abstract_logger.h:48
BT::StatusChangeLogger::subscribers_
std::vector< TreeNode::StatusChangeSubscriber > subscribers_
Definition: abstract_logger.h:61
BT::StatusChangeLogger::~StatusChangeLogger
virtual ~StatusChangeLogger()=default
BT::StatusChangeLogger::enableTransitionToIdle
void enableTransitionToIdle(bool enable)
Definition: abstract_logger.h:53
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
BT::TimestampType
TimestampType
Definition: abstract_logger.h:9
bt_factory.h
BT::TimePoint
std::chrono::high_resolution_clock::time_point TimePoint
Definition: basic_types.h:627
BT::StatusChangeLogger
Definition: abstract_logger.h:15
BT::Duration
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:628
BT::StatusChangeLogger::enabled_
bool enabled_
Definition: abstract_logger.h:59
BT::StatusChangeLogger::first_timestamp_
BT::TimePoint first_timestamp_
Definition: abstract_logger.h:63
BT::StatusChangeLogger::callback
virtual void callback(BT::Duration timestamp, const TreeNode &node, NodeStatus prev_status, NodeStatus status)=0
mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:77
BT::StatusChangeLogger::operator=
StatusChangeLogger & operator=(const StatusChangeLogger &other)=delete
behavior_tree.h
cx::prev
constexpr It prev(It it)
Definition: wildcards.hpp:631
BT::StatusChangeLogger::callback_mutex_
std::mutex callback_mutex_
Definition: abstract_logger.h:64
BT::TimestampType::relative
@ relative
BT::applyRecursiveVisitor
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)
Definition: behavior_tree.cpp:18
BT::StatusChangeLogger::StatusChangeLogger
StatusChangeLogger(TreeNode *root_node)
Definition: abstract_logger.h:69
BT::StatusChangeLogger::setTimestampType
void setTimestampType(TimestampType type)
Definition: abstract_logger.h:37
BT::StatusChangeLogger::flush
virtual void flush()=0
BT::StatusChangeLogger::show_transition_to_idle_
bool show_transition_to_idle_
Definition: abstract_logger.h:60
BT::NodeStatus::IDLE
@ IDLE
BT::TimestampType::absolute
@ absolute
BT::StatusChangeLogger::setEnabled
void setEnabled(bool enabled)
Definition: abstract_logger.h:32
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::StatusChangeLogger::enabled
bool enabled() const
Definition: abstract_logger.h:42
BT::StatusChangeLogger::type_
TimestampType type_
Definition: abstract_logger.h:62


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16