Skip to content

Instantly share code, notes, and snippets.

@sigman78
sigman78 / concat.cpp
Last active January 17, 2024 23:43
custom concat for strings and string_views
#include <iostream>
#include <string>
#include <string_view>
namespace impl {
template<typename T>
concept StringLike = requires(T t) {
{ t.data() } -> std::convertible_to<const char*>;
{ t.size() } -> std::convertible_to<std::size_t>;
template<typename T> struct type_id_impl {
static int* loc() {
static int i;
return &i;
}
}
template<typename T> type_id_t type_id() {
return (type_id_t)&type_id_impl<T>::loc;
}

Using O2D to Program Duck PCBs

Special Thanks to KacKLaPPen23 and iNViSiBiLiTi for making this guide possible and being generally excellent people!

Programming Duck PCBs with O2D is a notoriously shitty experience.

Luckily with O2D 1.10 and this guide it will be less shitty!

Prerequisites

@sigman78
sigman78 / strong.cpp
Created September 28, 2021 19:26
Strongeesh typedefs in C++17
#include <iostream>
struct UserName { std::string name; };
struct Password { std::string pwd; };
template<typename T>
auto peek(const T& packed) {
static_assert(std::is_aggregate_v<T>, "Should be struct");
// we can further enforce T to have single field with
const auto& [value] = packed;
@sigman78
sigman78 / constexpr_tuple.cpp
Created September 19, 2019 09:26
easy tuple on variadics
#include <utility>
#include <cstdio>
template <typename ... Ts>
constexpr auto tuple(Ts&& ... ts)
{
return [...ts = std::forward<Ts>(ts)](auto&& f) { return f(ts...);};
}
template <unsigned I, typename T, typename ... Ts>
@sigman78
sigman78 / rethrow.cpp
Created August 16, 2019 17:08
nested exceptions abuse
#include <exception>
#include <stdexcept>
#include <cstdio>
#include <sstream>
#include <iostream>
template<class Ctx, class...Args>
void rethrow(Ctx&& context, Args&&... args)
{
// build an error message
@sigman78
sigman78 / cstr.h
Created May 16, 2019 14:39
constexpr cstr mockup
constexpr const char* end_of(const char* s) {
while (*s != 0) ++s;
return s;
}
struct cstr {
const char* b_;
const char* e_;
constexpr cstr(const char* s)
@sigman78
sigman78 / float_opt.cpp
Created May 7, 2019 12:18
Draft of optional{float} for safe nan usage
#include <cmath>
#include <limits>
struct float_opt
{
float value_ = std::numeric_limits<float>::quiet_NaN();
explicit float_opt(float v): value_(v) {}
constexpr float_opt() = default;
@sigman78
sigman78 / registry.cpp
Created April 17, 2019 12:57
Discoverable 'global vars'
#include <memory>
#include <utility>
#include <cassert>
#include <iostream>
template<typename Service>
struct Registry
{
Registry() = delete;
~Registry() = delete;
$ cat /tmp/quine.pl
Illegal division by zero at /tmp/quine.pl line 1.
$ perl /tmp/quine.pl
Illegal division by zero at /tmp/quine.pl line 1.