#include "gauge.h"

namespace sled {
namespace metric {
Gauge::Gauge(double value) : value_(value) {}

void
Gauge::Increment()
{
    Change(1.0);
}

void
Gauge::Increment(double value)
{
    Change(value);
}

void
Gauge::Decrement()
{
    Change(-1.0);
}

void
Gauge::Decrement(double value)
{
    Change(-1.0 * value);
}

void
Gauge::Set(double value)
{
    value_.store(value);
}

void
Gauge::SetToCurrentTime()
{
    const auto time = std::time(nullptr);
    Set(static_cast<double>(time));
}

double
Gauge::Value() const
{
    return value_.load();
}

void
Gauge::Change(double delta)
{
    auto current = value_.load();
    while (!value_.compare_exchange_weak(current, current + delta)) {}
}

}// namespace metric
}// namespace sled
