char_wchar_holder.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2020-2021. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <cwchar>
  22. #include <cstring>
  23. namespace boost {
  24. namespace interprocess {
  25. class char_wchar_holder
  26. {
  27. public:
  28. char_wchar_holder()
  29. : m_str(), m_is_wide()
  30. {
  31. m_str.n = 0;
  32. }
  33. char_wchar_holder(const char *nstr)
  34. : m_str(), m_is_wide()
  35. {
  36. m_str.n = new char [std::strlen(nstr)+1];
  37. std::strcpy(m_str.n, nstr);
  38. }
  39. char_wchar_holder(const wchar_t *wstr)
  40. : m_str(), m_is_wide(true)
  41. {
  42. m_str.w = new wchar_t [std::wcslen(wstr)+1];
  43. std::wcscpy(m_str.w, wstr);
  44. }
  45. char_wchar_holder& operator=(const char *nstr)
  46. {
  47. char *tmp = new char [std::strlen(nstr)+1];
  48. this->delete_mem();
  49. m_str.n = tmp;
  50. std::strcpy(m_str.n, nstr);
  51. m_is_wide = false;
  52. return *this;
  53. }
  54. char_wchar_holder& operator=(const wchar_t *wstr)
  55. {
  56. wchar_t *tmp = new wchar_t [std::wcslen(wstr)+1];
  57. this->delete_mem();
  58. m_str.w = tmp;
  59. std::wcscpy(m_str.w, wstr);
  60. m_is_wide = true;
  61. return *this;
  62. }
  63. char_wchar_holder& operator=(const char_wchar_holder &other)
  64. {
  65. if (other.m_is_wide)
  66. *this = other.getw();
  67. else
  68. *this = other.getn();
  69. return *this;
  70. }
  71. ~char_wchar_holder()
  72. {
  73. this->delete_mem();
  74. }
  75. wchar_t *getw() const
  76. { return m_is_wide ? m_str.w : 0; }
  77. char *getn() const
  78. { return !m_is_wide ? m_str.n : 0; }
  79. void swap(char_wchar_holder& other)
  80. {
  81. char_wchar tmp;
  82. std::memcpy(&tmp, &m_str, sizeof(char_wchar));
  83. std::memcpy(&m_str, &other.m_str, sizeof(char_wchar));
  84. std::memcpy(&other.m_str, &tmp, sizeof(char_wchar));
  85. //
  86. bool b_tmp(m_is_wide);
  87. m_is_wide = other.m_is_wide;
  88. other.m_is_wide = b_tmp;
  89. }
  90. private:
  91. void delete_mem()
  92. {
  93. if(m_is_wide)
  94. delete [] m_str.w;
  95. else
  96. delete [] m_str.n;
  97. }
  98. union char_wchar
  99. {
  100. char *n;
  101. wchar_t *w;
  102. } m_str;
  103. bool m_is_wide;
  104. };
  105. } //namespace interprocess {
  106. } //namespace boost {
  107. #include <boost/interprocess/detail/config_end.hpp>
  108. #endif //BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP