Skip to content

Instantly share code, notes, and snippets.

@socantre
socantre / scope_exit.h
Created July 20, 2017 17:47
simple scope exit
template <typename Func> struct scope_exit
{
Func cleanup;
scope_exit(Func &&f) : cleanup(std::forward<Func>(f)) {}
~scope_exit() { cleanup(); }
};
template <typename Func> scope_exit<Func> make_scope_exit(Func &&f)
{
@socantre
socantre / require.hpp
Created June 1, 2017 19:24
require() with REQUIRE() macro to deal with source locations, until we get a real std::source_location type
#include <stdexcept>
struct require_error : std::runtime_error {
using std::runtime_error::runtime_error;
};
void require(bool b, char const *message) {
if (!b) {
throw require_error(message);
}
#include <utility>
#ifndef SCOPE_EXIT_H_
#define SCOPE_EXIT_H_
// modeled slightly after Andrescu’s talk and article(s)
namespace std {
namespace experimental {
template <typename EF> struct scope_exit {
// construction
@socantre
socantre / demangle.cpp
Last active September 26, 2016 16:39
Example use of the Itanium ABI demangling API.
#include <cxxabi.h>
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <memory>
struct clib_free {
template<typename T>
@socantre
socantre / CMakeLists.txt
Last active September 14, 2016 21:23
CMakeLists.txt for lua
cmake_minimum_required(VERSION 3.3)
project(lua VERSION 5.3.2 LANGUAGES C CXX)
set(LIB_SOURCE src/lapi.c src/lauxlib.c src/lbaselib.c src/lbitlib.c
src/lcode.c src/lcorolib.c src/lctype.c src/ldblib.c
src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c
src/linit.c src/liolib.c src/llex.c src/lmathlib.c src/lmem.c
src/loadlib.c src/lobject.c src/lopcodes.c src/loslib.c
src/lparser.c src/lstate.c src/lstring.c src/lstrlib.c
src/ltable.c src/ltablib.c src/ltm.c src/lundump.c
@socantre
socantre / .vimrc
Last active May 17, 2016 21:29
vimrc vim config file
" show command in bottom right corner
set showcmd
" show line numbers in gutter
set number
set relativenumber
" disable softwrap
set nowrap
set sidescroll=1
@socantre
socantre / music.sh
Created March 12, 2016 21:42
random note generator
# based on http://blog.robertelder.org/bash-one-liner-compose-music/
# with overflow, channels, timing, etc. bugs fixed
< /dev/urandom hexdump -v -e '/1 "%u\n"' |
gawk 'BEGIN { split("0,2,4,5,7,9,11,12",a,",")}
{ for (i = 0; i < 0.3125; i += 0.0000625)
printf("%02X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' |
sed -e 's/.*\(..\)$/\1/g' |
xxd -r -p |
sox -traw -c 1 -r16000 -b8 -e signed-integer - -tcoreaudio
@socantre
socantre / feature_check.cpp
Created January 24, 2014 23:34
C++ feature checking
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
@socantre
socantre / gist:8588819
Created January 23, 2014 23:17
factor factorials
#include <algorithm>
#include <iostream>
#include <random>
std::vector<int> &factor(int n, std::vector<int> &prime_factors) {
auto i = 2;
while (n > 1) {
if (0 == n % i) {
prime_factors.push_back(i);
n /= i;
@socantre
socantre / gist:7667148
Last active December 29, 2015 11:59
std::async program that hangs Windows ConcRT, but works with either std::launch::deferred or std::launch::async
#include <iostream>
#include <future>
#include <vector>
#include <atomic>
#include <random>
#include <string>
const int n_tasks = 1000;
std::atomic<bool> flags[n_tasks] = {};