Skip to content

Instantly share code, notes, and snippets.

@caryhaynie
caryhaynie / frp.coffee
Last active August 29, 2015 14:01
simple javascript object model featuring runtime type enforcement, interface verification, and cached virtual function dispatch tables.
class Observable
constructor: () ->
Object.defineProperty @, "_observers", {
configurable: false,
enumerable: false,
readable: true,
writable: false,
value: []
}
@caryhaynie
caryhaynie / test.cpp
Last active October 1, 2016 08:05
Simple CRTP pattern using friend classes to hide implementing methods.
#include <iostream>
template <typename T>
class TestBase {
public:
void TestIface() {
static_cast<T*>(this)->TestImpl();
}
};
template <typename T>
class BasePlatform {};
class SDLPlatform : BasePlatform<SDLPlatform> {};
class Win32Platform : BasePlatform<Win32Platform> {};
#ifdef USE_WINDERS
typedef Win32Platform Platform;
#else
@caryhaynie
caryhaynie / main.py
Created December 22, 2011 23:29
Simple PyGame App class with centralized event dispatch already setup.
import sys
import pygame
from pygame.locals import *
pygame.init()
FPS_RATE = 30
@caryhaynie
caryhaynie / dipatcher.py
Created April 6, 2011 13:23
Python class to marshall functions across threads.
from collections import deque
class Dispatcher(object):
_queue = deque()
@staticmethod
def dispatch(func):
Dispatcher._queue.appendleft(func)
@caryhaynie
caryhaynie / plugins.py
Created April 2, 2011 01:11
Python module to provide plugin support via a simple but effective import hook.
#!/usr/bin/python
import imp
import os
import os.path
import sys
# get the platform-specific list of possible module suffixes.
MODULE_DESC = imp.get_suffixes()
@caryhaynie
caryhaynie / eventable.rb
Created March 31, 2011 03:16
Simple Event Handling in Ruby
module Eventable
def def_event(evt_name)
#puts "creating a new event: #{evt_name}"
class_eval do
attr_reader "#{evt_name}_cb"
eval <<-END_EVAL
def on_#{evt_name}(&blk)
(@#{evt_name}_cb ||= []) << blk
nil
end