Skip to content

Instantly share code, notes, and snippets.

View dk949's full-sized avatar

David K dk949

View GitHub Profile
#ifndef UT_TRACED_ERROR_HPP
#define UT_TRACED_ERROR_HPP
/**
* An exception type which automatically stores a standard stacktrace
*
* Usage:
* On gcc and clang, linking against libstdc++exp (-lstdc++exp) is required and compiling with -g -Og and
* linking with -lg is advised.
*
* `throw`ing an instance of `TracedError` (or types derived from it) will capture the stack trace.
@dk949
dk949 / breakpoint.hpp
Last active September 1, 2025 13:54
Portable(ish) breakpoint macros
#ifndef UT_BREAKPOINT_HPP
#define UT_BREAKPOINT_HPP
#ifndef __has_builtin
# define UT_DETAIL_HAS_BUILTIN(x) 0
#else
# define UT_DETAIL_HAS_BUILTIN(x) __has_builtin(x)
#endif
#if defined(__GNUC__)
#ifndef UT_ERR
#define UT_ERR
#include <concepts>
#include <type_traits>
#if __cplusplus < 202'302L
# error this file has to be compiled with at least C++23
#endif
#include <expected>
@dk949
dk949 / overload.hpp
Last active June 8, 2025 23:17
Creates a c++ function object merging several other function objects as an overload set
#ifndef UT_OVERLOAD
#define UT_OVERLOAD
#if __cplusplus < 201'703L
# error this file has to be compiled with at least C++17
#endif
namespace ut {
/**
@dk949
dk949 / asis.hpp
Last active June 8, 2025 23:04
Provides a more convenient way to work with different kinds of dynamically polymorphic values
#ifndef UT_ASIS
#define UT_ASIS
#include <memory>
#include <type_traits>
#if __cplusplus < 202'002L
# error this file has to be compiled with at least C++20
#endif
namespace ut {
@dk949
dk949 / DateTime.zig
Created March 18, 2025 01:40
A very basic utility to manage date and time in UTC timezone
//! A very basic utility to manage date and time in UTC timezone.
//!
//! Usage:
//!
//! ```zig
//! // The following examples assume that the current date and time is 25th May 1977, at 08:48 PM
//! const now = DateTime.now();
//! std.debug.print("{}\n", .{now}); // 1977-05-25 20:48:00
//! std.debug.print("{%d %B %Y at %I}:{%M %p}\n", .{ now, now }); // 25 May 1977 at 08:48 PM
//! // NOTE: the format string has to be split in two, because `:` is a special character in fmt
#ifndef UT_CHECK_HPP
#define UT_CHECK_HPP
#if __cplusplus < 201'103L
# error this file has to be compiled with at least C++11
#endif
/// Class for checking when a c++ object is constructed, copied, moved from or destructed
/* Usage:
#ifndef UT_CONSTEXPR_HASH_HPP
#define UT_CONSTEXPR_HASH_HPP
#include <string_view>
#if __cplusplus < 201'703L
# error this file has to be compiled with at least C++17
#endif
/* Usage:
*
#ifndef UT_ADD_NOEXCEPT_H
#define UT_ADD_NOEXCEPT_H
#if __cplusplus < 201'703L
# error this file has to be compiled with at least C++17
#endif
namespace ut {
/*
* Add noexcept to function type
@dk949
dk949 / change_observer.hpp
Last active June 8, 2025 23:08
A C++ wrapper class that invokes callbacks when it's held value changes
#ifndef UT_CHANGE_OBSERVER
#define UT_CHANGE_OBSERVER
#if __cplusplus < 202'002L
# error this file has to be compiled with at least C++20
#endif
#include <functional>
#include <type_traits>
#include <vector>