clang-tags
C/C++ source code indexing tool based on libclang
 All Classes Functions Variables Typedefs Groups Pages
util.hxx
1 #pragma once
2 
3 #include <sys/time.h>
4 #include <iostream>
5 
17 class Timer {
18 public:
23  Timer () {
24  reset();
25  }
26 
33  double get () {
34  struct timeval now;
35  gettimeofday (&now, NULL);
36 
37  return now.tv_sec - start_.tv_sec + 1e-6 * (now.tv_usec - start_.tv_usec);
38  }
39 
44  void reset () {
45  gettimeofday (&start_, NULL);
46  }
47 
48 private:
49  struct timeval start_;
50 };
51 
52 
60 struct String : public std::string {
65  String (const std::string & other)
66  : std::string (other)
67  {}
68 
75  bool startsWith (const std::string & prefix) const {
76  return compare(0, prefix.size(), prefix) == 0;
77  }
78 };
79 
80 
88 class Tee {
89 public:
95  Tee (std::ostream & stream1,
96  std::ostream & stream2)
97  : stream1_ (stream1),
98  stream2_ (stream2)
99  { }
100 
109  template <typename T>
110  Tee & operator<< (T x) {
111  stream1_ << x;
112  stream2_ << x;
113  return *this;
114  }
115 
124  Tee & operator<< (std::ostream & (*x) (std::ostream&)) {
125  stream1_ << x;
126  stream2_ << x;
127  return *this;
128  }
129 
130 private:
131  std::ostream & stream1_;
132  std::ostream & stream2_;
133 };
134