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 
15 typedef std::array<uint8_t, 12> SerializedTransition;
16 
18 {
19  public:
20  StatusChangeLogger(TreeNode *root_node);
21  virtual ~StatusChangeLogger() = default;
22 
23  virtual void callback(BT::Duration timestamp, const TreeNode& node, NodeStatus prev_status,
24  NodeStatus status) = 0;
25 
26  virtual void flush() = 0;
27 
28  void setEnabled(bool enabled)
29  {
30  enabled_ = enabled;
31  }
32 
34  {
35  type_ = type;
36  }
37 
38  bool enabled() const
39  {
40  return enabled_;
41  }
42 
43  // false by default.
44  bool showsTransitionToIdle() const
45  {
46  return show_transition_to_idle_;
47  }
48 
49  void enableTransitionToIdle(bool enable)
50  {
51  show_transition_to_idle_ = enable;
52  }
53 
54  private:
55  bool enabled_;
57  std::vector<TreeNode::StatusChangeSubscriber> subscribers_;
60 };
61 
62 //--------------------------------------------
63 
65  : enabled_(true), show_transition_to_idle_(true), type_(TimestampType::absolute)
66 {
67  first_timestamp_ = std::chrono::high_resolution_clock::now();
68 
69  auto subscribeCallback = [this](TimePoint timestamp, const TreeNode& node, NodeStatus prev,
70  NodeStatus status) {
72  {
74  {
75  this->callback(timestamp.time_since_epoch(), node, prev, status);
76  }
77  else
78  {
79  this->callback(timestamp - first_timestamp_, node, prev, status);
80  }
81  }
82  };
83 
84  auto visitor = [this, subscribeCallback](TreeNode* node) {
85  subscribers_.push_back(node->subscribeToStatusChange(std::move(subscribeCallback)));
86  };
87 
88  applyRecursiveVisitor(root_node, visitor);
89 }
90 }
91 
92 #endif // ABSTRACT_LOGGER_H
BT::TimePoint first_timestamp_
std::chrono::high_resolution_clock::time_point TimePoint
Definition: basic_types.h:339
TimestampType
std::array< uint8_t, 12 > SerializedTransition
std::vector< TreeNode::StatusChangeSubscriber > subscribers_
void setTimestampType(TimestampType type)
bool showsTransitionToIdle() const
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:53
void setEnabled(bool enabled)
StatusChangeLogger(TreeNode *root_node)
NodeStatus
Definition: basic_types.h:35
virtual void callback(BT::Duration timestamp, const TreeNode &node, NodeStatus prev_status, NodeStatus status)=0
void enableTransitionToIdle(bool enable)
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:340
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24