forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid.cpp
50 lines (42 loc) · 1.41 KB
/
id.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "nmos/id.h"
#include <boost/uuid/name_generator.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/string_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "cpprest/basic_utils.h"
namespace nmos
{
struct id_generator::impl_t
{
boost::uuids::random_generator gen;
};
id_generator::id_generator()
: impl(new impl_t)
{
}
id_generator::~id_generator()
{
// explicitly defined so that impl_t is a complete type for the unique_ptr destructor
}
namespace details
{
template <typename StringT> StringT to(const boost::uuids::uuid& u);
template <> inline std::string to(const boost::uuids::uuid& u) { return boost::uuids::to_string(u); }
template <> inline std::wstring to(const boost::uuids::uuid& u) { return boost::uuids::to_wstring(u); }
}
id id_generator::operator()()
{
return details::to<id>(impl->gen());
}
// generate a random number-based UUID (v4)
// note, when creating multiple UUIDs, using a generator can be more efficient depending on platform and dependencies
id make_id()
{
return id_generator()();
}
// generate a name-based UUID (v5)
id make_repeatable_id(id namespace_id, const utility::string_t& name)
{
return details::to<id>(boost::uuids::name_generator(boost::uuids::string_generator()(namespace_id))(name));
}
}