GuidUtils.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #include "DCPS/DdsDcps_pch.h" //Only the _pch include should start with DCPS/
00009 #include "DCPS/SafetyProfileStreams.h"
00010 
00011 #include <cstdlib>
00012 #include <cstdio>
00013 #include "ace/ACE.h"
00014 #include "ace/OS_NS_string.h"
00015 
00016 #include "GuidBuilder.h"
00017 
00018 namespace {
00019 
00020 #ifndef OPENDDS_SAFETY_PROFILE
00021 inline std::ostream&
00022 sep(std::ostream& os)
00023 {
00024   return os << ".";
00025 }
00026 
00027 inline std::ostream&
00028 setopts(std::ostream& os)
00029 {
00030   return os << std::setfill('0') << std::setw(2);
00031 }
00032 #endif
00033 
00034 } // namespace
00035 
00036 namespace OpenDDS { namespace DCPS {
00037 
00038 OPENDDS_STRING
00039 to_string(const GUID_t& guid)
00040 {
00041   std::size_t len;
00042 
00043   OPENDDS_STRING ret;
00044 
00045   len = sizeof(guid.guidPrefix);
00046 
00047   for (std::size_t i = 0; i < len; ++i) {
00048     ret += to_dds_string(unsigned(guid.guidPrefix[i]), true);
00049 
00050     if ((i + 1) % 4 == 0) {
00051       ret += '.';
00052     }
00053   }
00054 
00055   len = sizeof(guid.entityId.entityKey);
00056 
00057   for (std::size_t i = 0; i < len; ++i) {
00058     ret += to_dds_string(unsigned(guid.entityId.entityKey[i]), true);
00059   }
00060 
00061   ret += to_dds_string(unsigned(guid.entityId.entityKind), true);
00062 
00063   return ret;
00064 }
00065 
00066 #ifndef OPENDDS_SAFETY_PROFILE
00067 std::ostream&
00068 operator<<(std::ostream& os, const GUID_t& rhs)
00069 {
00070   std::size_t len;
00071 
00072   len = sizeof(rhs.guidPrefix);
00073 
00074   os << std::hex;
00075 
00076   for (std::size_t i = 0; i < len; ++i) {
00077     os << setopts << unsigned(rhs.guidPrefix[i]);
00078 
00079     if ((i + 1) % 4 == 0) os << sep;
00080   }
00081 
00082   len = sizeof(rhs.entityId.entityKey);
00083 
00084   for (std::size_t i = 0; i < len; ++i) {
00085     os << setopts << unsigned(rhs.entityId.entityKey[i]);
00086   }
00087 
00088   os << setopts << unsigned(rhs.entityId.entityKind);
00089 
00090   // Reset, because hex is "sticky"
00091   os << std::dec;
00092 
00093   return os;
00094 }
00095 
00096 std::istream&
00097 operator>>(std::istream& is, GUID_t& rhs)
00098 {
00099   long word;
00100   char discard;
00101 
00102   GuidBuilder builder(rhs);
00103 
00104   is >> std::hex >> word;
00105   builder.guidPrefix0(word);
00106   is >> discard; // sep
00107 
00108   is >> std::hex >> word;
00109   builder.guidPrefix1(word);
00110   is >> discard; // sep
00111 
00112   is >> std::hex >> word;
00113   builder.guidPrefix2(word);
00114   is >> discard; // sep
00115 
00116   is >> std::hex >> word;
00117   builder.entityId(word);
00118 
00119   return is;
00120 }
00121 #endif
00122 
00123 }  }

Generated on Fri Feb 12 20:05:23 2016 for OpenDDS by  doxygen 1.4.7