Skip to content

Instantly share code, notes, and snippets.

@xealits
xealits / CMakeLists.txt
Last active November 16, 2025 18:02
CMake ctest --watch with entr
# in the top project:
cmake_minimum_required(VERSION ...)
project(alib LANGUAGES CXX)
# CTest setup
# not sure if there is a point to try to separate the ctest definitions from CMakeLists.txt
# add_test are a part of the project or the build pipeline?
#. ENABLE_TESTING() # this should already be done in include(CTest)
# even though kitware says it's required
@xealits
xealits / run.py
Last active October 2, 2025 18:53
C hotreload/figwheel in Python
#!/usr/bin/env python
import ctypes
from ctypes import cdll
cur_x = 10
libpath = "./some_lib.so"
lib = cdll.LoadLibrary(libpath)
print(f"loaded {lib}")
# https://stackoverflow.com/a/62021495/1420489
#include <iostream>
struct FuncPtr;
typedef FuncPtr(Func)(int);
struct FuncPtr {
void *func;
FuncPtr(Func& f) : func{(void *) &f} {};
FuncPtr operator()(int x) { return ((Func *)func)(x); }
};
@xealits
xealits / specialise_variadic_templates-dd3a0e.cpp
Created June 18, 2025 21:45
clang 18.1.3 bug report Preprocessed source
This file has been truncated, but you can view the full file.
# 1 "<built-in>"
# 1 "specialise_variadic_templates.cpp"
#if defined(__CLANG_REWRITTEN_INCLUDES) || defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) /* iostream expanded by -frewrite-includes */
#include <iostream>
#else /* iostream expanded by -frewrite-includes */
# 1 "specialise_variadic_templates.cpp"
# 1 "/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/iostream" 1 3
// Standard iostream objects -*- C++ -*-
@xealits
xealits / duck_typing_static.cpp
Last active May 28, 2025 21:52
Static duck typing in C++
#include <iostream>
#include <source_location>
#include <string>
struct Quack;
template <typename T>
concept Duck = requires(T t) {
{ t.quack() } -> std::convertible_to<Quack>;
// could be -> std::same_as<Quack>;
};
@xealits
xealits / query_examples.md
Created January 11, 2025 15:01
clang-query matches

Find references to non-const pointees:

clang-query -c "set output dump" -c "m varDecl(hasType(referenceType(pointee(unless(isConstQualified())))))" source.cpp
@xealits
xealits / pattern_command_1.cpp
Last active October 12, 2024 01:14
Simple pattern examples in C++
/*
* Just some notes while reading Game Programming Patterns by Robert Nystrom
* https://gameprogrammingpatterns.com
*
* A command or event made with C++ std::variant.
* The idea is to pack some similar-size but different types into the command.
* How to subscribe components to events from each other
* and construct it declaratively, at compile time?
* The components send events or commands (command pattern).
* You want to verify the interfaces, preferably statically, i.e. at link time.
@xealits
xealits / basic_macro.nim
Last active February 18, 2024 21:23
Basic macro in NIM
#[
# the example is from https://peterme.net/metaprogramming-and-read-and-maintainability-in-nim.html
# plus I used `quote` instead of direct tree nodes
# it almost works the same way
# except, it seems you cannot declare an empty `enum` under `quote`
]#
import math, strutils
import macros
@xealits
xealits / keybase.md
Created January 28, 2024 17:21
keybase verification

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@xealits
xealits / factorial.cpp
Created February 20, 2022 21:39
C/C++ doctest with separate implementation file and source with tests
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest.h"
//int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }
int factorial(int number) { return number <= 1 ? 1 : factorial(number - 1) * number; }
TEST_CASE("testing the factorial function") {
CHECK(factorial(0) == 1);
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);