Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timelinesearch.h
Go to the documentation of this file.
1 
18 #ifndef ION_PROFILE_TIMELINESEARCH_H_
19 #define ION_PROFILE_TIMELINESEARCH_H_
20 
21 #include <string>
22 
23 #include "ion/port/threadutils.h"
24 #include "ion/profile/timeline.h"
28 
29 typedef std::function<bool(const TimelineNode*)> Predicate;
30 
36  public:
40  TimelineSearch(const Timeline& timeline, TimelineNode::Type node_type,
41  const std::string& node_name);
44  TimelineSearch(const Timeline& timeline, TimelineNode::Type node_type,
45  uint32 begin, uint32 end);
48  TimelineSearch(const Timeline& timeline, TimelineNode::Type node_type,
49  const std::string& node_name, uint32 begin, uint32 end);
51  TimelineSearch(const Timeline& timeline,
52  const ion::port::ThreadId& thread_id);
54  TimelineSearch(const Timeline& timeline, const Predicate& predicate);
55 
57  public:
59  const TimelineNode* operator*() const { return *iter_; }
61  bool operator==(const const_iterator& other) const;
62  bool operator!=(const const_iterator& other) const;
63 
64  private:
66  const TimelineSearch* search_results_;
67  };
68 
69  const Predicate& predicate() const { return predicate_; }
70  const Timeline& timeline() const { return timeline_; }
71  bool empty() const { return begin() == end(); }
72  const_iterator begin() const;
73  const_iterator end() const;
74 
75  private:
76  const Timeline& timeline_;
77  Predicate predicate_;
78 };
79 
82  do {
83  ++iter_;
84  } while (iter_ != search_results_->timeline().end() &&
85  !search_results_->predicate()(*iter_));
86  return *this;
87 }
88 
90  Timeline::const_iterator iter, const TimelineSearch* search_results)
91  : iter_(iter), search_results_(search_results) {}
92 
94  const const_iterator& other) const {
95  return (iter_ == other.iter_) && (search_results_ == other.search_results_);
96 }
97 
99  const const_iterator& other) const {
100  return !(*this == other);
101 }
102 
104  TimelineNode::Type node_type)
105  : timeline_(timeline),
106  predicate_([node_type](const TimelineNode* node) {
107  return node->GetType() == node_type;
108  }) {}
109 
111  TimelineNode::Type node_type,
112  const std::string& node_name)
113  : timeline_(timeline),
114  predicate_([node_type, node_name](const TimelineNode* node) {
115  return node->GetType() == node_type && node->GetName() == node_name;
116  }) {}
117 
118 inline TimelineSearch::TimelineSearch(const Timeline& timeline,
119  TimelineNode::Type node_type,
120  uint32 begin, uint32 end)
121  : timeline_(timeline),
122  predicate_([node_type, begin, end](const TimelineNode* node) {
123  return node->GetType() == node_type && node->GetBegin() >= begin &&
124  node->GetEnd() <= end;
125  }) {}
126 
128  TimelineNode::Type node_type,
129  const std::string& node_name,
130  uint32 begin, uint32 end)
131  : timeline_(timeline),
132  predicate_([node_type, node_name, begin, end](const TimelineNode* node) {
133  return node->GetType() == node_type && node->GetName() == node_name &&
134  node->GetBegin() >= begin && node->GetEnd() <= end;
135  }) {}
136 
137 inline TimelineSearch::TimelineSearch(const Timeline& timeline,
138  const ion::port::ThreadId& thread_id)
139  : timeline_(timeline),
140  predicate_([thread_id](const TimelineNode* node) {
141  if (node->GetType() != TimelineNode::Type::kThread) return false;
142  const TimelineThread* thread = static_cast<const TimelineThread*>(node);
143  return thread->GetThreadId() == thread_id;
144  }) {}
145 
147  const Predicate& predicate)
148  : timeline_(timeline), predicate_(predicate) {}
149 
151  auto iter = timeline_.begin();
152  while (iter != timeline_.end() && !predicate_(*iter)) {
153  ++iter;
154  }
155  return const_iterator(iter, this);
156 }
157 
159  return const_iterator(timeline_.end(), this);
160 }
161 
162 #endif // ION_PROFILE_TIMELINESEARCH_H_
Traverses the hierarchy in pre-order.
Definition: timeline.h:56
const ion::port::ThreadId & GetThreadId() const
const_iterator end() const
Returns a const iterator to the end of the timeline.
Definition: timeline.h:79
Copyright 2016 Google Inc.
const_iterator begin() const
Returns a const iterator over the timeline. The root node is skipped.
Definition: timeline.h:75
Copyright 2016 Google Inc.
Definition: timeline.h:47
const_iterator end() const
Search all nodes in a timeline that match a predicate.
const Timeline & timeline() const
TimelineSearch(const Timeline &timeline, TimelineNode::Type node_type)
Searches nodes by type.
const_iterator begin() const
const Predicate & predicate() const
std::function< bool(const TimelineNode *)> Predicate
Copyright 2016 Google Inc.
Copyright 2016 Google Inc.
Definition: timelinenode.h:28
bool operator!=(const const_iterator &other) const
const_iterator(Timeline::const_iterator iter, const TimelineSearch *search)
pthread_t ThreadId
Defines a type that can be used to identify a thread.
Definition: threadutils.h:40
predicate_([node_type, begin, end](const TimelineNode *node){return node->GetType()==node_type &&node->GetBegin() >=begin &&node->GetEnd()<=end;})
bool empty() const
bool operator==(const const_iterator &other) const
const TimelineNode * operator*() const