OpenDDS  Snapshot(2023/04/28-20:55)
SafetyProfileStreams.h
Go to the documentation of this file.
1 #ifndef OPENDDS_DCPS_SAFETYPROFILESTREAMS_H
2 #define OPENDDS_DCPS_SAFETYPROFILESTREAMS_H
3 
4 #include "dcps_export.h"
5 #include "PoolAllocator.h"
6 
7 #include <ace/INET_Addr.h>
8 #include <ace/OS_NS_stdio.h>
9 
10 #ifdef OPENDDS_SAFETY_PROFILE
11 # include <cstdlib> // For strto*
12 #else
13 # include <fstream>
14 # include <iostream>
15 # include <iomanip>
16 # include <sstream>
17 #endif
18 
20 
21 namespace OpenDDS {
22 namespace DCPS {
23 
24 OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert);
26 OpenDDS_Dcps_Export String to_dds_string(unsigned int to_convert, bool as_hex = false);
28 OpenDDS_Dcps_Export String to_dds_string(long long to_convert);
29 OpenDDS_Dcps_Export String to_dds_string(unsigned long long to_convert, bool as_hex = false);
30 OpenDDS_Dcps_Export String to_dds_string(unsigned long to_convert, bool as_hex = false);
31 OpenDDS_Dcps_Export String to_dds_string(const unsigned char* array, size_t length);
32 // Pass-through for conditional compilation situtations, i.e., type may be an integer or string.
33 inline String to_dds_string(const String& to_convert)
34 {
35  return to_convert;
36 }
37 
38 //@{
39 /**
40  * Converts a series of bytes at data to a optionally delimited String
41  * of hexadecimal numbers.
42  *
43  * If delim is '\0' (the default) or delim_every is 0, then the output will not
44  * be delimited.
45  */
47  const unsigned char* data, size_t size, char delim = '\0', size_t delim_every = 1);
49  const char* data, size_t size, char delim = '\0', size_t delim_every = 1);
50 //@}
51 
52 /// Convert Pointer to String
53 template <typename T>
54 inline
55 String to_dds_string(const T* to_convert)
56 {
57  const char* fmt = "%p";
58  const int buff_size = 20 + 1; // note +1 for null terminator
59  char buf[buff_size];
60  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
61  return String(buf);
62 }
63 
64 /**
65  * Convert string s to value of integral type T.
66  *
67  * Returns true for success, false for error
68  */
69 template <typename T>
70 bool convertToInteger(const String& s, T& value)
71 {
72 #ifdef OPENDDS_SAFETY_PROFILE
73  char* end;
74  const long conv = std::strtol(s.c_str(), &end, 10);
75  if (end == s.c_str()) return false;
76  value = static_cast<T>(conv);
77 #else
78  std::stringstream istr(s.c_str());
79  if (!(istr >> value) || (istr.peek() != EOF)) return false;
80 #endif
81  return true;
82 }
83 
84 /**
85  * Convert string s to value of double type T.
86  *
87  * Returns true for success, false for error
88  */
89 template <typename T>
90 bool convertToDouble(const String& s, T& value)
91 {
92 #ifdef OPENDDS_SAFETY_PROFILE
93  char* end;
94  const double conv = std::strtod(s.c_str(), &end);
95  if (end == s.c_str()) return false;
96  value = static_cast<T>(conv);
97 #else
98  std::stringstream istr(s.c_str());
99  if (!(istr >> value) || (istr.peek() != EOF)) return false;
100 #endif
101  return true;
102 }
103 
104 } // namespace DCPS
105 } // namespace OpenDDS
106 
108 
109 #endif // OPENDDS_DCPS_SAFETYPROFILESTREAMS_H
const LogLevel::Value value
Definition: debug.cpp:61
std::string String
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
int snprintf(char *buf, size_t maxlen, const char *format,...) ACE_GCC_FORMAT_ATTRIBUTE(printf
String to_dds_string(unsigned short to_convert)
String to_hex_dds_string(const unsigned char *data, const size_t size, const char delim, const size_t delim_every)
bool convertToDouble(const String &s, T &value)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
bool convertToInteger(const String &s, T &value)