• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.vscode/H29-May-2019-55

android/H03-May-2022-722508

cmake/H29-May-2019-4436

include/crossguid/H29-May-2019-15290

src/H03-May-2022-393285

test/H29-May-2019-201163

.gitattributesH A D29-May-20192.5 KiB6459

.gitignoreH A D29-May-201964 87

.travis.ymlH A D29-May-20192.1 KiB7061

LICENSEH A D29-May-20191.1 KiB2217

README.mdH A D29-May-20196.7 KiB226166

android.shH A D29-May-2019332 117

crossguid.pc.inH A D29-May-2019337 1210

README.md

1# CrossGuid [![Build Status](https://travis-ci.org/graeme-hill/crossguid.svg?branch=master)](https://travis-ci.org/graeme-hill/crossguid)
2
3CrossGuid is a minimal, cross platform, C++ GUID library. It uses the best
4native GUID/UUID generator on the given platform and has a generic class for
5parsing, stringifying, and comparing IDs. The guid generation technique is
6determined by your platform:
7
8## Linux
9
10On linux you can use `libuuid` which is pretty standard. On distros like Ubuntu
11it is available by default but to use it you need the header files so you have
12to do:
13
14    sudo apt-get install uuid-dev
15
16## Mac/iOS
17
18On Mac or iOS you can use `CFUUIDCreate` from `CoreFoundation`. Since it's a
19plain C function you don't even need to compile as Objective-C++.
20
21## Windows
22
23On Windows we just use the the built-in function `CoCreateGuid`. CMake can
24generate a Visual Studio project if that's your thing.
25
26## Android
27
28The Android version uses a handle to a `JNIEnv` object to invoke the
29`randomUUID()` function on `java.util.UUID` from C++. The Android specific code
30is all in the `android/` subdirectory. If you have an emulator already running,
31then you can run the `android.sh` script in the root directory. It has the
32following requirements:
33
34- Android emulator is already running (or you have physical device connected).
35- You're using bash.
36- adb is in your path.
37- You have an Android sdk setup including `ANDROID_HOME` environment variable.
38
39## Versions
40
41This is version 0.2 of CrossGuid. If you all already using CrossGuid and your code
42uses `GuidGenerator` then you are using version 0.1. Differences in version 0.2:
43
44- Put everything inside the namespace `xg` instead of using the global
45  namespace.
46- Removed `GuidGenerator` class and replaced with the free function
47  `xg::newGuid`. This is the way I originally wanted it to work but since Android
48  is a special snowflake requiring state (`JNIEnv *`) I introduced the
49  `GuidGenerator` class specifically so that there would be somewhere to store
50  the `JNIEnv *` when running on Android. However, this basically meant
51  complicating the library for the sake of one platform. In version 0.2 the goal is
52  to design for the normal platforms and let Android be weird. In Android you just
53  need to run `xg::initJni(JNIEnv *)` before you create any guids. The `JNIEnv *`
54  is just stored as a global variable.
55- Added CMake build system. Instead of different scripts for each platform you
56  can just run cmake and it should handle each platform (except Android which
57  again is special).
58- Actual guid bytes are stored in `std::array<unsigned char, 16>` instead of
59  `std::vector<unsigned char>`.
60- More error checking (like if you try to create a guid with invalid number of
61  bytes).
62
63If you're happily using version 0.1 then there's not really any reason to
64change.
65
66## Compiling
67
68Just do the normal cmake thing:
69
70```
71mkdir build
72cd build
73cmake ..
74make install
75```
76
77## Running tests
78
79After compiling as described above you should get two files: `libcrossguid.a` (the
80static library) and `crossguid-test` (the test runner). So to run the tests just do:
81
82```
83./crossguid-test
84```
85
86## Basic usage
87
88### Creating guids
89
90Create a new random guid:
91
92```cpp
93#include <crossguid/guid.hpp>
94...
95auto g = xg::newGuid();
96```
97
98**NOTE:** On Android you need to call `xg::initJni(JNIEnv *)` first so that it
99is possible for `xg::newGuid()` to call back into java libraries. `initJni`
100only needs to be called once when the process starts.
101
102Create a new zero guid:
103
104```cpp
105xg::Guid g;
106```
107
108Create from a string:
109
110```cpp
111xg::Guid g("c405c66c-ccbb-4ffd-9b62-c286c0fd7a3b");
112```
113
114### Checking validity
115
116If you have some string value and you need to check whether it is a valid guid
117then you can simply attempt to construct the guid:
118
119```cpp
120xg::Guid g("bad-guid-string");
121if (!g.isValid())
122{
123	// do stuff
124}
125```
126
127If the guid string is not valid then all bytes are set to zero and `isValid()`
128returns `false`.
129
130### Converting guid to string
131
132First of all, there is normally no reason to convert a guid to a string except
133for in debugging or when serializing for API calls or whatever. You should
134definitely avoid storing guids as strings or using strings for any
135computations. If you do need to convert a guid to a string, then you can
136utilize strings because the `<<` operator is overloaded. To print a guid to
137`std::cout`:
138
139```cpp
140void doGuidStuff()
141{
142    auto myGuid = xg::newGuid();
143    std::cout << "Here is a guid: " << myGuid << std::endl;
144}
145```
146
147Or to store a guid in a `std::string`:
148
149```cpp
150void doGuidStuff()
151{
152    auto myGuid = xg::newGuid();
153    std::stringstream stream;
154    stream << myGuid;
155    auto guidString = stream.str();
156}
157```
158
159There is also a `str()` function that returns a `std::string`:
160
161```cpp
162std::string guidStr = xg::newGuid().str();
163```
164
165### Creating a guid from raw bytes
166
167It's unlikely that you will need this, but this is done within the library
168internally to construct a `Guid` object from the raw data given by the system's
169built-in guid generation function. There are two key constructors for this:
170
171```cpp
172Guid(std::array<unsigned char, 16> &bytes);
173```
174
175and
176
177```cpp
178Guid(const unsigned char * bytes);
179```
180
181When possible prefer the `std::array` constructor because it is safer. If you
182pass in an incorrectly sized C array then bad things will happen.
183
184### Comparing guids
185
186`==` and `!=` are implemented, so the following works as expected:
187
188```cpp
189void doGuidStuff()
190{
191    auto guid1 = xg::newGuid();
192    auto guid2 = xg::newGuid();
193
194    auto guidsAreEqual = guid1 == guid2;
195    auto guidsAreNotEqual = guid1 != guid2;
196}
197```
198
199### Hashing guids
200
201Guids can be used directly in containers requireing `std::hash` such as `std::map,`std::unordered_map` etc.
202
203## License
204
205The MIT License (MIT)
206
207Copyright (c) 2014 Graeme Hill (http://graemehill.ca)
208
209Permission is hereby granted, free of charge, to any person obtaining a copy
210of this software and associated documentation files (the "Software"), to deal
211in the Software without restriction, including without limitation the rights
212to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
213copies of the Software, and to permit persons to whom the Software is
214furnished to do so, subject to the following conditions:
215
216The above copyright notice and this permission notice shall be included in
217all copies or substantial portions of the Software.
218
219THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
220IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
221FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
222AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
223LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
224OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
225THE SOFTWARE.
226