Board index » cppbuilder » How to save a previous f-ion address

How to save a previous f-ion address


2004-11-30 11:38:31 PM
cppbuilder2
Hi,
Today is not my day... How can I save a previous
f-on address, that I can reasing it later if needed.
// Remember last address...
Tmp = Application->OnMinimize;
// Assign new...
Application->OnMinimize = MyMinimize;
DoSomething();
// Reasign remembered...
Application->OnMinimize = Tmp;
How to declare Tmp ?
Best regards,
Vladimir Stefanovic
 
 

Re:How to save a previous f-ion address

Vladimir Stefanovic wrote:
Quote
// Assign new...
Application->OnMinimize = MyMinimize;

DoSomething();

// Reasign remembered...
Application->OnMinimize = Tmp;

How to declare Tmp ?
Ah, a chance to publicise one of my favourite RAII templates:
template<typename RAII_TmpValue>class TGeneral_RAII_TempValueChange
{
RAII_TmpValue * MyOrgPointer,
MyOrgPointerValue;
public:
TGeneral_RAII_TempValueChange( RAII_TmpValue * OrgPointer,
RAII_TmpValue TemporaryValue )
{
MyOrgPointer=OrgPointer;
MyOrgPointerValue=*MyOrgPointer;
*MyOrgPointer=TemporaryValue;
}
~TGeneral_RAII_TempValueChange()
{
*MyOrgPointer=MyOrgPointerValue;
}
};
And in answer to your question:
{ // RAII code block
TGeneral_RAII_TempValueChange<TNotifyEvent>
wibble( &( Application->OnMinimize ), TNotifyEvent( NULL ) );
DoSomething();
}
Once you are aware of this class you'd be surprised at just how often
it comes in useful.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:How to save a previous f-ion address

Thanks,
I have just tested it! Very nice indeed. Every month
somebody forces me to use the RAII model ;)
Do you have some other useful RAII templates
(to share with us)?
Best regards,
Vladimir Stefanovic
Andrue Cope [TeamB] < XXXX@XXXXX.COM >wrote in message
Quote
Vladimir Stefanovic wrote:

>// Assign new...
>Application->OnMinimize = MyMinimize;
>
>DoSomething();
>
>// Reasign remembered...
>Application->OnMinimize = Tmp;
>
>How to declare Tmp ?

Ah, a chance to publicise one of my favourite RAII templates:

template<typename RAII_TmpValue>class TGeneral_RAII_TempValueChange
{
RAII_TmpValue * MyOrgPointer,
MyOrgPointerValue;
public:
TGeneral_RAII_TempValueChange( RAII_TmpValue * OrgPointer,
RAII_TmpValue TemporaryValue )
{
MyOrgPointer=OrgPointer;

MyOrgPointerValue=*MyOrgPointer;

*MyOrgPointer=TemporaryValue;
}
~TGeneral_RAII_TempValueChange()
{
*MyOrgPointer=MyOrgPointerValue;
}
};

And in answer to your question:

{ // RAII code block
TGeneral_RAII_TempValueChange<TNotifyEvent>
wibble( &( Application->OnMinimize ), TNotifyEvent( NULL ) );

DoSomething();
}

Once you are aware of this class you'd be surprised at just how
often
it comes in useful.
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

{smallsort}

Re:How to save a previous f-ion address

Vladimir Stefanovic wrote:
Quote
Do you have some other useful RAII templates
(to share with us)?
// Temporarily activate the hourglass cursor.
class TOGeneral_RAII_HourGlass
{
static HourGlassCounter;
public:
TOGeneral_RAII_HourGlass()
{
++HourGlassCounter;
Screen->Cursor=crHourGlass;
}
~TOGeneral_RAII_HourGlass()
{
if( HourGlassCounter )
--HourGlassCounter;
if( !HourGlassCounter )
Screen->Cursor=crDefault;
}
};
usage (and it supports recursion):
{
TOGeneral_RAII_HourGlass hourGlass;
... do something
}
/////////////////
template<typename TRAIIEnabler>class TGeneral_RAII_Enable
{
TRAIIEnabler * MyObjectToControl;
bool MyInitialValueF;
public:
TGeneral_RAII_Enable( TRAIIEnabler * ObjectToControl,
bool DesiredValueF )
{
MyObjectToControl=ObjectToControl;
if( MyObjectToControl )
{
MyInitialValueF=ObjectToControl->Enabled;
MyObjectToControl->Enabled=DesiredValueF;
}
}
virtual ~TGeneral_RAII_Enable()
{
if( MyObjectToControl )
MyObjectToControl->Enabled=MyInitialValueF;
}
};
usage:
{
TGeneral_RAII_Enable<TSpeedButton>
speedButton1Disabler( SpeedButton1, false );
}
///////////////////
template<typename RAIIBeginEndUpdate>class TGeneral_RAII_BeginEndUpdate
{
RAIIBeginEndUpdate *MyObjectToControl;
public:
TGeneral_RAII_BeginEndUpdate( RAIIBeginEndUpdate *ObjectToControl )
{
ObjectToControl->BeginUpdate();
MyObjectToControl=ObjectToControl;
}
virtual ~TGeneral_RAII_BeginEndUpdate()
{
MyObjectToControl->EndUpdate();
}
};
usage:
{
TGeneral_RAII_BeginEndUpdate<TListItems>
listItems( lvwEntries->Items );
lvwEntries->Items->Clear();
}
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:How to save a previous f-ion address

"Vladimir Stefanovic" < XXXX@XXXXX.COM >wrote in message
Quote
// Remember last address...
Tmp = Application->OnMinimize;
<snip>
Quote
How to declare Tmp ?
If you read the documentation, you will see that OnMinimize is declared as a
TNotifyEvent, so simply declare your variable as the same:
TNotifyEvent Tmp = Application->OnMinimize;
Gambit
 

Re:How to save a previous f-ion address

Thanks for the code.
Best regards,
Vladimir Stefanovic
Andrue Cope [TeamB] < XXXX@XXXXX.COM >wrote in message
Quote
Vladimir Stefanovic wrote:

>Do you have some other useful RAII templates
>(to share with us)?

// Temporarily activate the hourglass cursor.
class TOGeneral_RAII_HourGlass
{
static HourGlassCounter;
public:
TOGeneral_RAII_HourGlass()
{
++HourGlassCounter;

Screen->Cursor=crHourGlass;
}
~TOGeneral_RAII_HourGlass()
{
if( HourGlassCounter )
--HourGlassCounter;

if( !HourGlassCounter )
Screen->Cursor=crDefault;
}
};

usage (and it supports recursion):

{
TOGeneral_RAII_HourGlass hourGlass;

... do something
}

/////////////////

template<typename TRAIIEnabler>class TGeneral_RAII_Enable
{
TRAIIEnabler * MyObjectToControl;
bool MyInitialValueF;
public:
TGeneral_RAII_Enable( TRAIIEnabler * ObjectToControl,
bool DesiredValueF )
{
MyObjectToControl=ObjectToControl;

if( MyObjectToControl )
{
MyInitialValueF=ObjectToControl->Enabled;
MyObjectToControl->Enabled=DesiredValueF;
}
}

virtual ~TGeneral_RAII_Enable()
{
if( MyObjectToControl )
MyObjectToControl->Enabled=MyInitialValueF;
}
};

usage:

{
TGeneral_RAII_Enable<TSpeedButton>
speedButton1Disabler( SpeedButton1, false );
}

///////////////////

template<typename RAIIBeginEndUpdate>class
TGeneral_RAII_BeginEndUpdate
{
RAIIBeginEndUpdate *MyObjectToControl;
public:
TGeneral_RAII_BeginEndUpdate( RAIIBeginEndUpdate
*ObjectToControl )
{
ObjectToControl->BeginUpdate();
MyObjectToControl=ObjectToControl;
}

virtual ~TGeneral_RAII_BeginEndUpdate()
{
MyObjectToControl->EndUpdate();
}
};

usage:

{
TGeneral_RAII_BeginEndUpdate<TListItems>
listItems( lvwEntries->Items );

lvwEntries->Items->Clear();
}
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:How to save a previous f-ion address

Quote
If you read the documentation, you will see that
OnMinimize is declared as a TNotifyEvent, so
simply declare your variable as the same:

TNotifyEvent Tmp = Application->OnMinimize;
I have read the doc but in the wrong way, sorry...
Best regards,
Vladimir Stefanovic