Skip to content

Instantly share code, notes, and snippets.

@schaumb
schaumb / st_route.py
Last active December 21, 2023 01:45
Streamlit - Support custom HTTP requests
import functools
import gc
import weakref
from typing import Optional, Callable, Union
from weakref import WeakSet
from streamlit import config
from streamlit.runtime import Runtime
from streamlit.runtime.scriptrunner import get_script_run_ctx, add_script_run_ctx
from streamlit.web.server.server_util import make_url_path_regex
@schaumb
schaumb / WORKING_AROUND_OFFSETOF_LIMITATIONS.MD
Last active March 16, 2023 15:58 — forked from graphitemaster/WORKING_AROUND_OFFSETOF_LIMITATIONS.MD
Working around offsetof limitations in C++

Working around offsetof limitations in C++:

There is sometimes a situation in which one needs to get the relative offset of a structure field, common examples of this include serialization frameworks which aid to serialize objects, vertex attributes for rendering (D3D, GL.), etc.

The most common technique for getting this information is through the offsetof macro defined in stddef.h. Unfortunately using the macro in C++ comes with a new set of restrictions that prevent some (subjectively valid) uses of it.

@schaumb
schaumb / access_private.cpp
Last active March 16, 2023 21:06
access private c++17
#ifndef ACCESS_PRIVATE_CPP17
#define ACCESS_PRIVATE_CPP17
#include <functional>
#define ACCESS_PRIVATE_IMPL(CLASS, NAME, CASTER, TYPE) \
namespace { \
namespace access_tag::CLASS {\
template<class T>\
struct NAME; \
@schaumb
schaumb / bitest_like_iterator_helper.hpp
Last active October 17, 2022 21:33
optimalized bitset like true value iterator helper, like std::bitset and std::vector<bool>
#ifndef BITSET_ITERATOR_HELPER_HPP
#define BITSET_ITERATOR_HELPER_HPP
#include <utility>
#include <limits>
#include <functional>
#include <cstdint>
template<class T>
struct bitset_like_iterator_helper {
@schaumb
schaumb / ptr_from_mptr.cpp
Last active August 15, 2022 11:53
member pointer to pointer constexpr conversion
#include <type_traits>
#include <utility>
namespace Impl {
// <e> <e> <e> <e>
// const
// volatile
@schaumb
schaumb / dynamic_implementation.cpp
Created August 3, 2022 18:49
C++ interface dynamic implementation
#include <iostream>
#include <stdexcept>
template<typename T>
struct Mock {
void** mfptr = funptrs + 5;
void* funptrs[15] = {{}, {}, {}, {}, {}};
@schaumb
schaumb / get_member_name.hpp
Last active May 30, 2022 11:28
c++ get member name from member pointer
// see at godbolt : https://godbolt.org/z/zMGEGs99v
#include <string_view>
template<auto V>
constexpr static auto n() noexcept {
#ifdef _MSC_VER
std::string_view sv = __FUNCSIG__;
auto to = sv.rfind('>')-1;
for (std::size_t close = sv[to] == ')'; close > 0; ) {
@schaumb
schaumb / lambda_traits.hpp
Created May 2, 2022 20:19
lambda traits header only lib. min/max arity, has capture, is variadic etc
#include <tuple>
#include <limits>
#define BXLX_MAXIMUM_ARG_TEMPLATE_LAMBDA 100
namespace bxlx {
namespace impl {
template<std::size_t>
struct arg {
using type = arg;
@schaumb
schaumb / long_type_traits.hpp
Created April 26, 2022 17:11
add_long, remove_long, has_long type traits
#include <type_traits>
namespace bxlx {
namespace impl {
template<typename T, typename = void>
struct add_long {
using type = T;
};
template<>
@schaumb
schaumb / pretty_function_processor.cpp
Created April 22, 2022 18:48
Pretty function processor.
#include <type_traits>
#include <string_view>
#include <iostream>
#include <atomic>
#include <utility>
#include <tuple>
#include <cassert>
template<char... Chars>
struct str {