Skip to content

Instantly share code, notes, and snippets.

View kerrytazi's full-sized avatar

Alexander Akhundzhanov kerrytazi

View GitHub Profile
#pragma once
#include <cstddef>
template <typename TRet, typename TVal>
TRet *poff(TVal *ptr, ptrdiff_t offset)
{
return reinterpret_cast<TRet *>(reinterpret_cast<char *>(ptr) + offset);
}
@kerrytazi
kerrytazi / constexpr_offsetof.hpp
Created December 24, 2023 03:57
static_assert(_offsetof_(MyClass, my_field) == 0, "my_field must be first field");
#pragma once
#ifdef _MSC_VER
#define _offsetof_(s, m) offsetof(s, m)
#else
namespace detail {
template<typename T> struct declval_helper { static T value; };
#pragma once
#include <vector>
template <typename T, size_t N>
struct allocator_with_buffer
{
using value_type = T;
template <class U>
struct rebind { typedef allocator_with_buffer<U, N> other; };
@kerrytazi
kerrytazi / Mutexed.hpp
Last active June 12, 2023 19:54
Safe wrapper of mutex that won't allow using data without proper lock.
#pragma once
#include <mutex>
#include <thread>
#include <new>
template <typename TObj, typename TMutex = std::mutex>
class MutexedGuard;
@kerrytazi
kerrytazi / Function.hpp
Created April 14, 2019 01:47
Template copy/move and move-only functors with configurable size.
#pragma once
#ifndef _FUNCTION_HPP_INCLUDED_
#define _FUNCTION_HPP_INCLUDED_
#include <type_traits>
namespace
{