The Battle for Wesnoth  1.15.0-dev
unicode_cast.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "ucs4_convert_impl.hpp"
18 #include <iostream> //for std::cerr
19 #include <iterator>
20 
21 namespace ucs4_convert_impl
22 {
23  /**
24  * Transforms an output iterator to a writer for ucs4_convert_impl functions.
25  */
26  template<typename oitor_t>
28  {
29  oitor_t& out_;
30  iteratorwriter(oitor_t& out) : out_(out) {}
31 
32  bool can_push(std::size_t /*count*/)
33  {
34  return true;
35  }
36  template<typename value_type>
37  void push(value_type val)
38  {
39  *out_++ = val;
40  }
41  };
42  template<typename Tret, typename Tcheck>
43  struct enableif
44  {
45  typedef Tcheck ignore;
46  typedef Tret type;
47  };
48 }
49 
50 /**
51  * @tparam TD Output, a collection type.
52  * @tparam TS Input, a collection type.
53  *
54  * @return An instance of TD.
55  */
56 template<typename TD , typename TS>
58 //TD unicode_cast(const TS& source)
59 {
60  using namespace ucs4_convert_impl;
61  typedef typename convert_impl<typename TD::value_type>::type impl_writer;
62  typedef typename convert_impl<typename TS::value_type>::type impl_reader;
63  typedef typename std::back_insert_iterator<TD> output_itor;
64  typedef typename TS::const_iterator input_itor;
65 
66  TD res;
67  try
68  {
69  output_itor inserter(res);
70  iteratorwriter<output_itor> dst(inserter);
71  input_itor i1 = source.begin();
72  input_itor i2 = source.end();
73 
74  while(i1 != i2) {
76  }
77  }
78  catch(const utf8::invalid_utf8_exception&)
79  {
80  // TODO: use a ERR_.. stream but I don't know whether I can do so in header easily.
81  std::cerr << "Failed to convert a string from " << impl_reader::get_name() << " to " << impl_writer::get_name() << "\n";
82  return res;
83  }
84  return res;
85 }
86 
87 /**
88  * @tparam TD Output, a collection type.
89  *
90  * @return An instance of TD.
91  */
92 template<typename TD>
93 TD unicode_cast(char32_t onechar)
94 {
95  using namespace ucs4_convert_impl;
96  typedef typename convert_impl<typename TD::value_type>::type impl_writer;
97  typedef convert_impl<char32_t>::type impl_reader;
98  typedef typename std::back_insert_iterator<TD> output_itor;
99 
100  TD res;
101  try
102  {
103  output_itor inserter(res);
104  iteratorwriter<output_itor> dst(inserter);
105  impl_writer::write (dst, onechar);
106  }
107  catch(const utf8::invalid_utf8_exception&)
108  {
109  // TODO: use a ERR_.. stream but I don't know whether I can do so in header easily.
110  std::cerr << "Failed to convert a string from " << impl_reader::get_name() << " to " << impl_writer::get_name() << "\n";
111  return res;
112  }
113  return res;
114 }
ucs4_convert_impl::enableif< TD, typename TS::value_type >::type unicode_cast(const TS &source)
Transforms an output iterator to a writer for ucs4_convert_impl functions.
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
Definition: parser.cpp:749
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:612
Thrown by operations encountering invalid UTF-8 data.