Skip to content

Instantly share code, notes, and snippets.

View cbsmith's full-sized avatar

Christopher Smith cbsmith

View GitHub Profile
@cbsmith
cbsmith / protoc-gen-depends.py
Created April 16, 2012 05:15
Makefile dependency generator plugin for protoc.
#!/usr/bin/python
# Save this file as protoc-gen-depends, put it in your path (executable) and add "--depends-out=/whatever/path" to your protoc invocation
from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest,CodeGeneratorResponse
from sys import stdin,stdout
req = CodeGeneratorRequest()
req.MergeFromString(''.join(stdin.readlines()))
res = CodeGeneratorResponse()
@cbsmith
cbsmith / joyent-instant-centos-salt-minion.sh
Created April 29, 2012 03:22
Instant CentOS Minion on Joyent
#!/bin/bash
#
# Setups up a new CentOS machine on Joyent running a Salt minion talking to your salt master. From there, you can do the rest of your setup work from the Salt Master.
# Requires Joyent Command Line SDK be installed as well as Node's jsontool (npm install jsontool)
# If you don't provide a Joyent ID, the code assumes the master is labeled with metadata "salt-role=master" or with "role=salt-master"
# Tweak these variables should have done anything creative with your salt setup
SALT_ROOT=/
SALT_MASTER_UID=root
@cbsmith
cbsmith / dont_terminate.cpp
Created August 8, 2012 23:50
The problem with std::uncaught_exception illustrated
// -*- compile-command: "clang++ -ggdb -o dont_terminate -std=c++0x -stdlib=libc++ dont_terminate.cpp" -*-
// Demonstration of the problem that can happen with using std::uncaught_exception() to determine
// whether it is safe to throw from within a destructor
// Problem identified, as always, by GOTW: http://www.gotw.ca/gotw/047.htm
#include <stdexcept>
#include <iostream>
#include <cassert>
using namespace std;
@cbsmith
cbsmith / brace_initializer.cpp
Created November 27, 2012 04:55
Demonstration of brace initialization as well as variadic templates.
// -*- compile-command: "clang++ -ggdb -o brace_initializer -std=c++0x -stdlib=libc++ brace_initializer.cpp" -*-
#include <string>
#include <ostream>
#include <memory>
#include <iostream>
#include <sstream>
#include <new>
class Foo {
@cbsmith
cbsmith / func.cc
Last active December 14, 2015 00:59
Me proving myself wrong.
// -*- compile-command: "clang++ -std=c++11 -stdlib=libc++ -Wall -Werror func.cc -o func" -*-
#include <iostream>
#include <functional>
#include <utility>
#include <iomanip>
//The ugly beast we're wrapping
template <typename T1, typename T2, typename T3>
struct Hooks3 {
virtual bool Fire (int *, T1 t1, T2 t2, T3 t3) { return false; }
@cbsmith
cbsmith / declarative_storage.py
Last active December 14, 2015 10:09
My rather lame approach to setting parameter storage class. It also demonstrates how to write an IPN handler with Flask, as that was my use case where form argument order mattered (most annoying). End result is uglier than just setting the storage class inside your function, but does make it easier to see when that special case is being used.
#demonstration of hack to declaratively set param_storage_class declaratively using a decorator
#also demonstrates how to write an IPN handler with Flask
from flask import Flask, make_response
from itertools import chain
app = Flask(__name__)
#Normally this parameter would come from a config
IPN_URLSTRING = 'https://www.sandbox.paypal.com/cgi-bin/webscr'
IPN_VERIFY_EXTRA_PARAMS = (('cmd', '_notify-validate'),)
@cbsmith
cbsmith / the_nullptr_story.cpp
Last active December 16, 2015 02:19
A simple example of how it is perfectly reasonable to have a pointer that may or may not be nullptr, and therefore delete a pointer that may or may not be nullptr. We've got people of type Person who, when they get married, may choose to take on their spouse's last name. These people always have a given last name, called "lastname", but they may…
// -*- compile-command: "clang++ -pedantic -g -O3 -o the_nullptr_story -std=c++0x -stdlib=libc++ the_nullptr_story.cpp" -*-
// Simple example of how it is perfectly reasonable to have a pointer that may or may not be nullptr,
// and therefore delete a pointer that may or may not be nullptr.
// Formatting is a bit unusual/dense to make it easy to focus on the important bits.
#include <string>
#include <ostream>
class Person {
public:
//Moved to the top so nobody misses it
@cbsmith
cbsmith / cleanup.py
Last active December 16, 2015 16:59
Python code that traverses one or more directories, removing any orphaned .pyc's.
"""Traverses the directory tree deleting any .pyc's who do not have a source .py.
Helpful when switching between revisions with source control."""
from os.path import join
from os import walk, unlink
import re
import sys
PYTHON_RE = re.compile(R'^(.*\.py)(c)?$')
global count
@cbsmith
cbsmith / random_selection.cpp
Last active August 9, 2022 12:29
Hopefully serves as a reference implementation on how to do random selection of an element from a container.
// -*- compile-command: "clang++ -ggdb -o random_selection -std=c++0x -stdlib=libc++ random_selection.cpp" -*-
//Reference implementation for doing random number selection from a container.
//Kept for posterity and because I made a surprising number of subtle mistakes on my first attempt.
#include <random>
#include <iterator>
template <typename RandomGenerator = std::default_random_engine>
struct random_selector
{
//On most platforms, you probably want to use std::random_device("/dev/urandom")()
@cbsmith
cbsmith / two_stars.cpp
Created May 21, 2013 16:41
Demonstration of how two_star programming works with STL containers and algorithms.d
// -*- compile-command: "clang++ -ggdb -o two_stars -std=c++0x -stdlib=libc++ two_stars.cpp" -*-
#include <algorithm>
#include <iostream>
using namespace std; //laziness
int main(int argc, char** argv) {
int a = 1;
int b = 2;
int c = 3;