Skip to content

Instantly share code, notes, and snippets.

@foobit
foobit / JsonCompressDecompress.cs
Created March 20, 2021 15:02
json -> gzip compress -> base64 -> data[] -> gzip decompress -> json
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
// read json
var file = @"somedata.json";
using var inStream = File.OpenRead(file);
// compress
@foobit
foobit / TaskGetResult.cs
Created October 7, 2018 19:47
returns Task<T>.Result when "T" can not be provided at compile time
public static class TaskExtensions
{
public static object GetResult(this Task task)
{
task.Wait();
var taskType = task.GetType();
var resultProperty = taskType.GetProperty("Result");
return resultProperty?.GetValue(task);
}
}
@foobit
foobit / StateMachine.cs
Last active October 7, 2018 19:44
Simple C# Task based state machine
using System;
using System.Threading.Tasks;
public interface IState
{
Task RunAsync();
}
public class StateMachine
{
@foobit
foobit / BroadcastEventSample.cs
Created September 19, 2018 21:53
C# broadcast signaled event via Task completion
using System;
using System.Threading;
using System.Threading.Tasks;
namespace BroadcastEventSample
{
public class EventBroadcastTask
{
private readonly CancellationTokenSource cancelSource = new CancellationTokenSource();
private readonly TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
@foobit
foobit / main.cpp
Created May 25, 2018 19:22
odd vs warning: cl.exe main.cpp /W4
struct foo
{
template<typename... Args>
void operator[](Args&&...)
{
}
};
struct bit {};
@foobit
foobit / vsredist.iss
Last active January 2, 2016 11:08
VC Redist check from http://stackoverflow.com/a/11172939 (updated for VC2013x86)
[Setup]
[Run]
; add the Parameters, WorkingDir and StatusMsg as you wish, just keep here
; the conditional installation Check
Filename: "{tmp}\vcredist_x86.exe"; Check: VCRedistNeedsInstall
[Code]
#ifdef UNICODE
#DEFINE AW "W"
@foobit
foobit / gist:7930545
Last active December 31, 2015 03:49
my simple text data format
//
// C++ style comments
//
name "Test model" // key-value pair (kvp)
version 1.0 // kvp as number
rev 3182 // ...
full_screen true // boolean value - can not use 'true'/ 'false as kvp key
resizable false
@foobit
foobit / fancyprint.cpp
Created December 8, 2013 16:03
Hacking variadic functions for converting types to string
#include <stdio.h>
#include <string>
std::string printy(const std::string& p)
{
return std::move(p);
}
template<typename ... Types>
std::string printy(const std::string& p, float first, Types ... rest)
@foobit
foobit / WinMainNot.cpp
Created November 16, 2013 02:22
Make Windows app entry point portable main() vs WinMain
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
int main(int, const char**)
{
// create window normally
return 0;
}
@foobit
foobit / main.cpp
Created November 14, 2013 02:40
DirectX 11 creation with C++11 fun
// main.cpp
//
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3d11_2.h>
#include <algorithm>