Skip to content

Instantly share code, notes, and snippets.

View jonwis's full-sized avatar

Jon Wiswall jonwis

View GitHub Profile
@jonwis
jonwis / ChangeTrackerSample.cs
Last active July 22, 2020 02:29
Sample of tracking changes
private async void StartTracking_Click(object sender, RoutedEventArgs e)
{
FolderPicker p = new FolderPicker();
p.FileTypeFilter.Add(".txt");
p.FileTypeFilter.Add(".jpg");
var f = await p.PickSingleFolderAsync();
var t = f.TryGetChangeTracker();
if (t != null)
{
t.Enable();
template<typename TDevice> winrt::IAsyncOperation<TDevice> GetFirstDeviceAsync(winrt::hstring selector)
{
// Create a watcher. Try converting all the discovered device information records
// into real device instances. When the enumeration is complete, stop everything.
auto watcher = winrt::DeviceInformation::CreateWatcher(selector);
// DeviceWatcher enumeration ensures that all devices are enumerated, then
// raises 'EnumerationCompleted'. We'll use coroutines to model this.
winrt::handle event{ ::CreateEvent(nullptr, true, false, nullptr) };
winrt::check_bool(static_cast<bool>(event));
std::pair<std::wstring_view, std::wstring_view> SplitAt(std::wstring_view const& source, wchar_t split)
{
auto splitpoint = source.find(split);
if (splitpoint != std::wstring_view::npos)
{
return { source.substr(0, splitpoint), source.substr(splitpoint + 1) };
}
else
{
return { source, { } };
#include <wil/resource.h>
template<typename Q> bool SubmitThreadpoolLambda(Q&& fn)
{
using base_type = std::decay_t<Q>;
struct ContextObject : base_type
{
ContextObject(Q&& q) : base_type(std::move(q)) {}
@jonwis
jonwis / is_win10_build.h
Last active July 9, 2022 02:15
Version query for Windows 10 builds
#include <versionhelpers.h>
bool IsWindows10BuildOrGreater(uint16_t buildNumber)
{
OSVERSIONINFOEXW osvi = { sizeof(osvi) };
const auto conditionMask = VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(
0, VER_MAJORVERSION, VER_GREATER_EQUAL),
@jonwis
jonwis / funcorfield.h
Created October 23, 2022 04:30
Func or field
// Tool to either invoke a method or read a field on a type.
class MyTypeName {
using TSelf = MyTypeName;
template<typename T> using func_or_field = std::variant<
T(TSelf::*)(),
T TSelf::*>;
template<typename T> auto invoke_func_or_field(func_or_field<T> const& fof)
{
@jonwis
jonwis / app2app.vcxproj
Created May 24, 2023 04:06
VCXProj for nomidl
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props')" />
<PropertyGroup Label="Globals">
<CppWinRTOptimized>true</CppWinRTOptimized>
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge>
<CppWinRTGenerateWindowsMetadata>true</CppWinRTGenerateWindowsMetadata>
<MinimalCoreWin>true</MinimalCoreWin>
<ProjectGuid>{86b1df74-0449-4ae4-a8ee-284912b2a7d1}</ProjectGuid>
<ProjectName>App2AppCore</ProjectName>
@jonwis
jonwis / utf8wifstream.cpp
Last active July 1, 2023 23:20
UTF-8 Encoded std::wifstream support
#include <iostream>
#include <ifstream>
#include <string>
// 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;
@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;
@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()))
{