Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
uid.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44# ifndef UID_H
45# define UID_H
46
47# include <stdint.h>
48# include <iostream>
49# include <aleph.H>
50
94class Uid
95{
96 friend struct Uid_Offsets;
97
99 uint32_t port_number = 0;
100 uint64_t counter = 0;
101 uint64_t random_number = 0;
102
103public:
109 static constexpr int stringSize = 2 * (sizeof(uint32_t) + sizeof(uint32_t) +
110 sizeof(uint64_t) + sizeof(uint64_t)) + 1;
111
112private:
118 char * stringficate(char *buffer, const size_t & src_size) const;
119
123 void destringficate(char *str);
124
125public:
136 Uid() = default;
137
149 Uid(const Aleph::IPv4_Address & _ipAddr,
150 const uint64_t & _counter,
151 const uint32_t & _port_number);
152
165 [[deprecated("Use Uid(const Aleph::IPv4_Address&, const uint64_t&, const uint32_t&) instead")]]
166 Uid(const Aleph::IPv4_Address & _ipAddr,
167 const unsigned int & _counter,
168 const unsigned int & _port_number)
169 : Uid(_ipAddr,
170 static_cast<uint64_t>(_counter),
171 static_cast<uint32_t>(_port_number))
172 {}
173
183 Uid(char *str);
184
195 bool operator ==(const Uid & uid) const noexcept;
196
208 char * getStringUid(char *str, const size_t & size) const;
209
216 [[nodiscard]] const Aleph::IPv4_Address &getIpAddr() const noexcept { return ipAddr; }
217
224 [[nodiscard]] const uint32_t &get_port_number() const noexcept { return port_number; }
225
232 [[nodiscard]] const uint64_t &get_counter() const noexcept { return counter; }
233
240 [[nodiscard]] const uint64_t &get_random_number() const noexcept { return random_number; }
241
246 void print() const
247 {
248 std::cout << "Uid:" << '\n'
249 << " ipAddr = " << ipAddr << '\n'
250 << " port_number = " << port_number << '\n'
251 << " counter = " << counter << '\n'
252 << " random_number = " << random_number << '\n'
253 << '\n';
254 }
255};
256
257# endif // UID_H
Core header for the Aleph-w library.
Unique identifier for distributed systems.
Definition uid.H:95
static constexpr int stringSize
Required buffer size for string representation.
Definition uid.H:109
const Aleph::IPv4_Address & getIpAddr() const noexcept
Get IP address component.
Definition uid.H:216
void print() const
Print UID components to standard output.
Definition uid.H:246
const uint64_t & get_random_number() const noexcept
Get a random number component.
Definition uid.H:240
const uint64_t & get_counter() const noexcept
Get counter component.
Definition uid.H:232
char * stringficate(char *buffer, const size_t &src_size) const
Convert UID components to hexadecimal string.
Definition uid.C:62
Aleph::IPv4_Address ipAddr
IPv4 address component.
Definition uid.H:98
char * getStringUid(char *str, const size_t &size) const
Convert UID to hexadecimal string representation.
Definition uid.C:136
uint32_t port_number
Port number component.
Definition uid.H:99
Uid(const Aleph::IPv4_Address &_ipAddr, const unsigned int &_counter, const unsigned int &_port_number)
Backward-compatible constructor with legacy parameter types.
Definition uid.H:166
uint64_t counter
Counter component.
Definition uid.H:100
uint64_t random_number
Random number component.
Definition uid.H:101
friend struct Uid_Offsets
Definition uid.H:96
const uint32_t & get_port_number() const noexcept
Get port number component.
Definition uid.H:224
void destringficate(char *str)
Parse hexadecimal string and populate UID components.
Definition uid.C:82
Uid()=default
Default constructor.
bool operator==(const Uid &uid) const noexcept
Compare two UIDs for equality.
Definition uid.C:128
uint32_t IPv4_Address
Type alias for IPv4 addresses stored as 32-bit integers.
Definition ahDefs.H:89