Skip to content

Scope Guard - #3

Open
joren-dev wants to merge 2 commits into
masterfrom
scope_guard
Open

Scope Guard#3
joren-dev wants to merge 2 commits into
masterfrom
scope_guard

Conversation

@joren-dev

@joren-dev joren-dev commented Nov 21, 2020

Copy link
Copy Markdown
Owner

Overview

This pull request and code is written by both me and @Nerlant. Credits to him for helping and partly writing the code

Changes:

  • adds scope guard base

Usage

#include <concepts>
#include <tuple>
#include <type_traits>
#include <utility>

#include <Windows.h>
#include <tlhelp32.h>

#include <pe_scope_guard/pe_scope_guard.hpp>

int main()
{
	const HANDLE a = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	const HANDLE b = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	{

		// lambda
		auto c = [a]() { CloseHandle(a); };

		// A combination of c and rvalue lambda's is completely fine
		peach::ScopeGuard test(
			[a]() { CloseHandle(a); },
			[b]() { CloseHandle(b); },
			c
		);

	}

	// Handles closed here
}

Any changes on files besides the ones directly connected to your feature/change (emtpy if none):

  • none

Additional Notes:

With clang 11 in release the dissambly will look like:

  HANDLE v0; // rsi
  HANDLE v1; // rsi

  v0 = CreateToolhelp32Snapshot(2u, 0);
  v1 = CreateToolhelp32Snapshot(2u, 0);
  v0 = CreateToolhelp32Snapshot(2u, 0);
  CloseHandle(v0);
  CloseHandle(v1);
  JUMPOUT(func);

Quite optimal to say the least.

  • One can pass lvalue lambda's, rvalue lambda's and plain c-style functions
  • Amount of arguments and functors is unlimited
  • The constructor is constexpr if the passed values passed through the capture list are compile time
  • If the dtor throws std::terminate() is called
  • The functor passed must have void as return type because the return value is neglected either way

Tests:

  • Test feature thorougly
  • Compiles w/ clang 11
  • Formatted according to the .clang-format

Extra

  • Add explanation/documentation on wiki/readme page

@joren-dev
joren-dev requested a review from Nerlant November 21, 2020 00:42
@joren-dev joren-dev added the enhancement New feature or request label Nov 21, 2020
Comment thread pe_scope_guard/pe_scope_guard.hpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants