THRIFT-5773 Strong UUID wrapper for C++
Client: cpp/CMakeLists.txt
Patch: Carel Combrink

This closes #2958
diff --git a/lib/cpp/CMakeLists.txt b/lib/cpp/CMakeLists.txt
index c2f15dd..5980734 100644
--- a/lib/cpp/CMakeLists.txt
+++ b/lib/cpp/CMakeLists.txt
@@ -31,6 +31,7 @@
 set(thriftcpp_SOURCES
    src/thrift/TApplicationException.cpp
    src/thrift/TOutput.cpp
+   src/thrift/TUuid.cpp
    src/thrift/async/TAsyncChannel.cpp
    src/thrift/async/TAsyncProtocolProcessor.cpp
    src/thrift/async/TConcurrentClientSyncInfo.h
@@ -43,7 +44,6 @@
    src/thrift/protocol/TJSONProtocol.cpp
    src/thrift/protocol/TMultiplexedProtocol.cpp
    src/thrift/protocol/TProtocol.cpp
-   src/thrift/protocol/TUuidUtils.cpp
    src/thrift/transport/TTransportException.cpp
    src/thrift/transport/TFDTransport.cpp
    src/thrift/transport/TSimpleFileTransport.cpp
diff --git a/lib/cpp/Makefile.am b/lib/cpp/Makefile.am
index 12b8d82..3d7beab 100644
--- a/lib/cpp/Makefile.am
+++ b/lib/cpp/Makefile.am
@@ -57,6 +57,7 @@
 
 libthrift_la_SOURCES = src/thrift/TApplicationException.cpp \
                        src/thrift/TOutput.cpp \
+                       src/thrift/TUuid.cpp \
                        src/thrift/VirtualProfiling.cpp \
                        src/thrift/async/TAsyncChannel.cpp \
                        src/thrift/async/TAsyncProtocolProcessor.cpp \
@@ -69,7 +70,6 @@
                        src/thrift/protocol/TBase64Utils.cpp \
                        src/thrift/protocol/TMultiplexedProtocol.cpp \
                        src/thrift/protocol/TProtocol.cpp \
-                       src/thrift/protocol/TUuidUtils.cpp \
                        src/thrift/transport/TTransportException.cpp \
                        src/thrift/transport/TFDTransport.cpp \
                        src/thrift/transport/TFileTransport.cpp \
@@ -137,6 +137,7 @@
                          src/thrift/thrift-config.h \
                          src/thrift/thrift_export.h \
                          src/thrift/TDispatchProcessor.h \
+                         src/thrift/TUuid.h \
                          src/thrift/Thrift.h \
                          src/thrift/TOutput.h \
                          src/thrift/TProcessor.h \
diff --git a/lib/cpp/README.md b/lib/cpp/README.md
index 8074484..74983ae 100644
--- a/lib/cpp/README.md
+++ b/lib/cpp/README.md
@@ -232,6 +232,32 @@
 The PRNG seed is key to the application security. This method should be
 overridden if it's not strong enough for you.
 
+# Thrift UUID
+
+The `uuid` `BaseType` is implemented in C++ by the `apache::thrift::TUuid` class. This class
+is a strong wrapper class around an internal buffer of 16 bytes.
+
+The `apache::thrift::TUuid` supports construction from different UUID string representations.
+Some examples of supported string formats are:
+
+* `"hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh"`
+* `"{hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh}"`
+* `"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"`
+* `"{hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh}"`
+
+## `TUuid` and `boost::uuids::uuid`
+
+Internally the TUuid class is implemented using the `boost::uuids::uuid` library. As a result the TUuid
+can seamlessly interoperate with the boost UUID type since the underlying data structure is the same.
+
+For convenience, when boost is already used by a project the `THRIFT_TUUID_SUPPORT_BOOST_UUID` preprocessor
+directive can be set when including the thrift library to enable construction of a `TUuid` from a
+`boost::uuids::uuid`. By default this is an implicit constructor that can be changed to be explicit
+by defining the `THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT` preprocessor directive.
+
+The thrift library does not need to be compiled differently when this constructor is needed. The preprocessor
+directives can be set on the project that uses the thrift library.
+
 # Deprecations
 
 ## 0.12.0
diff --git a/lib/cpp/libthrift.vcxproj b/lib/cpp/libthrift.vcxproj
index 1b413f8..7fcccfe 100644
--- a/lib/cpp/libthrift.vcxproj
+++ b/lib/cpp/libthrift.vcxproj
@@ -49,7 +49,6 @@
     <ClCompile Include="src\thrift\protocol\TJSONProtocol.cpp" />
     <ClCompile Include="src\thrift\protocol\TMultiplexedProtocol.cpp" />
     <ClCompile Include="src\thrift\protocol\TProtocol.cpp" />
-    <ClCompile Include="src\thrift\protocol\TUuidUtils.cpp" />
     <ClCompile Include="src\thrift\server\TConnectedClient.cpp" />
     <ClCompile Include="src\thrift\server\TServer.cpp" />
     <ClCompile Include="src\thrift\server\TServerFramework.cpp" />
@@ -58,6 +57,7 @@
     <ClCompile Include="src\thrift\server\TThreadPoolServer.cpp" />
     <ClCompile Include="src\thrift\TApplicationException.cpp" />
     <ClCompile Include="src\thrift\TOutput.cpp" />
+    <ClCompile Include="src\thrift\TUuid.cpp" />
     <ClCompile Include="src\thrift\transport\SocketCommon.cpp" />
     <ClCompile Include="src\thrift\transport\TBufferTransports.cpp" />
     <ClCompile Include="src\thrift\transport\TFDTransport.cpp" />
@@ -97,6 +97,7 @@
     <ClInclude Include="src\thrift\Thrift.h" />
     <ClInclude Include="src\thrift\TOutput.h" />
     <ClInclude Include="src\thrift\TProcessor.h" />
+    <ClInclude Include="src\thrift\TUuid.h" />
     <ClInclude Include="src\thrift\transport\TBufferTransports.h" />
     <ClInclude Include="src\thrift\transport\TFDTransport.h" />
     <ClInclude Include="src\thrift\transport\TFileTransport.h" />
diff --git a/lib/cpp/libthrift.vcxproj.filters b/lib/cpp/libthrift.vcxproj.filters
index fb94f60..4b2ba3b 100644
--- a/lib/cpp/libthrift.vcxproj.filters
+++ b/lib/cpp/libthrift.vcxproj.filters
@@ -4,6 +4,7 @@
     <ClCompile Include="src\thrift\transport\TBufferTransports.cpp">
       <Filter>transport</Filter>
     </ClCompile>
+    <ClCompile Include="src\thrift\TUuid.cpp" />
     <ClCompile Include="src\thrift\TOutput.cpp" />
     <ClCompile Include="src\thrift\TApplicationException.cpp" />
     <ClCompile Include="src\thrift\transport\TTransportException.cpp">
@@ -30,9 +31,6 @@
     <ClCompile Include="src\thrift\protocol\TMultiplexedProtocol.cpp">
       <Filter>protocol</Filter>
     </ClCompile>
-    <ClCompile Include="src\thrift\protocol\TUuidUtils.cpp">
-      <Filter>protocol</Filter>
-    </ClCompile>
     <ClCompile Include="src\thrift\transport\TFDTransport.cpp">
       <Filter>transport</Filter>
     </ClCompile>
@@ -109,6 +107,7 @@
     <ClInclude Include="src\thrift\protocol\TBinaryProtocol.h">
       <Filter>protocol</Filter>
     </ClInclude>
+    <ClInclude Include="src\thrift\TUuid.h" />
     <ClInclude Include="src\thrift\Thrift.h" />
     <ClInclude Include="src\thrift\TProcessor.h" />
     <ClInclude Include="src\thrift\TApplicationException.h" />
diff --git a/lib/cpp/src/thrift/TUuid.cpp b/lib/cpp/src/thrift/TUuid.cpp
new file mode 100644
index 0000000..a0c45f9
--- /dev/null
+++ b/lib/cpp/src/thrift/TUuid.cpp
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <thrift/TUuid.h>
+
+#include <boost/uuid/string_generator.hpp>
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_io.hpp>
+
+namespace apache {
+namespace thrift {
+
+namespace {
+static const boost::uuids::string_generator gen;
+}
+
+TUuid::TUuid(const std::string& str) noexcept {
+  std::fill(this->begin(), this->end(), 0);
+  if (str.empty()) {
+    return ;
+  }
+
+  try {
+    const boost::uuids::uuid uuid{gen(str)};
+    std::copy(uuid.begin(), uuid.end(), this->begin());
+  } catch (const std::runtime_error&) {
+    // Invalid string most probably
+  }
+}
+
+bool TUuid::is_nil() const noexcept {
+  boost::uuids::uuid uuid_tmp{};
+  std::copy(this->begin(), this->end(), std::begin(uuid_tmp));
+  return uuid_tmp.is_nil();
+}
+
+std::string to_string(const TUuid& in) {
+  boost::uuids::uuid uuid_tmp{};
+  std::copy(std::begin(in), std::end(in), std::begin(uuid_tmp));
+  return boost::uuids::to_string(uuid_tmp);
+}
+
+
+}
+} // apache::thrift
\ No newline at end of file
diff --git a/lib/cpp/src/thrift/TUuid.h b/lib/cpp/src/thrift/TUuid.h
new file mode 100644
index 0000000..bcf0160
--- /dev/null
+++ b/lib/cpp/src/thrift/TUuid.h
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _THRIFT_TUUID_H_
+#define _THRIFT_TUUID_H_ 1
+
+#include <thrift/Thrift.h>
+
+#ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+#include <boost/uuid/uuid.hpp>
+#endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+
+#include <algorithm>
+
+namespace apache {
+namespace thrift {
+
+/**
+ * Thrift wrapper class for a UUID type.
+ *
+ * The UUID is stored as a 16 byte buffer.
+ * This class stores the UUID in network order when assigned from a string.
+ */
+class TUuid {
+public:
+  typedef uint8_t value_type;
+  typedef uint8_t* iterator;
+  typedef uint8_t const* const_iterator;
+  typedef std::size_t size_type;
+  typedef std::ptrdiff_t difference_type;
+
+  TUuid() = default;
+  TUuid(const TUuid& other) = default;
+  TUuid(TUuid&& other) = default;
+  TUuid& operator=(const TUuid&) = default;
+  TUuid& operator=(TUuid&&) = default;
+  ~TUuid() = default;
+
+  /**
+   * Construct the object from a 16 byte buffer.
+   */
+  explicit TUuid(const uint8_t (&data)[16]) noexcept
+  {
+    std::copy(std::begin(data), std::end(data), std::begin(this->data_));
+  }
+
+  /**
+   * Construct the object from the specified string.
+   *
+   * Supported string formats are:
+   *   - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh"
+   *   - "{hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh}"
+   *   - "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
+   *   - "{hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh}"
+   *
+   * If the string is invalid, the object will be set to a
+   * nil (empty) UUID.
+   */
+  explicit TUuid(const std::string& str) noexcept;
+
+#ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+  /**
+   * Construct the TUuid from a boost::uuids::uuid.
+   *
+   * This constructor will only be available if the <tt>THRIFT_TUUID_SUPPORT_BOOST_UUID</tt>
+   * compiler directive is set when this file is included.
+   *
+   * This constructor is by default implicit. It can be made explicit by defining the
+   * <tt>THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT</tt> compiler directive.
+   */
+  #ifdef THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT
+  explicit
+  #endif // THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT
+  TUuid(const boost::uuids::uuid& buuid) noexcept
+  {
+    std::copy(std::begin(buuid.data), std::end(buuid.data), std::begin(this->data_));
+  }
+#endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+
+  /**
+   * Check if the UUID is nil.
+   */
+  bool is_nil() const noexcept;
+
+  /**
+   * Compare two TUuid objects for equality.
+   */
+  inline bool operator==(const TUuid& other) const;
+
+  /**
+   * Compare two TUuid objects for inequality.
+   */
+  inline bool operator!=(const TUuid& other) const;
+
+  iterator begin() noexcept { return data_; }
+  const_iterator begin() const noexcept { return data_; }
+  iterator end() noexcept { return data_ + size(); }
+  const_iterator end() const noexcept { return data_ + size(); }
+  size_type size() const noexcept { return 16; }
+  inline const_iterator data() const { return data_; }
+  inline iterator data() { return data_; }
+
+  void swap(TUuid& other) noexcept { std::swap(data_, other.data_); }
+
+private:
+  /**
+   * The UUID data.
+   */
+  uint8_t data_[16] = {};
+};
+
+/**
+ * Get the String representation of a TUUID.
+ *
+ * The format returned is:
+ *   - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh"
+ */
+std::string to_string(const TUuid& uuid) noexcept(false);
+
+/**
+ * Swap two TUuid objects
+ */
+inline void swap(TUuid& lhs, TUuid& rhs) noexcept {
+  lhs.swap(rhs);
+}
+
+/**
+ * TUuid equality comparison operator implementation
+ */
+inline bool TUuid::operator==(const TUuid& other) const {
+  // Compare using temporary strings.
+  // Can't use strcmp() since we expect embeded zeros
+  // Perhaps the reason we should use std::array instead
+  return std::string(this->begin(), this->end()) == std::string(other.begin(), other.end());
+}
+
+/**
+ * TUuid inequality comparison operator implementation
+ */
+inline bool TUuid::operator!=(const TUuid& other) const {
+  return !(*this == other);
+}
+
+/**
+ * TUuid ostream stream operator implementation
+ */
+inline std::ostream& operator<<(std::ostream& out, const TUuid& obj) {
+  out << to_string(obj);
+  return out;
+}
+
+} // namespace thrift
+} // namespace apache
+
+#endif // #ifndef _THRIFT_TUUID_H_
diff --git a/lib/cpp/src/thrift/protocol/TBinaryProtocol.h b/lib/cpp/src/thrift/protocol/TBinaryProtocol.h
index 24e51f7..cba6e69 100644
--- a/lib/cpp/src/thrift/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/thrift/protocol/TBinaryProtocol.h
@@ -119,7 +119,7 @@
 
   inline uint32_t writeBinary(const std::string& str);
 
-  inline uint32_t writeUUID(const std::string& str);
+  inline uint32_t writeUUID(const TUuid& uuid);
 
   /**
    * Reading functions
@@ -168,7 +168,7 @@
 
   inline uint32_t readBinary(std::string& str);
 
-  inline uint32_t readUUID(std::string& str);
+  inline uint32_t readUUID(TUuid& uuid);
 
   int getMinSerializedSize(TType type) override;
 
diff --git a/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc b/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc
index 6a54ece..1bd7a1a 100644
--- a/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc
+++ b/lib/cpp/src/thrift/protocol/TBinaryProtocol.tcc
@@ -21,7 +21,6 @@
 #define _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ 1
 
 #include <thrift/protocol/TBinaryProtocol.h>
-#include <thrift/protocol/TUuidUtils.hpp>
 #include <thrift/transport/TTransportException.h>
 
 #include <limits>
@@ -195,16 +194,9 @@
 }
 
 template <class Transport_, class ByteOrder_>
-uint32_t TBinaryProtocolT<Transport_, ByteOrder_>::writeUUID(const std::string& str) {
-  std::string out;
-  const bool encoded = uuid_encode(str, out);
-  if(!encoded)
-    throw TProtocolException(TProtocolException::INVALID_DATA);
-  // This should not happen, but check for now
-  if(out.size() != 16)
-    throw TProtocolException(TProtocolException::UNKNOWN);
+uint32_t TBinaryProtocolT<Transport_, ByteOrder_>::writeUUID(const TUuid& uuid) {
   // TODO: Consider endian swapping, see lib/delphi/src/Thrift.Utils.pas:377
-  this->trans_->write((uint8_t*)out.data(), 16);
+  this->trans_->write(uuid.data(), uuid.size());
   return 16;
 }
 
@@ -444,10 +436,8 @@
 }
 
 template <class Transport_, class ByteOrder_>
-uint32_t TBinaryProtocolT<Transport_, ByteOrder_>::readUUID(std::string& str) {
-  std::string in;
-  readStringBody(in, 16);
-  uuid_decode(in, str);
+uint32_t TBinaryProtocolT<Transport_, ByteOrder_>::readUUID(TUuid& uuid) {
+  this->trans_->readAll(uuid.begin(), uuid.size());
   return 16;
 }
 
diff --git a/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp b/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp
index 0a2eaed..6e88313 100644
--- a/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp
+++ b/lib/cpp/src/thrift/protocol/TDebugProtocol.cpp
@@ -18,7 +18,6 @@
  */
 
 #include <thrift/protocol/TDebugProtocol.h>
-#include <thrift/protocol/TUuidUtils.hpp>
 
 #include <thrift/TToString.h>
 #include <cassert>
@@ -388,19 +387,12 @@
   return TDebugProtocol::writeString(str);
 }
 
-uint32_t TDebugProtocol::writeUUID(const string& str) {
-  std::string out_raw;
-  uuid_encode(str, out_raw);
-
-  std::string out_encoded;
-  uuid_decode(out_raw, out_encoded);
-
+uint32_t TDebugProtocol::writeUUID(const TUuid& uuid) {
   size_t size = writePlain("{\n");
   indentUp();
-  size += writeIndented("[in ] = \"" + str + "\",\n");
   size += writeIndented("[raw] = ");
-  size += writeString(out_raw);
-  size += writeIndented("[enc] = \"" + out_encoded + "\"\n");
+  size += writeString(std::string(std::begin(uuid), std::end(uuid)));
+  size += writeIndented("[enc] = \"" + to_string(uuid) + "\"\n");
   indentDown();
   size += writeIndented("}\n");
   return size;
diff --git a/lib/cpp/src/thrift/protocol/TDebugProtocol.h b/lib/cpp/src/thrift/protocol/TDebugProtocol.h
index af89cc5..02662f1 100644
--- a/lib/cpp/src/thrift/protocol/TDebugProtocol.h
+++ b/lib/cpp/src/thrift/protocol/TDebugProtocol.h
@@ -110,7 +110,7 @@
 
   uint32_t writeBinary(const std::string& str);
 
-  uint32_t writeUUID(const std::string& str);
+  uint32_t writeUUID(const TUuid& uuid);
 
 private:
   void indentUp();
diff --git a/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp b/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
index 0899f00..3805869 100644
--- a/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
+++ b/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
@@ -18,7 +18,6 @@
  */
 
 #include <thrift/protocol/TJSONProtocol.h>
-#include <thrift/protocol/TUuidUtils.hpp>
 
 #include <boost/locale.hpp>
 
@@ -717,14 +716,8 @@
   return writeJSONBase64(str);
 }
 
-uint32_t TJSONProtocol::writeUUID(const std::string& str) {
-  std::string out_raw;
-  uuid_encode(str, out_raw);
-
-  std::string out_encoded;
-  uuid_decode(out_raw, out_encoded);
-
-  return writeJSONString(out_encoded);
+uint32_t TJSONProtocol::writeUUID(const TUuid& uuid) {
+  return writeJSONString(to_string(uuid));
 }
 
 /**
@@ -1123,8 +1116,11 @@
   return readJSONBase64(str);
 }
 
-uint32_t TJSONProtocol::readUUID(std::string& str) {
-  return readJSONString(str);
+uint32_t TJSONProtocol::readUUID(TUuid& uuid) {
+  std::string uuid_str;
+  const uint32_t result = readJSONString(uuid_str);
+  uuid = TUuid{uuid_str};
+  return result;
 }
 
 // Return the minimum number of bytes a type will consume on the wire
diff --git a/lib/cpp/src/thrift/protocol/TJSONProtocol.h b/lib/cpp/src/thrift/protocol/TJSONProtocol.h
index 069a990..09eb6ea 100644
--- a/lib/cpp/src/thrift/protocol/TJSONProtocol.h
+++ b/lib/cpp/src/thrift/protocol/TJSONProtocol.h
@@ -198,7 +198,7 @@
 
   uint32_t writeBinary(const std::string& str);
 
-  uint32_t writeUUID(const std::string& str);
+  uint32_t writeUUID(const TUuid& uuid);
 
   /**
    * Reading functions
@@ -247,7 +247,7 @@
 
   uint32_t readBinary(std::string& str);
 
-  uint32_t readUUID(std::string& str);
+  uint32_t readUUID(TUuid& uuid);
 
   int getMinSerializedSize(TType type) override;
 
diff --git a/lib/cpp/src/thrift/protocol/TProtocol.h b/lib/cpp/src/thrift/protocol/TProtocol.h
index d29df1c..035b745 100644
--- a/lib/cpp/src/thrift/protocol/TProtocol.h
+++ b/lib/cpp/src/thrift/protocol/TProtocol.h
@@ -49,6 +49,7 @@
 #include <thrift/protocol/TList.h>
 #include <thrift/protocol/TSet.h>
 #include <thrift/protocol/TMap.h>
+#include <thrift/TUuid.h>
 
 #include <memory>
 
@@ -275,7 +276,7 @@
 
   virtual uint32_t writeBinary_virt(const std::string& str) = 0;
 
-  virtual uint32_t writeUUID_virt(const std::string& str) = 0;
+  virtual uint32_t writeUUID_virt(const TUuid& uuid) = 0;
 
   uint32_t writeMessageBegin(const std::string& name,
                              const TMessageType messageType,
@@ -384,9 +385,9 @@
     return writeBinary_virt(str);
   }
 
-  uint32_t writeUUID(const std::string& str) {
+  uint32_t writeUUID(const TUuid& uuid) {
     T_VIRTUAL_CALL();
-    return writeUUID_virt(str);
+    return writeUUID_virt(uuid);
   }
 
   /**
@@ -437,7 +438,7 @@
 
   virtual uint32_t readBinary_virt(std::string& str) = 0;
 
-  virtual uint32_t readUUID_virt(std::string& str) = 0;
+  virtual uint32_t readUUID_virt(TUuid& uuid) = 0;
 
   uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid) {
     T_VIRTUAL_CALL();
@@ -539,9 +540,9 @@
     return readBinary_virt(str);
   }
 
-  uint32_t readUUID(std::string& str) {
+  uint32_t readUUID(TUuid& uuid) {
     T_VIRTUAL_CALL();
-    return readUUID_virt(str);
+    return readUUID_virt(uuid);
   }
 
   /*
diff --git a/lib/cpp/src/thrift/protocol/TProtocolDecorator.h b/lib/cpp/src/thrift/protocol/TProtocolDecorator.h
index 9eb1566..88fbbde 100644
--- a/lib/cpp/src/thrift/protocol/TProtocolDecorator.h
+++ b/lib/cpp/src/thrift/protocol/TProtocolDecorator.h
@@ -92,7 +92,7 @@
   uint32_t writeDouble_virt(const double dub) override { return protocol->writeDouble(dub); }
   uint32_t writeString_virt(const std::string& str) override { return protocol->writeString(str); }
   uint32_t writeBinary_virt(const std::string& str) override { return protocol->writeBinary(str); }
-  uint32_t writeUUID_virt(const std::string& str) override { return protocol->writeUUID(str); }
+  uint32_t writeUUID_virt(const TUuid& uuid) override { return protocol->writeUUID(uuid); }
 
   uint32_t readMessageBegin_virt(std::string& name,
                                          TMessageType& messageType,
@@ -141,7 +141,7 @@
 
   uint32_t readString_virt(std::string& str) override { return protocol->readString(str); }
   uint32_t readBinary_virt(std::string& str) override { return protocol->readBinary(str); }
-  uint32_t readUUID_virt(std::string& str) override { return protocol->readUUID(str); }
+  uint32_t readUUID_virt(TUuid& uuid) override { return protocol->readUUID(uuid); }
 
 private:
   shared_ptr<TProtocol> protocol;
diff --git a/lib/cpp/src/thrift/protocol/TUuidUtils.cpp b/lib/cpp/src/thrift/protocol/TUuidUtils.cpp
deleted file mode 100644
index e1eab22..0000000
--- a/lib/cpp/src/thrift/protocol/TUuidUtils.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <thrift/protocol/TUuidUtils.hpp>
-
-#include <boost/uuid/string_generator.hpp>
-#include <boost/uuid/uuid.hpp>
-#include <boost/uuid/uuid_io.hpp>
-
-namespace apache {
-namespace thrift {
-namespace protocol {
-
-bool uuid_encode(const std::string& in, std::string& out) {
-  static const boost::uuids::string_generator gen;
-  static const std::string empty_uuid(boost::uuids::uuid::static_size(), '\0');
-  out = empty_uuid;
-  if (in.empty()) {
-    return true;
-  }
-  try {
-    const boost::uuids::uuid uuid{gen(in)};
-    std::copy(uuid.begin(), uuid.end(), out.begin());
-    return true;
-  } catch (const std::runtime_error&) {
-    // Invalid string most probably
-    return false;
-  }
-}
-
-void uuid_decode(const std::string& in, std::string& out) {
-  boost::uuids::uuid uuid{};
-  const size_t to_copy = std::min(in.size(), uuid.size());
-  std::copy(in.begin(), in.begin() + to_copy, uuid.begin());
-  out = boost::uuids::to_string(uuid);
-}
-
-}
-}
-} // apache::thrift::protocol
diff --git a/lib/cpp/src/thrift/protocol/TUuidUtils.hpp b/lib/cpp/src/thrift/protocol/TUuidUtils.hpp
deleted file mode 100644
index 583147f..0000000
--- a/lib/cpp/src/thrift/protocol/TUuidUtils.hpp
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef _THRIFT_PROTOCOL_TUUIDUTILS_H_
-#define _THRIFT_PROTOCOL_TUUIDUTILS_H_
-
-#include <string>
-
-namespace apache {
-namespace thrift {
-namespace protocol {
-
-// Encode canonical UUID string to a 16 char representation
-// Supported formats for in:
-//   - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh"
-//   - "{hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh}"
-//   - "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
-//   - "{hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh}"
-// Returns false if the string was invalid and the value was not encoded.
-bool uuid_encode(const std::string& in, std::string& out);
-
-// Decode 16 char UUID buffer to 36 characted string
-void uuid_decode(const std::string& in, std::string& out);
-
-}
-}
-} // apache::thrift::protocol
-
-#endif // #define _THRIFT_PROTOCOL_TUUIDUTILS_H_
diff --git a/lib/cpp/src/thrift/protocol/TVirtualProtocol.h b/lib/cpp/src/thrift/protocol/TVirtualProtocol.h
index 4698081..8789e47 100644
--- a/lib/cpp/src/thrift/protocol/TVirtualProtocol.h
+++ b/lib/cpp/src/thrift/protocol/TVirtualProtocol.h
@@ -393,8 +393,8 @@
     return static_cast<Protocol_*>(this)->writeBinary(str);
   }
 
-  uint32_t writeUUID_virt(const std::string& str) override {
-    return static_cast<Protocol_*>(this)->writeUUID(str);
+  uint32_t writeUUID_virt(const TUuid& uuid) override {
+    return static_cast<Protocol_*>(this)->writeUUID(uuid);
   }
 
   /**
@@ -475,8 +475,8 @@
     return static_cast<Protocol_*>(this)->readBinary(str);
   }
 
-  uint32_t readUUID_virt(std::string& str) override {
-    return static_cast<Protocol_*>(this)->readUUID(str);
+  uint32_t readUUID_virt(TUuid& uuid) override {
+    return static_cast<Protocol_*>(this)->readUUID(uuid);
   }
 
   uint32_t skip_virt(TType type) override { return static_cast<Protocol_*>(this)->skip(type); }
diff --git a/lib/cpp/test/Benchmark.cpp b/lib/cpp/test/Benchmark.cpp
index ba8c345..22ec86c 100644
--- a/lib/cpp/test/Benchmark.cpp
+++ b/lib/cpp/test/Benchmark.cpp
@@ -65,7 +65,7 @@
   ooe.some_characters = "JSON THIS! \"\1";
   ooe.zomg_unicode = "\xd7\n\a\t";
   ooe.base64 = "\1\2\3\255";
-  ooe.rfc4122_uuid = "{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}";
+  ooe.rfc4122_uuid = apache::thrift::TUuid{"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}"};
 
   int num = 100000;
   std::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer(num*1000));
diff --git a/lib/cpp/test/CMakeLists.txt b/lib/cpp/test/CMakeLists.txt
index 87ed109..5ad8d74 100644
--- a/lib/cpp/test/CMakeLists.txt
+++ b/lib/cpp/test/CMakeLists.txt
@@ -82,6 +82,7 @@
     TServerSocketTest.cpp
     TServerTransportTest.cpp
     ThrifttReadCheckTests.cpp
+    TUuidTest.cpp
 )
 
 add_executable(UnitTests ${UnitTest_SOURCES})
@@ -94,6 +95,25 @@
     set_property( TARGET UnitTests APPEND_STRING PROPERTY COMPILE_FLAGS /wd4503 )
 endif()
 
+# Test the THRIFT_TUUID_SUPPORT_BOOST_UUID compiler directive globally set on the target
+add_executable(UnitTestsUuid
+    UnitTestMain.cpp
+    TUuidTestBoost.cpp
+)
+target_link_libraries(UnitTestsUuid testgencpp ${Boost_LIBRARIES})
+target_link_libraries(UnitTestsUuid thrift)
+target_compile_definitions(UnitTestsUuid PUBLIC THRIFT_TUUID_SUPPORT_BOOST_UUID)
+add_test(NAME UnitTestsUuid COMMAND UnitTestsUuid)
+
+# Test not setting the THRIFT_TUUID_SUPPORT_BOOST_UUID compiler directive as with the test above.
+# The test does set the directive before including the thrift header to test the behaviour
+add_executable(UnitTestsUuidNoDirective
+    UnitTestMain.cpp
+    TUuidTestBoostNoDirective.cpp
+)
+target_link_libraries(UnitTestsUuidNoDirective testgencpp ${Boost_LIBRARIES})
+target_link_libraries(UnitTestsUuidNoDirective thrift)
+add_test(NAME UnitTestsUuidNoDirective COMMAND UnitTestsUuidNoDirective)
 
 set( TInterruptTest_SOURCES
      TSocketInterruptTest.cpp
diff --git a/lib/cpp/test/DebugProtoTest.cpp b/lib/cpp/test/DebugProtoTest.cpp
index cc4e5ff..44eeef7 100644
--- a/lib/cpp/test/DebugProtoTest.cpp
+++ b/lib/cpp/test/DebugProtoTest.cpp
@@ -75,7 +75,6 @@
     "    [2] = 3,\n"
     "  },\n"
     "  15: rfc4122_uuid (uuid) = {\n"
-    "    [in ] = \"\",\n"
     "    [raw] = \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n"
     "    [enc] = \"00000000-0000-0000-0000-000000000000\"\n"
     "  }\n"
@@ -103,7 +102,7 @@
                                "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
                                "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
                                "\xbc";
-  n->my_ooe.rfc4122_uuid = "{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}";
+  n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}"};
   n->my_bonk.type = 31337;
   n->my_bonk.message = "I am a bonk... xor!";
 }
@@ -148,7 +147,6 @@
     "      [2] = 3,\n"
     "    },\n"
     "    15: rfc4122_uuid (uuid) = {\n"
-    "      [in ] = \"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}\",\n"
     "      [raw] = \"^*\\xb1\\x88\\x17&Nu\\xa0O\\x1e\\xd9\\xa6\\xa8\\x9cL\",\n"
     "      [enc] = \"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c\"\n"
     "    }\n"
@@ -240,7 +238,6 @@
     "        [2] = 3,\n"
     "      },\n"
     "      15: rfc4122_uuid (uuid) = {\n"
-    "        [in ] = \"\",\n"
     "        [raw] = \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n"
     "        [enc] = \"00000000-0000-0000-0000-000000000000\"\n"
     "      }\n"
@@ -276,7 +273,6 @@
     "        [2] = 3,\n"
     "      },\n"
     "      15: rfc4122_uuid (uuid) = {\n"
-    "        [in ] = \"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}\",\n"
     "        [raw] = \"^*\\xb1\\x88\\x17&Nu\\xa0O\\x1e\\xd9\\xa6\\xa8\\x9cL\",\n"
     "        [enc] = \"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c\"\n"
     "      }\n"
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
index fedf99e..b96638e 100644
--- a/lib/cpp/test/JSONProtoTest.cpp
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -48,7 +48,7 @@
   ooe->some_characters = "JSON THIS! \"\1";
   ooe->zomg_unicode = "\xd7\n\a\t";
   ooe->base64 = "\1\2\3\255";
-  ooe->rfc4122_uuid = "00000000-0000-0000-0000-000000000000";
+  ooe->rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"};
 }
 
 BOOST_AUTO_TEST_CASE(test_json_proto_1) {
@@ -85,7 +85,7 @@
                                "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
                                "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
                                "\xbc";
-  n->my_ooe.rfc4122_uuid = "5e2ab188-1726-4e75-a04f-1ed9a6a89c4c";
+  n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
   n->my_bonk.type = 31337;
   n->my_bonk.message = "I am a bonk... xor!";
 }
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
index a744039..adb923a 100644
--- a/lib/cpp/test/Makefile.am
+++ b/lib/cpp/test/Makefile.am
@@ -83,6 +83,8 @@
 
 check_PROGRAMS = \
 	UnitTests \
+	UnitTestsUuid \
+	UnitTestsUuidNoDirective \
 	TFDTransportTest \
 	TPipedTransportTest \
 	DebugProtoTest \
@@ -131,7 +133,8 @@
 	TServerSocketTest.cpp \
 	TServerTransportTest.cpp \
 	TTransportCheckThrow.h \
-	ThrifttReadCheckTests.cpp
+	ThrifttReadCheckTests.cpp \
+	TUuidTest.cpp
 
 UnitTests_LDADD = \
   libtestgencpp.la \
@@ -139,6 +142,30 @@
   $(BOOST_SYSTEM_LDADD) \
   $(BOOST_THREAD_LDADD)
 
+UnitTestsUuid_SOURCES = \
+	UnitTestMain.cpp \
+	TUuidTestBoost.cpp
+
+UnitTestsUuid_LDADD = \
+  libtestgencpp.la \
+  $(BOOST_TEST_LDADD) \
+  $(BOOST_SYSTEM_LDADD) \
+  $(BOOST_THREAD_LDADD)
+
+UnitTestsUuid_CPPFLAGS = \
+   $(AM_CPPFLAGS) \
+  -DTHRIFT_TUUID_SUPPORT_BOOST_UUID
+
+UnitTestsUuidNoDirective_SOURCES = \
+	UnitTestMain.cpp \
+	TUuidTestBoostNoDirective.cpp
+
+UnitTestsUuidNoDirective_LDADD = \
+  libtestgencpp.la \
+  $(BOOST_TEST_LDADD) \
+  $(BOOST_SYSTEM_LDADD) \
+  $(BOOST_THREAD_LDADD)
+
 TInterruptTest_SOURCES = \
 	TSocketInterruptTest.cpp \
 	TSSLSocketInterruptTest.cpp
@@ -386,7 +413,7 @@
 gen-cpp/AnnotationTest_constants.cpp gen-cpp/AnnotationTest_constants.h gen-cpp/AnnotationTest_types.cpp gen-cpp/AnnotationTest_types.h: $(top_srcdir)/test/AnnotationTest.thrift
 	$(THRIFT) --gen cpp $<
 
-gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h: $(top_srcdir)/test/v0.16/DebugProtoTest.thrift
+gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h gen-cpp/EmptyService.cpp gen-cpp/EmptyService.h: $(top_srcdir)/test/DebugProtoTest.thrift
 	$(THRIFT) --gen cpp $<
 
 gen-cpp/DoubleConstantsTest_constants.cpp gen-cpp/DoubleConstantsTest_constants.h: $(top_srcdir)/test/DoubleConstantsTest.thrift
@@ -408,7 +435,7 @@
 gen-cpp/Service.cpp gen-cpp/StressTest_types.cpp: $(top_srcdir)/test/StressTest.thrift
 	$(THRIFT) --gen cpp $<
 
-gen-cpp/SecondService.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp gen-cpp/ThriftTest_types.h: $(top_srcdir)/test/v0.16/ThriftTest.thrift
+gen-cpp/SecondService.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp gen-cpp/ThriftTest_types.h: $(top_srcdir)/test/ThriftTest.thrift
 	$(THRIFT) --gen cpp $<
 
 gen-cpp/OneWayService.cpp gen-cpp/OneWayTest_types.h gen-cpp/OneWayService.h: OneWayTest.thrift
diff --git a/lib/cpp/test/SpecializationTest.cpp b/lib/cpp/test/SpecializationTest.cpp
index 0976112..7256c20 100644
--- a/lib/cpp/test/SpecializationTest.cpp
+++ b/lib/cpp/test/SpecializationTest.cpp
@@ -26,7 +26,7 @@
   ooe.some_characters = "JSON THIS! \"\1";
   ooe.zomg_unicode = "\xd7\n\a\t";
   ooe.base64 = "\1\2\3\255";
-  ooe.rfc4122_uuid = "00000000-0000-0000-0000-000000000000";
+  ooe.rfc4122_uuid = apache::thrift::TUuid{"00000000-0000-0000-0000-000000000000"};
 
   Nesting n;
   n.my_ooe = ooe;
diff --git a/lib/cpp/test/TUuidTest.cpp b/lib/cpp/test/TUuidTest.cpp
new file mode 100644
index 0000000..4a521cf
--- /dev/null
+++ b/lib/cpp/test/TUuidTest.cpp
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#include <boost/test/unit_test.hpp>
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/string_generator.hpp>
+
+#include <thrift/TUuid.h>
+
+using apache::thrift::TUuid;
+
+BOOST_AUTO_TEST_SUITE(TUuidTest)
+
+BOOST_AUTO_TEST_CASE(construction) {
+    BOOST_TEST(TUuid().is_nil());
+}
+
+BOOST_AUTO_TEST_CASE(construction_string_valid) {
+  const std::string expected_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+
+  BOOST_TEST(to_string(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")) == expected_1);
+  BOOST_TEST(to_string(TUuid("{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}")) == expected_1);
+  BOOST_TEST(to_string(TUuid("{5e2ab18817264e75a04f1ed9a6a89c4c}")) == expected_1);
+  BOOST_TEST(to_string(TUuid("5e2ab18817264e75a04f1ed9a6a89c4c")) == expected_1);
+}
+
+BOOST_AUTO_TEST_CASE(construction_string_invalid) {
+  // This test also ensures that the constructor does not throw
+  const std::string expected{"00000000-0000-0000-0000-000000000000"};
+
+  BOOST_TEST(to_string(TUuid("5e2ab188-1726-4e75-a04f")) == expected);
+  BOOST_TEST(to_string(TUuid("{}")) == expected);
+  BOOST_TEST(to_string(TUuid("{5e2ab18817264e75a04f1ed9a6a89c4c")) == expected);
+  BOOST_TEST(to_string(TUuid("5e2ab18817264e75a04f1ed9a689c4c")) == expected);
+}
+
+BOOST_AUTO_TEST_CASE(compare) {
+  BOOST_TEST(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")
+             == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+  BOOST_TEST(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")
+             != TUuid("00000000-1726-4e75-a04f-1ed9a6a89c4c"));
+  BOOST_TEST(TUuid("{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}")
+             == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+
+  // This comparison is expected to fail if strcmp is used
+  TUuid uuid_1{};
+  TUuid uuid_2{};
+  uuid_2.data()[15] = 0x64;
+  BOOST_TEST(uuid_1 != uuid_2);
+}
+
+BOOST_AUTO_TEST_CASE(assign_valid) {
+  TUuid uuid_1{};
+  BOOST_TEST(uuid_1.is_nil());
+  uuid_1 = TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  BOOST_TEST(!uuid_1.is_nil());
+
+  BOOST_TEST(uuid_1 == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+
+  uuid_1 = TUuid{"{12345678-1726-4e75-a04f-1ed9a6a89c4c}"};
+  BOOST_TEST(uuid_1 != TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
+  BOOST_TEST(uuid_1 == TUuid("{12345678-1726-4e75-a04f-1ed9a6a89c4c}"));
+}
+
+BOOST_AUTO_TEST_CASE(assign_invalid) {
+  TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  BOOST_TEST(!uuid_1.is_nil());
+
+  BOOST_CHECK_NO_THROW(uuid_1 = TUuid{"123"});
+  BOOST_TEST(uuid_1.is_nil());
+  BOOST_TEST(to_string(uuid_1) == std::string{"00000000-0000-0000-0000-000000000000"});
+}
+
+BOOST_AUTO_TEST_CASE(swap) {
+  TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  TUuid uuid_2{};
+  BOOST_TEST(!uuid_1.is_nil());
+  BOOST_TEST(uuid_2.is_nil());
+
+  using std::swap;
+  swap(uuid_1, uuid_2);
+
+  BOOST_TEST(uuid_1.is_nil());
+  BOOST_TEST(!uuid_2.is_nil());
+
+  BOOST_TEST(to_string(uuid_1) == std::string{"00000000-0000-0000-0000-000000000000"});
+  BOOST_TEST(to_string(uuid_2) == std::string{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"});
+}
+
+BOOST_AUTO_TEST_CASE(begin_end) {
+  TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  BOOST_TEST(std::distance(std::begin(uuid_1), std::end(uuid_1)) == uuid_1.size());
+}
+
+BOOST_AUTO_TEST_CASE(into_boost_uuid) {
+  TUuid uuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  boost::uuids::uuid boost_uuid{};
+  BOOST_TEST(boost_uuid.is_nil());
+  std::copy(std::begin(uuid), std::end(uuid), boost_uuid.begin());
+  BOOST_TEST(!boost_uuid.is_nil());
+  BOOST_TEST(boost::uuids::to_string(boost_uuid) == "5e2ab188-1726-4e75-a04f-1ed9a6a89c4c");
+  BOOST_TEST(boost::uuids::to_string(boost_uuid) == to_string(uuid));
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  TUuid uuid;
+  BOOST_TEST(uuid.is_nil());
+
+  std::copy(std::begin(boost_uuid), std::end(boost_uuid), uuid.begin());
+  BOOST_TEST(!uuid.is_nil());
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+}
+
+BOOST_AUTO_TEST_CASE(test_byte_order_variant) {
+  TUuid uuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
+  boost::uuids::uuid boost_uuid{};
+  BOOST_TEST(boost_uuid.is_nil());
+  std::copy(std::begin(uuid), std::end(uuid), boost_uuid.begin());
+  BOOST_TEST(!boost_uuid.is_nil());
+  BOOST_TEST(boost_uuid.variant() == boost::uuids::uuid::variant_rfc_4122);
+}
+
+BOOST_AUTO_TEST_CASE(test_byte_order_verify_network) {
+  const TUuid uuid{"{00112233-4455-6677-8899-aabbccddeeff}"};
+
+  for (uint8_t idx = 0; idx < uuid.size(); ++idx) {
+    const uint8_t expected = idx * 0x11;
+    BOOST_TEST(*(std::begin(uuid) + idx) == expected);
+  }
+
+  const uint8_t test[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                            0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
+
+  TUuid new_uuid;
+  std::copy(std::begin(test), std::end(test), std::begin(new_uuid));
+
+  BOOST_TEST(!new_uuid.is_nil());
+  BOOST_TEST(to_string(new_uuid) == std::string{"00112233-4455-6677-8899-aabbccddeeff"});
+
+  BOOST_TEST(new_uuid == uuid);
+}
+
+BOOST_AUTO_TEST_CASE(test_character_buffer) {
+
+  const uint8_t test[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                            0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
+
+  const TUuid uuid{test};
+
+  BOOST_TEST(to_string(uuid) == std::string{"00112233-4455-6677-8899-aabbccddeeff"});
+}
+
+BOOST_AUTO_TEST_CASE(test_boost_buffer) {
+
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+
+  const TUuid uuid{boost_uuid.data};
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/lib/cpp/test/TUuidTestBoost.cpp b/lib/cpp/test/TUuidTestBoost.cpp
new file mode 100644
index 0000000..81c3559
--- /dev/null
+++ b/lib/cpp/test/TUuidTestBoost.cpp
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#include <boost/test/unit_test.hpp>
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/string_generator.hpp>
+
+#include <thrift/TUuid.h>
+
+using apache::thrift::TUuid;
+
+BOOST_AUTO_TEST_SUITE(TUuidBoostTest)
+
+BOOST_AUTO_TEST_CASE(compiler_directive) {
+    // Test if the macro is set as expected
+    #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+    BOOST_TEST(true);
+    #else
+    BOOST_TEST(false, "The 'THRIFT_TUUID_SUPPORT_BOOST_UUID' preprocessor directive must be set for these tests");
+    #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_constructor) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  const TUuid uuid{boost_uuid};
+  BOOST_TEST(!uuid.is_nil());
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"1f610073-db33-4d21-adf2-75460d4955cc"});
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_assignment) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("5cb719a4-cd15-4476-8bcc-f1834b2527ee")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  TUuid uuid{};
+  BOOST_TEST(uuid.is_nil());
+
+  uuid = boost_uuid;
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"5cb719a4-cd15-4476-8bcc-f1834b2527ee"});
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/lib/cpp/test/TUuidTestBoostNoDirective.cpp b/lib/cpp/test/TUuidTestBoostNoDirective.cpp
new file mode 100644
index 0000000..8af9c35
--- /dev/null
+++ b/lib/cpp/test/TUuidTestBoostNoDirective.cpp
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_SUITE(TUuidBoostTestNoDirective)
+
+BOOST_AUTO_TEST_CASE(compiler_directive_not_set) {
+    // Test if the macro is set as expected
+    #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+    BOOST_TEST(false, "The 'THRIFT_TUUID_SUPPORT_BOOST_UUID' preprocessor must NOT be set for these tests");
+    #else
+    BOOST_TEST(true);
+    #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+// This inclusion order is unconventional: This test specifcially tests that
+// the THRIFT_TUUID_SUPPORT_BOOST_UUID directive can be set before including the header
+// to enable boost::uuid support without causing linking or other errors with
+// the compiled thrift library.
+
+#define THRIFT_TUUID_SUPPORT_BOOST_UUID
+#define THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT
+
+#include <boost/uuid/uuid_io.hpp>
+#include <boost/uuid/string_generator.hpp>
+
+#include <thrift/TUuid.h>
+using apache::thrift::TUuid;
+
+BOOST_AUTO_TEST_SUITE(TUuidBoostTestNoDirective)
+
+BOOST_AUTO_TEST_CASE(compiler_directive_set) {
+    // Test if the macro is set as expected
+    #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
+    BOOST_TEST(true);
+    #else
+    BOOST_TEST(false, "The 'THRIFT_TUUID_SUPPORT_BOOST_UUID' preprocessor must now be set for these tests");
+    #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_constructor) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("5cb719a4-cd15-4476-8bcc-f1834b2527ee")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  const TUuid uuid{boost_uuid};
+  BOOST_TEST(!uuid.is_nil());
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"5cb719a4-cd15-4476-8bcc-f1834b2527ee"});
+}
+
+BOOST_AUTO_TEST_CASE(from_boost_uuid_assignment) {
+  static boost::uuids::string_generator gen;
+  boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
+  BOOST_TEST(!boost_uuid.is_nil());
+  TUuid uuid{};
+  BOOST_TEST(uuid.is_nil());
+
+  uuid = TUuid{boost_uuid};
+
+  BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
+  BOOST_TEST(to_string(uuid) == std::string{"1f610073-db33-4d21-adf2-75460d4955cc"});
+}
+BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file
diff --git a/lib/cpp/test/ToStringTest.cpp b/lib/cpp/test/ToStringTest.cpp
index 68c82ad..cb792df 100644
--- a/lib/cpp/test/ToStringTest.cpp
+++ b/lib/cpp/test/ToStringTest.cpp
@@ -162,11 +162,11 @@
 
 BOOST_AUTO_TEST_CASE(generated_uuid_to_string) {
   thrift::test::CrazyNesting l;
-  l.uuid_field = "{4b686716-5f20-4deb-8ce0-9eaf379e8a3d}";
+  l.uuid_field = apache::thrift::TUuid{"{4b686716-5f20-4deb-8ce0-9eaf379e8a3d}"};
 
   BOOST_CHECK_EQUAL(to_string(l),
                     "CrazyNesting(string_field=, set_field=<null>, list_field=[], binary_field=, "
-                    "uuid_field={4b686716-5f20-4deb-8ce0-9eaf379e8a3d})");
+                    "uuid_field=4b686716-5f20-4deb-8ce0-9eaf379e8a3d)");
 }
 
 BOOST_AUTO_TEST_SUITE_END()