Skip to content

Instantly share code, notes, and snippets.

View eplawless's full-sized avatar

Eric Lawless eplawless

View GitHub Profile
@eplawless
eplawless / enum-class-hack.cpp
Created October 19, 2018 15:52
Hack to get type-safe enum classes with implicit conversion to int.
#include <iostream>
#define ENUM_CLASS(Name, ...) \
struct Name { \
static int sCounter; \
int mValue; \
Name() : mValue(++Name::sCounter) {} \
operator int() { return mValue; } \
static Name __VA_ARGS__; \
}; \
@eplawless
eplawless / node-is-a-liar-snippet-4.js
Last active April 18, 2016 22:00
Implementation of "get next available TCP port"
var net = require('net');
var os = require('os');
function getLocalHosts() {
var hosts = ['0.0.0.0', '::'];
var interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(function(interfaceName) {
var interface = interfaces[interfaceName];
interface.forEach(function(info) {
var address = info.address;
[ '0.0.0.0',
'::',
'::1',
'127.0.0.1',
'fe80::1%lo0',
'192.168.1.7' ]
@eplawless
eplawless / node-is-a-liar-snippet-3.js
Created April 18, 2016 08:53
Get all local host addresses
var os = require('os');
function getLocalHosts() {
var hosts = ['0.0.0.0', '::'];
var interfaces = os.networkInterfaces();
Object.keys(interfaces).forEach(function(interfaceName) {
var interface = interfaces[interfaceName];
interface.forEach(function(info) {
var address = info.address;
// append the interface name onto link-local IPv6 addresses
@eplawless
eplawless / node-is-a-liar-snippet-2.js
Created April 18, 2016 07:46
This had better not work...
var net = require('net');
function startServer(port, host, callback) {
var server = net.createServer();
server.listen(port, host, function() {
callback(undefined, server);
});
server.on('error', function(error) {
console.error('Ah damn!', error);
callback(error);
@eplawless
eplawless / node-is-a-liar-snippet-1.js
Created April 18, 2016 05:46
Let's open up a TCP server on port 4000!
var net = require('net');
var server = net.createServer();
server.listen(4000, function() {
console.log('Listening on port 4000!');
});
server.on('error', function(error) {
console.error('Help!', error);
});
@eplawless
eplawless / output.txt
Last active April 18, 2016 05:15
Node's TCP sockets hardcode SO_REUSEADDR by default and this cannot be changed
Using IPv4 unspecified address 0.0.0.0
Using IPv6 unspecified address ::
Interface lo0 has address ::1
Interface lo0 has address 127.0.0.1
Interface lo0 has address fe80::1%lo0
Interface en0 has address 192.168.1.7
Starting too many servers on port 4567...
Listening on 0.0.0.0 port 4567
Listening on :: port 4567
Listening on ::1 port 4567
@eplawless
eplawless / Navigation.js
Last active August 29, 2015 14:23
react router injection
import getNavigation from './getNavigation';
import React from 'react';
export default getNavigation({ React });
function hash(str) {
var len = str.length;
var hash = 5381;
for (var idx = 0; idx < len; ++idx) {
hash = 33 * hash + str.charCodeAt(idx);
}
return hash;
}
function NOOP() {}
var button = getSomeButtonSomehow();
// all of these Animation objects are immutable, and just describe the motion
var moveRight = Animate.positionX({ from: 0, to: 100, ease: Easing.easeOut, duration: 200 });
var moveDown = Animate.positionY({ from: 0, to: 100, ease: Easing.easeOut, duration: 200 });
var fadeOut = Animate.opacity({ to: 0, ease: Easing.cubicBezier(0,1,1,0), duration: 50 });
var move = moveRight.with(moveDown, 100); // start moveDown 100ms after moveRight starts
var reusableAnimation = move.then(fadeOut, -fadeOut.duration); // start fadeOut 50ms before move ends