StringManager.cpp

Go to the documentation of this file.
00001 #include "FACE/StringManager.h"
00002 #include "dds/DCPS/SafetyProfilePool.h"
00003 #include "dds/DCPS/Serializer.h"
00004 #include <cstring>
00005 
00006 namespace FACE {
00007 
00008 namespace {
00009   static Char s_empty = 0;
00010 #ifdef DDS_HAS_WCHAR
00011   static WChar s_wempty = 0;
00012 #endif
00013 }
00014 
00015 Char* string_alloc(UnsignedLong len)
00016 {
00017   if (len == 0) return &s_empty;
00018   void* const raw =
00019     ACE_Allocator::instance()->malloc(len + 1);
00020   Char* const str = static_cast<Char*>(raw);
00021   if (str) str[0] = static_cast<Char>(0);
00022   return str;
00023 }
00024 
00025 Char* string_dup(const Char* str)
00026 {
00027   if (!str) return 0;
00028   if (!*str) return &s_empty;
00029   const size_t len = std::strlen(str);
00030   Char* const cpy = string_alloc(static_cast<UnsignedLong>(len));
00031   if (cpy) std::strncpy(cpy, str, len + 1);
00032   return cpy;
00033 }
00034 
00035 void string_free(Char* str)
00036 {
00037   if (str != &s_empty) ACE_Allocator::instance()->free(str);
00038 }
00039 
00040 #ifdef DDS_HAS_WCHAR
00041 WChar* wstring_alloc(UnsignedLong len)
00042 {
00043   if (len == 0) return &s_wempty;
00044   const size_t n = (len + 1) * sizeof(WChar);
00045   void* const raw = ACE_Allocator::instance()->malloc(n);
00046   WChar* const str = static_cast<WChar*>(raw);
00047   if (str) str[0] = static_cast<WChar>(0);
00048   return str;
00049 }
00050 
00051 WChar* wstring_dup(const WChar* str)
00052 {
00053   if (!str) return 0;
00054   if (!*str) return &s_wempty;
00055   const size_t len = std::wcslen(str);
00056   WChar* const cpy = wstring_alloc(static_cast<UnsignedLong>(len));
00057   if (cpy) std::wcsncpy(cpy, str, len + 1);
00058   return cpy;
00059 }
00060 
00061 void wstring_free(WChar* str)
00062 {
00063   if (str != &s_wempty) ACE_Allocator::instance()->free(str);
00064 }
00065 #endif // DDS_HAS_WCHAR
00066 }
00067 
00068 namespace OpenDDS {
00069 namespace FaceTypes {
00070 
00071 bool operator>>(DCPS::Serializer& ser, StringBase<FACE::Char>& str)
00072 {
00073   ser.read_string(str.out(), FACE::string_alloc, FACE::string_free);
00074   return ser.good_bit();
00075 }
00076 
00077 #ifdef DDS_HAS_WCHAR
00078 bool operator>>(DCPS::Serializer& ser, StringBase<FACE::WChar>& str)
00079 {
00080   ser.read_string(str.out(), FACE::wstring_alloc, FACE::wstring_free);
00081   return ser.good_bit();
00082 }
00083 #endif
00084 
00085 }
00086 }

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