Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Hollandt Trass3r

View GitHub Profile
View gtest-parameterized-tests.cpp
#define TEST_PV(testsuite, testname, ...) \
class testsuite##Parameterized : public testsuite, \
public ::testing::WithParamInterface<decltype(GTEST_GET_FIRST_(__VA_ARGS__))> {}; \
INSTANTIATE_TEST_SUITE_P(, testsuite##Parameterized, ::testing::Values(__VA_ARGS__)); \
TEST_P(testsuite##Parameterized, testname)
@Trass3r
Trass3r / foridx.cpp
Created Jul 28, 2020
C++17 range-for with index macro: https://godbolt.org/z/Yd4j8G
View foridx.cpp
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <math.h>
#define for_i(...) \
if (decltype(sizeof(0)) i_next = 0; true) \
for (__VA_ARGS__) \
if (auto i = i_next++; true)
@Trass3r
Trass3r / stl.natvis
Last active Jul 22, 2020
libstdc++ natvis
View stl.natvis
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<!-- gnu stl -->
<Type Name="std::initializer_list&lt;*&gt;">
<DisplayString>{{ size={_M_len} }}</DisplayString>
<Expand>
<ArrayItems>
<Size>_M_len</Size>
@Trass3r
Trass3r / prepare-commit-msg.sh
Created Jul 13, 2020
Git hook that inserts the git branch name into the commit message, useful for ticket references
View prepare-commit-msg.sh
#!/bin/bash
branchPath=$(git symbolic-ref -q HEAD) # refs/heads/branchName
branchName=${branchPath##*/} # get text behind the last /
# check that this is not an amend by checking that the first line is empty
firstLine=$(head -n1 $1)
if [ -z "$firstLine" ] ; then
# insert template at the start of the commit message file
sed -i "1s/^/$branchName \n/" $1
@Trass3r
Trass3r / enumflags.h
Last active Jul 28, 2020
proper enum flags based on conventional "Flags" suffix (try with -Wall -W -Og -std=c++14)
View enumflags.h
#pragma once
/*!
* Copyright (C) 2020 Andreas Hollandt
*
* Distributed under the Boost Software License, Version 1.0.
* See copy at http://boost.org/LICENSE_1_0.txt.
*/
// requires clang 3.6, msvc 2017 or gcc 9 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66639)
@Trass3r
Trass3r / algorithms.h
Created Jul 10, 2020
useful algorithms
View algorithms.h
template <typename Container>
static void moveElementToFront(Container& c, const typename Container::value_type& e)
{
auto it = std::find(c.begin(), c.end(), e);
if (it != c.end())
std::rotate(c.begin(), it, it + 1);
}
// TODO: multi-element version and non-contiguous version using std::partition
template <typename Container>
@Trass3r
Trass3r / copyWithDeps.sh
Last active Jul 22, 2020
copy executable with dependencies and fix rpath
View copyWithDeps.sh
#!/bin/bash
set -e
mkdir -p $2 && cd $2
ldd $1 | grep "=> /" | awk '{print $3}' | xargs -I '{}' cp -v '{}' .
cp $1 .
patchelf --set-rpath '$ORIGIN/.' *
rm libc.* libgcc* librt.* libpthread* libdl.*
@Trass3r
Trass3r / dll2lib.bat
Last active May 31, 2020
generate import library from a dll
View dll2lib.bat
@echo off
REM Usage: dll2lib [32|64] some-file.dll
REM
REM Generates some-file.lib from some-file.dll, making an intermediate
REM some-file.def from the results of dumpbin /exports some-file.dll.
REM
REM Requires 'dumpbin' and 'lib' in PATH - run from VS developer prompt.
REM
REM Script inspired by http://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dll
@Trass3r
Trass3r / CMakeLists.txt
Created Mar 1, 2020
template for OpenGL with CMake, Conan, glad and glfw
View CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "")
project(cppgl)
set(CMAKE_CXX_STANDARD 20)
if (NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
View clang-tidy.sh
for %f in (src/*.cpp) do clang-tidy -p buildNinja --config="{Checks: '-*,modernize-use-default-member-init', CheckOptions: [{key: UseAssignment, value: 1}]}" --header-filter=src src\%f --fix-errors
for %f in (src/*.cpp) do clang-tidy -p buildNinja --config="{Checks: '-*,cppcoreguidelines-pro-type-member-init', CheckOptions: [{key: UseAssignment, value: 1}, {key: IgnoreArrays, value: 1}]}" --header-filter=src src\%f --fix-errors
for %f in (src/*.cpp) do clang-tidy -p buildNinja --checks=-*,modernize-loop-convert --header-filter=src src\%f --fix-errors
for %f in (src/*.cpp) do clang-tidy -p buildNinja --config="{Checks: '-*,modernize-use-override', CheckOptions: [{key: IgnoreDestructors, value: 1}]}" --header-filter=src src\%f --fix-errors
# redundant casts
"C:\Program Files\Git\usr\bin\find.exe" src -name "*.cpp" | "C:\Program Files\Git\usr\bin\xargs.exe" clang-tidy -p buildNinja --checks=-*,google-readability-casting --header-filter=src --export-fixes=casts.yml
You can’t perform that action at this time.