From b9203ee84953006547f4afd58f405874c87bf0dc Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:02:17 +0200 Subject: 3.1 --- shared/guid.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 shared/guid.cpp (limited to 'shared/guid.cpp') diff --git a/shared/guid.cpp b/shared/guid.cpp new file mode 100644 index 00000000..70282efa --- /dev/null +++ b/shared/guid.cpp @@ -0,0 +1,114 @@ +#include "guid.h" +#include + +#ifdef FFS_WIN +#include "Objbase.h" + +#elif defined FFS_LINUX +#include "ossp_uuid/uuid++.hh" +#endif + +using namespace Utility; + + +#ifdef FFS_WIN +struct UniqueId::IntData +{ + GUID nativeRep; +}; +#elif defined FFS_LINUX +struct UniqueId::IntData +{ + uuid nativeRep; //ossp uuid +}; +#endif + + +UniqueId::UniqueId() : + pData(new IntData) +{ +#ifdef FFS_WIN + if (FAILED(::CoCreateGuid(&pData->nativeRep))) + throw std::runtime_error("Error creating UUID!"); +#elif defined FFS_LINUX + pData->nativeRep.make(UUID_MAKE_V1); +#endif +} + + +bool UniqueId::operator==(const UniqueId rhs) const +{ +#ifdef FFS_WIN + //return ::IsEqualGUID(pData->nativeRep, rhs.pData->nativeRep); -> harmonize with operator< + + const GUID& guidL = pData->nativeRep; + const GUID& guidR = rhs.pData->nativeRep; + + return guidL.Data1 == guidR.Data1 && + guidL.Data2 == guidR.Data2 && + guidL.Data3 == guidR.Data3 && + ::memcmp(guidL.Data4, guidR.Data4, sizeof(guidR.Data4)) == 0; +#elif defined FFS_LINUX + return pData->nativeRep == rhs.pData->nativeRep; +#endif +} + + +bool UniqueId::operator<(const UniqueId rhs) const +{ +#ifdef FFS_WIN + const GUID& guidL = pData->nativeRep; + const GUID& guidR = rhs.pData->nativeRep; + + if (guidL.Data1 != guidR.Data1) + return guidL.Data1 < guidR.Data1; + if (guidL.Data2 != guidR.Data2) + return guidL.Data2 < guidR.Data2; + if (guidL.Data3 != guidR.Data3) + return guidL.Data3 < guidR.Data3; + + return ::memcmp(guidL.Data4, guidR.Data4, sizeof(guidR.Data4)) < 0; +#elif defined FFS_LINUX + return pData->nativeRep < rhs.pData->nativeRep; +#endif +} + + +UniqueId::UniqueId(wxInputStream& stream) : //read + pData(new IntData) +{ +#ifdef FFS_WIN + stream.Read(&pData->nativeRep, sizeof(GUID)); +#elif defined FFS_LINUX + char buffer[UUID_LEN_BIN] = {0}; + stream.Read(buffer, sizeof(buffer)); + + pData->nativeRep.import(static_cast(buffer)); //warning: import is overloaded with void*/char*! +#endif +} + + +#ifdef FFS_LINUX +struct MallocDeleter +{ + void operator() (void* ptr) + { + free(ptr); + } +}; +#endif + + +void UniqueId::toStream(wxOutputStream& stream) const //write +{ +#ifdef FFS_WIN + stream.Write(&pData->nativeRep, sizeof(GUID)); +#elif defined FFS_LINUX + boost::shared_ptr buffer(pData->nativeRep.binary(), //caller has to "free" memory (of size UUID_LEN_BIN) + MallocDeleter()); + + stream.Write(buffer.get(), UUID_LEN_BIN); +#endif +} + + -- cgit