Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Uid Class Reference

Unique identifier for distributed systems. More...

#include <uid.H>

Public Member Functions

 Uid ()=default
 Default constructor.
 
 Uid (const Aleph::IPv4_Address &_ipAddr, const uint64_t &_counter, const uint32_t &_port_number)
 Construct UID from components.
 
 Uid (const Aleph::IPv4_Address &_ipAddr, const unsigned int &_counter, const unsigned int &_port_number)
 Backward-compatible constructor with legacy parameter types.
 
 Uid (char *str)
 Construct UID from string representation.
 
bool operator== (const Uid &uid) const noexcept
 Compare two UIDs for equality.
 
char * getStringUid (char *str, const size_t &size) const
 Convert UID to hexadecimal string representation.
 
const Aleph::IPv4_AddressgetIpAddr () const noexcept
 Get IP address component.
 
const uint32_t & get_port_number () const noexcept
 Get port number component.
 
const uint64_t & get_counter () const noexcept
 Get counter component.
 
const uint64_t & get_random_number () const noexcept
 Get a random number component.
 
void print () const
 Print UID components to standard output.
 

Static Public Attributes

static constexpr int stringSize
 Required buffer size for string representation.
 

Private Member Functions

char * stringficate (char *buffer, const size_t &src_size) const
 Convert UID components to hexadecimal string.
 
void destringficate (char *str)
 Parse hexadecimal string and populate UID components.
 

Private Attributes

Aleph::IPv4_Address ipAddr = 0
 IPv4 address component.
 
uint32_t port_number = 0
 Port number component.
 
uint64_t counter = 0
 Counter component.
 
uint64_t random_number = 0
 Random number component.
 

Friends

struct Uid_Offsets
 

Detailed Description

Unique identifier for distributed systems.

The Uid class generates and manages globally unique identifiers by combining:

  • IPv4 address (32 bits)
  • Port number (32 bits)
  • Counter (64 bits)
  • Random number (64 bits)

This combination ensures uniqueness even across multiple machines and processes running simultaneously in a distributed system.

String Representation

UIDs can be converted to/from hexadecimal string format for storage and transmission. The string size is fixed at stringSize bytes.

Use Cases

  • Distributed database keys
  • Message identifiers in distributed systems
  • Session identifiers
  • Transaction identifiers
Thread Safety
Not thread-safe. Use external synchronization if sharing Uid objects across threads.
Example
Aleph::IPv4_Address my_ip = 0xC0A80164u; // 192.168.1.100
Uid id(my_ip, 12345, 8080);
char buffer[Uid::stringSize];
id.getStringUid(buffer, sizeof(buffer));
std::cout << "UID: " << buffer << '\n';
// Reconstruct from string
Uid id2(buffer);
assert(id == id2);
Unique identifier for distributed systems.
Definition uid.H:95
static constexpr int stringSize
Required buffer size for string representation.
Definition uid.H:109
uint32_t IPv4_Address
Type alias for IPv4 addresses stored as 32-bit integers.
Definition ahDefs.H:89

Definition at line 94 of file uid.H.

Constructor & Destructor Documentation

◆ Uid() [1/4]

Uid::Uid ( )
default

Default constructor.

Creates a zero-initialized UID. All components (ipAddr, port_number, counter, random_number) are set to 0. Components can be overwritten later via assignment or other methods.

Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Referenced by destringficate(), and stringficate().

◆ Uid() [2/4]

Uid::Uid ( const Aleph::IPv4_Address _ipAddr,
const uint64_t &  _counter,
const uint32_t &  _port_number 
)

Construct UID from components.

Creates a unique identifier from the provided components.

Parameters
_ipAddrIPv4 address of the originating machine
_countersequence counter (typically incremented per UID)
_port_numberport number of the generating process
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 92 of file uid.C.

References random_number.

◆ Uid() [3/4]

Uid::Uid ( const Aleph::IPv4_Address _ipAddr,
const unsigned int &  _counter,
const unsigned int &  _port_number 
)
inline

Backward-compatible constructor with legacy parameter types.

This overload preserves source compatibility with older code that used unsigned int for the counter and port number. It delegates to the constructor taking uint64_t and uint32_t.

Deprecated:
Use Uid(const Aleph::IPv4_Address&, const uint64_t&, const uint32_t&) instead.
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 166 of file uid.H.

◆ Uid() [4/4]

Uid::Uid ( char *  str)

Construct UID from string representation.

Reconstructs a UID from its hexadecimal string format.

Parameters
strhexadecimal string (must be at least stringSize bytes)
Exceptions
std::invalid_argumentif the string format is invalid
Note
Complexity: O(1) (fixed size parsing)
Exception safety: strong

Definition at line 112 of file uid.C.

References ah_invalid_argument_if, destringficate(), and stringSize.

Member Function Documentation

◆ destringficate()

void Uid::destringficate ( char *  str)
private

Parse hexadecimal string and populate UID components.

Parameters
strinput string in hexadecimal format

Definition at line 82 of file uid.C.

References Uid(), and unhexadecimalize().

Referenced by Uid().

◆ get_counter()

const uint64_t & Uid::get_counter ( ) const
inlinenoexcept

Get counter component.

Returns
the counter value
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 232 of file uid.H.

References counter.

◆ get_port_number()

const uint32_t & Uid::get_port_number ( ) const
inlinenoexcept

Get port number component.

Returns
the port number
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 224 of file uid.H.

References port_number.

◆ get_random_number()

const uint64_t & Uid::get_random_number ( ) const
inlinenoexcept

Get a random number component.

Returns
the random number
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 240 of file uid.H.

References random_number.

Referenced by main().

◆ getIpAddr()

const Aleph::IPv4_Address & Uid::getIpAddr ( ) const
inlinenoexcept

Get IP address component.

Returns
the IPv4 address
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 216 of file uid.H.

References ipAddr.

◆ getStringUid()

char * Uid::getStringUid ( char *  str,
const size_t &  size 
) const

Convert UID to hexadecimal string representation.

Generates a string representation suitable for storage or transmission.

Parameters
stroutput buffer (must be at least stringSize bytes)
sizesize of output buffer
Returns
pointer to the output buffer
Exceptions
std::range_errorif buffer size is insufficient
Note
Complexity: O(1) (fixed size generation)
Exception safety: strong

Definition at line 136 of file uid.C.

References ah_range_error_if, stringficate(), and stringSize.

◆ operator==()

bool Uid::operator== ( const Uid uid) const
noexcept

Compare two UIDs for equality.

Two UIDs are equal if all their components match.

Parameters
uidthe UID to compare against
Returns
true if UIDs are identical, false otherwise
Exceptions
none
Note
Complexity: O(1)
Exception safety: noexcept

Definition at line 128 of file uid.C.

◆ print()

void Uid::print ( ) const
inline

Print UID components to standard output.

Prints all four components in human-readable format for debugging.

Definition at line 246 of file uid.H.

References counter, ipAddr, port_number, and random_number.

Referenced by main().

◆ stringficate()

char * Uid::stringficate ( char *  buffer,
const size_t &  src_size 
) const
private

Convert UID components to hexadecimal string.

Parameters
bufferoutput buffer for string
src_sizesize of output buffer
Returns
pointer to buffer

Definition at line 62 of file uid.C.

References Uid(), ah_range_error_if, and hexadecimalize().

Referenced by getStringUid().

Friends And Related Symbol Documentation

◆ Uid_Offsets

friend struct Uid_Offsets
friend

Definition at line 96 of file uid.H.

Member Data Documentation

◆ counter

uint64_t Uid::counter = 0
private

Counter component.

Definition at line 100 of file uid.H.

Referenced by get_counter(), and print().

◆ ipAddr

Aleph::IPv4_Address Uid::ipAddr = 0
private

IPv4 address component.

Definition at line 98 of file uid.H.

Referenced by getIpAddr(), and print().

◆ port_number

uint32_t Uid::port_number = 0
private

Port number component.

Definition at line 99 of file uid.H.

Referenced by get_port_number(), and print().

◆ random_number

uint64_t Uid::random_number = 0
private

Random number component.

Definition at line 101 of file uid.H.

Referenced by Uid(), get_random_number(), and print().

◆ stringSize

constexpr int Uid::stringSize
staticconstexpr
Initial value:
= 2 * (sizeof(uint32_t) + sizeof(uint32_t) +
sizeof(uint64_t) + sizeof(uint64_t)) + 1

Required buffer size for string representation.

The string representation uses hexadecimal encoding, requiring 2 characters per byte plus null terminator.

Definition at line 109 of file uid.H.

Referenced by Uid(), getStringUid(), TEST(), and TEST().


The documentation for this class was generated from the following files: