src/utilities/core/UUID.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "UUID.hpp" | ||
| 7 | #include "String.hpp" | ||
| 8 | |||
| 9 | #include <sstream> | ||
| 10 | |||
| 11 | #include <boost/uuid/uuid_io.hpp> | ||
| 12 | #include <boost/uuid/uuid_generators.hpp> | ||
| 13 | #include <boost/thread/tss.hpp> | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | 5817994 | UUID::UUID() : boost::uuids::uuid(boost::uuids::nil_uuid()) {} | |
| 18 | |||
| 19 | 2040377 | UUID::UUID(const boost::uuids::uuid& t_other) : boost::uuids::uuid(t_other) {} | |
| 20 | |||
| 21 | 883987 | UUID UUID::random_generate() { | |
| 22 |
2/2✓ Branch 0 taken 4075 times.
✓ Branch 1 taken 879912 times.
|
883987 | static thread_local boost::uuids::random_generator gen; |
| 23 |
2/2✓ Branch 1 taken 883987 times.
✓ Branch 4 taken 883987 times.
|
883987 | return UUID(gen()); |
| 24 | } | ||
| 25 | |||
| 26 | 1163405 | UUID UUID::string_generate(const std::string& t_str) { | |
| 27 | static thread_local boost::uuids::string_generator gen; | ||
| 28 |
2/2✓ Branch 1 taken 1156390 times.
✓ Branch 4 taken 1156390 times.
|
1163405 | return UUID(gen(t_str)); |
| 29 | } | ||
| 30 | |||
| 31 | 883987 | UUID createUUID() { | |
| 32 | 883987 | return UUID::random_generate(); | |
| 33 | } | ||
| 34 | |||
| 35 | 1163405 | UUID toUUID(const std::string& str) { | |
| 36 | try { | ||
| 37 |
1/1✓ Branch 1 taken 1156390 times.
|
1163405 | return UUID::string_generate(str); |
| 38 | 7015 | } catch (...) { | |
| 39 |
1/1✓ Branch 1 taken 7015 times.
|
7015 | return {}; |
| 40 | 7015 | } | |
| 41 | } | ||
| 42 | |||
| 43 | // Finds Version 4 uuid in a string including {} | ||
| 44 | 665 | const boost::regex& uuidInString() { | |
| 45 |
4/7✓ Branch 0 taken 170 times.
✓ Branch 1 taken 495 times.
✓ Branch 3 taken 170 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 170 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
665 | static const boost::regex result("(\\{[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}\\})"); |
| 46 | 665 | return result; | |
| 47 | } | ||
| 48 | |||
| 49 | 7051 | bool operator!=(const UUID& lhs, const UUID& rhs) { | |
| 50 | 7051 | return static_cast<const boost::uuids::uuid&>(lhs) != static_cast<const boost::uuids::uuid&>(rhs); | |
| 51 | } | ||
| 52 | |||
| 53 | 3117265 | bool operator<(const UUID& lhs, const UUID& rhs) { | |
| 54 | 3117265 | return static_cast<const boost::uuids::uuid&>(lhs) < static_cast<const boost::uuids::uuid&>(rhs); | |
| 55 | } | ||
| 56 | |||
| 57 | 4109138798 | bool operator==(const UUID& lhs, const UUID& rhs) { | |
| 58 | 4109138798 | return static_cast<const boost::uuids::uuid&>(lhs) == static_cast<const boost::uuids::uuid&>(rhs); | |
| 59 | } | ||
| 60 | |||
| 61 | ✗ | bool operator>(const UUID& lhs, const UUID& rhs) { | |
| 62 | ✗ | return static_cast<const boost::uuids::uuid&>(lhs) > static_cast<const boost::uuids::uuid&>(rhs); | |
| 63 | } | ||
| 64 | |||
| 65 | 3116809 | std::string toString(const UUID& uuid) { | |
| 66 |
1/1✓ Branch 1 taken 3116809 times.
|
3116809 | std::stringstream ss; |
| 67 |
1/1✓ Branch 1 taken 3116809 times.
|
3116809 | ss << '{'; |
| 68 |
1/1✓ Branch 1 taken 3116809 times.
|
3116809 | boost::uuids::operator<<(ss, uuid); |
| 69 |
1/1✓ Branch 1 taken 3116809 times.
|
3116809 | ss << '}'; |
| 70 |
1/1✓ Branch 1 taken 3116809 times.
|
6233618 | return ss.str(); |
| 71 | 3116809 | } | |
| 72 | |||
| 73 | 1 | std::string createUniqueName(const std::string& prefix) { | |
| 74 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::stringstream ss; |
| 75 |
1/2✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
|
1 | if (!prefix.empty()) { |
| 76 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | ss << prefix << " "; |
| 77 | } | ||
| 78 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | ss << toString(createUUID()); |
| 79 |
1/1✓ Branch 1 taken 1 time.
|
2 | return ss.str(); |
| 80 | 1 | } | |
| 81 | |||
| 82 | 78170 | std::string removeBraces(const UUID& uuid) { | |
| 83 |
1/1✓ Branch 1 taken 78170 times.
|
78170 | std::stringstream ss; |
| 84 |
1/1✓ Branch 1 taken 78170 times.
|
78170 | boost::uuids::operator<<(ss, uuid); |
| 85 |
1/1✓ Branch 1 taken 78170 times.
|
156340 | return ss.str(); |
| 86 | 78170 | } | |
| 87 | |||
| 88 | 392254 | std::ostream& operator<<(std::ostream& os, const UUID& uuid) { | |
| 89 |
2/2✓ Branch 1 taken 392254 times.
✓ Branch 4 taken 392254 times.
|
392254 | os << toString(uuid); |
| 90 | 392254 | return os; | |
| 91 | } | ||
| 92 | |||
| 93 | } // namespace openstudio | ||
| 94 |