Skip to content

Instantly share code, notes, and snippets.

@Gnomorian
Gnomorian / gist:ac2c3a73ec52278e8477efdb42bf39aa
Created July 4, 2018 03:32
websocket.h, WebSocketEndClientHandshake not working for some unspecified reason
#include <Windows.h>
#include <websocket.h>
#include <iostream>
#include <winerror.h>
ULONG RecieveBuffer = 4096;
ULONG SendBuffer = 4096;
BOOL DisableMasking = false; // by default, this is not used. try disabling it first
BOOL DisableUTF8Verification = false;
@Gnomorian
Gnomorian / bmk_split.cpp
Created August 29, 2020 11:08
Benchmark of different built in ways to split a string in c++
#include <string>
#include <regex>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <chrono>
#include <assert.h>
using namespace std;
using Splits = vector<string>;
@Gnomorian
Gnomorian / timerResolution.cpp
Created July 31, 2021 12:54
Test timer resolution on windows using TimerQueues. the regular StartTimer function has a resoltion of 15ms, this can go down to 1ms it appears
#include <Windows.h>
#include <stdexcept>
#include <string>
#include <memory>
#include <vector>
#include <chrono>
#include <thread>
#include <iostream>
class TimerQueue
@Gnomorian
Gnomorian / win32exception.h
Created August 16, 2021 10:47
an exception that takes (or gets automatically) the windows error code and populates what() with the error message so you can throw on win32 errors and get useful information in the catch without code repeat.
#pragma once
/* an exeption object that gets the error message for windows error codes. */
/* default constructor will get the error code from windows, alternativly you can provide the error code. */
/* exmple:
#include "win32exception.h"
#include <iostream>
int main()
{
try
@Gnomorian
Gnomorian / iterateVectors.cpp
Last active October 30, 2021 10:23
Using C++17 fold expressions iterate an infinate number of vectors at the same time until the length of the shortest vector
#include <vector>
#include <iostream>
#include <algorithm>
constexpr auto iterateVectors = [](auto...vectors)
{
constexpr auto getSmallestVector = [](auto...vectors)
{
size_t end{ reinterpret_cast<size_t>(std::numeric_limits<size_t>::max) };
((end = std::min(vectors.size(), end)), ...);
@Gnomorian
Gnomorian / simple_string_concatination.cpp
Last active November 7, 2021 11:10
use function template params to concatenate parameters that can be inserted to streams sort of like a simple formatting function
template<typename...Args>
std::string concat(Args...args)
{
std::stringstream stream;
((stream << args), ...);
return std::move(stream.str());
}
int main()
{
@Gnomorian
Gnomorian / custom_std_formatter.cpp
Created December 20, 2021 11:17
an example of how to write a custom std::formatter
#include <iostream>
#include <format>
#include <iterator>
class Thing
{
public:
std::string s{ "some text" };
};
@Gnomorian
Gnomorian / gist:bc828cb69f48295dbc0412fd43bd3ad8
Created December 21, 2021 01:13
adding handle.exe to context menu
adding Handle.exe to the context menu:
Computer\HKEY_CLASSES_ROOT\Directory\shell\runas
HasLUASHield = ""
MUIVerb = "Query open handles"
Computer\HKEY_CLASSES_ROOT\Directory\shell\runas\Command
Default="powershell.exe -noexit -command handle.exe -u '%V'"
Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\runas
HasLUASHield = ""
@Gnomorian
Gnomorian / README.txt
Created December 22, 2021 06:00
Custom Formatter for non-copyable references
std::format is a nice simple way to do string interpolation in c++
std::format("hello, {}!", "World); would print "`hello, World!`" for example.
I wanted to learn to create my own formatters so i could provide custom objects and have them printed nicely by just providing
it. There is not currently much documentation on the subject, but i managed to make it happen however one problem is the
std::formatter implementations will only take values, not references which means either you pass std::format a pointer to your object or
you allow it to take copies of your object.
## What if you dont want std::format to copy your object
Either you can define a formatter for a pointer of your object, or you can copy the code in this gist.
@Gnomorian
Gnomorian / SomeClass.cpp
Created December 22, 2021 13:38
example for someone on discord
#include "SomeClass.h"
#include <iostream>
// implementation
void SomeClass::doSomething()
{
std::cout << "doing saomething" << std::endl;
}
int SomeClass::addTen(int from)