Skip to content

Instantly share code, notes, and snippets.

#include<stdlib.h>
#include<stdio.h>
typedef struct state_t {
int current_line;
int heading_level;
token* previous_token;
} state;
state* state_with(state* s) /* just for readability when invoking */
@wilhelmtell
wilhelmtell / gist:347445
Created March 29, 2010 05:10
Dynamically load and invoke a function in C++
///////////////////////////////////////////////////////////////////////////////
// dyn.cc - compiled and linked as a dynamic library
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
extern "C" void fun()
{
std::cout << "BOOYA!!1! fun() invoked!\n";
}
# polymorphism in bash ?
prepare_c() {
cp ~/c_skeleton $1
cp ~/c_makefile_skeleton $1
}
prepare_bash() {
cp ~/bash_skeleton $1
}
template<typename T, size_t N>
T* end_of(T (&arr)[N])
{
return &arr[0]+N;
}
// ...
int arr[42];
std::fill(arr, end_of(arr), 25);
#include <iostream>
using namespace std;
union ufloat {
float f;
char b[sizeof(float)];
};
int main(int argc, char* argv[])
from_dot()
{
[ $# -gt 0 ] && local FROM="$@";
sed '/[{}]/d; s/^\s*\|\s*\[[^]]*\]\s*\|\s*;\s*$//g; s/\s/_/g; s/\s*->\s*/ /' $FROM;
}
from_dot graph.dot |tsort #|tac
namespace System {
class Exception<T> : Exception {
public Exception() { }
public Exception(string message) : base(message) { }
public Exception(string message, Exception inner)
: base(message, inner){ }
}
}
namespace App {
#!/bin/bash
# git read eval print loop
P='git> ';
echo -e -n "$P";
while read L; do
if [ "$L" = "quit" -o "$L" = q ]; then
break;
fi;
git "$L";
try {
std::vector<int> v;
// gtfo of try, so we only catch the ctor's error
} catch (const std::bad_alloc& e) {
// ...
}
// damn it, so now v's out of scope!
@wilhelmtell
wilhelmtell / gist:845950
Created February 27, 2011 06:02
n_iterator
#include <iterator>
#include <iostream>
#include <vector>
#include <algorithm>
template<class I, class S>
struct n_iterator : public std::iterator_traits<I> {
n_iterator();
n_iterator(I iter, S n);