Skip to content

Commit

Permalink
refactored the zeroInit functions to use template magic
Browse files Browse the repository at this point in the history
  • Loading branch information
ariccio committed Jan 29, 2015
1 parent e323d74 commit 5c649d2
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 429 deletions.
6 changes: 4 additions & 2 deletions WinDirStat/windirstat/SelectDrivesDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ void CDrivesList::OnLButtonDown( const UINT /*nFlags*/, const CPoint /*point*/ )
else {
SetFocus( );
// Send a LVN_ITEMCHANGED to the parent, so that it can update the radio button.
auto lv = zeroInitNMLISTVIEW( );
//auto lv = zeroInitNMLISTVIEW( );
auto lv = zero_init_struct<NMLISTVIEW>( );
lv.hdr.hwndFrom = m_hWnd;
lv.hdr.idFrom = static_cast<UINT_PTR>( GetDlgCtrlID( ) );
lv.hdr.code = LVN_ITEMCHANGED;
Expand Down Expand Up @@ -714,7 +715,8 @@ LRESULT _Function_class_( "GUI_THREAD" ) CSelectDrivesDlg::OnWmuThreadFinished(


// For paranoia's sake we check, whether driveItem is in our list. (and we so find its index.)
auto fi = zeroInitLVFINDINFO( );
//auto fi = zeroInitLVFINDINFO( );
auto fi = zero_init_struct<LVFINDINFO>( );
fi.flags = LVFI_PARAM;
fi.lParam = driveItem;

Expand Down
3 changes: 2 additions & 1 deletion WinDirStat/windirstat/TreeListControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,8 @@ void CTreeListControl::DrawNode( _In_ const CTreeListItem* const item, _In_ CDC&
void CTreeListControl::OnLButtonDown( UINT nFlags, CPoint point ) {
m_lButtonDownItem = -1;

auto hti = zeroInitLVHITTESTINFO( );
//auto hti = zeroInitLVHITTESTINFO( );
auto hti = zero_init_struct<LVHITTESTINFO>( );

hti.pt = point;

Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/dirstatdoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ BEGIN_MESSAGE_MAP(CDirstatDoc, CDocument)
END_MESSAGE_MAP()

void CDirstatDoc::OnUpdateEditCopy( _In_ CCmdUI* pCmdUI ) {
pCmdUI->Enable( /*( GetMainFrame( )->m_logicalFocus == focus::LF_DIRECTORYLIST ) &&*/ m_selectedItem != NULL );
pCmdUI->Enable( m_selectedItem != NULL );
}

void CDirstatDoc::OnEditCopy( ) {
Expand Down
635 changes: 258 additions & 377 deletions WinDirStat/windirstat/globalhelpers.cpp

Large diffs are not rendered by default.

58 changes: 23 additions & 35 deletions WinDirStat/windirstat/globalhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
//
// Last modified: $Date$

#ifndef GLOBALHELPERS_H
#define GLOBALHELPERS_H

#pragma once
#include "stdafx.h"

//struct SExtensionRecord;

#ifndef GLOBALHELPERS_H
#define GLOBALHELPERS_H

//
#include "stdafx.h"

//_Success_( SUCCEEDED( return ) ) HRESULT FormatBytes ( _In_ const std::uint64_t n, _Out_writes_z_( strSize ) _Pre_writable_size_( strSize ) PWSTR psz_formatted_bytes, _In_range_( 38, 64 ) const rsize_t strSize );

Expand All @@ -42,20 +39,6 @@ _Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatDouble ( _In_ const
//maximum representable integral component of a double SEEMS to be 15 characters long, so we need at least 17
_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatDouble ( _In_ const DOUBLE d, WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_double, _In_range_( 17, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written );

_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatLongLongHuman_0( WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 8, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written );

_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatLongLongHuman_B( WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 8, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written, _In_ const DOUBLE B );

_Success_( SUCCEEDED( return ) ) _Pre_satisfies_( chars_written == 0 )
HRESULT CStyle_FormatLongLongHuman_KB( WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 23, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written, _In_ const DOUBLE B, _In_ const DOUBLE KB );

_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatLongLongHuman_MB( WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 23, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written, _In_ const DOUBLE KB, _In_ const DOUBLE MB );

_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatLongLongHuman_GB( WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 8, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written, _In_ const DOUBLE MB, _In_ const DOUBLE GB );

_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatLongLongHuman_TB( WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 8, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written, _In_ const DOUBLE GB, _In_ const DOUBLE TB );


_Success_( SUCCEEDED( return ) ) HRESULT CStyle_FormatLongLongHuman ( _In_ std::uint64_t n, WDS_WRITES_TO_STACK( strSize, chars_written ) PWSTR psz_formatted_LONGLONG_HUMAN, _In_range_( 8, 64 ) const rsize_t strSize, _Out_ rsize_t& chars_written );


Expand Down Expand Up @@ -117,24 +100,29 @@ const LARGE_INTEGER help_QueryPerformanceCounter( );
const LARGE_INTEGER help_QueryPerformanceFrequency( );


LVITEM partInitLVITEM ( ) ;
//SHELLEXECUTEINFO partInitSEI ( ) ;
WINDOWPLACEMENT zeroInitWINDOWPLACEMENT ( ) ;
LVHITTESTINFO zeroInitLVHITTESTINFO ( ) ;
HDITEM zeroInitHDITEM ( ) ;
LVFINDINFO zeroInitLVFINDINFO ( ) ;
PROCESS_MEMORY_COUNTERS zeroInitPROCESS_MEMORY_COUNTERS ( ) ;
//STARTUPINFO zeroInitSTARTUPINFO ( ) ;
//PROCESS_INFORMATION zeroInitPROCESS_INFORMATION ( ) ;
NMLISTVIEW zeroInitNMLISTVIEW ( ) ;
//BROWSEINFO zeroInitBROWSEINFO ( ) ;
//SHFILEOPSTRUCT zeroInitSHFILEOPSTRUCT ( ) ;
template<typename type_struct_to_init>
type_struct_to_init zero_init_struct( ) {
static_assert( std::is_pod<type_struct_to_init>::value, "can't memset a non-pod struct!" );
static_assert( !std::is_polymorphic<type_struct_to_init>::value, "can't memset a polymorphic type!" );
static_assert( std::is_standard_layout<type_struct_to_init>::value, "can't memset a non-standard layout struct!" );
static_assert( std::is_trivially_default_constructible<type_struct_to_init>::value, "can't memset a struct that isn't trivially default constructable!" );
static_assert( std::is_trivially_copyable<type_struct_to_init>::value, "might have trouble returning a non-trivially-copyable item by value. You've been warned!" );
type_struct_to_init the_struct;
memset( &the_struct, 0, sizeof( the_struct ) );
return the_struct;
}

//LVITEM partInitLVITEM ( ) ;
//WINDOWPLACEMENT zeroInitWINDOWPLACEMENT ( ) ;
//LVHITTESTINFO zeroInitLVHITTESTINFO ( ) ;
//HDITEM zeroInitHDITEM ( ) ;
//LVFINDINFO zeroInitLVFINDINFO ( ) ;
//PROCESS_MEMORY_COUNTERS zeroInitPROCESS_MEMORY_COUNTERS ( ) ;
//NMLISTVIEW zeroInitNMLISTVIEW ( ) ;

//This is UNconditionally called from one place. Compiler does not inline. TODO: consider inlining.
FILETIME zeroInitFILETIME ( ) ;

//NMLISTVIEW* zeroInitNMLISTVIEW_heap ( ) ;
//This is UNconditionally called from one place. Compiler does not inline. TODO: consider inlining.
//FILETIME zeroInitFILETIME ( ) ;


std::wstring EncodeSelection( _In_ const RADIO radio, _In_ const std::wstring folder, _In_ const std::vector<std::wstring>& drives );
Expand Down
3 changes: 2 additions & 1 deletion WinDirStat/windirstat/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,8 @@ FILETIME CItemBranch::FILETIME_recurse( ) const {
return m_vi->filetime_cache;
}
}
auto ft = zeroInitFILETIME( );
//auto ft = zeroInitFILETIME( );
auto ft = zero_init_struct<FILETIME>( );
if ( Compare_FILETIME_cast( ft, m_lastChange ) ) {
ft = m_lastChange;
}
Expand Down
6 changes: 4 additions & 2 deletions WinDirStat/windirstat/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ void CMainFrame::OnClose( ) {
UNREFERENCED_PARAMETER( timing );
#endif
TRACE( _T( "OnClose timing: %f\r\n" ), timing );
auto pmc = zeroInitPROCESS_MEMORY_COUNTERS( );
//auto pmc = zeroInitPROCESS_MEMORY_COUNTERS( );
auto pmc = zero_init_struct<PROCESS_MEMORY_COUNTERS>( );
pmc.cb = sizeof( pmc );

if ( GetProcessMemoryInfo( GetCurrentProcess( ), &pmc, sizeof( pmc ) ) ) {
Expand All @@ -341,7 +342,8 @@ void CMainFrame::OnClose( ) {
}

void CMainFrame::OnDestroy( ) {
auto wp = zeroInitWINDOWPLACEMENT( );
//auto wp = zeroInitWINDOWPLACEMENT( );
auto wp = zero_init_struct<WINDOWPLACEMENT>( );
GetWindowPlacement( &wp );
CPersistence::SetMainWindowPlacement( wp );
const auto TypeView = GetTypeView( );
Expand Down
22 changes: 13 additions & 9 deletions WinDirStat/windirstat/ownerdrawnlistcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ class COwnerDrawnListCtrl : public CListCtrl {

void SortItems( ) {
VERIFY( CListCtrl::SortItems( &_CompareFunc, reinterpret_cast<DWORD_PTR>( &m_sorting ) ) );
auto hditem = zeroInitHDITEM( );

//auto hditem = zeroInitHDITEM( );
auto hditem = zero_init_struct<HDITEM>( );
auto thisHeaderCtrl = GetHeaderCtrl( );

//http://msdn.microsoft.com/en-us/library/windows/desktop/bb775247(v=vs.85).aspx specifies 260
Expand Down Expand Up @@ -530,7 +530,9 @@ class COwnerDrawnListCtrl : public CListCtrl {

_Success_( return != -1 ) _Ret_range_( -1, INT_MAX )
INT FindListItem( _In_ const COwnerDrawnListItem* const item ) const {
auto fi = zeroInitLVFINDINFO( );
//auto fi = zeroInitLVFINDINFO( );
auto fi = zero_init_struct<LVFINDINFO>( );

fi.flags = LVFI_PARAM;
fi.lParam = reinterpret_cast<LPARAM>( item );

Expand Down Expand Up @@ -586,7 +588,8 @@ class COwnerDrawnListCtrl : public CListCtrl {
}

void InsertListItem( _In_ _In_range_( 0, INT32_MAX ) const INT_PTR i, _In_ const COwnerDrawnListItem* const item ) {
auto lvitem = partInitLVITEM( );
//auto lvitem = partInitLVITEM( );
auto lvitem = zero_init_struct<LVITEM>( );

lvitem.mask = LVIF_TEXT | LVIF_PARAM;
lvitem.iItem = static_cast<int>( i );
Expand Down Expand Up @@ -616,8 +619,8 @@ class COwnerDrawnListCtrl : public CListCtrl {
if ( subitem == 0 ) {
// Special case column 0:
// If we did GetSubItemRect(item 0, LVIR_LABEL, rc) and we have an image list, then we would get the rectangle excluding the image.
HDITEM hditem = zeroInitHDITEM( );

//HDITEM hditem = zeroInitHDITEM( );
auto hditem = zero_init_struct<HDITEM>( );
hditem.mask = HDI_WIDTH;
VERIFY( thisHeader->GetItem( 0, &hditem ) );

Expand Down Expand Up @@ -1024,7 +1027,8 @@ class COwnerDrawnListCtrl : public CListCtrl {

public:
bool IsColumnRightAligned( _In_ const INT col, const CHeaderCtrl* const thisHeaderControl ) const {
auto hditem = zeroInitHDITEM( );
//auto hditem = zeroInitHDITEM( );
auto hditem = zero_init_struct<HDITEM>( );
hditem.mask = HDI_FORMAT;
VERIFY( thisHeaderControl->GetItem( col, &hditem ) );
return ( hditem.fmt bitand HDF_RIGHT ) != 0;
Expand Down Expand Up @@ -1142,8 +1146,8 @@ class COwnerDrawnListCtrl : public CListCtrl {


auto x = -GetScrollPos( SB_HORZ );
auto hdi = zeroInitHDITEM( );

//auto hdi = zeroInitHDITEM( );
auto hdi = zero_init_struct<HDITEM>( );
hdi.mask = HDI_WIDTH;
const auto header_ctrl_item_count = header_ctrl->GetItemCount( );
if ( header_ctrl_item_count <= 0 ) {
Expand Down
4 changes: 3 additions & 1 deletion WinDirStat/windirstat/windirstat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ _Success_( SUCCEEDED( return ) ) HRESULT CDirstatApp::GetCurrentProcessMemoryInf
}

_Success_( return == true ) bool CDirstatApp::UpdateMemoryInfo( ) {
auto pmc = zeroInitPROCESS_MEMORY_COUNTERS( );
//auto pmc = zeroInitPROCESS_MEMORY_COUNTERS( );
auto pmc = zero_init_struct<PROCESS_MEMORY_COUNTERS>( );

pmc.cb = sizeof( pmc );

if ( !GetProcessMemoryInfo( GetCurrentProcess( ), &pmc, sizeof( pmc ) ) ) {
Expand Down

0 comments on commit 5c649d2

Please sign in to comment.