Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

codecvt.h 25KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // Locale support (codecvt) -*- C++ -*-
  2. // Copyright (C) 2000-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/codecvt.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{locale}
  23. */
  24. //
  25. // ISO C++ 14882: 22.2.1.5 Template class codecvt
  26. //
  27. // Written by Benjamin Kosnik <bkoz@redhat.com>
  28. #ifndef _CODECVT_H
  29. #define _CODECVT_H 1
  30. #pragma GCC system_header
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /// Empty base class for codecvt facet [22.2.1.5].
  35. class codecvt_base
  36. {
  37. public:
  38. enum result
  39. {
  40. ok,
  41. partial,
  42. error,
  43. noconv
  44. };
  45. };
  46. /**
  47. * @brief Common base for codecvt functions.
  48. *
  49. * This template class provides implementations of the public functions
  50. * that forward to the protected virtual functions.
  51. *
  52. * This template also provides abstract stubs for the protected virtual
  53. * functions.
  54. */
  55. template<typename _InternT, typename _ExternT, typename _StateT>
  56. class __codecvt_abstract_base
  57. : public locale::facet, public codecvt_base
  58. {
  59. public:
  60. // Types:
  61. typedef codecvt_base::result result;
  62. typedef _InternT intern_type;
  63. typedef _ExternT extern_type;
  64. typedef _StateT state_type;
  65. // 22.2.1.5.1 codecvt members
  66. /**
  67. * @brief Convert from internal to external character set.
  68. *
  69. * Converts input string of intern_type to output string of
  70. * extern_type. This is analogous to wcsrtombs. It does this by
  71. * calling codecvt::do_out.
  72. *
  73. * The source and destination character sets are determined by the
  74. * facet's locale, internal and external types.
  75. *
  76. * The characters in [from,from_end) are converted and written to
  77. * [to,to_end). from_next and to_next are set to point to the
  78. * character following the last successfully converted character,
  79. * respectively. If the result needed no conversion, from_next and
  80. * to_next are not affected.
  81. *
  82. * The @a state argument should be initialized if the input is at the
  83. * beginning and carried from a previous call if continuing
  84. * conversion. There are no guarantees about how @a state is used.
  85. *
  86. * The result returned is a member of codecvt_base::result. If
  87. * all the input is converted, returns codecvt_base::ok. If no
  88. * conversion is necessary, returns codecvt_base::noconv. If
  89. * the input ends early or there is insufficient space in the
  90. * output, returns codecvt_base::partial. Otherwise the
  91. * conversion failed and codecvt_base::error is returned.
  92. *
  93. * @param __state Persistent conversion state data.
  94. * @param __from Start of input.
  95. * @param __from_end End of input.
  96. * @param __from_next Returns start of unconverted data.
  97. * @param __to Start of output buffer.
  98. * @param __to_end End of output buffer.
  99. * @param __to_next Returns start of unused output area.
  100. * @return codecvt_base::result.
  101. */
  102. result
  103. out(state_type& __state, const intern_type* __from,
  104. const intern_type* __from_end, const intern_type*& __from_next,
  105. extern_type* __to, extern_type* __to_end,
  106. extern_type*& __to_next) const
  107. {
  108. return this->do_out(__state, __from, __from_end, __from_next,
  109. __to, __to_end, __to_next);
  110. }
  111. /**
  112. * @brief Reset conversion state.
  113. *
  114. * Writes characters to output that would restore @a state to initial
  115. * conditions. The idea is that if a partial conversion occurs, then
  116. * the converting the characters written by this function would leave
  117. * the state in initial conditions, rather than partial conversion
  118. * state. It does this by calling codecvt::do_unshift().
  119. *
  120. * For example, if 4 external characters always converted to 1 internal
  121. * character, and input to in() had 6 external characters with state
  122. * saved, this function would write two characters to the output and
  123. * set the state to initialized conditions.
  124. *
  125. * The source and destination character sets are determined by the
  126. * facet's locale, internal and external types.
  127. *
  128. * The result returned is a member of codecvt_base::result. If the
  129. * state could be reset and data written, returns codecvt_base::ok. If
  130. * no conversion is necessary, returns codecvt_base::noconv. If the
  131. * output has insufficient space, returns codecvt_base::partial.
  132. * Otherwise the reset failed and codecvt_base::error is returned.
  133. *
  134. * @param __state Persistent conversion state data.
  135. * @param __to Start of output buffer.
  136. * @param __to_end End of output buffer.
  137. * @param __to_next Returns start of unused output area.
  138. * @return codecvt_base::result.
  139. */
  140. result
  141. unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
  142. extern_type*& __to_next) const
  143. { return this->do_unshift(__state, __to,__to_end,__to_next); }
  144. /**
  145. * @brief Convert from external to internal character set.
  146. *
  147. * Converts input string of extern_type to output string of
  148. * intern_type. This is analogous to mbsrtowcs. It does this by
  149. * calling codecvt::do_in.
  150. *
  151. * The source and destination character sets are determined by the
  152. * facet's locale, internal and external types.
  153. *
  154. * The characters in [from,from_end) are converted and written to
  155. * [to,to_end). from_next and to_next are set to point to the
  156. * character following the last successfully converted character,
  157. * respectively. If the result needed no conversion, from_next and
  158. * to_next are not affected.
  159. *
  160. * The @a state argument should be initialized if the input is at the
  161. * beginning and carried from a previous call if continuing
  162. * conversion. There are no guarantees about how @a state is used.
  163. *
  164. * The result returned is a member of codecvt_base::result. If
  165. * all the input is converted, returns codecvt_base::ok. If no
  166. * conversion is necessary, returns codecvt_base::noconv. If
  167. * the input ends early or there is insufficient space in the
  168. * output, returns codecvt_base::partial. Otherwise the
  169. * conversion failed and codecvt_base::error is returned.
  170. *
  171. * @param __state Persistent conversion state data.
  172. * @param __from Start of input.
  173. * @param __from_end End of input.
  174. * @param __from_next Returns start of unconverted data.
  175. * @param __to Start of output buffer.
  176. * @param __to_end End of output buffer.
  177. * @param __to_next Returns start of unused output area.
  178. * @return codecvt_base::result.
  179. */
  180. result
  181. in(state_type& __state, const extern_type* __from,
  182. const extern_type* __from_end, const extern_type*& __from_next,
  183. intern_type* __to, intern_type* __to_end,
  184. intern_type*& __to_next) const
  185. {
  186. return this->do_in(__state, __from, __from_end, __from_next,
  187. __to, __to_end, __to_next);
  188. }
  189. int
  190. encoding() const throw()
  191. { return this->do_encoding(); }
  192. bool
  193. always_noconv() const throw()
  194. { return this->do_always_noconv(); }
  195. int
  196. length(state_type& __state, const extern_type* __from,
  197. const extern_type* __end, size_t __max) const
  198. { return this->do_length(__state, __from, __end, __max); }
  199. int
  200. max_length() const throw()
  201. { return this->do_max_length(); }
  202. protected:
  203. explicit
  204. __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
  205. virtual
  206. ~__codecvt_abstract_base() { }
  207. /**
  208. * @brief Convert from internal to external character set.
  209. *
  210. * Converts input string of intern_type to output string of
  211. * extern_type. This function is a hook for derived classes to change
  212. * the value returned. @see out for more information.
  213. */
  214. virtual result
  215. do_out(state_type& __state, const intern_type* __from,
  216. const intern_type* __from_end, const intern_type*& __from_next,
  217. extern_type* __to, extern_type* __to_end,
  218. extern_type*& __to_next) const = 0;
  219. virtual result
  220. do_unshift(state_type& __state, extern_type* __to,
  221. extern_type* __to_end, extern_type*& __to_next) const = 0;
  222. virtual result
  223. do_in(state_type& __state, const extern_type* __from,
  224. const extern_type* __from_end, const extern_type*& __from_next,
  225. intern_type* __to, intern_type* __to_end,
  226. intern_type*& __to_next) const = 0;
  227. virtual int
  228. do_encoding() const throw() = 0;
  229. virtual bool
  230. do_always_noconv() const throw() = 0;
  231. virtual int
  232. do_length(state_type&, const extern_type* __from,
  233. const extern_type* __end, size_t __max) const = 0;
  234. virtual int
  235. do_max_length() const throw() = 0;
  236. };
  237. /**
  238. * @brief Primary class template codecvt.
  239. * @ingroup locales
  240. *
  241. * NB: Generic, mostly useless implementation.
  242. *
  243. */
  244. template<typename _InternT, typename _ExternT, typename _StateT>
  245. class codecvt
  246. : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
  247. {
  248. public:
  249. // Types:
  250. typedef codecvt_base::result result;
  251. typedef _InternT intern_type;
  252. typedef _ExternT extern_type;
  253. typedef _StateT state_type;
  254. protected:
  255. __c_locale _M_c_locale_codecvt;
  256. public:
  257. static locale::id id;
  258. explicit
  259. codecvt(size_t __refs = 0)
  260. : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs),
  261. _M_c_locale_codecvt(0)
  262. { }
  263. explicit
  264. codecvt(__c_locale __cloc, size_t __refs = 0);
  265. protected:
  266. virtual
  267. ~codecvt() { }
  268. virtual result
  269. do_out(state_type& __state, const intern_type* __from,
  270. const intern_type* __from_end, const intern_type*& __from_next,
  271. extern_type* __to, extern_type* __to_end,
  272. extern_type*& __to_next) const;
  273. virtual result
  274. do_unshift(state_type& __state, extern_type* __to,
  275. extern_type* __to_end, extern_type*& __to_next) const;
  276. virtual result
  277. do_in(state_type& __state, const extern_type* __from,
  278. const extern_type* __from_end, const extern_type*& __from_next,
  279. intern_type* __to, intern_type* __to_end,
  280. intern_type*& __to_next) const;
  281. virtual int
  282. do_encoding() const throw();
  283. virtual bool
  284. do_always_noconv() const throw();
  285. virtual int
  286. do_length(state_type&, const extern_type* __from,
  287. const extern_type* __end, size_t __max) const;
  288. virtual int
  289. do_max_length() const throw();
  290. };
  291. template<typename _InternT, typename _ExternT, typename _StateT>
  292. locale::id codecvt<_InternT, _ExternT, _StateT>::id;
  293. /// class codecvt<char, char, mbstate_t> specialization.
  294. template<>
  295. class codecvt<char, char, mbstate_t>
  296. : public __codecvt_abstract_base<char, char, mbstate_t>
  297. {
  298. friend class messages<char>;
  299. public:
  300. // Types:
  301. typedef char intern_type;
  302. typedef char extern_type;
  303. typedef mbstate_t state_type;
  304. protected:
  305. __c_locale _M_c_locale_codecvt;
  306. public:
  307. static locale::id id;
  308. explicit
  309. codecvt(size_t __refs = 0);
  310. explicit
  311. codecvt(__c_locale __cloc, size_t __refs = 0);
  312. protected:
  313. virtual
  314. ~codecvt();
  315. virtual result
  316. do_out(state_type& __state, const intern_type* __from,
  317. const intern_type* __from_end, const intern_type*& __from_next,
  318. extern_type* __to, extern_type* __to_end,
  319. extern_type*& __to_next) const;
  320. virtual result
  321. do_unshift(state_type& __state, extern_type* __to,
  322. extern_type* __to_end, extern_type*& __to_next) const;
  323. virtual result
  324. do_in(state_type& __state, const extern_type* __from,
  325. const extern_type* __from_end, const extern_type*& __from_next,
  326. intern_type* __to, intern_type* __to_end,
  327. intern_type*& __to_next) const;
  328. virtual int
  329. do_encoding() const throw();
  330. virtual bool
  331. do_always_noconv() const throw();
  332. virtual int
  333. do_length(state_type&, const extern_type* __from,
  334. const extern_type* __end, size_t __max) const;
  335. virtual int
  336. do_max_length() const throw();
  337. };
  338. #ifdef _GLIBCXX_USE_WCHAR_T
  339. /** @brief Class codecvt<wchar_t, char, mbstate_t> specialization.
  340. *
  341. * Converts between narrow and wide characters in the native character set
  342. */
  343. template<>
  344. class codecvt<wchar_t, char, mbstate_t>
  345. : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
  346. {
  347. friend class messages<wchar_t>;
  348. public:
  349. // Types:
  350. typedef wchar_t intern_type;
  351. typedef char extern_type;
  352. typedef mbstate_t state_type;
  353. protected:
  354. __c_locale _M_c_locale_codecvt;
  355. public:
  356. static locale::id id;
  357. explicit
  358. codecvt(size_t __refs = 0);
  359. explicit
  360. codecvt(__c_locale __cloc, size_t __refs = 0);
  361. protected:
  362. virtual
  363. ~codecvt();
  364. virtual result
  365. do_out(state_type& __state, const intern_type* __from,
  366. const intern_type* __from_end, const intern_type*& __from_next,
  367. extern_type* __to, extern_type* __to_end,
  368. extern_type*& __to_next) const;
  369. virtual result
  370. do_unshift(state_type& __state,
  371. extern_type* __to, extern_type* __to_end,
  372. extern_type*& __to_next) const;
  373. virtual result
  374. do_in(state_type& __state,
  375. const extern_type* __from, const extern_type* __from_end,
  376. const extern_type*& __from_next,
  377. intern_type* __to, intern_type* __to_end,
  378. intern_type*& __to_next) const;
  379. virtual
  380. int do_encoding() const throw();
  381. virtual
  382. bool do_always_noconv() const throw();
  383. virtual
  384. int do_length(state_type&, const extern_type* __from,
  385. const extern_type* __end, size_t __max) const;
  386. virtual int
  387. do_max_length() const throw();
  388. };
  389. #endif //_GLIBCXX_USE_WCHAR_T
  390. #if __cplusplus >= 201103L
  391. /** @brief Class codecvt<char16_t, char, mbstate_t> specialization.
  392. *
  393. * Converts between UTF-16 and UTF-8.
  394. */
  395. template<>
  396. class codecvt<char16_t, char, mbstate_t>
  397. : public __codecvt_abstract_base<char16_t, char, mbstate_t>
  398. {
  399. public:
  400. // Types:
  401. typedef char16_t intern_type;
  402. typedef char extern_type;
  403. typedef mbstate_t state_type;
  404. public:
  405. static locale::id id;
  406. explicit
  407. codecvt(size_t __refs = 0)
  408. : __codecvt_abstract_base<char16_t, char, mbstate_t>(__refs) { }
  409. protected:
  410. virtual
  411. ~codecvt();
  412. virtual result
  413. do_out(state_type& __state, const intern_type* __from,
  414. const intern_type* __from_end, const intern_type*& __from_next,
  415. extern_type* __to, extern_type* __to_end,
  416. extern_type*& __to_next) const;
  417. virtual result
  418. do_unshift(state_type& __state,
  419. extern_type* __to, extern_type* __to_end,
  420. extern_type*& __to_next) const;
  421. virtual result
  422. do_in(state_type& __state,
  423. const extern_type* __from, const extern_type* __from_end,
  424. const extern_type*& __from_next,
  425. intern_type* __to, intern_type* __to_end,
  426. intern_type*& __to_next) const;
  427. virtual
  428. int do_encoding() const throw();
  429. virtual
  430. bool do_always_noconv() const throw();
  431. virtual
  432. int do_length(state_type&, const extern_type* __from,
  433. const extern_type* __end, size_t __max) const;
  434. virtual int
  435. do_max_length() const throw();
  436. };
  437. /** @brief Class codecvt<char32_t, char, mbstate_t> specialization.
  438. *
  439. * Converts between UTF-32 and UTF-8.
  440. */
  441. template<>
  442. class codecvt<char32_t, char, mbstate_t>
  443. : public __codecvt_abstract_base<char32_t, char, mbstate_t>
  444. {
  445. public:
  446. // Types:
  447. typedef char32_t intern_type;
  448. typedef char extern_type;
  449. typedef mbstate_t state_type;
  450. public:
  451. static locale::id id;
  452. explicit
  453. codecvt(size_t __refs = 0)
  454. : __codecvt_abstract_base<char32_t, char, mbstate_t>(__refs) { }
  455. protected:
  456. virtual
  457. ~codecvt();
  458. virtual result
  459. do_out(state_type& __state, const intern_type* __from,
  460. const intern_type* __from_end, const intern_type*& __from_next,
  461. extern_type* __to, extern_type* __to_end,
  462. extern_type*& __to_next) const;
  463. virtual result
  464. do_unshift(state_type& __state,
  465. extern_type* __to, extern_type* __to_end,
  466. extern_type*& __to_next) const;
  467. virtual result
  468. do_in(state_type& __state,
  469. const extern_type* __from, const extern_type* __from_end,
  470. const extern_type*& __from_next,
  471. intern_type* __to, intern_type* __to_end,
  472. intern_type*& __to_next) const;
  473. virtual
  474. int do_encoding() const throw();
  475. virtual
  476. bool do_always_noconv() const throw();
  477. virtual
  478. int do_length(state_type&, const extern_type* __from,
  479. const extern_type* __end, size_t __max) const;
  480. virtual int
  481. do_max_length() const throw();
  482. };
  483. #ifdef _GLIBCXX_USE_CHAR8_T
  484. /** @brief Class codecvt<char16_t, char8_t, mbstate_t> specialization.
  485. *
  486. * Converts between UTF-16 and UTF-8.
  487. */
  488. template<>
  489. class codecvt<char16_t, char8_t, mbstate_t>
  490. : public __codecvt_abstract_base<char16_t, char8_t, mbstate_t>
  491. {
  492. public:
  493. // Types:
  494. typedef char16_t intern_type;
  495. typedef char8_t extern_type;
  496. typedef mbstate_t state_type;
  497. public:
  498. static locale::id id;
  499. explicit
  500. codecvt(size_t __refs = 0)
  501. : __codecvt_abstract_base<char16_t, char8_t, mbstate_t>(__refs) { }
  502. protected:
  503. virtual
  504. ~codecvt();
  505. virtual result
  506. do_out(state_type& __state, const intern_type* __from,
  507. const intern_type* __from_end, const intern_type*& __from_next,
  508. extern_type* __to, extern_type* __to_end,
  509. extern_type*& __to_next) const;
  510. virtual result
  511. do_unshift(state_type& __state,
  512. extern_type* __to, extern_type* __to_end,
  513. extern_type*& __to_next) const;
  514. virtual result
  515. do_in(state_type& __state,
  516. const extern_type* __from, const extern_type* __from_end,
  517. const extern_type*& __from_next,
  518. intern_type* __to, intern_type* __to_end,
  519. intern_type*& __to_next) const;
  520. virtual
  521. int do_encoding() const throw();
  522. virtual
  523. bool do_always_noconv() const throw();
  524. virtual
  525. int do_length(state_type&, const extern_type* __from,
  526. const extern_type* __end, size_t __max) const;
  527. virtual int
  528. do_max_length() const throw();
  529. };
  530. /** @brief Class codecvt<char32_t, char8_t, mbstate_t> specialization.
  531. *
  532. * Converts between UTF-32 and UTF-8.
  533. */
  534. template<>
  535. class codecvt<char32_t, char8_t, mbstate_t>
  536. : public __codecvt_abstract_base<char32_t, char8_t, mbstate_t>
  537. {
  538. public:
  539. // Types:
  540. typedef char32_t intern_type;
  541. typedef char8_t extern_type;
  542. typedef mbstate_t state_type;
  543. public:
  544. static locale::id id;
  545. explicit
  546. codecvt(size_t __refs = 0)
  547. : __codecvt_abstract_base<char32_t, char8_t, mbstate_t>(__refs) { }
  548. protected:
  549. virtual
  550. ~codecvt();
  551. virtual result
  552. do_out(state_type& __state, const intern_type* __from,
  553. const intern_type* __from_end, const intern_type*& __from_next,
  554. extern_type* __to, extern_type* __to_end,
  555. extern_type*& __to_next) const;
  556. virtual result
  557. do_unshift(state_type& __state,
  558. extern_type* __to, extern_type* __to_end,
  559. extern_type*& __to_next) const;
  560. virtual result
  561. do_in(state_type& __state,
  562. const extern_type* __from, const extern_type* __from_end,
  563. const extern_type*& __from_next,
  564. intern_type* __to, intern_type* __to_end,
  565. intern_type*& __to_next) const;
  566. virtual
  567. int do_encoding() const throw();
  568. virtual
  569. bool do_always_noconv() const throw();
  570. virtual
  571. int do_length(state_type&, const extern_type* __from,
  572. const extern_type* __end, size_t __max) const;
  573. virtual int
  574. do_max_length() const throw();
  575. };
  576. #endif // _GLIBCXX_USE_CHAR8_T
  577. #endif // C++11
  578. /// class codecvt_byname [22.2.1.6].
  579. template<typename _InternT, typename _ExternT, typename _StateT>
  580. class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
  581. {
  582. public:
  583. explicit
  584. codecvt_byname(const char* __s, size_t __refs = 0)
  585. : codecvt<_InternT, _ExternT, _StateT>(__refs)
  586. {
  587. if (__builtin_strcmp(__s, "C") != 0
  588. && __builtin_strcmp(__s, "POSIX") != 0)
  589. {
  590. this->_S_destroy_c_locale(this->_M_c_locale_codecvt);
  591. this->_S_create_c_locale(this->_M_c_locale_codecvt, __s);
  592. }
  593. }
  594. #if __cplusplus >= 201103L
  595. explicit
  596. codecvt_byname(const string& __s, size_t __refs = 0)
  597. : codecvt_byname(__s.c_str(), __refs) { }
  598. #endif
  599. protected:
  600. virtual
  601. ~codecvt_byname() { }
  602. };
  603. #if __cplusplus >= 201103L
  604. template<>
  605. class codecvt_byname<char16_t, char, mbstate_t>
  606. : public codecvt<char16_t, char, mbstate_t>
  607. {
  608. public:
  609. explicit
  610. codecvt_byname(const char*, size_t __refs = 0)
  611. : codecvt<char16_t, char, mbstate_t>(__refs) { }
  612. explicit
  613. codecvt_byname(const string& __s, size_t __refs = 0)
  614. : codecvt_byname(__s.c_str(), __refs) { }
  615. protected:
  616. virtual
  617. ~codecvt_byname() { }
  618. };
  619. template<>
  620. class codecvt_byname<char32_t, char, mbstate_t>
  621. : public codecvt<char32_t, char, mbstate_t>
  622. {
  623. public:
  624. explicit
  625. codecvt_byname(const char*, size_t __refs = 0)
  626. : codecvt<char32_t, char, mbstate_t>(__refs) { }
  627. explicit
  628. codecvt_byname(const string& __s, size_t __refs = 0)
  629. : codecvt_byname(__s.c_str(), __refs) { }
  630. protected:
  631. virtual
  632. ~codecvt_byname() { }
  633. };
  634. #if defined(_GLIBCXX_USE_CHAR8_T)
  635. template<>
  636. class codecvt_byname<char16_t, char8_t, mbstate_t>
  637. : public codecvt<char16_t, char8_t, mbstate_t>
  638. {
  639. public:
  640. explicit
  641. codecvt_byname(const char* __s, size_t __refs = 0)
  642. : codecvt<char16_t, char8_t, mbstate_t>(__refs) { }
  643. explicit
  644. codecvt_byname(const string& __s, size_t __refs = 0)
  645. : codecvt_byname(__s.c_str(), __refs) { }
  646. protected:
  647. virtual
  648. ~codecvt_byname() { }
  649. };
  650. template<>
  651. class codecvt_byname<char32_t, char8_t, mbstate_t>
  652. : public codecvt<char32_t, char8_t, mbstate_t>
  653. {
  654. public:
  655. explicit
  656. codecvt_byname(const char* __s, size_t __refs = 0)
  657. : codecvt<char32_t, char8_t, mbstate_t>(__refs) { }
  658. explicit
  659. codecvt_byname(const string& __s, size_t __refs = 0)
  660. : codecvt_byname(__s.c_str(), __refs) { }
  661. protected:
  662. virtual
  663. ~codecvt_byname() { }
  664. };
  665. #endif
  666. #endif // C++11
  667. // Inhibit implicit instantiations for required instantiations,
  668. // which are defined via explicit instantiations elsewhere.
  669. #if _GLIBCXX_EXTERN_TEMPLATE
  670. extern template class codecvt_byname<char, char, mbstate_t>;
  671. extern template
  672. const codecvt<char, char, mbstate_t>&
  673. use_facet<codecvt<char, char, mbstate_t> >(const locale&);
  674. extern template
  675. bool
  676. has_facet<codecvt<char, char, mbstate_t> >(const locale&);
  677. #ifdef _GLIBCXX_USE_WCHAR_T
  678. extern template class codecvt_byname<wchar_t, char, mbstate_t>;
  679. extern template
  680. const codecvt<wchar_t, char, mbstate_t>&
  681. use_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
  682. extern template
  683. bool
  684. has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
  685. #endif
  686. #if __cplusplus >= 201103L
  687. extern template class codecvt_byname<char16_t, char, mbstate_t>;
  688. extern template class codecvt_byname<char32_t, char, mbstate_t>;
  689. #if defined(_GLIBCXX_USE_CHAR8_T)
  690. extern template class codecvt_byname<char16_t, char8_t, mbstate_t>;
  691. extern template class codecvt_byname<char32_t, char8_t, mbstate_t>;
  692. #endif
  693. #endif
  694. #endif
  695. _GLIBCXX_END_NAMESPACE_VERSION
  696. } // namespace std
  697. #endif // _CODECVT_H