#ifndef __JLANG__JUUID__ #define __JLANG__JUUID__ #include #include #include #include "JLang/JComparable.hh" /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * Simple wrapper for UUID. */ struct JUUID : public JComparable { static const int BUFFER_SIZE = 36; //!< number of characters for I/O of uuid_t without trailing '\0', see e.g.\ man uuid_parse /** * Default constructor. */ JUUID() { clear(); } /** * Copy constructor. * * \param object UUID */ JUUID(const uuid_t& object) { uuid_copy(this->uuid, object); } /** * Copy constructor. * * \param object UUID */ JUUID(const JUUID& object) { uuid_copy(this->uuid, object.uuid); } /** * Randomizde this UUID. * * \return this UUID */ const JUUID& operator()() { uuid_generate_random(this->uuid); return *this; } /** * Generate random UUID. * * \return UUID */ static inline const JUUID& rndm() { static JUUID id; return id(); } /** * Check validity. * * \return true if valid; else false */ inline bool is_valid() const { return uuid_is_null(this->uuid) == 0; } /** * Clear UUID. */ inline void clear() { return uuid_clear(this->uuid); } /** * Less than method. * * \param object UUID * \return true if this UUID less than given UUID; else false */ inline bool less(const JUUID& object) const { return uuid_compare(this->uuid, object.uuid) < 0; } /** * Extract UUID. * * \param buffer UUID * \return UUID */ static JUUID valueOf(const std::string& buffer) { JUUID object; uuid_parse(buffer.c_str(), object.uuid); return object; } /** * Read object identifier from input. * * \param in input stream * \param object object identifier * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JUUID& object) { char buffer[BUFFER_SIZE + 1]; if (in.read(buffer, BUFFER_SIZE)) { buffer[BUFFER_SIZE] = '\0'; uuid_parse(buffer, object.uuid); } return in; } /** * Write object identifier to output. * * \param out output stream * \param object object identifier * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JUUID& object) { char buffer[BUFFER_SIZE + 1]; uuid_unparse_lower(object.uuid, buffer); return out.write(buffer, BUFFER_SIZE); } uuid_t uuid; }; } #endif