Skip to content

Instantly share code, notes, and snippets.

View JPGygax68's full-sized avatar

Hans-Peter Gygax JPGygax68

View GitHub Profile
@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;
#include <iostream>
/*=======================================================================
* ASPECT COMPOSITION
*======================================================================*/
/* This is the base class to be specialized via CRTP.
*
* It must provide all the methods that aspects can override as no-ops.
*/
@JPGygax68
JPGygax68 / Aspect_composition.cpp
Last active May 17, 2016 21:23
Use C++11 template template parameters to add pseudo-aspects via chained inheritance
#include <iostream>
/*=======================================================================
* ASPECT COMPOSITION
*======================================================================*/
template<class Parent>
struct Nil_aspect: public Parent
{
void tell() {} // no-op, and ends the chain
@JPGygax68
JPGygax68 / method_checking.cpp
Last active September 18, 2017 19:21
Check/assert whether a #class has a #method with a given #signature.
#include <iostream>
#include <string>
class A {
public:
void method1();
void method2(int);
static void method3(int []);
};
@JPGygax68
JPGygax68 / find_next_nal_unit.cpp
Last active September 18, 2017 19:03
Function that finds the next #NALU inside #H264 bitstream data.
/*
* Find next NAL unit from the specified H.264 bitstream data.
*/
static auto find_next_nal_unit(const uint8_t *start, const uint8_t *end) -> const uint8_t *
{
const uint8_t *p = start;
/* Simply lookup "0x000001" pattern */
while (p <= end - 3 && (p[0] || p[1] || p[2] != 1))
++p;
@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 / opengl_rectangle.hpp
Last active September 18, 2017 19:04
Defining and drawing a rectangle with #OpenGL using a #VAO (vertex array object) and a vertex buffer.
/* Example class for rendering a rectangle using OpenGL 4, using a vertex buffer and a
* vertex array object and the triangle strip primitive.
*
* Note: GL calls are wrapped into a variadic macro of the form GL(func, arg1, arg2,...).
*/
class opengl_rectangle {
public:
void get_resources()
@JPGygax68
JPGygax68 / indexed_struct.cpp
Last active September 18, 2017 19:22
Proof-of-concept for something similar to #tuple, but based on struct #composition instead of struct inheritance.
#include <iostream>
template <typename IndexedStruct, int Index>
struct _getter {
using Rest = decltype(std::declval<IndexedStruct>().rest);
static auto& get(IndexedStruct &inst)
{
return _getter<decltype(inst.rest), Index-1>::get(inst.rest);
@JPGygax68
JPGygax68 / pipe.cpp
Last active September 18, 2017 19:04
How to support #piping (input/output redirection) in C++
ifstream ifs;
ofstream ofs;
istream *is = nullptr;
ostream *os = nullptr;
if (!input_file.empty()) {
ios_base::sync_with_stdio(false);
ifs.open(input_file, ios::binary);
is = &ifs;
}