Leosac  0.8.0
Open Source Access Control
Uuid.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <boost/lexical_cast.hpp>
23 #include <boost/uuid/random_generator.hpp>
24 #include <boost/uuid/uuid.hpp>
25 #include <boost/uuid/uuid_io.hpp>
26 #include <odb/core.hxx>
27 #include <string>
28 
29 namespace Leosac
30 {
34 #pragma db value
35 class UUID
36 {
37  public:
38  UUID() = default;
39 
40  UUID(const UUID &) = default;
41 
42  explicit UUID(const boost::uuids::uuid &uuid)
43  : uuid_(uuid)
44  {
45  }
46 
47  bool operator==(const UUID &other) const
48  {
49  return uuid_ == other.uuid_;
50  }
51 
52  bool operator!=(const UUID &other) const
53  {
54  return uuid_ != other.uuid_;
55  }
56 
57  bool is_nil() const
58  {
59  return uuid_.is_nil();
60  }
61 
62  std::string to_string() const
63  {
64  return boost::lexical_cast<std::string>(uuid_);
65  }
66 
70  static UUID null_uuid()
71  {
72  UUID u{};
73  assert(u.uuid_.is_nil());
74  return u;
75  }
76 
77  static UUID random_uuid()
78  {
79  boost::uuids::uuid uuid = boost::uuids::random_generator()();
80  return UUID(uuid);
81  }
82 
83  bool operator<(const UUID &o) const
84  {
85  return uuid_ < o.uuid_;
86  }
87 
88  private:
89  boost::uuids::uuid uuid_;
90 
91  friend class odb::access;
92 };
93 }
Leosac::UUID::UUID
UUID()=default
Leosac::UUID::null_uuid
static UUID null_uuid()
Returns a null UUID with a full zero value.
Definition: Uuid.hpp:70
Leosac::UUID::random_uuid
static UUID random_uuid()
Definition: Uuid.hpp:77
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::UUID::access
friend class odb::access
Definition: Uuid.hpp:91
Leosac::UUID::uuid_
boost::uuids::uuid uuid_
Definition: Uuid.hpp:89
Leosac::UUID::operator<
bool operator<(const UUID &o) const
Definition: Uuid.hpp:83
Leosac::UUID::is_nil
bool is_nil() const
Definition: Uuid.hpp:57
Leosac::UUID::to_string
std::string to_string() const
Definition: Uuid.hpp:62
Leosac::UUID::operator!=
bool operator!=(const UUID &other) const
Definition: Uuid.hpp:52
Leosac::UUID::UUID
UUID(const boost::uuids::uuid &uuid)
Definition: Uuid.hpp:42
Leosac::UUID::operator==
bool operator==(const UUID &other) const
Definition: Uuid.hpp:47
Leosac::UUID
Thin wrapper around boost::uuids::uuid.
Definition: Uuid.hpp:35