// Copyright (c) 2005-2007 Hartmut Kaiser (hartmut.kaiser@gmail.com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef SAGA_IMPL_ENGINE_UUID_HPP #define SAGA_IMPL_ENGINE_UUID_HPP #include #include #include #include #include #include // FIXME: impl should only get pulled in cpp -- move implementation of methods // into cpp /////////////////////////////////////////////////////////////////////////////// namespace saga { namespace impl { /////////////////////////////////////////////////////////////////////////////// class uuid { private: struct destroy_uuid { void operator() (saga_uuid_t * to_destroy) { saga_uuid_destroy (to_destroy); } }; friend struct destroy_uuid; TR1::shared_ptr uuid_; public: uuid (void) { create (); //std::cout << this->string()<< std::endl; } uuid (char const * uuid_str) { create (uuid_str); } std::string string (void) const { char buffer[SAGA_UUID_LEN_STR + 1]; void * str = buffer; size_t len = sizeof (buffer); saga_uuid_rc_t uuid_rc = saga_uuid_export (uuid_.get (), SAGA_UUID_FMT_STR, & str, &len); if ( SAGA_UUID_RC_OK != uuid_rc ) { SAGA_OSSTREAM strm; strm << "saga::uuid: couldn't export UUID to string, error was " << saga_uuid_error (uuid_rc); // NoSuccess SAGA_THROW(SAGA_OSSTREAM_GETSTRING (strm), saga::NoSuccess); } std::string result ((char *) buffer); return (result); } // create new uuid from scratch void create (void) { saga_uuid_t * uuid_new = NULL; saga_uuid_rc_t uuid_rc = SAGA_UUID_RC_OK; if ( SAGA_UUID_RC_OK != (uuid_rc = saga_uuid_create (&uuid_new)) || SAGA_UUID_RC_OK != (uuid_rc = saga_uuid_make ( uuid_new, SAGA_UUID_MAKE_SYSTEM)) ) { saga_uuid_destroy (uuid_new); SAGA_OSSTREAM strm; strm << "saga::uuid: couldn't create new UUID, error was " << saga_uuid_error (uuid_rc); // NoSuccess SAGA_THROW(SAGA_OSSTREAM_GETSTRING (strm), saga::NoSuccess); } uuid_.reset (uuid_new, destroy_uuid ()); } // create new uuid from string void create (char const * str, size_t len = 0) { saga_uuid_t * uuid_new = NULL; saga_uuid_rc_t uuid_rc = SAGA_UUID_RC_OK; if ( 0 == len ) { len = strlen (str); } if ( SAGA_UUID_RC_OK != (uuid_rc = saga_uuid_create (&uuid_new)) || SAGA_UUID_RC_OK != (uuid_rc = saga_uuid_import ( uuid_new, SAGA_UUID_FMT_STR, str, len)) ) { saga_uuid_destroy (uuid_new); SAGA_OSSTREAM strm; strm << "saga::uuid: couldn't create UUID from uuid, error was " << saga_uuid_error (uuid_rc); // NoSuccess SAGA_THROW(SAGA_OSSTREAM_GETSTRING (strm), saga::NoSuccess); } uuid_.reset (uuid_new, destroy_uuid()); } // comparison operators friend bool operator== (uuid const & lhs, uuid const & rhs) { int result = 0; saga_uuid_rc_t uuid_rc = saga_uuid_compare (lhs.uuid_.get (), rhs.uuid_.get (), &result); if ( SAGA_UUID_RC_OK != uuid_rc ) { SAGA_OSSTREAM strm; strm << "saga::uuid: couldn't compare two UUIDs, error was " << saga_uuid_error (uuid_rc); // NoSuccess SAGA_THROW_VERBATIM(saga::object(), SAGA_OSSTREAM_GETSTRING (strm), saga::NoSuccess); } return (0 == result) ? true : false; } friend bool operator!= (uuid const & lhs, uuid const & rhs) { return ( ! (lhs == rhs) ); } friend bool operator< (uuid const & lhs, uuid const & rhs) { int result = 0; saga_uuid_rc_t uuid_rc = saga_uuid_compare (lhs.uuid_.get (), rhs.uuid_.get (), &result); if ( SAGA_UUID_RC_OK != uuid_rc ) { SAGA_OSSTREAM strm; strm << "saga::uuid: couldn't compare two UUIDs, error was " << saga_uuid_error (uuid_rc); // NoSuccess SAGA_THROW_VERBATIM(saga::object(), SAGA_OSSTREAM_GETSTRING (strm), saga::NoSuccess); } return (result < 0) ? true : false; } friend bool operator> (uuid const & lhs, uuid const & rhs) { int result = 0; saga_uuid_rc_t uuid_rc = saga_uuid_compare (lhs.uuid_.get (), rhs.uuid_.get (), &result); if ( SAGA_UUID_RC_OK != uuid_rc ) { SAGA_OSSTREAM strm; strm << "saga::uuid: couldn't compare two UUIDs, error was " << saga_uuid_error (uuid_rc); // NoSuccess SAGA_THROW_VERBATIM(saga::object(), SAGA_OSSTREAM_GETSTRING (strm), saga::NoSuccess); } return (result > 0) ? true : false; } friend bool operator<= (uuid const & lhs, uuid const & rhs) { return ( ! (lhs > rhs) ); } friend bool operator>= (uuid const & lhs, uuid const & rhs) { return ( ! (lhs < rhs) ); } // streaming operators friend std::ostream & operator<< (std::ostream & ostrm, uuid const & rhs) { return ostrm << rhs.string (); } friend std::istream & operator>> (std::istream & istrm, uuid & rhs) { std::string instr; istrm >> instr; rhs.create (instr.c_str (), instr.size ()); return istrm; } }; uuid const null_uuid = uuid("00000000-0000-0000-0000-000000000000"); /////////////////////////////////////////////////////////////////////////////// } } // namespace saga::impl #endif // SAGA_SAGA_ENGINE_UUID_UUID_HPP