aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/localename.cc
blob: 1b231c02bcda30bd0b720a074f031ac3590a3522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.


#include <bits/std_clocale.h>
#include <bits/std_locale.h>
#include <bits/std_cstring.h>
#include <bits/std_cassert.h>
#include <bits/std_vector.h>
#include <bits/std_stdexcept.h>

namespace std {

  locale::_Impl::
  ~_Impl() throw()
  {
    std::vector<facet*>::iterator it = _M_facets->begin();
    for (; it != _M_facets->end(); ++it)
      (*it)->_M_remove_reference();
    delete _M_facets;
    delete _M_category_names;
  }

  // This constructor is used to correctly initialize the standard,
  // required facets.
  locale::_Impl::
  _Impl(size_t __numfacets, size_t __refs, bool __has_name = false, 
	string __name = "*")
  : _M_references(__refs - 1), _M_facets(0), _M_category_names(0), 
    _M_has_name(__has_name), _M_name(__name)
  { 
    typedef vector<facet*, allocator<facet*> > __vec_facet;
    typedef vector<string, allocator<string> > __vec_string;

    auto_ptr<__vec_facet> __pvf(new __vec_facet(__numfacets, (facet*)0));
    auto_ptr<__vec_string> __pcn(new __vec_string(_S_num_categories, _M_name));
    _M_facets = __pvf.release();
    _M_category_names = __pcn.release();
  }
  
  locale::_Impl::
  _Impl(const _Impl& __other, size_t __refs)
  : _M_references(__refs - 1), _M_facets(0), _M_category_names(0), 
    _M_has_name(__other._M_has_name), _M_name(__other._M_name)
  {
    typedef vector<facet*, allocator<facet*> > __vec_facet;
    typedef vector<string, allocator<string> > __vec_string;

    auto_ptr<__vec_facet> __pvf(new __vec_facet(*(__other._M_facets)));
    auto_ptr<__vec_string> 
      __pcn(new __vec_string(*(__other._M_category_names)));

    std::vector<facet*>::iterator __it = __pvf->begin();
    for (; __it != __pvf->end(); ++__it)
      (*__it)->_M_add_reference();

    // These must be last since in the presence of an exception, the 
    // destructor for 'this' won't run until AFTER execution has passed  
    // the closing brace of the constructor.
    _M_facets = __pvf.release();
    _M_category_names = __pcn.release();
  }

  // Construct specific categories, leaving unselected ones alone
  locale::_Impl::
  _Impl(const _Impl& __other, const string& __name, category __cat, 
	size_t __refs)
    : _M_references(__refs - 1), _M_has_name(__other._M_name != "*")
  {
    typedef vector<facet*, allocator<facet*> > __vec_facet;
    typedef vector<string, allocator<string> > __vec_string;

    __cat = _S_normalize_category(__cat);  // might throw
    try {
      _M_facets = new __vec_facet(*(__other._M_facets));
    }
    catch(...) {
      delete _M_facets;
      throw;
    }
    try {
       _M_category_names = new __vec_string(*(__other._M_category_names));
    }
    catch(...) {
      delete _M_category_names;
      throw;
    }

    static void(_Impl::* ctors[]) (const char*) = 
    {
      //  NB: Order must match the decl order in class locale.
      &locale::_Impl::_M_construct_collate,
      &locale::_Impl::_M_construct_ctype,
      &locale::_Impl::_M_construct_monetary,
      &locale::_Impl::_M_construct_numeric,
      &locale::_Impl::_M_construct_time,
      &locale::_Impl::_M_construct_messages,
      0
    };
    
    __vec_facet::iterator __it = _M_facets->begin();
    for (; __it != _M_facets->end(); ++__it)
      (*__it)->_M_add_reference();

    try 
      {
	unsigned mask = (locale::all & -(unsigned)locale::all);
	for (unsigned ix = 0; (-mask & __cat) != 0; ++ix, (mask <<= 1))
	  {
	    if (!(mask & __cat))
	      continue;
	    
	    if (mask & __cat)
	      _M_replace_category(_S_classic, _S_facet_categories[ix]);
	    else
	      (this->*ctors[ix])(__name.c_str());
	  }
      }
    catch(...) 
      {
	__it = _M_facets->begin();
	for (; __it != _M_facets->end(); ++__it)
	  (*__it)->_M_remove_reference();
	throw;
      }

    // XXX May need to be adjusted
    if (__cat == all)
      _M_name = __name;
  }
  
  void
  locale::_Impl::
  _M_replace_categories(const _Impl* __other, category __cat)
  {
    assert((__cat & locale::all) && !(__cat & ~locale::all));
    
    unsigned int __mask = locale::all & -static_cast<unsigned int>(locale::all);
    for (unsigned int __ix = 0; (-__mask & __cat) != 0; ++__ix, (__mask <<= 1))
      {
	if (__mask & __cat)
	  {
	    _M_replace_category(__other, _S_facet_categories[__ix]);
	    (*_M_category_names)[__ix] = (*(__other->_M_category_names))[__ix];
	  }
      }
  }

  void
  locale::_Impl::
  _M_replace_category(const _Impl* __other, const locale::id* const* __idpp)
  {
    for (; *__idpp; ++__idpp)
      _M_replace_facet(__other, *__idpp);
  }
  
  void
  locale::_Impl::
  _M_replace_facet(const _Impl* __other, const locale::id* __idp)
  {
    size_t __index = __idp->_M_index;
    if (__index == 0 
	|| __other->_M_facets->size() <= __index 
	|| (*(__other->_M_facets))[__index] == 0)
      throw runtime_error("no locale facet");
	
    _M_install_facet(__idp, (*(__other->_M_facets))[__index]); 
  }

  void
  locale::_Impl::
  _M_install_facet(const locale::id* __idp, facet* __fp)
  {
    if (__fp == 0)
      return;

    size_t& __index = __idp->_M_index;
    if (!__index)
      __index = ++locale::id::_S_highwater;  // XXX MT

    if (__index >= _M_facets->size())
      _M_facets->resize(__index + 1, 0);  // might throw
    facet*& __fpr = (*_M_facets)[__index];
    // Order matters, here:
    __fp->_M_add_reference();
    if (__fpr) 
      __fpr->_M_remove_reference();
    __fpr = __fp;
  }
 
  void 
  locale::_Impl::_M_construct_collate(const char* __name)
  {
    _M_facet_init(new collate_byname<char>(__name, 0));
    _M_facet_init(new collate_byname<wchar_t>(__name, 0));
  }

  void 
  locale::_Impl::_M_construct_ctype(const char* __name)
  {
    _M_facet_init(new ctype_byname<char>(__name, 0));
    _M_facet_init(new ctype_byname<wchar_t>(__name, 0));
    _M_facet_init(new codecvt_byname<char, char, mbstate_t>(__name));
    _M_facet_init(new codecvt_byname<wchar_t, char, mbstate_t>(__name));
  }
    
  void 
  locale::_Impl::_M_construct_monetary(const char* __name)
  {
    _M_facet_init(new moneypunct_byname<char, false>(__name, 0));
    _M_facet_init(new moneypunct_byname<wchar_t, false>(__name, 0));
    _M_facet_init(new moneypunct_byname<char, true >(__name, 0));
    _M_facet_init(new moneypunct_byname<wchar_t, true >(__name, 0));

    _M_replace_facet(locale::_S_classic, &money_get<char>::id);
    _M_replace_facet(locale::_S_classic, &money_get<wchar_t>::id);
    _M_replace_facet(locale::_S_classic, &money_put<char>::id);
    _M_replace_facet(locale::_S_classic, &money_put<wchar_t>::id);
  }
    
  void 
  locale::_Impl::_M_construct_numeric(const char* __name)
  {
    _M_facet_init(new numpunct_byname<char>(__name, 0));
    _M_facet_init(new numpunct_byname<wchar_t>(__name, 0));

    _M_replace_facet(locale::_S_classic, &num_get<char>::id);
    _M_replace_facet(locale::_S_classic, &num_get<wchar_t>::id);
    _M_replace_facet(locale::_S_classic, &num_put<char>::id);
    _M_replace_facet(locale::_S_classic, &num_put<wchar_t>::id);
  }
    
  void 
  locale::_Impl::_M_construct_time(const char* __name)
  {
    _M_facet_init(new time_get_byname<char>(__name, 0));
    _M_facet_init(new time_get_byname<wchar_t>(__name, 0));
    _M_facet_init(new time_put_byname<char>(__name, 0));
    _M_facet_init(new time_put_byname<wchar_t>(__name, 0));
  }
    
  void 
  locale::_Impl::_M_construct_messages(const char* __name)
  {
    _M_facet_init(new messages_byname<char>(__name, 0));
    _M_facet_init(new messages_byname<wchar_t>(__name, 0));
  }
}