Skip to content

Commit

Permalink
I suppose this is what happens when I have four uninterrupted hours a…
Browse files Browse the repository at this point in the history
…way from the kid
  • Loading branch information
zadjii-msft committed Jul 23, 2022
1 parent eb88cd3 commit b535a5f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/cascadia/UnitTests_TerminalCore/TilWinRtHelpersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ class TerminalCoreUnitTests::TilWinRtHelpersTests final
TEST_CLASS(TilWinRtHelpersTests);
TEST_METHOD(TestPropertySimple);
TEST_METHOD(TestPropertyHString);

TEST_METHOD(TestEvent);
TEST_METHOD(TestForwardedEvent);

TEST_METHOD(TestTypedEvent);
};

void TilWinRtHelpersTests::TestPropertySimple()
Expand Down Expand Up @@ -124,3 +127,22 @@ void TilWinRtHelpersTests::TestForwardedEvent()
VERIFY_ARE_EQUAL(3, handledOne);
VERIFY_ARE_EQUAL(3, handledTwo);
}

void TilWinRtHelpersTests::TestTypedEvent()
{
bool handledOne = false;
bool handledTwo = false;

auto handler = [&](const winrt::hstring sender, const int& v) -> void {
VERIFY_ARE_EQUAL(L"sure", sender);
VERIFY_ARE_EQUAL(42, v);
handledOne = true;
};

til::typed_event<winrt::hstring, int> MyEvent;
MyEvent(handler);
MyEvent([&](winrt::hstring, int) { handledTwo = true; });
MyEvent.raise(L"sure", 42);
VERIFY_ARE_EQUAL(true, handledOne);
VERIFY_ARE_EQUAL(true, handledTwo);
}
38 changes: 34 additions & 4 deletions src/inc/til/winrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
property<T>() = default;
property<T>(T defaultValue) :
_value{ defaultValue } {}
// T& operator()()
// {
// return _value;
// }

T operator()()
{
return _value;
Expand Down Expand Up @@ -76,6 +73,39 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
}
event<ArgsT>& _origin;
};

template<typename SenderT, typename ArgsT>
struct typed_event
{
typed_event<SenderT, ArgsT>() = default;
winrt::event_token operator()(const winrt::Windows::Foundation::TypedEventHandler<SenderT, ArgsT>& handler) { return _handlers.add(handler); }
void operator()(const winrt::event_token& token) { _handlers.remove(token); }
template<typename... Arg>
void raise(Arg const&... args)
{
_handlers(args...);
}
winrt::event<winrt::Windows::Foundation::TypedEventHandler<SenderT, ArgsT>> _handlers;
};

template<typename SenderT, typename ArgsT>
struct forwarded_typed_event
{
forwarded_typed_event<SenderT, ArgsT>() = delete;
forwarded_typed_event<SenderT, ArgsT>(typed_event<SenderT, ArgsT>& other) :
_origin{ other } {}
forwarded_typed_event<SenderT, ArgsT>(forwarded_typed_event<SenderT, ArgsT>& other) :
_origin{ other._origin } {}

winrt::event_token operator()(const winrt::Windows::Foundation::TypedEventHandler<SenderT, ArgsT>& handler) { return _origin(handler); }
void operator()(const winrt::event_token& token) { _origin(token); }
template<typename... Arg>
void raise(Arg const&... args)
{
_origin.raise(args...);
}
typed_event<SenderT, ArgsT>& _origin;
};
#endif

}

0 comments on commit b535a5f

Please sign in to comment.