The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unicode_cast.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
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 #ifndef SERIALIZATION_UNICODE_CAST_HPP_INCLUDED
15 #define SERIALIZATION_UNICODE_CAST_HPP_INCLUDED
16 
17 #include "ucs4_convert_impl.hpp"
18 #include <iostream> //for std::cerr
19 #include <iterator>
20 #include <boost/utility/enable_if.hpp>
21 #include <boost/type_traits/is_arithmetic.hpp>
22 
23 namespace ucs4_convert_impl
24 {
25  /**
26  * Transforms an output iterator to a writer for ucs4_convert_impl functions.
27  */
28  template<typename oitor_t>
30  {
31  oitor_t& out_;
32  iteratorwriter(oitor_t& out) : out_(out) {}
33 
34  bool can_push(size_t /*count*/)
35  {
36  return true;
37  }
38  template<typename value_type>
39  void push(value_type val)
40  {
41  *out_++ = val;
42  }
43  };
44  template<typename Tret, typename Tcheck>
45  struct enableif
46  {
47  typedef Tcheck ignore;
48  typedef Tret type;
49  };
50 }
51 
52 /**
53  * @tparam TD Output, a collection type.
54  * @tparam TS Input, a collection type.
55  *
56  * @return An instance of TD.
57  */
58 template<typename TD , typename TS>
60 //TD unicode_cast(const TS& source)
61 {
62  using namespace ucs4_convert_impl;
63  typedef typename convert_impl<typename TD::value_type>::type t_impl_writer;
64  typedef typename convert_impl<typename TS::value_type>::type t_impl_reader;
65  typedef typename std::back_insert_iterator<TD> t_outputitor;
66  typedef typename TS::const_iterator t_inputitor;
67 
68  TD res;
69  try
70  {
71  t_outputitor inserter(res);
73  t_inputitor i1 = source.begin();
74  t_inputitor i2 = source.end();
75 
76  while(i1 != i2) {
78  }
79  }
81  {
82  // TODO: use a ERR_.. stream but i dont know whether i can so to in header easily.
83  std::cerr << "Failed to convert a string from " << t_impl_reader::get_name() << " to " << t_impl_writer::get_name() << "\n";
84  return res;
85  }
86  return res;
87 }
88 
89 /**
90  * @tparam TD Output, a collection type.
91  *
92  * @return An instance of TD.
93  */
94 template<typename TD>
96 {
97  using namespace ucs4_convert_impl;
98  typedef typename convert_impl<typename TD::value_type>::type t_impl_writer;
99  typedef convert_impl<ucs4::char_t>::type t_impl_reader;
100  typedef typename std::back_insert_iterator<TD> t_outputitor;
101 
102  TD res;
103  try
104  {
105  t_outputitor inserter(res);
107  t_impl_writer::write (dst, onechar);
108  }
110  {
111  // TODO: use a ERR_.. stream but i dont know whether i can so to in header easily.
112  std::cerr << "Failed to convert a string from " << t_impl_reader::get_name() << " to " << t_impl_writer::get_name() << "\n";
113  return res;
114  }
115  return res;
116 }
117 #endif
ucs4_convert_impl::enableif< TD, typename TS::value_type >::type unicode_cast(const TS &source)
GLuint const GLfloat * val
Definition: glew.h:2614
Transforms an output iterator to a writer for ucs4_convert_impl functions.
GLenum GLenum dst
Definition: glew.h:2392
GLuint res
Definition: glew.h:9258
Thrown by operations encountering invalid UTF-8 data.
boost::uint32_t char_t
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:400
void write(std::ostream &out, configr_of const &cfg, unsigned int level)
Definition: parser.cpp:621
GLsizei GLsizei GLchar * source
Definition: glew.h:1800