The Battle for Wesnoth  1.15.0-dev
tstring.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2018 by Philippe Plantier <ayin@anathas.org>
3  Part of the Battle for Wesnoth Project https://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 <memory>
18 #include <string>
19 
20 /**
21  * Helper class for translatable strings.
22  */
23 class t_string;
25 {
26 public:
27  class walker
28  {
29  public:
30  explicit walker(const t_string_base& string);
31 
32  void next() { begin_ = end_; update(); }
33  bool eos() const { return begin_ == string_.size(); }
34  bool last() const { return end_ == string_.size(); }
35  bool translatable() const { return translatable_; }
36  bool countable() const { return countable_; }
37  int count() const { return count_; }
38  const std::string& textdomain() const { return textdomain_; }
39  std::string::const_iterator begin() const { return string_.begin() + begin_; }
40  std::string::const_iterator end() const { return string_.begin() + end_; }
41  std::string::const_iterator plural_begin() const;
42  std::string::const_iterator plural_end() const;
43 
44  private:
45  void update();
46 
47  const std::string& string_;
48  std::string::size_type begin_;
49  std::string::size_type end_;
50  std::string textdomain_;
52  int count_;
53  };
54 
55  friend class walker;
56 
57  t_string_base();
58 
59  /** Default implementation, but defined out-of-line for efficiency reasons. */
61 
62  /** Default implementation, but defined out-of-line for efficiency reasons. */
64  t_string_base(const std::string& string);
65  t_string_base(const std::string& string, const std::string& textdomain);
66  t_string_base(const std::string& sing, const std::string& pl, int count, const std::string& textdomain);
67  t_string_base(const char* string);
68 
69  static t_string_base from_serialized(const std::string& string);
70  std::string to_serialized() const;
71 
72  /** Default implementation, but defined out-of-line for efficiency reasons. */
74  t_string_base& operator=(const std::string&);
75  t_string_base& operator=(const char*);
76 
78  t_string_base operator+(const std::string&) const;
79  t_string_base operator+(const char*) const;
80 
82  t_string_base& operator+=(const std::string&);
83  t_string_base& operator+=(const char*);
84 
85  bool operator==(const t_string_base &) const;
86  bool operator==(const std::string &) const;
87  bool operator==(const char* string) const;
88 
89  bool operator!=(const t_string_base &that) const
90  { return !operator==(that); }
91  bool operator!=(const std::string &that) const
92  { return !operator==(that); }
93  bool operator!=(const char *that) const
94  { return !operator==(that); }
95 
96  bool operator<(const t_string_base& string) const;
97 
98  bool empty() const { return value_.empty(); }
99  std::string::size_type size() const { return str().size(); }
100 
101  operator const std::string&() const { return str(); }
102  const std::string& str() const;
103  const char* c_str() const { return str().c_str(); }
104  bool translatable() const { return translatable_; }
105 
106  // Warning: value() may contain platform dependent prefix bytes !
107  // Consider base_str() for a more reliable untranslated string
108  const std::string& value() const { return value_; }
109  std::string base_str() const;
110 
111  std::size_t hash_value() const;
112 
113 private:
114  std::string value_;
115  mutable std::string translated_value_;
116  mutable unsigned translation_timestamp_;
118 };
119 
120 inline std::size_t hash_value(const t_string_base& str) { return str.hash_value(); }
121 std::ostream& operator<<(std::ostream&, const t_string_base&);
122 
123 class t_string
124 {
125 public:
128 
129  /** Default implementation, but defined out-of-line for efficiency reasons. */
130  t_string();
131 
132  /** Default implementation, but defined out-of-line for efficiency reasons. */
133  ~t_string();
134 
135  /** Default implementation, but defined out-of-line for efficiency reasons. */
136  t_string(const t_string&);
137 
138  /** Default implementation, but defined out-of-line for efficiency reasons. */
139  t_string& operator=(const t_string&);
140 
141  t_string(const base &);
142  t_string(const char *);
143  t_string(const std::string &);
144  t_string(const std::string &str, const std::string &textdomain);
145  t_string(const std::string& sing, const std::string& pl, int count, const std::string& textdomain);
146 
147  t_string& operator=(const char *o);
148 
149  static t_string from_serialized(const std::string& string) { return t_string(base::from_serialized(string)); }
150  std::string to_serialized() const { return get().to_serialized(); }
151 
152  operator const t_string_base &() const { return get(); }
153 
154  t_string operator+(const t_string& o) const { return get() + o.get(); }
155  t_string operator+(const std::string& o) const { return get() + o; }
156  t_string operator+(const char* o) const { return get() + o; }
157 
158 private:
159  template<typename T>
160  void increase_impl(const T& other)
161  {
162  base * nw = new base(get());
163  *nw += other;
164  val_.reset(nw);
165  }
166 
167 public:
168  t_string& operator+=(const t_string& o) { increase_impl(o.get()); return *this; }
169  t_string& operator+=(const std::string& o) { increase_impl(o); return *this; }
170  t_string& operator+=(const char* o) { increase_impl(o); return *this; }
171 
172  bool operator==(const t_string& o) const { return get() == o.get(); }
173  bool operator==(const std::string& o) const { return get() == o; }
174  bool operator==(const char* o) const { return get() == o; }
175 
176  bool operator!=(const t_string& o) const { return !operator==(o); }
177  bool operator!=(const std::string& o) const { return !operator==(o); }
178  bool operator!=(const char* o) const { return !operator==(o); }
179 
180  bool operator<(const t_string& o) const { return get() < o.get(); }
181 
182  bool empty() const { return get().empty(); }
183  std::string::size_type size() const { return get().size(); }
184 
185  operator const std::string&() const { return get(); }
186  const std::string& str() const { return get().str(); }
187  const char* c_str() const { return get().c_str(); }
188  bool translatable() const { return get().translatable(); }
189  const std::string& value() const { return get().value(); }
190  std::string base_str() const { return get().base_str(); }
191 
192  static void add_textdomain(const std::string &name, const std::string &path);
193  static void reset_translations();
194 
195  const t_string_base& get() const { return *val_; }
196  void swap(t_string& other) { val_.swap(other.val_); }
197 
198 private:
199  //never null
200  std::shared_ptr<const t_string_base> val_;
201 };
202 
203 /** Implement non-member swap function for std::swap (calls @ref t_string::swap). */
204 void swap(t_string& lhs, t_string& rhs);
205 
206 inline std::ostream& operator<<(std::ostream& os, const t_string& str) { return os << str.get(); }
207 inline bool operator==(const std::string &a, const t_string& b) { return b == a; }
208 inline bool operator==(const char *a, const t_string& b) { return b == a; }
209 inline bool operator!=(const std::string &a, const t_string& b) { return b != a; }
210 inline bool operator!=(const char *a, const t_string& b) { return b != a; }
211 inline t_string operator+(const std::string &a, const t_string& b) { return t_string(a) + b; }
212 inline t_string operator+(const char *a, const t_string& b) { return t_string(a) + b; }
std::string::const_iterator begin() const
Definition: tstring.hpp:39
t_string & operator+=(const t_string &o)
Definition: tstring.hpp:168
t_string_base base
Definition: tstring.hpp:126
bool operator!=(const t_string_base &that) const
Definition: tstring.hpp:89
std::string base_str() const
Definition: tstring.cpp:359
std::string::const_iterator plural_begin() const
Definition: tstring.cpp:200
bool operator==(const std::string &o) const
Definition: tstring.hpp:173
bool translatable() const
Definition: tstring.hpp:35
bool operator!=(const std::string &o) const
Definition: tstring.hpp:177
t_string operator+(const char *o) const
Definition: tstring.hpp:156
void increase_impl(const T &other)
Definition: tstring.hpp:160
#define a
~t_string_base()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:232
t_string & operator+=(const std::string &o)
Definition: tstring.hpp:169
bool translatable() const
Definition: tstring.hpp:104
std::string to_serialized() const
Definition: tstring.cpp:369
t_string operator+(const t_string &o) const
Definition: tstring.hpp:154
#define val_(o)
Definition: lobject.h:123
void swap(t_string &other)
Definition: tstring.hpp:196
bool operator!=(const char *o) const
Definition: tstring.hpp:178
std::string value_
Definition: tstring.hpp:114
std::string::size_type end_
Definition: tstring.hpp:49
bool operator==(const t_string &o) const
Definition: tstring.hpp:172
bool operator!=(const t_string &o) const
Definition: tstring.hpp:176
const char * c_str() const
Definition: tstring.hpp:187
const std::string & string_
Definition: tstring.hpp:47
bool operator==(const t_string_base &) const
Definition: tstring.cpp:541
const std::string & value() const
Definition: tstring.hpp:108
bool last_untranslatable_
Definition: tstring.hpp:117
std::string::size_type size() const
Definition: tstring.hpp:99
const t_string_base & get() const
Definition: tstring.hpp:195
bool operator<(const t_string_base &string) const
Definition: tstring.cpp:556
const std::string & str() const
Definition: tstring.cpp:561
std::string textdomain_
Definition: tstring.hpp:50
bool eos() const
Definition: tstring.hpp:33
t_string_base & operator=(const t_string_base &)
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:392
unsigned translation_timestamp_
Definition: tstring.hpp:116
std::string::size_type size() const
Definition: tstring.hpp:183
std::string translated_value_
Definition: tstring.hpp:115
#define b
std::string::size_type begin_
Definition: tstring.hpp:48
std::ostream & operator<<(std::ostream &, const t_string_base &)
Definition: tstring.cpp:664
void swap(t_string &lhs, t_string &rhs)
Implement non-member swap function for std::swap (calls t_string::swap).
Definition: tstring.cpp:659
const t_string name
bool countable() const
Definition: tstring.hpp:36
t_string operator+(const std::string &o) const
Definition: tstring.hpp:155
const std::string & textdomain() const
Definition: tstring.hpp:38
std::size_t hash_value() const
Definition: tstring.cpp:50
walker(const t_string_base &string)
Definition: tstring.cpp:59
std::string::const_iterator plural_end() const
Definition: tstring.cpp:209
int count() const
Definition: tstring.hpp:37
static t_string from_serialized(const std::string &string)
Definition: tstring.hpp:149
std::string to_serialized() const
Definition: tstring.hpp:150
bool empty() const
Definition: tstring.hpp:98
bool operator!=(const char *that) const
Definition: tstring.hpp:93
const std::string & value() const
Definition: tstring.hpp:189
std::string::const_iterator end() const
Definition: tstring.hpp:40
t_string & operator+=(const char *o)
Definition: tstring.hpp:170
bool empty() const
Definition: tstring.hpp:182
std::shared_ptr< const t_string_base > val_
Definition: tstring.hpp:200
static map_location::DIRECTION nw
bool operator!=(const std::string &that) const
Definition: tstring.hpp:91
bool last() const
Definition: tstring.hpp:34
t_string_base::walker walker
Definition: tstring.hpp:127
std::string base_str() const
Definition: tstring.hpp:190
t_string_base & operator+=(const t_string_base &)
Definition: tstring.cpp:446
bool translatable() const
Definition: tstring.hpp:188
bool operator<(const t_string &o) const
Definition: tstring.hpp:180
t_string_base operator+(const t_string_base &) const
Definition: tstring.cpp:425
bool operator==(const char *o) const
Definition: tstring.hpp:174
const std::string & str() const
Definition: tstring.hpp:186
bool translatable_
Definition: tstring.hpp:117
std::string path
File path.
const char * c_str() const
Definition: tstring.hpp:103
static t_string_base from_serialized(const std::string &string)
Definition: tstring.cpp:334