Skip to content

Instantly share code, notes, and snippets.

View johnbartholomew's full-sized avatar

John Bartholomew johnbartholomew

View GitHub Profile
#if 0 /* example use: */
#define EMIT_FIXUP_DEFINITIONS 1
/* if EMIT_FIXUP_DEFINITIONS is 0 or undefined then
* only the struct declaration will be emitted
* typically you want to only emit fixup function
* definitions in a single translation unit.
*/
#define DECLARE_STRUCT_NAME BITMAPFILEHEADER
@johnbartholomew
johnbartholomew / str2long.c
Created March 6, 2013 12:20
str2long function with correct range checks.
/* Written for
*
* A Quick Coding Contest: Convert String to Integer Without Overflow
*
* http://blog.regehr.org/archives/909
*
* This has not yet been run through clang's integer behaviour sanitizer,
* because I don't have a recent enough clang build.
*/
function numbered_keys(step, context, position)
local k = position
local f = function(s, i)
local v
k,v = step(s, k)
if k ~= nil then
return (i+1), v
end
end
function MinMaxCommodity(sys)
sys = sys or Game.system
if sys:isa('SystemPath') then
sys = sys:GetStarSystem()
end
local alt = sys:GetCommodityBasePriceAlterations()
local minPrice = math.huge
local maxPrice = -math.huge
local minCargo, maxCargo
@johnbartholomew
johnbartholomew / test.cpp
Created December 16, 2012 23:09
Checking overloads for sin and cos functions in C++11
#include <cmath>
#include <type_traits>
static_assert(std::is_same<float, decltype(std::sin(3.1f))>::value,
"std::sin not overloaded for float");
static_assert(std::is_same<double, decltype(std::sin(3.1))>::value,
"std::sin not overloaded for double");
static_assert(std::is_same<long double, decltype(std::sin(3.1l))>::value,
"std::sin not overloaded for long double");
Translate:Add({
English = {
cheats = 'Cheats!',
}
})
local ui = Engine.ui
local t = Translate:GetTranslator()
local cheatMission = function (loaded)
template <typename T> struct ForceConstRef { typedef const T& type; };
template <typename T> struct ForceConstRef<const T&> { typedef const T& type; };
template <typename T> struct ForceConstRef<const T> { typedef const T& type; };
template <typename T> struct ForceConstRef<T&> { typedef const T& type; };
template <typename T0, typename T1>
class LuaSignalTrampoline : public LuaSignalTrampolineBase {
public:
typedef typename ForceConstRef<T0>::type type0;
typedef typename ForceConstRef<T1>::type type1;
@johnbartholomew
johnbartholomew / gist:3969948
Created October 28, 2012 21:15
Some git instructions

This assumes you have no changes loose in your working copy.

At any point you can run gitk to view the commit history. I use gitk all the time, to view and search through the commit history, find the SHA1 hash of a particular commit, and check the commit graph.

First, make sure master is up to date (it most likely already is, but no harm in checking)

git checkout master

git fetch upstream

@johnbartholomew
johnbartholomew / byte-order-test.c
Created April 4, 2012 20:48
compiler smarts: recognising shifts and ors as a load or byte-swap
#include <string.h>
#include <stdint.h>
/* compiled with:
* {gcc,clang} -std=c89 -pedantic -Wall -Wextra -O3 -S -c byte-order-test.c
*
* Built on and for an x86-64 system.
* The full assembly output from both compilers is embedded at the bottom.
*
* Summary:
// simplest version
struct StateCapturer {
StateCapturer() {
glPushAttrib(...);
glPushMatrix(...);
glPushTheWholeDamnWorld();
}
~StateCapturer() {