libstdc++
messages_members.h
Go to the documentation of this file.
00001 // std::messages implementation details, GNU version -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/messages_members.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{locale}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 22.2.7.1.2  messages functions
00032 //
00033 
00034 // Written by Benjamin Kosnik <bkoz@redhat.com>
00035 
00036 #include <libintl.h>
00037 
00038 namespace std _GLIBCXX_VISIBILITY(default)
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041 
00042   // Non-virtual member functions.
00043   template<typename _CharT>
00044     messages<_CharT>::messages(size_t __refs)
00045     : facet(__refs), _M_c_locale_messages(_S_get_c_locale()),
00046       _M_name_messages(_S_get_c_name())
00047     { }
00048 
00049   template<typename _CharT>
00050     messages<_CharT>::messages(__c_locale __cloc, const char* __s,
00051                                size_t __refs)
00052     : facet(__refs), _M_c_locale_messages(0), _M_name_messages(0)
00053     {
00054       if (__builtin_strcmp(__s, _S_get_c_name()) != 0)
00055         {
00056           const size_t __len = __builtin_strlen(__s) + 1;
00057           char* __tmp = new char[__len];
00058           __builtin_memcpy(__tmp, __s, __len);
00059           _M_name_messages = __tmp;
00060         }
00061       else
00062         _M_name_messages = _S_get_c_name();
00063 
00064       // Last to avoid leaking memory if new throws.
00065       _M_c_locale_messages = _S_clone_c_locale(__cloc);
00066     }
00067 
00068   template<typename _CharT>
00069     typename messages<_CharT>::catalog
00070     messages<_CharT>::open(const basic_string<char>& __s, const locale& __loc,
00071                            const char* __dir) const
00072     {
00073       bindtextdomain(__s.c_str(), __dir);
00074       return this->do_open(__s, __loc);
00075     }
00076 
00077   // Virtual member functions.
00078   template<typename _CharT>
00079     messages<_CharT>::~messages()
00080     {
00081       if (_M_name_messages != _S_get_c_name())
00082         delete [] _M_name_messages;
00083       _S_destroy_c_locale(_M_c_locale_messages);
00084     }
00085 
00086   template<typename _CharT>
00087     typename messages<_CharT>::catalog
00088     messages<_CharT>::do_open(const basic_string<char>& __s,
00089                               const locale&) const
00090     {
00091       // No error checking is done, assume the catalog exists and can
00092       // be used.
00093       textdomain(__s.c_str());
00094       return 0;
00095     }
00096 
00097   template<typename _CharT>
00098     void
00099     messages<_CharT>::do_close(catalog) const
00100     { }
00101 
00102   // messages_byname
00103   template<typename _CharT>
00104     messages_byname<_CharT>::messages_byname(const char* __s, size_t __refs)
00105     : messages<_CharT>(__refs)
00106     {
00107       if (this->_M_name_messages != locale::facet::_S_get_c_name())
00108         {
00109           delete [] this->_M_name_messages;
00110           if (__builtin_strcmp(__s, locale::facet::_S_get_c_name()) != 0)
00111             {
00112               const size_t __len = __builtin_strlen(__s) + 1;
00113               char* __tmp = new char[__len];
00114               __builtin_memcpy(__tmp, __s, __len);
00115               this->_M_name_messages = __tmp;
00116             }
00117           else
00118             this->_M_name_messages = locale::facet::_S_get_c_name();
00119         }
00120 
00121       if (__builtin_strcmp(__s, "C") != 0
00122           && __builtin_strcmp(__s, "POSIX") != 0)
00123         {
00124           this->_S_destroy_c_locale(this->_M_c_locale_messages);
00125           this->_S_create_c_locale(this->_M_c_locale_messages, __s);
00126         }
00127     }
00128 
00129    //Specializations.
00130   template<>
00131     typename messages<char>::catalog
00132     messages<char>::do_open(const basic_string<char>&,
00133                             const locale&) const;
00134 
00135   template<>
00136     void
00137     messages<char>::do_close(catalog) const;
00138 
00139 #ifdef _GLIBCXX_USE_WCHAR_T
00140   template<>
00141     typename messages<wchar_t>::catalog
00142     messages<wchar_t>::do_open(const basic_string<char>&,
00143                                const locale&) const;
00144 
00145   template<>
00146     void
00147     messages<wchar_t>::do_close(catalog) const;
00148 #endif
00149 
00150 _GLIBCXX_END_NAMESPACE_VERSION
00151 } // namespace