This is actually nice

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

This is actually nice

Post by albinopapa » April 29th, 2020, 6:55 pm

Code: Select all

                pushEvent( {
                    Event::KeyPressed,
                    Event::KeyEvent{
                        .code = virtualKeyCodeToSF( wParam, lParam ),
                        .alt = HIWORD( GetKeyState( VK_MENU ) ) != 0,
                        .control = HIWORD( GetKeyState( VK_CONTROL ) ) != 0,
                        .shift = HIWORD( GetKeyState( VK_SHIFT ) ) != 0,
                        .system = HIWORD( GetKeyState( VK_LWIN ) ) || HIWORD( GetKeyState( VK_RWIN ) )
                    } }
                );
This C++20 feature is nice.

This would be equivalent to

Code: Select all

                pushEvent( {
                    Event::KeyPressed,
                    Event::KeyEvent{
                        virtualKeyCodeToSF( wParam, lParam ),
                        HIWORD( GetKeyState( VK_MENU ) ) != 0,
                        HIWORD( GetKeyState( VK_CONTROL ) ) != 0,
                        HIWORD( GetKeyState( VK_SHIFT ) ) != 0,
                        HIWORD( GetKeyState( VK_LWIN ) ) || HIWORD( GetKeyState( VK_RWIN ) )
                    } }
                );


But reads like and is as safe as:

Code: Select all

                Event::KeyEvent kevent;
                kevent.code = virtualKeyCodeToSF( wParam, lParam );
                kevent.alt = HIWORD( GetKeyState( VK_MENU ) ) != 0
                kevent.control = HIWORD( GetKeyState( VK_CONTROL ) ) != 0;
                kevent.shift = HIWORD( GetKeyState( VK_SHIFT ) ) != 0;
                kevent.system = HIWORD( GetKeyState( VK_LWIN ) ) || HIWORD( GetKeyState( VK_RWIN ) );

                pushEvent( { Event::KeyPressed, kevent } );
I say safer because .alt, .control, .shift and .system are all bool values so getting the order of initialization wrong would cause issues. With being able to specify the members in the braced initialization you get the safety of directly initializing the correct members and the efficiency of aggregate initialization.

I may have to give up on VS2017 now that more C++20 features are making it's way into VS2019.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: This is actually nice

Post by chili » May 2nd, 2020, 8:42 am

Oh, i didn't know that that one was available now. I should start using it.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: This is actually nice

Post by albinopapa » May 3rd, 2020, 3:19 am

I found it when I was looking through the roadmap link you had posted some where...here, discord or twitter, can't remember.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: This is actually nice

Post by albinopapa » May 3rd, 2020, 3:21 am

I keep forgetting about this syntax when needing a counter variable with an STL algorithm:

Code: Select all

auto create_thread = [ &, count = 0 ]( std::thread& t ) mutable
{ 
	t = std::thread{ task, count++ }; 
};
Never mind what I'm doing creating threads like this, the thing I'm pointing out is declaring the count inside the capture section and declaring the lambda as mutable so that count can be incremented.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: This is actually nice

Post by cyboryxmen » May 11th, 2020, 2:13 pm

I think you're actually nice Albinopapa
Zekilk

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: This is actually nice

Post by albinopapa » May 12th, 2020, 1:08 am

Well, that came outta nowhere, I'm a little confused and flattered, thanks.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply