is it possible to reduce redundant code like this?
Apr 21, 2008 at 2:58pm UTC
hi,
Im writing a hash calculator. This means that a lot of stuff in my code exists for each algorithm... I was wondering if things like the code below, which deals with some gui elements like checkboxes and buttons, could be somehow be made less bulky with macros maybe, or if you should just leave things like that in your code, considering that it grows even more as there might in the end be 20 odd algorithms?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
void on_CRC32CBox_stateChanged ( int state ) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( CRC32, state ); };
void on_MD2CBox_stateChanged ( int state ) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( MD2, state ); };
void on_MD4CBox_stateChanged ( int state ) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( MD4, state ); };
void on_MD5CBox_stateChanged ( int state ) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( MD5, state ); };
void on_RIPEMD160CBox_stateChanged ( int state ) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( RIPEMD160, state ); };
void on_PANAMACBox_stateChanged ( int state ) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( PANAMA, state ); };
void on_CRC32CopyBut_clicked () { copyTextToClipboard( CRC32 ); };
void on_MD2CopyBut_clicked () { copyTextToClipboard( MD2 ); };
void on_MD4CopyBut_clicked () { copyTextToClipboard( MD4 ); };
void on_MD5CopyBut_clicked () { copyTextToClipboard( MD5 ); };
void on_RIPEMD160CopyBut_clicked () { copyTextToClipboard( RIPEMD160 ); };
void on_PANAMACopyBut_clicked () { copyTextToClipboard( PANAMA ); };
void on_CRC32HashBut_clicked () { _mainWindow->myTabs->hashHash( CRC32 ); };
void on_MD2HashBut_clicked () { _mainWindow->myTabs->hashHash( MD2 ); };
void on_MD4HashBut_clicked () { _mainWindow->myTabs->hashHash( MD4 ); };
void on_MD5HashBut_clicked () { _mainWindow->myTabs->hashHash( MD5 ); };
void on_RIPEMD160HashBut_clicked () { _mainWindow->myTabs->hashHash( RIPEMD160 ); };
void on_PANAMAHashBut_clicked () { _mainWindow->myTabs->hashHash( PANAMA ); };
Last edited on Apr 21, 2008 at 3:07pm UTC
Apr 22, 2008 at 12:02am UTC
Use polymorphism:
1 2 3 4 5 6 7 8 9 10 11
class Calculator {
virtual void onBoxStateChanged(int state) = 0;
virtual void onHashClicked() = 0;
virtual void onCopyClicked = 0;
};
classCRC32 : public Calculator {
virtual void onBoxStateChanged(int state) { if ( _mainWindow->myTabs ) _mainWindow->myTabs->updateCheckbox( CRC32, state ); };
virtual void onHashClicked() { copyTextToClipboard( CRC32 ); };
virtual void onCopyClicked() { _mainWindow->myTabs->hashHash( CRC32 );}
};
Topic archived. No new replies allowed.