Skip to content

Instantly share code, notes, and snippets.

View cbsmith's full-sized avatar

Christopher Smith cbsmith

View GitHub Profile
@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 / 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;
@cbsmith
cbsmith / two_stars_98.cpp
Created May 21, 2013 17:09
C++98 version of two_stars
// -*- compile-command: "clang++ -ggdb -o two_stars_98 -std=c++98 -stdlib=libc++ two_stars_98.cpp" -*-
#include <algorithm>
#include <iostream>
using namespace std;
bool points_to_two(int* x) {
return *x == 2;
}
@cbsmith
cbsmith / test.cpp
Last active December 23, 2015 20:29
C++11 lambda sorcery
// -*- compile-command: "clang++ -ggdb -o test -std=c++11 -stdlib=libc++ test.cpp" -*-
#include <iostream>
float add(float x, float y) {
return x+y;
}
auto add_fourty_two = +[](float x) { return add(x, 42); };
auto add_pie_and_fourty_two = +[]() { return add_fourty_two(3.1415926); };
@cbsmith
cbsmith / test.cpp
Last active December 23, 2015 21:19
Attempt to create an operator||=
// -*- compile-command: "clang++ -Weverything -Wno-c++98-compat -ggdb -o test -std=c++11 -stdlib=libc++ test.cpp" -*-
#include <iostream>
#include <type_traits>
template <typename T, typename U, bool boolable = std::is_convertible<T,bool>::value, bool assignable = std::is_assignable<T, U>::value >
struct or_assign_helper;
template <typename T, typename U>
struct or_assign_helper<T, U, true, true> {
@cbsmith
cbsmith / keybase.md
Created December 27, 2015 22:19
Proving who I am to the world...

Keybase proof

I hereby claim:

  • I am cbsmith on github.
  • I am cbsmith (https://keybase.io/cbsmith) on keybase.
  • I have a public key ASAP-aANv7TdH9M92MiL7AmjcQmFCR4m9bNymFF-1y6LIgo

To claim this, I am signing this object:

@cbsmith
cbsmith / endian.cpp
Last active December 30, 2015 13:29
My demonstration of how byte swap can still be useful despite http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
// -*- compile-command: "g++ -O3 -march=native -std=c++11 -Wall -Werror endian.cpp -o endian" -*-
#include <fstream>
#include <ostream>
#include <istream>
#include <cstdint>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <limits>