Boost logo

Boost :

From: Andy (atompkins_at_[hidden])
Date: 2007-06-05 15:49:53


"Jos Hickson" <jos.hickson_at_[hidden]> wrote in
news:fc03d05a0706050741r3dfff77fqc5f26a5fb9a6adac_at_[hidden]:

> On 05/06/07, Peter Dimov <pdimov_at_[hidden]> wrote:
>> Jos Hickson wrote:
>>
>> > Understood. However, if the best implementation of uuid::create()
>> > depends on which of two usage patterns is assumed ("batch" or "one
>> > per program") maybe a better approach is to have both
>> > uuid_generator (for batch purposes) and uuid::create() (for the
>> > one-per-program usage) with the generator seeded using the
>> > randomness that uuid::create() uses.
>>
>> We can consider dropping the two forms of uuid::create entirely and
>> only provide uuid_generator. The discussion has demonstrated that
>> they have some non-obvious pitfalls (one is slower than expected, the
>> other assumes that the user has supplied a good seed and this is
>> unlikely).
>>
>> class uuid_generator
>> {
>> unsigned rd_[5];
>> mt19937 en_;
>>
>> uuid_generator()
>> {
>> init rd_;
>> seed en_ with rd_[4];
>> }
>>
>> operator()
>> {
>> generate a 128-bit number using en_;
>> xor it with rd_[0..3];
>> return a uuid based on it;
>> }
>> };
>>
>> seems to address both issues.
>>
>
> Yes, that's probably a better idea but if both versions of create()
> are dropped then should there be some means of providing a different
> engine for those who really want to?
>
> Jos
>

Yes. Change uuid_generator to be a template

template <typename Engine>
class uuid_generator
{
    unsigned rd_[5];
    Engine en_;

    uuid_generator()
    {
        init rd_;
        seed en_ with rd_[4];
    }

    uuid operator()
    {
        generate a 128-bit number using en_;
        xor it with rd_[0..3];
        return a uuid based on it;
    }
};

But, how does the xor part help? It is just for the case where we are
only producing one uuid?

Also it is possible that one chooses to use a random number generator
that can't be seeded (for example based on /dev/urandom), and I'm not
sure how this fits in. Actually, this could just be a specialization.

Andy.


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