Skip to content

Instantly share code, notes, and snippets.

View jonwis's full-sized avatar

Jon Wiswall jonwis

View GitHub Profile
@jonwis
jonwis / bijection.h
Created January 12, 2024 21:37
Bijection support-ish?
#include <tuple>
#include <iostream>
#include <algorithm>
#include <string_view>
#include <array>
#include <stdexcept>
#include <optional>
// This is a bijection lookup helper that given an array of pairs finds the key or
// the value. The keys should be sorted.
@jonwis
jonwis / winrt_obfuscate.cpp
Last active December 20, 2023 04:25
Obfuscation from a seed number
#include <random>
#include <string>
#include <vector>
#include <span>
#include <winrt/windows.storage.streams.h>
#include <winrt/windows.security.cryptography.h>
#include <winrt/windows.security.cryptography.core.h>
#include <winrt/windows.security.cryptography.certificates.h>
namespace winrt {
@jonwis
jonwis / transmute.hxx
Created November 21, 2023 05:01
A function that transmutes any container into a vector of another type
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
// Given a container that supports "begin(c)" / "end(c)", uses std::transform plus a
// provided transmutor function to produce an output vector of the results. The
// type of the vector returned is derived from the return type of the transmutor
// operation.
template<typename Z, typename Q>
@jonwis
jonwis / enum_iter.hxx
Created November 19, 2023 07:15
Yet another enum iterator attempt
#include <wil/com.h>
// Wraps a type like IEnumIUknown and exposes it as a forward iterator,
// or like IEnumIDList and exposes it as a forward iterator of type TStoredType.
// The IEnumType must have the following methods:
// HRESULT Next(ULONG celt, T* rgelt, ULONG* pceltFetched)
template <typename IEnumType, typename TStoredType>
struct iterator
{
wil::com_ptr<IEnumType> m_enum{};
@jonwis
jonwis / finder.cpp
Created October 2, 2023 02:51
Compile time sort and lookup example
#include <array>
#include <algorithm>
#include <string_view>
// From https://medium.com/@vgasparyan1995/compile-time-merge-sort-c-bb0ace62cc23
constexpr auto sort(const auto& arr)
{
auto result = arr;
auto sortless = [](auto const& a, auto const& b) { return a.first < b.first; };
std::sort(std::begin(result), std::end(result), sortless);
@jonwis
jonwis / iface_cache.hxx
Created September 26, 2023 18:59
Interface cache implementation
#include <mutex>
struct guid { uint64_t id[2]; };
template<typename T> guid const& guid_of()
{
return T::iid;
}
struct IUnknown {
virtual uint32_t AddRef() = 0;
@jonwis
jonwis / json_utf8_read.cpp
Created September 10, 2023 17:01
Read UTF8 stream file into a Windows.Data.Json.JsonObject isntance
// Opens a file by path name. Assumes the file is UTF-8 encoded. If the file
// has a BOM, it'll be skipped so the first access to the file is positioned
// correctly.
std::wifstream open_utf8_with_maybe_bom(std::filesystem::path const& path)
{
std::wifstream ss;
ss.imbue(std::locale(".utf-8"));
ss.open(path);
if (ss.good() && (ss.peek() == 0xFEFF))
@jonwis
jonwis / DirectValueSet.idl
Last active December 12, 2023 17:33
Direct value set
/*
Here's some powershell to run to generate this type
cls
$typeList = @(
"UInt8",
"UInt16",
"UInt32",
"UInt64",
# "Int8", -- not a valid type
@jonwis
jonwis / bad_span.h
Created August 12, 2023 03:36
Bad use of initializer-list
#include <span>
template<typename T> struct bad_span
{
bad_span() : m_data(nullptr), m_size(0) {}
bad_span(std::initializer_list<T> value) :
m_data(value.begin()),
m_size(static_cast<uint32_t>(value.size()))
{
@jonwis
jonwis / snippet_base.h
Created August 9, 2023 21:01
More constexpr of hstring?
WINRT_EXPORT namespace winrt::param
{
struct hstring
{
#ifdef _MSC_VER
#pragma warning(suppress: 26495)
#endif
hstring() noexcept : m_handle(nullptr) {}
hstring(hstring const& values) = delete;
hstring& operator=(hstring const& values) = delete;