Skip to content

Instantly share code, notes, and snippets.

//------------------------------------------------------------------------------
namespace sim
{
template<class tag, typename T, T invalid>
class id
{
public:
id() : m_Value(invalid) { }
explicit id(T val) : m_Value(val) { }
@learywood
learywood / gist:7b498bee245251201d20
Created April 12, 2015 18:01
C++11 Chrono - stopwatch_base for article
template<class clock, typename duration, typename real>
class stopwatch_base
{
public:
stopwatch_base(const clock& c)
: m_Clock(c)
, m_Start(c.now())
{}
delta_clock::duration_type elapsed() const
@learywood
learywood / gist:15b2722d71381dc30847
Created April 12, 2015 18:00
C++11 Chrono - timer_base for article
template<class clock, typename duration, typename real>
class timer_base
{
public:
timer_base(const clock& c, duration finish_in)
: m_Clock(c)
, m_End(c.now() + finish_in)
{}
bool finished() const { return m_Clock.now() >= m_End; }
@learywood
learywood / sim_time.h
Created April 12, 2015 17:51
C++11 <chrono> - delta time
#pragma once
#include <chrono>
namespace sim
{
namespace time
{
//-----------------------------------------------------------------------------
typedef std::chrono::seconds se;
@learywood
learywood / gist:7098aef75b4b72c3646b
Last active August 29, 2015 14:18
C++ exception catcher
template<class F>
static void try_catch(const F& f)
{
try
{
f();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
@learywood
learywood / gist:3dedcd583db95b666dd6
Last active August 29, 2015 14:18
DeltaClock - C++11, Chrono
template<typename chrono_clock, typename duration>
class delta_clock_base
{
public:
typedef typename chrono_clock::time_point time_point;
typedef typename duration duration_type;
delta_clock_base()
{
tick();
@learywood
learywood / gist:028df5f6f4f88d6ee3b3
Last active August 29, 2015 14:18
SimpleTimer - C++11, Chrono
#include <chrono>
template<class Clock, typename Interval>
class SimpleTimer
{
public:
SimpleTimer(int duration) : m_Duration(Interval(duration)), m_Start(Clock::now()) {}
bool Done()
{
@learywood
learywood / gist:d2be6758083db81b2d72
Last active August 29, 2015 14:18
SimpleStopwatch - C++11, Chrono
#include <chrono>
template<typename Clock, typename Interval>
class SimpleStopwatch
{
public:
SimpleStopwatch() : m_Start(Clock::now()) {}
Interval Elapsed()
{