Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:25

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2020-2021. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 
0025 #include <cwchar>
0026 #include <cstring>
0027 
0028 namespace boost {
0029 namespace interprocess {
0030 
0031 class char_wchar_holder
0032 {
0033    public:
0034    char_wchar_holder()
0035       : m_str(), m_is_wide()
0036    {
0037       m_str.n = 0;
0038    }
0039 
0040    char_wchar_holder(const char *nstr)
0041       : m_str(), m_is_wide()
0042    {
0043       m_str.n = new char [std::strlen(nstr)+1];
0044       std::strcpy(m_str.n, nstr);
0045    }
0046 
0047    char_wchar_holder(const wchar_t *wstr)
0048       : m_str(), m_is_wide(true)
0049    {
0050       m_str.w = new wchar_t [std::wcslen(wstr)+1];
0051       std::wcscpy(m_str.w, wstr);
0052    }
0053 
0054    char_wchar_holder& operator=(const char *nstr)
0055    {
0056       char *tmp = new char [std::strlen(nstr)+1];
0057       this->delete_mem();
0058       m_str.n = tmp;
0059       std::strcpy(m_str.n, nstr);
0060       return *this;
0061    }
0062 
0063    char_wchar_holder& operator=(const wchar_t *wstr)
0064    {
0065       wchar_t *tmp = new wchar_t [std::wcslen(wstr)+1];
0066       this->delete_mem();
0067       m_str.w = tmp;
0068       std::wcscpy(m_str.w, wstr);
0069       return *this;
0070    }
0071 
0072    char_wchar_holder& operator=(const char_wchar_holder &other)
0073    {
0074       if (other.m_is_wide)
0075          *this = other.getn();
0076       else
0077          *this = other.getw();
0078       return *this;
0079    }
0080 
0081    ~char_wchar_holder()
0082    {
0083       this->delete_mem();
0084    }
0085 
0086    wchar_t *getw() const
0087    {  return m_is_wide ? m_str.w : 0; }
0088 
0089    char *getn() const
0090    {  return !m_is_wide ? m_str.n : 0; }
0091 
0092    void swap(char_wchar_holder& other)
0093    {
0094       char_wchar tmp;
0095       std::memcpy(&tmp, &m_str, sizeof(char_wchar));
0096       std::memcpy(&m_str, &other.m_str, sizeof(char_wchar));
0097       std::memcpy(&other.m_str, &tmp, sizeof(char_wchar));
0098       //
0099       bool b_tmp(m_is_wide);
0100       m_is_wide = other.m_is_wide;
0101       other.m_is_wide = b_tmp;
0102    }
0103 
0104    private:
0105 
0106    void delete_mem()
0107    {
0108       if(m_is_wide)
0109          delete [] m_str.w;
0110       else
0111          delete [] m_str.n;
0112    }
0113 
0114    union char_wchar
0115    {
0116       char    *n;
0117       wchar_t *w;
0118    } m_str;
0119    bool m_is_wide;
0120 };
0121 
0122 }  //namespace interprocess {
0123 }  //namespace boost {
0124 
0125 #include <boost/interprocess/detail/config_end.hpp>
0126 
0127 #endif   //BOOST_INTERPROCESS_DETAIL_CHAR_WCHAR_HOLDER_HPP