Skip to content

API Reference

All declarations reside in namespace siddiqsoft.

#include "siddiqsoft/timethis.hpp"
using namespace siddiqsoft;

Classes

class timethis

RAII-based stopwatch class measuring elapsed time with optional callback invocation on destruction.

namespace siddiqsoft {
    class timethis;
}

Type Aliases

Alias Definition Description
duration_type std::chrono::system_clock::duration System clock duration type used for elapsed measurements
time_point_type std::chrono::system_clock::time_point Time point type captured upon construction or reset
callback_type std::function<void(const std::chrono::system_clock::duration&)> Callback invoked with elapsed duration upon destruction

Constructors & Destructor

explicit timethis(const std::source_location& sl = std::source_location::current()) noexcept;
Constructs the timer, capturing the current time and caller source location.

explicit timethis(callback_type&& callback,
                  const std::source_location& sl = std::source_location::current()) noexcept;
Constructs the timer with a completion callback to be invoked upon destruction.

~timethis() noexcept;
Destructor. If a callback was provided, invokes it with elapsed().

Semantics (Copy & Move)

timethis(const timethis&) = delete;
timethis& operator=(const timethis&) = delete;
timethis(timethis&&) = delete;
timethis& operator=(timethis&&) = delete;
Single-ownership RAII semantics. Timers cannot be copied or moved.

Member Functions

elapsed()

[[nodiscard]] duration_type elapsed() const noexcept;
Calculates and returns the duration elapsed since construction or the last reset().

lap()

template <typename DC = std::chrono::microseconds>
[[nodiscard]] std::string lap() const noexcept;
Returns elapsed time formatted as a numeric string in the duration unit specified by DC (defaults to std::chrono::microseconds).

to_string()

template <typename charT = char>
[[nodiscard]] auto to_string() const;
Returns a human-readable string containing the source location function name, start timestamp, and elapsed microseconds.

reset()

void reset() noexcept;
Resets the timer's start time to the current clock time.


Stream & Formatter Support

Stream Operator

std::ostream& operator<<(std::ostream& os, const timethis& src);
Writes <function_name> took <elapsed>ns to the output stream.

std::formatter Specialization

template <class charT>
struct std::formatter<siddiqsoft::timethis, charT>;
Enables direct formatting via std::format("{}", timer) matching to_string().