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