scclib
Stable Cloud Computing C++ Library
uuid.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_CRYPTO_UUID_H
32 #define _SCC_CRYPTO_UUID_H
33 
34 #include <string>
35 #include <ostream>
36 #include <cstring>
37 
38 namespace scc::crypto {
39 
62 class Uuid
63 {
64  std::string m_uuid;
65  void assign(const std::string&);
66 
67 public:
68  Uuid()
69  {
70  generate();
71  }
72 
73  virtual ~Uuid()
74  {
75  explicit_bzero(m_uuid.data(), m_uuid.size());
76  }
77  Uuid(Uuid&&) = delete; // move not allowed
78  Uuid(Uuid& b)
79  {
80  m_uuid = b.m_uuid;
81  }
83  Uuid& operator =(const Uuid& b)
84  {
85  m_uuid = b.m_uuid;
86  return *this;
87  }
92  Uuid& operator =(const std::string& b)
93  {
94  assign(b);
95  return *this;
96  }
97 
102  Uuid(const std::string& s)
103  {
104  assign(s);
105  }
106 
111  std::string generate();
112 
114  operator std::string() const { return m_uuid; }
115 
117  std::string val() const { return m_uuid; }
118 
120  bool operator ==(const Uuid &b) const
121  {
122  return m_uuid == b.m_uuid;
123  }
125  bool operator !=(const Uuid &b) const
126  {
127  return m_uuid != b.m_uuid;
128  }
129 
131  static const std::string zero;
132 };
133 
135 void PrintTo(const Uuid&, std::ostream*);
136 
140 }
141 
143 std::ostream& operator <<(std::ostream&, const scc::crypto::Uuid&);
144 
145 #endif
Universally unique identifier (uuid).
Definition: uuid.h:63
Uuid(const std::string &s)
String initializer.
Definition: uuid.h:102
bool operator!=(const Uuid &b) const
Not equals operator.
Definition: uuid.h:125
std::string val() const
Return the uuid value.
Definition: uuid.h:117
bool operator==(const Uuid &b) const
Equality operator.
Definition: uuid.h:120
Uuid & operator=(const Uuid &b)
Copy assignment operator.
Definition: uuid.h:83
static const std::string zero
Zero uuid is 00000000-0000-0000-000000000000.
Definition: uuid.h:131
std::string generate()
Generate a new uuid.
Definition: uuid.cc:95
void PrintTo(const Uuid &, std::ostream *)
Googletest printer.
Definition: uuid.cc:48
std::ostream & operator<<(std::ostream &, const scc::crypto::Uuid &)
Print the uuid to stream.
Definition: uuid.cc:44