char_wchar_holder.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. return *this;
  52. }
  53. char_wchar_holder& operator=(const wchar_t *wstr)
  54. {
  55. wchar_t *tmp = new wchar_t [std::wcslen(wstr)+1];
  56. this->delete_mem();
  57. m_str.w = tmp;
  58. std::wcscpy(m_str.w, wstr);
  59. return *this;
  60. }
  61. char_wchar_holder& operator=(const char_wchar_holder &other)
  62. {
  63. if (other.m_is_wide)
  64. *this = other.getn();
  65. else
  66. *this = other.getw();
  67. return *this;
  68. }
  69. ~char_wchar_holder()
  70. {
  71. this->delete_mem();
  72. }
  73. wchar_t *getw() const
  74. { return m_is_wide ? m_str.w : 0; }
  75. char *getn() const
  76. { return !m_is_wide ? m_str.n : 0; }
  77. void swap(char_wchar_holder& other)
  78. {
  79. char_wchar tmp;
  80. std::memcpy(&tmp, &m_str, sizeof(char_wchar));
  81. std::memcpy(&m_str, &other.m_str, sizeof(char_wchar));
  82. std::memcpy(&other.m_str, &tmp, sizeof(char_wchar));
  83. //
  84. bool b_tmp(m_is_wide);
  85. m_is_wide = other.m_is_wide;
  86. other.m_is_wide = b_tmp;
  87. }
  88. private:
  89. void delete_mem()
  90. {
  91. if(m_is_wide)
  92. delete [] m_str.w;
  93. else
  94. delete [] m_str.n;
  95. }
  96. union char_wchar
  97. {
  98. char *n;
  99. wchar_t *w;
  100. } m_str;
  101. bool m_is_wide;
  102. };
  103. } //namespace interprocess {
  104. } //namespace boost {
  105. #include <boost/interprocess/detail/config_end.hpp>
  106. #endif //BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP