utf8.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 by Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_UTF8_H
30 #define CXXTOOLS_UTF8_H
31 
32 #include <cxxtools/utf8codec.h>
33 #include <cxxtools/string.h>
35 
36 namespace cxxtools
37 {
39  {
40  String _constStr;
41 
42  public:
43  explicit Utf8OString(const String& str)
44  : _constStr(str)
45  { }
46 
47  const String& ustr() const
48  { return _constStr; }
49 
50  std::string str() const
51  { return encode<Utf8Codec>(_constStr); }
52 
53  operator std::string() const
54  { return encode<Utf8Codec>(_constStr); }
55 
56  };
57 
58  class Utf8IOString : public Utf8OString
59  {
60  std::string& _str;
61 
62  public:
63  explicit Utf8IOString(std::string& str)
64  : Utf8OString(decode<Utf8Codec>(str)),
65  _str(str)
66  { }
67 
68  std::string& str()
69  { return _str; }
70 
71  String ustr() const
72  { return decode<Utf8Codec>(_str); }
73 
74  operator String() const
75  { return decode<Utf8Codec>(_str); }
76 
77  };
78 
82  inline Utf8OString Utf8(const String& str)
83  {
84  return Utf8OString(str);
85  }
86 
87  inline Utf8OString Utf8(const std::string& str)
88  {
89  return Utf8OString(decode<Utf8Codec>(str));
90  }
91 
94  inline Utf8IOString Utf8(std::string& str)
95  {
96  return Utf8IOString(str);
97  }
98 
99  inline void operator<<= (SerializationInfo& si, const Utf8OString& str)
100  {
101  si <<= str.ustr();
102  }
103 
104  inline void operator>>= (const SerializationInfo& si, Utf8IOString str)
105  {
106  String s;
107  si >>= s;
108  str.str() = encode<Utf8Codec>(s);
109  }
110 
111 }
112 
113 #endif // CXXTOOLS_UTF8_H
114