Skip to content

Commit

Permalink
DeadFocusWnd now using ATL, added ScopeGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
ariccio committed Feb 23, 2015
1 parent c307b0a commit e557bfe
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 249 deletions.
17 changes: 17 additions & 0 deletions WinDirStat/windirstat/ScopeGuard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "stdafx.h"
#include "ScopeGuard.h"

#ifndef WDS_SCOPEGUARD_CPP_INCLUDED
#define WDS_SCOPEGUARD_CPP_INCLUDED

//intentionally NOT defined as part of ScopeGuard, to reduce code duplication. //Also, produces cleaner `TRACE` output.
void trace_out( _In_z_ PCSTR const file_name, _In_z_ PCSTR const func_name, _In_ _In_range_( 0, INT_MAX ) const int line_number ) {
TRACE( L"Scope guard triggered!"
L"\r\n\t\tScope guard initialized in file: `%S`,"
L"\r\n\t\tfunction: `%S`,"
L"\r\n\t\tline: `%i`\r\n", file_name, func_name, line_number );
}

#else

#endif
89 changes: 89 additions & 0 deletions WinDirStat/windirstat/ScopeGuard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "stdafx.h"

#ifndef WDS_SCOPEGUARD_H_INCLUDED
#define WDS_SCOPEGUARD_H_INCLUDED

void trace_out( _In_z_ PCSTR const file_name, _In_z_ PCSTR const func_name, _In_ _In_range_( 0, INT_MAX ) const int line_number );




//based on Andrei Alexandrescu's "Systematic Error Handling in C++"
// To initialize, use something like: auto g1 = scopeGuard( [ ]{ fclose( fd ); unlink( name ); }, __FILE__, __FUNCSIG__, __LINE__ );
// To dismiss
template <class Fun>
class ScopeGuard {
Fun function_to_call_on_scope_exit;
bool active_;


//static_assert( std::is_function<Fun>::value, "This template NEEDS a function to call. `Fun` is not callable." );
static_assert( std::is_move_constructible<Fun>::value, "It's important that `Fun` be move-constructable, as ScopeGuard has a move constructor" );

#if !_HAS_EXCEPTIONS
static_assert( std::is_nothrow_move_constructible<Fun>::value, "It's important that `Fun` be move-constructable WITHOUT throwing exceptions, as ScopeGuard has a move constructor, and I have exceptions disabled." );
#endif

#ifdef DEBUG
_Field_z_ PCSTR const file_name;
_Field_z_ PCSTR const func_name;
_Field_range_( 0, INT_MAX ) const int line_number;
#endif

public:
ScopeGuard( Fun f, _In_z_ PCSTR const file_name_in, _In_z_ PCSTR const func_name_in, _In_ _In_range_( 0, INT_MAX ) const int line_number_in ) : function_to_call_on_scope_exit{ std::move( f ) }, active_{ true },
#ifdef DEBUG
file_name{ file_name_in },
func_name{ func_name_in },
line_number{ line_number_in }
#else
UNREFERENCED_PARAMETER( file_name_in );
UNREFERENCED_PARAMETER( func_name_in );
UNREFERENCED_PARAMETER( line_number_in );
#endif
{ }

//intentionally asked to NOT inline, to reduce code duplication.
__declspec(noinline)
~ScopeGuard( ) {
if ( active_ ) {
#ifdef DEBUG
trace_out( file_name, func_name, line_number );
//TRACE( L"Scope guard triggered!"
// L"\r\n\t\tScope guard initialized in file: `%S`,"
// L"\r\n\t\tfunction: `%S`,"
// L"\r\n\t\tline: `%i`\r\n", file_name, func_name, line_number );
#endif
function_to_call_on_scope_exit( );
}
}

//intentionally ASKING for inlining.
inline void dismiss( ) {
ASSERT( active_ == true );
active_ = false;
}


ScopeGuard( ) = delete;
ScopeGuard( const ScopeGuard& ) = delete;
ScopeGuard& operator=( const ScopeGuard& ) = delete;

//intentionally asked to NOT inline, to reduce code duplication.
__declspec(noinline)
ScopeGuard( ScopeGuard&& rhs ) : function_to_call_on_scope_exit( std::move( rhs.function_to_call_on_scope_exit ) ), active_( rhs.active_ ) {
rhs.dismiss( );
}

};


//intentionally ASKING for inlining.
template <class Fun>
inline ScopeGuard<Fun> scopeGuard( Fun f, _In_z_ PCSTR const file_name_in, _In_z_ PCSTR const func_name_in, _In_ _In_range_( 0, INT_MAX ) const int line_number_in ) {
return ScopeGuard<Fun>( std::move( f ), file_name_in, func_name_in, line_number_in );
}

#else

#endif

0 comments on commit e557bfe

Please sign in to comment.