Skip to content

Instantly share code, notes, and snippets.

View dk949's full-sized avatar

David K dk949

View GitHub Profile
@dk949
dk949 / overload.hpp
Last active April 20, 2025 12:47
Creates a c++ function object merging several other function objects as an overload set
#ifndef UT_OVERLOAD
#define UT_OVERLOAD
namespace ut {
/**
* Create an overload set for lambdas
*
* Requires C++17
*
@dk949
dk949 / asis.hpp
Last active April 19, 2025 03:13
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>
namespace ut {
/**
* Try to downcast from reference type `From` to pointer to `To`.
*
@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
@dk949
dk949 / check.hpp
Last active February 25, 2025 10:50
#ifndef UT_CHECK_HPP
#define UT_CHECK_HPP
/// Class for checking when a c++ object is constructed, copied, moved from or destructed
/* Usage:
*
* std >= c++11
*
* #include "check.hpp"
#ifndef UT_CONSTEXPR_HASH_HPP
#define UT_CONSTEXPR_HASH_HPP
#include <string_view>
/* Usage:
*
* int main(int argc, char const **argv) {
* constexpr auto hash = ut::fnv_1a<>; // defaults to uint64_t
*
* for (int i = 1; i < argc; i++) {
#ifndef UT_ADD_NOEXCEPT_H
#define UT_ADD_NOEXCEPT_H
namespace ut {
/* Add noexcept to function type
*
* std >= c++17
*/
/* Usage:
@dk949
dk949 / change_observer.hpp
Last active March 2, 2025 00:53
A C++ wrapper class that invokes callbacks when it's held value changes
#ifndef UT_CHANGE_OBSERVER
#define UT_CHANGE_OBSERVER
#include <functional>
#include <type_traits>
#include <vector>
namespace ut {
// NOTE: There's 2 versions of several member functions because if the move
@dk949
dk949 / mtime-to-exif.py
Created February 17, 2025 05:39
A small script to fix the datetime of jpg files in a directory. It copies the exif datetime to the file's mtime and vice versa. This can fix various importing issues with photo management software (e.g. Google photos).
"""
A small script to fix the datetime of jpg files in a directory.
It copies the exif datetime to the file's mtime and vice versa.
This can fix various importing issues with photo management software (e.g. Google photos).
NOTE: This script uses the piexif library to read and write exif data, you will need to install it.
NOTE 2: This script will overwrite the mtime of the files. If you use the
@dk949
dk949 / copy_traits.hpp
Last active February 25, 2025 10:50
Copies const, volatile or reference qualifiers from one type to another
#ifndef UT_COPY_TRAITS
#define UT_COPY_TRAITS
/**
* Usage:
* std >= c++20
*
* using A = int;
* using B = float;
* using C = copy_cvref_t<A, B>; // decltype(C) == float
@dk949
dk949 / inspect.lua
Last active February 25, 2025 10:51
Print any lua value as a readable string
---Inspect a value
---
---Converts nil, boolean, number, string or table to string
---Any other type is converted with `tostring`.
---
---List-like tables are printed without their indices
---
---Usage:
---
---```lua