The Battle for Wesnoth  1.17.12+dev
unicode_cast.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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 "log.hpp"
19 #include "ucs4_convert_impl.hpp"
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  PLAIN_LOG << "Failed to convert a string from " << impl_reader::get_name() << " to " << impl_writer::get_name();
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  PLAIN_LOG << "Failed to convert a string from " << impl_reader::get_name() << " to " << impl_writer::get_name();
110  return res;
111  }
112  return res;
113 }
#define PLAIN_LOG
Definition: log.hpp:258
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.
Standard logging facilities (interface).