Skip to content

Instantly share code, notes, and snippets.

View seanmiddleditch's full-sized avatar

Sean Middleditch seanmiddleditch

View GitHub Profile
@seanmiddleditch
seanmiddleditch / cplusplus-enum-members.md
Last active March 28, 2024 01:43
Meta-Proposal for Enum Member Functions

Meta-Proposal for Enum Member Functions

Problem

Enums today cannot provide any custom behavior via member functions. Behavior for enumeration types must be provided by traits classes or namespace-scope operator overloads and functions.

For some uses of enums, such as opaque ID wrapper or flags types, the inability to introduce hidden friend functions poisons the namespace scope of the enum (resulting in worse diagnostic and build times). Typically, custom operations and extension points should be defined as hidden friends to improve the experience of C++ developers using the type.

Consider:

@seanmiddleditch
seanmiddleditch / cplusplus-newtype.md
Last active May 18, 2021 21:39
Meta-Proposal for C++ New-types

Meta-Proposal for C++ new-types ("opaque typedefs")

Motivation

Use cases for "opaque typedefs" or new-types come up frequently in the C++ community, such as:

  • Numeric types with units
  • Flags or unique IDs that eschew arithmetic operations
  • Type-system-enforced wrappers of types (e.g. to differentiate user-input strings vs santitized strings)
  • More
## General Options ##
# Indentation for all files
ColumnLimit: 0
IndentWidth: 4
TabWidth: 4
UseTab: Never
## C++ Options ##
---
@seanmiddleditch
seanmiddleditch / FindSDL2.cmake
Last active August 15, 2023 15:57
Modernized FindSDL2 module for CMake with improved Windows support
# This module defines the library modules
# SDL2::SDL2 and SDL2::SDL2main
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
/*
Copyright (C) 2016 Sean Middleditch <sean.middleditch@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECT
// Copyright (C) 2014 Sean Middleditch, all rights reserverd.
#pragma once
#include <gmcore/Platform.h>
#if !defined(GM_PLATFORM_WINDOWS)
# error "WindowsKit.h must only be included on Windows, check for GM_PLATFORM_WINDOWS before including"
#endif
@seanmiddleditch
seanmiddleditch / strong_enum_handle_example
Last active August 29, 2015 14:26
Strong enum handle example
template <typename HandleT, unsigned IndexBits = 24U>
HandleT pack(unsigned index, unsigned version) {
using T = std::underline_type<HandleT>::type;
auto result = T(version << IndexBits);
auto const mask = T(1 << IndexBits) - 1;
result |= T(index) & mask;
return HandleT(result);
}
template <typename HandleT>