Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:04

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 
0020 #ifndef _THRIFT_TUUID_H_
0021 #define _THRIFT_TUUID_H_ 1
0022 
0023 #include <thrift/Thrift.h>
0024 
0025 #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
0026 #include <boost/uuid/uuid.hpp>
0027 #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
0028 
0029 #include <algorithm>
0030 #include <sstream>
0031 
0032 namespace apache {
0033 namespace thrift {
0034 
0035 /**
0036  * Thrift wrapper class for a UUID type.
0037  *
0038  * The UUID is stored as a 16 byte buffer.
0039  * This class stores the UUID in network order when assigned from a string.
0040  */
0041 class TUuid {
0042 public:
0043   typedef uint8_t value_type;
0044   typedef uint8_t* iterator;
0045   typedef uint8_t const* const_iterator;
0046   typedef std::size_t size_type;
0047   typedef std::ptrdiff_t difference_type;
0048 
0049   TUuid() = default;
0050   TUuid(const TUuid& other) = default;
0051   TUuid(TUuid&& other) = default;
0052   TUuid& operator=(const TUuid&) = default;
0053   TUuid& operator=(TUuid&&) = default;
0054   ~TUuid() = default;
0055 
0056   /**
0057    * Construct the object from a 16 byte buffer.
0058    */
0059   explicit TUuid(const uint8_t (&data)[16]) noexcept
0060   {
0061     std::copy(std::begin(data), std::end(data), std::begin(this->data_));
0062   }
0063 
0064   /**
0065    * Construct the object from the specified string.
0066    *
0067    * Supported string formats are:
0068    *   - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh"
0069    *   - "{hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh}"
0070    *   - "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
0071    *   - "{hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh}"
0072    *
0073    * If the string is invalid, the object will be set to a
0074    * nil (empty) UUID.
0075    */
0076   explicit TUuid(const std::string& str) noexcept;
0077 
0078 #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID
0079   /**
0080    * Construct the TUuid from a boost::uuids::uuid.
0081    *
0082    * This constructor will only be available if the <tt>THRIFT_TUUID_SUPPORT_BOOST_UUID</tt>
0083    * compiler directive is set when this file is included.
0084    *
0085    * This constructor is by default implicit. It can be made explicit by defining the
0086    * <tt>THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT</tt> compiler directive.
0087    */
0088   #ifdef THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT
0089   explicit
0090   #endif // THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT
0091   TUuid(const boost::uuids::uuid& buuid) noexcept
0092   {
0093     std::copy(buuid.begin(), buuid.end(), std::begin(this->data_));
0094   }
0095 #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID
0096 
0097   /**
0098    * Check if the UUID is nil.
0099    */
0100   bool is_nil() const noexcept;
0101 
0102   /**
0103    * Compare two TUuid objects for equality.
0104    */
0105   inline bool operator==(const TUuid& other) const;
0106 
0107   /**
0108    * Compare two TUuid objects for inequality.
0109    */
0110   inline bool operator!=(const TUuid& other) const;
0111 
0112   iterator begin() noexcept { return data_; }
0113   const_iterator begin() const noexcept { return data_; }
0114   iterator end() noexcept { return data_ + size(); }
0115   const_iterator end() const noexcept { return data_ + size(); }
0116   size_type size() const noexcept { return 16; }
0117   inline const_iterator data() const { return data_; }
0118   inline iterator data() { return data_; }
0119 
0120   void swap(TUuid& other) noexcept { std::swap(data_, other.data_); }
0121 
0122 private:
0123   /**
0124    * The UUID data.
0125    */
0126   uint8_t data_[16] = {};
0127 };
0128 
0129 /**
0130  * Get the String representation of a TUUID.
0131  *
0132  * The format returned is:
0133  *   - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh"
0134  */
0135 std::string to_string(const TUuid& uuid) noexcept(false);
0136 
0137 /**
0138  * Swap two TUuid objects
0139  */
0140 inline void swap(TUuid& lhs, TUuid& rhs) noexcept {
0141   lhs.swap(rhs);
0142 }
0143 
0144 /**
0145  * TUuid equality comparison operator implementation
0146  */
0147 inline bool TUuid::operator==(const TUuid& other) const {
0148   // Compare using temporary strings.
0149   // Can't use strcmp() since we expect embeded zeros
0150   // Perhaps the reason we should use std::array instead
0151   return std::string(this->begin(), this->end()) == std::string(other.begin(), other.end());
0152 }
0153 
0154 /**
0155  * TUuid inequality comparison operator implementation
0156  */
0157 inline bool TUuid::operator!=(const TUuid& other) const {
0158   return !(*this == other);
0159 }
0160 
0161 /**
0162  * TUuid ostream stream operator implementation
0163  */
0164 inline std::ostream& operator<<(std::ostream& out, const TUuid& obj) {
0165   out << to_string(obj);
0166   return out;
0167 }
0168 
0169 } // namespace thrift
0170 } // namespace apache
0171 
0172 #endif // #ifndef _THRIFT_TUUID_H_