Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
#include <tuple>
#include <boost/callable_traits.hpp>
#include <boost/type_index/ctti_type_index.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
template<template <typename> typename Base, std::size_t ...Dependents>
struct Depends : Base<Depends<Base, Dependents...>> {
@schaumb
schaumb / collection.hpp
Last active July 23, 2020 16:57
Create a function returnable range collection.
#include <boost/utility/base_from_member.hpp>
#include <boost/range/any_range.hpp>
#include <boost/iterator/iterator_traits.hpp>
#include <any>
#include <type_traits>
template<typename T, typename Traversal = boost::forward_traversal_tag>
struct collection : boost::base_from_member<std::any>,
boost::base_from_member<std::function<boost::any_range<T, Traversal>(std::any&)>>,
boost::any_range<T, Traversal> {
@schaumb
schaumb / limit_iterator.hpp
Last active July 28, 2020 21:00
limiting iterator maximum range. output_iterator too
#pragma once
#include <boost/iterator/iterator_categories.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
namespace bxlx {
namespace detail {
template<typename Proxy>
struct enable_proxy {
Proxy p;
@schaumb
schaumb / bxlx_memory.hpp
Created April 20, 2022 16:05
make_temporary_for_overwrite, make_function_scoped_for_overwrite
#include <type_traits>
#if __has_include(<alloca.h>)
#include <alloca.h>
#else
#define alloca _alloca
#endif
namespace bxlx {
@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 {
@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 / 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 / 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 / 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 / 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.