// Copyright Paul A. Bristow. // Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // libs/uuid/example/example.cpp -------------------------------// #define _CRT_SECURE_NO_WARNINGS // h:\uuid\boost\uuid\seed_rng.hpp(132) : warning C4996: 'fopen': // This function or variable may be unsafe. Consider using fopen_s instead. // To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. // c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(237) : // see declaration of 'fopen' #define _SCL_SECURE_NO_WARNINGS #include #include #include #include #include using std::cout; using std::endl; using std::string; using std::vector; using std::copy; using std::ostream_iterator; int main() { using boost::uuids::uuid; using boost::uuids::uuid_generator; using boost::uuids::showbraces; using boost::uuids::noshowbraces; uuid u; // Default constructor makes a null string - boring! cout << u << endl; // 00000000-0000-0000-0000-000000000000 if (u.is_null()) { cout << "uuid is null!" << endl; // uuid is null! } // Create from a std string. string s1("01234567-89ab-cdef-0123-456789abcdef"); uuid us1(s1); cout << us1 << endl; // 01234567-89ab-cdef-0123-456789abcdef // Or a C string. const char* s2 = "{76543210-89ab-cdef-0123-456789abcdef}"; uuid uc1(s2); cout << uc1 << endl; // 76543210-89ab-cdef-0123-456789abcdef // and also show {} braces around string. cout << showbraces << uc1 << endl; // {76543210-89ab-cdef-0123-456789abcdef} cout << uc1 << endl; // {76543210-89ab-cdef-0123-456789abcdef} // Get at the UUID individual bytes. vector v(u.size()); // Always 16. copy(us1.begin(), us1.end(), v.begin()); cout << "Length in bytes is " << v.size() << ", and values are: " << endl; for (int i = 0; i < 16; i++) { cout << int(v[i]) << " "; } // 1 35 69 103 -119 -85 -51 -17 1 35 69 103 -119 -85 -51 -17 cout << endl; // Create uuid from name. uuid dns_namespace_uuid("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); const char* url = "www.boost.org"; uuid uurl = boost::uuids::uuid::create(dns_namespace_uuid, url, 13); cout << uurl << endl; string suurl = uurl.to_string(); cout << suurl << endl; // ccb046db-74eb-53c9-9b6b-3ce5740dd29e // Generate uuid from random number, different every time. uuid_generator ur; // cout << showbraces << ur() << endl; // {f850857f-86cb-4518-ba45-c826aac45a03} cout << ur() << endl; // {b04004aa-c261-4a91-a79f-357d71780d0c} cout << ur() << endl; // {b305b7e1-80ef-4fa9-beed-c26b86a08cbb} // Note showbraces 'sticks' until noshowbraces. cout << noshowbraces << ur() << endl; // b305b7e1-80ef-4fa9-beed-c26b86a08cbb uuid u1 = ur(); uuid u2 = ur(); if (u1 == u2) { cout << "Ooops - UUIDs should be unique!" << endl; } } // int main() /* Output: 1>Autorun "h:\uuid\libs\uuid\Example\Example\Debug\Example.exe" 1>00000000-0000-0000-0000-000000000000 1>uuid is null! 1>01234567-89ab-cdef-0123-456789abcdef 1>76543210-89ab-cdef-0123-456789abcdef 1>{76543210-89ab-cdef-0123-456789abcdef} 1>{76543210-89ab-cdef-0123-456789abcdef} 1>Length in bytes is 16, and values are: 1>1 35 69 103 -119 -85 -51 -17 1 35 69 103 -119 -85 -51 -17 1>{ccb046db-74eb-53c9-9b6b-3ce5740dd29e} 1>ccb046db-74eb-53c9-9b6b-3ce5740dd29e 1>{d0b211fd-6e42-4112-8270-dd470f0ccb08} 1>{6fa9bc27-ee4b-4d52-ac83-0868e94698ab} 1>{9fc020e5-54ea-4e42-b716-7b8c89e5b5f0} 1>b3e3936b-e6f1-4909-915a-8c7a4aba76a6 1>Build Time 0:02 */