mirror of
https://github.com/codership/wsrep-lib.git
synced 2025-07-30 07:23:07 +03:00
Refactored seqno and id classes out of provider.hpp
This commit is contained in:
75
include/wsrep/id.hpp
Normal file
75
include/wsrep/id.hpp
Normal file
@ -0,0 +1,75 @@
|
||||
//
|
||||
// Copyright (C) 2018 Codership Oy <info@codership.com>
|
||||
//
|
||||
|
||||
/*! \file id.hpp
|
||||
*
|
||||
* A generic identifier utility class.
|
||||
*/
|
||||
#ifndef WSREP_ID_HPP
|
||||
#define WSREP_ID_HPP
|
||||
|
||||
#include "exception.hpp"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <cstring> // std::memset()
|
||||
|
||||
namespace wsrep
|
||||
{
|
||||
/*!
|
||||
* The idientifier class stores identifiers either in UUID
|
||||
* format or in string format. The storage format is decided
|
||||
* upon construction. If the given string contains a valid
|
||||
* UUID, the storage format will be binary. Otherwise the
|
||||
* string will be copied verbatim. If the string format is used,
|
||||
* the maximum length of the identifier is limited to 16 bytes.
|
||||
*/
|
||||
class id
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* Default constructor. Constructs an empty identifier.
|
||||
*/
|
||||
id() : data_() { std::memset(data_, 0, sizeof(data_)); }
|
||||
|
||||
/*!
|
||||
* Construct from string. The input string may contain either
|
||||
* valid UUID or a string with maximum 16 bytes length.
|
||||
*/
|
||||
id(const std::string&);
|
||||
|
||||
/*!
|
||||
* Construct from void pointer.
|
||||
*/
|
||||
id (const void* data, size_t size) : data_()
|
||||
{
|
||||
if (size > 16)
|
||||
{
|
||||
throw wsrep::runtime_error("Too long identifier");
|
||||
}
|
||||
std::memset(data_, 0, sizeof(data_));
|
||||
std::memcpy(data_, data, size);
|
||||
}
|
||||
|
||||
bool operator<(const id& other) const
|
||||
{
|
||||
return (std::memcmp(data_, other.data_, sizeof(data_)) < 0);
|
||||
}
|
||||
|
||||
bool operator==(const id& other) const
|
||||
{
|
||||
return (std::memcmp(data_, other.data_, sizeof(data_)) == 0);
|
||||
}
|
||||
|
||||
const void* data() const { return data_; }
|
||||
|
||||
size_t size() const { return sizeof(data_); }
|
||||
|
||||
private:
|
||||
unsigned char data_[16];
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream&, const wsrep::id& id);
|
||||
}
|
||||
|
||||
#endif // WSREP_ID_HPP
|
Reference in New Issue
Block a user