Skip to content

Commit

Permalink
Merge pull request #593 from leapmotion/feature-chrono-ext
Browse files Browse the repository at this point in the history
Added basic_timer.h and chrono_types.h
  • Loading branch information
codemercenary committed Jun 19, 2015
2 parents 2feddc2 + b4e5b36 commit 5230cff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions autowiring/basic_timer.h
@@ -0,0 +1,25 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#pragma once
#include <chrono>

template<class clock_t, class duration_t = typename clock_t::duration>
class basic_timer {
public:
basic_timer(const typename clock_t::time_point& initial = clock_t::now()) : m_start(initial) {}

//A convenient method to avoid having to use duration_cast all the time.
duration_t now() const { return std::chrono::duration_cast<duration_t>(clock_t::now().time_since_epoch()); }
duration_t elapsed() const { return std::chrono::duration_cast<duration_t>(clock_t::now() - m_start); }
typename clock_t::time_point start_time() const { return m_start; }

void start() { m_start = clock_t::now(); }
duration_t mark() {
const auto now = clock_t::now();
const auto oldStart = m_start;
m_start = now;
return std::chrono::duration_cast<duration_t>(now - oldStart);
}
private:
typename clock_t::time_point m_start;
};

21 changes: 21 additions & 0 deletions autowiring/chrono_types.h
@@ -0,0 +1,21 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#pragma once
#include <autowiring/C++11/cpp11.h>
#include CHRONO_HEADER
#include "chrono_timer.h"

//Common chrono types
typedef std::chrono::duration<double> seconds_d;
typedef std::chrono::duration<double, std::milli> milliseconds_d;
typedef std::chrono::duration<double, std::micro> microseconds_d;
typedef std::chrono::duration<double, std::nano> nanoseconds_d;

typedef std::chrono::duration<float> seconds_f;
typedef std::chrono::duration<float, std::milli> milliseconds_f;
typedef std::chrono::duration<float, std::micro> microseconds_f;
typedef std::chrono::duration<float, std::nano> nanoseconds_f;

typedef basic_timer<std::chrono::profiling_clock> profiling_timer;
typedef basic_timer<std::chrono::profiling_clock, seconds_d> profiling_timer_seconds;
typedef basic_timer<std::chrono::profiling_clock, milliseconds_d> profiling_timer_milliseconds;
typedef basic_timer<std::chrono::profiling_clock, microseconds_d> profiling_timer_microseconds;

0 comments on commit 5230cff

Please sign in to comment.