Skip to content

Instantly share code, notes, and snippets.

View JPGygax68's full-sized avatar

Hans-Peter Gygax JPGygax68

View GitHub Profile
@JPGygax68
JPGygax68 / consume-ffmpeg-libs.cmake
Last active July 12, 2023 09:35
#CMake: how to add the #FFmpeg libraries to a target
# Detect architecture
if (CMAKE_SIZEOF_VOID_P MATCHES 8)
set( PROJECT_ARCH "x86_64" )
else(CMAKE_SIZEOF_VOID_P MATCHES 8)
set( PROJECT_ARCH "x86" )
endif(CMAKE_SIZEOF_VOID_P MATCHES 8)
# FFmpeg
@JPGygax68
JPGygax68 / utf8-utf32.cpp
Last active July 15, 2022 08:13
#Unicode conversion: UTF-8 to UTF-32 in C++11
#include <codecvt>
using namespace std;
#if _MSC_VER >= 1900
wstring_convert<codecvt_utf8<int32_t>, int32_t> utf32conv;
auto utf32 = utf32conv.from_bytes("The quick brown fox jumped over the lazy dog.");
// use reinterpret_cast<const char32_t *>(utf32.c_str())
#else
wstring_convert<codecvt_utf8<char32_t>, char32_t> utf32conv;
@JPGygax68
JPGygax68 / traits.cpp
Last active December 23, 2020 07:20
How to implement custom #traits (C++11)
#include <type_traits>
#include <iostream>
using std::enable_if;
using std::is_same;
using std::declval;
using std::integral_constant;
template <typename T>
using Invoke = typename T::type;
@JPGygax68
JPGygax68 / async_data.h
Last active November 3, 2020 10:19
A template class for obtaining data in the background, based on std::async() and std::future<>
#pragma once
#include <future>
/**
* Class template based on std::async and std::future<> to help in fetching data asynchronously.
*/
template <typename T>
struct async_data {
@JPGygax68
JPGygax68 / zls_problem.zig
Created June 8, 2020 12:45
(Posted to help debugging a ZLS problem under VSCode)
const std = @import("std");
const c = @cImport({
// @cInclude("imgui/imgui.h");
// @cInclude("imgui/imgui_internal.h");
@cInclude("imgui/imstb_truetype.h");
@cInclude("imgui/imstb_rectpack.h");
});
const assert = std.debug.assert;
@JPGygax68
JPGygax68 / local_webserver_Gruntfile.js
Last active September 18, 2017 20:58
Using #grunt-express as a local web server during development #tags: web server, grunt-server
"use strict";
var path = require('path');
module.exports = function(grunt) {
grunt.initConfig({
express: {
defaults: {
options: {
port: 3000,
@JPGygax68
JPGygax68 / main.js
Last active September 18, 2017 19:36
Node with #Express, #Jade, #Stylus, and #Mongoose
var express = require('express')
, path = require('path')
, stylus = require('stylus')
, nib = require('nib')
, mongoose = require('mongoose');
var app = express();
app.locals.title = 'My Web App';
@JPGygax68
JPGygax68 / nestedAsync.js
Last active September 18, 2017 19:36
Simple pattern to detect when nested #asynchronous operations are fully done. Caveats: no error handling - use for simple scripts only!
"use strict";
var pending_asops = 0;
var global_data;
startingAsop();
asyncFunction('param1', 'param2', function(err, data) { global_data = data; } );
function allDone() {
// Use the complete data!
@JPGygax68
JPGygax68 / FilePtr.cpp
Last active September 18, 2017 19:35
#RIIA wrapper for FILE*. No copy semantics yet.
class FilePtr {
public:
FilePtr() { fp = nullptr; }
FilePtr(FILE *fp_) { fp = fp_; }
FilePtr & operator = (FILE *fp_) { assert(fp == nullptr); fp = fp_; return *this; }
~FilePtr() { if (fp != nullptr) fclose(fp); }
operator FILE * () { return fp; }
private:
FILE *fp;
};
@JPGygax68
JPGygax68 / promisechain.js
Last active September 18, 2017 19:35
How to create a dynamic #promise chain.
// var index = ... hash of id => filename
return _.reduce(index, function(seq, filename, id) {
return seq.then( function() { return fs.read(filename); } )
}, Q.resolve());