From 14818fc34de983e80da851fbd3c3887c578a8ab4 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 1 Apr 2019 19:41:19 +0300
Subject: [PATCH] Make UUID work in Windows.

---
 net/cpp/include/ftl/uuid.hpp   | 33 ++++++++++++++++++++++++++-------
 net/cpp/test/p2p_base_unit.cpp |  2 ++
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/net/cpp/include/ftl/uuid.hpp b/net/cpp/include/ftl/uuid.hpp
index 4726ebd3b..e8bca1702 100644
--- a/net/cpp/include/ftl/uuid.hpp
+++ b/net/cpp/include/ftl/uuid.hpp
@@ -1,7 +1,12 @@
 #ifndef _FTL_UUID_HPP_
 #define _FTL_UUID_HPP_
 
+#ifndef WIN32
 #include <uuid/uuid.h>
+#else
+#include <Rpc.h>
+#endif
+
 #include <memory>
 #include <string>
 #include <functional>
@@ -13,25 +18,35 @@ namespace ftl {
 	 */
 	class UUID {
 		public:
-		UUID() { uuid_generate(uuid_); };
-		UUID(int u) { memset(uuid_,u,16); };
-		UUID(const UUID &u) { memcpy(uuid_,u.uuid_,16); }
+		UUID() {
+#ifdef WIN32
+			::UuidCreate(&uuid_);
+#else
+			uuid_generate(uuid_);
+#endif
+		};
+		UUID(int u) { memset(&uuid_,u,16); };
+		UUID(const UUID &u) { memcpy(&uuid_,&u.uuid_,16); }
 		
-		bool operator==(const UUID &u) const { return memcmp(uuid_,u.uuid_,16) == 0; }
-		bool operator!=(const UUID &u) const { return memcmp(uuid_,u.uuid_,16) != 0; }
+		bool operator==(const UUID &u) const { return memcmp(&uuid_,&u.uuid_,16) == 0; }
+		bool operator!=(const UUID &u) const { return memcmp(&uuid_,&u.uuid_,16) != 0; }
 		
 		/**
 		 * Get a raw data string.
 		 */
-		std::string str() const { return std::string((char*)uuid_,16); };
-		const unsigned char *raw() const { return &uuid_[0]; }
+		std::string str() const { return std::string((char*)&uuid_,16); };
+		const unsigned char *raw() const { return (const unsigned char*)&uuid_; }
 		
 		/**
 		 * Get a pretty string.
 		 */
 		std::string to_string() const {
 			char b[37];
+#ifdef WIN32
+			UuidToString(&uuid_, (RPC_CSTR*)b);
+#else
 			uuid_unparse(uuid_, b);
+#endif
 			return std::string(b);
 		}
 		
@@ -39,7 +54,11 @@ namespace ftl {
 		MSGPACK_DEFINE(uuid_);
 		
 		private:
+#ifdef WIN32
+		_GUID uuid_;
+#else
 		unsigned char uuid_[16];
+#endif
 	};
 };
 
diff --git a/net/cpp/test/p2p_base_unit.cpp b/net/cpp/test/p2p_base_unit.cpp
index c07c5593d..656f8ddc7 100644
--- a/net/cpp/test/p2p_base_unit.cpp
+++ b/net/cpp/test/p2p_base_unit.cpp
@@ -5,7 +5,9 @@
 #include <memory>
 #include <iostream>
 
+#ifndef WIN32
 #include <sys/select.h>
+#endif
 
 using ftl::net::Dispatcher;
 using ftl::net::Protocol;
-- 
GitLab