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

Revision 56, 2.7 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) 2008 Max Samukha. All rights reserved
4
5         license:        BSD style: $(LICENSE)
6
7         authors:        Max Samukha
8
9         This module defines functions that generate UUIDs using a generator provided by
10         the target platrorm. UUIDs returned by these functions may deviate from the RFC-4122 standard.
11
12         It is recommended to use these functions if there are no special requirements to
13         the uniqueness of generated UUIDs, generation speed and standards compliance.
14
15         Portability_Notes:
16         Requires $(I libuuid.a) on Linux and $(I Rpcrt4.lib) on Windows.
17
18 *******************************************************************************/
19 module tango.scrapple.util.uuid.NativeUuidGen;
20
21 public import tango.scrapple.util.uuid.Uuid;
22
23 version (Posix)
24 {
25     version (darwin) {}
26     else
27     {
28         version (build)
29         {
30             pragma(link, "uuid");
31         }
32         else
33             pragma(lib, "libuuid.a");
34     }
35
36     extern (C)
37     {
38         void uuid_generate_time(Uuid* uuid);
39         void uuid_generate_random(Uuid* uuid);
40     }
41 }
42 else version (Windows)
43 {
44     version (build)
45     {
46         pragma (link, "Rpcrt4");
47     }
48     else
49         pragma (lib, "Rpcrt4.lib");
50
51     extern (Windows)
52     {
53         int UuidCreate(Uuid* uuid);
54         int UuidCreateSequential(Uuid* uuid);
55     }
56 }
57 else
58     static assert(false, "Platform is not supported.");
59
60 /*******************************************************************************
61     Generates a UUID using the safest method available on the target platform.
62
63     Example:
64 ----
65 auto uuid = newUuid();
66 ----
67  *******************************************************************************/
68 Uuid newUuid()
69 {
70     Uuid uuid;
71
72     version (Posix)
73     {
74         uuid_generate_random(&uuid);
75     }
76     else // Windows
77     {
78         UuidCreate(&uuid);
79     }
80
81     return uuid;
82 }
83
84 /*******************************************************************************
85     Generates a time-based (version 1) UUID if supported by the target platform.
86     Otherwise, is the same as $(DDOC_PSYMBOL newUuid).
87
88     Example:
89 ----
90 auto uuid = newTimeUuid();
91 ----
92 *******************************************************************************/
93 Uuid newTimeUuid()
94 {
95     Uuid uuid;
96
97     version (Posix)
98     {
99         uuid_generate_time(&uuid);
100     }
101     else // Windows
102     {
103         UuidCreateSequential(&uuid); // TODO: not supported on windows < 0x500;
104     }
105
106     return uuid;
107 }
108
109 debug (UuidUnitTest)
110 {
111     import tango.io.Stdout;
112     unittest
113     {
114         assert (newUuid != newUuid);
115         assert (newTimeUuid != newTimeUuid);
116     }
117 }
Note: See TracBrowser for help on using the browser.