root/trunk/tango/scrapple/util/uuid/RandomUuidGen.d

Revision 56, 3.4 kB (checked in by maxter, 8 months ago)

Removed the time-based generator, sys and ipc packages. Added more API documentation.

Line 
1 /*******************************************************************************
2
3         copyright:      Copyright (c) 2007 Max Samukha. All rights reserved
4
5         license:        BSD style: $(LICENSE)
6
7         author:         Max Samukha
8
9         Random (version 4) UUID generator. The likelihood of duplicates is
10         relatively high with this type of generator. Try to avoid using it
11         in production.
12
13 *******************************************************************************/
14 module tango.scrapple.util.uuid.RandomUuidGen;
15 public import tango.scrapple.util.uuid.Uuid;
16 import tango.stdc.stdlib : crand = rand, srand;
17 import tango.stdc.time;
18
19 static this()
20 {
21     srand(time(null));
22 }
23
24 /*******************************************************************************
25     Represents a random UUID generator.
26     Params:
27         Rand        = Randomizer type. The rendomizer type must be a callable type
28                       taking no arguments and returning int or uint.
29
30     Examples:
31 ----
32 import tango.math.Random;
33
34 auto random = new Random;
35
36 // Create a random UUID generator that will use
37 // Tango's randomizer.
38 auto randGen = RandomUuidGen!(typeof(&random.next))(&random.next);
39
40 // Generate a random UUID.
41 auto uuid = randGen();
42 ----
43 *******************************************************************************/
44 struct RandomUuidGen(Rand)
45 {
46     /*****************************************************************
47         Randomizer.
48     *****************************************************************/
49     Rand rand;
50
51     /*****************************************************************
52         Creates a new generator instance, which will use the supplied
53         randomizer.
54
55         Params:
56             rand        = Randomizer.
57     *****************************************************************/
58     static RandomUuidGen opCall(Rand rand)
59     {
60         RandomUuidGen ru;
61         ru.rand = rand;
62         return ru;
63     }
64
65     /*****************************************************************
66         Generates a random UUID. Not thread-safe.
67     *****************************************************************/
68     Uuid opCall()
69     {
70         Uuid result = void;
71
72         uint* ptr = cast(uint*)&result;
73
74         ptr[0] = rand();
75         ptr[1] = rand();
76         ptr[2] = rand();
77         ptr[3] = rand();
78
79         result.versionTimeHigh = (result.versionTimeHigh & 0x0FFF) | (UuidVersion.Random << 12);
80         result.variantClockSeqHigh = (result.variantClockSeqHigh | 0b1100_0000) & 0b1011_1111;
81
82         return result;
83     }
84 }
85
86 /******************************************************************************
87     Generates a random UUID using the C runtime's random number generator.
88
89 Example:
90 ----
91 auto uuid = newUuid();
92 ----
93 ******************************************************************************/
94 const RandomUuidGen!(typeof(&crand)) newUuid = { &crand };
95
96 debug (UuidUnitTest)
97 {
98     import tango.math.Random;
99     import tango.io.Stdout;
100
101     unittest
102     {
103         auto random = new Random;
104         auto randGen = RandomUuidGen!(typeof(&random.next))(&random.next);
105
106         auto uuid = randGen();
107         assert(uuid.ver == UuidVersion.Random);
108         assert(uuid.variant == UuidVariant.Standard);
109
110         uuid = newUuid();
111         assert(uuid.ver == UuidVersion.Random);
112         assert(uuid.variant == UuidVariant.Standard);
113     }
114 }
Note: See TracBrowser for help on using the browser.