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 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00037 
00038 namespace OpenDDS { namespace DCPS {
00039 
00040 OPENDDS_STRING
00041 to_string(const GUID_t& guid)
00042 {
00043   std::size_t len;
00044 
00045   OPENDDS_STRING ret;
00046 
00047   len = sizeof(guid.guidPrefix);
00048 
00049   for (std::size_t i = 0; i < len; ++i) {
00050     ret += to_dds_string(unsigned(guid.guidPrefix[i]), true);
00051 
00052     if ((i + 1) % 4 == 0) {
00053       ret += '.';
00054     }
00055   }
00056 
00057   len = sizeof(guid.entityId.entityKey);
00058 
00059   for (std::size_t i = 0; i < len; ++i) {
00060     ret += to_dds_string(unsigned(guid.entityId.entityKey[i]), true);
00061   }
00062 
00063   ret += to_dds_string(unsigned(guid.entityId.entityKind), true);
00064 
00065   return ret;
00066 }
00067 
00068 #ifndef OPENDDS_SAFETY_PROFILE
00069 std::ostream&
00070 operator<<(std::ostream& os, const GUID_t& rhs)
00071 {
00072   std::size_t len;
00073 
00074   len = sizeof(rhs.guidPrefix);
00075 
00076   os << std::hex;
00077 
00078   for (std::size_t i = 0; i < len; ++i) {
00079     os << setopts << unsigned(rhs.guidPrefix[i]);
00080 
00081     if ((i + 1) % 4 == 0) os << sep;
00082   }
00083 
00084   len = sizeof(rhs.entityId.entityKey);
00085 
00086   for (std::size_t i = 0; i < len; ++i) {
00087     os << setopts << unsigned(rhs.entityId.entityKey[i]);
00088   }
00089 
00090   os << setopts << unsigned(rhs.entityId.entityKind);
00091 
00092   // Reset, because hex is "sticky"
00093   os << std::dec;
00094 
00095   return os;
00096 }
00097 
00098 std::istream&
00099 operator>>(std::istream& is, GUID_t& rhs)
00100 {
00101   long word;
00102   char discard;
00103 
00104   GuidBuilder builder(rhs);
00105 
00106   is >> std::hex >> word;
00107   builder.guidPrefix0(word);
00108   is >> discard; // sep
00109 
00110   is >> std::hex >> word;
00111   builder.guidPrefix1(word);
00112   is >> discard; // sep
00113 
00114   is >> std::hex >> word;
00115   builder.guidPrefix2(word);
00116   is >> discard; // sep
00117 
00118   is >> std::hex >> word;
00119   builder.entityId(word);
00120 
00121   return is;
00122 }
00123 #endif
00124 
00125 }  }
00126 
00127 OPENDDS_END_VERSIONED_NAMESPACE_DECL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1