Skip to content

Instantly share code, notes, and snippets.

View codebrainz's full-sized avatar

Matthew Brush codebrainz

View GitHub Profile
@codebrainz
codebrainz / gist:69b8df344481b51112c5
Last active August 29, 2015 14:13
Run build system when sources change
#!/bin/bash
# Inspired by: https://exyr.org/2011/inotify-run/
# base source dir, found from script's dir, in my case it's `..` to the base srcdir
SRCDIR=$(cd "$(dirname $(dirname "$0"))"; pwd -P)
# command to run initially and on file changes, in my case in `build` dir
COMMAND="make -C $SRCDIR/build"
@codebrainz
codebrainz / listdeps.py
Last active August 29, 2015 14:12
Getting a list of header dependencies using GCC -MM
#!/usr/bin/env python3
import argparse
import json
import re
import sys
from collections import OrderedDict
from subprocess import Popen, PIPE
RE_INC = re.compile(r'^(?P<target>.+?):\s+(?P<deps>.+?)$', re.MULTILINE)
@codebrainz
codebrainz / scope_exit.hxx
Last active August 29, 2015 14:12
A macro/template implementation of C++ scope guard
#pragma once
#include <utility>
namespace ScopeExitNamespace__ {
template<typename T> struct OnScopeExit__ {
T func;
OnScopeExit__(T func) : func(std::move(func)) {}
~OnScopeExit__() noexcept { func(); }
};
@codebrainz
codebrainz / test.ast
Last active August 29, 2015 14:11
Sample C++ TreeGen output
//
// test.ast - Sample AST description for treegen
//
target CPlusPlus {
// In string literals,
// $$ is target/variable (for externs)
// $_ is "this" or "self", etc. (for nodes and externs)
// $@ is the type (for nodes and externs)
#include <setjmp.h>
#include <stdio.h>
#define WHILE(while_expr) { \
int jumped____; \
jmp_buf redo_target____; \
int restarted____; \
restarted____ = setjmp(redo_target____); \
jumped____ = 1; \
while ((while_expr)||(jumped____=0)) {
@codebrainz
codebrainz / gist:8ece2a9015a3ed0d260f
Last active July 23, 2018 23:49
Using std::hash with char* as a C string rather than a pointer.
namespace std
{
template <>
struct hash<char *>
{
size_t operator()(const char *s) const
{
// http://www.cse.yorku.ca/~oz/hash.html
size_t h = 5381;
int c;
@codebrainz
codebrainz / encoder.cxx
Last active August 29, 2015 14:08
Byte encoder for some basic types
#include <cstdint>
#include <type_traits>
/*
* Encodes the value as bytes and inserts into the iterator.
*
* Example:
* std::vector<uint8_t> buf;
* int some_val = 0x12345678;
* encode(some_val, back_inserter(buf));
@codebrainz
codebrainz / after.txt
Created October 25, 2014 17:35
nm -g <exe_or_lib> | grep " T "
0000000000040ce0 T build_activate_menu_item
000000000003f3c0 T build_get_current_menu_item
0000000000042a50 T build_get_group_count
000000000003f2a0 T build_remove_menu_item
000000000003fd60 T build_set_menu_item
00000000000489e0 T dialogs_show_input
0000000000048a60 T dialogs_show_input_numeric
00000000000474f0 T dialogs_show_msgbox
00000000000493c0 T dialogs_show_question
00000000000495c0 T dialogs_show_save_as

Callbacks Handlers

  • Signal connections should be done using GtkBuilder/Glade where possible. If you're moving any manual GUI building code to the GtkBuilder file, you should ensure to move the signal connections there where possible (ex. where you don't need special user_data that cannot be set through Glade).

  • If you're making a callback handler for any non-GtkBuilder-related signals place the handler nearby (ex. directly above) the function that uses it. For

@codebrainz
codebrainz / foobar.c
Created May 13, 2014 06:28
Minimal GObject boilerplate (~ 8 lines of additional code)
#include "foobar.h"
G_DEFINE_TYPE (FooBar, foo_bar, G_TYPE_OBJECT)
static void foo_bar_class_init (FooBarClass *klass) {}
static void foo_bar_init (FooBar *self) {}
// Test program, doesn't count :)
int main()
{
g_type_init();