Skip to content

Instantly share code, notes, and snippets.

@Gnomorian
Gnomorian / main.cpp
Last active January 17, 2024 02:19
A Hacky way to use strings in switch statements c++
#include <iostream>
#include <array>
template<size_t NumOfStrings>
struct StringToInt
{
std::array<std::string_view, NumOfStrings> strings{};
constexpr StringToInt(auto&&...args)
{
auto addEntry = [](auto&& collection, auto&& argument, size_t i)
@Gnomorian
Gnomorian / main.cpp
Created December 4, 2023 23:42
Using Concepts
// a blank struct to give an example of a failing concept
struct Something {};
// a concept called Addable wich to satisfy requires that instances of T can use the + operator
template<typename T>
concept Addable = requires(T x) { x + x; };
// a struct called Thing, which takes a type that is constrained by the 3 concepts after the 'requires' statement.
template<typename T> requires Addable<T> && std::copyable<T> && std::movable<T>
struct Thing
@Gnomorian
Gnomorian / main.cpp
Created October 4, 2023 21:37
make_array implementation
#include <iostream>
#include <array>
// simple template method that creates a std::array
template<typename...T>
auto make_array(T...data) -> std::array<std::tuple_element_t<0, std::tuple<T...>>, sizeof...(data)>
{
return {data...};
}
@Gnomorian
Gnomorian / lru.h
Created June 7, 2023 02:25
LRU C++ std implementation example
#pragma once
#include <list>
#include <unordered_map>
#include <optional>
#include <iostream>
#include <typeinfo>
template<typename K, typename V, size_t MaxEntries>
class LRUCache
@Gnomorian
Gnomorian / main.cpp
Created March 12, 2022 18:27
get media metadata from a file
#include <iostream>
#include <Windows.h>
#include <filesystem>
#include <array>
#include <string>
#include <string_view>
#include <vector>
#include <variant>
#include <Wmsdk.h>
#include <stdexcept>
@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)
@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 / 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 / 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 / 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()
{