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 / IsNumeric.cpp
Created May 21, 2014 03:05
C++11 fancy iteration
bool isNumeric(const std::string& input)
{
return std::all_of(input.begin(), input.end(), ::isdigit);
}
class Stopwatch {
using clock = std::chrono::high_resolution_clock;
bool is_running() const { return stop_time_ == clock::time_point::min(); }
clock::time_point end_time() const { return is_running() ? clock::now() : stop_time_; }
clock::time_point begin_time_{clock::now()}, stop_time_{clock::time_point::min()};
public:
void stop() { if (is_running()) stop_time_ = clock::now(); }
clock::duration elapsed() const { return end_time() - begin_time_; }
};
@foobit
foobit / get_widget.cpp
Created April 1, 2014 19:32
from: Herb Sutter's - Going Native 2013
std::shared_ptr<widget> get_widget(int id)
{
static std::map<int, std::weak_ptr<widget>> cache;
static std::mutex m;
std::lock_guard<std::mutex> hold(m);
auto& wp = cache[id];
auto sp = wp.lock();
if (!sp)
{
std::vector<std::string>
sorted(std::vector<std::string> names)
{
std::sort(names);
return names;
}
// names is an lvalue; a copy is required so we don't modify names
std::vector<std::string> sorted_names1 = sorted( names );
@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"