Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2005-02-01 13:23:38


On 2/1/05 1:50 AM, "Gennadiy Rozental" <gennadiy.rozental_at_[hidden]>
wrote:

>>> I want to save state once at the beggining of something and restore it
>>> multiple times after some steps that may pollute io state.
>>
>> Nope, the classes are pure-RAII. The classes either save or do an "atomic"
>> save-and-change in the constructor and restore in the destructor. There are
>> no member functions you could even use for arbitrarily-timed restoring. If
>> you could abuse the RAII paradigm to do this, then....
>
> Could I make an object during startup and then force restore by making copy
> and destroying one immediately?

Pure-RAII classes don't use anything besides their constructors and
destructor, so I never bothered to check how copying worked. Copying a RAII
class can only work if it stores a pointer/reference to its changeable
object and the old aspect values. (I think my classes do this, but you're
depending on an implementation detail, so don't do it!)

>> BTW, why would you need to do this unusual kind of multiple restoring in the
>> first place?
>
> I want to save state of cout (or any other current log/report stream for that
> matter) at the start of test case/framework run and restore it after each
> assertion?

What's wrong with wrapping each trial in a block?

void my_test()
{
    {
        saver_type s( cout );
        // do trial 1...
    }

    {
        saver_type s( cout );
        // do trial 2...
    }

    for ( int i = 3 ; i < 10 ; ++i )
    {
        saver_type s( cout );
        // do one of the trials 3 through 9...
    }

    //...
}

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk