The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tstring.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[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 
15 #ifndef TSTRING_H_INCLUDED
16 #define TSTRING_H_INCLUDED
17 
18 #include <boost/shared_ptr.hpp>
19 #include <string>
20 
21 /**
22  * Helper class for translatable strings.
23  */
24 class t_string;
26 {
27 public:
28  class walker
29  {
30  public:
31  explicit walker(const t_string_base& string);
32 
33  void next() { begin_ = end_; update(); }
34  bool eos() const { return begin_ == string_.size(); }
35  bool last() const { return end_ == string_.size(); }
36  bool translatable() const { return translatable_; }
37  const std::string& textdomain() const { return textdomain_; }
38  std::string::const_iterator begin() const { return string_.begin() + begin_; }
39  std::string::const_iterator end() const { return string_.begin() + end_; }
40  private:
41  void update();
42 
44  std::string::size_type begin_;
45  std::string::size_type end_;
48  };
49 
50  friend class walker;
51 
52  t_string_base();
53  /** Default implementation, but defined out-of-line for efficiency reasons. */
55  /** Default implementation, but defined out-of-line for efficiency reasons. */
57  t_string_base(const std::string& string);
58  t_string_base(const std::string& string, const std::string& textdomain);
59  t_string_base(const char* string);
60 
61  static t_string_base from_serialized(const std::string& string);
62  std::string to_serialized() const;
63 
64  /** Default implementation, but defined out-of-line for efficiency reasons. */
67  t_string_base& operator=(const char*);
68 
70  t_string_base operator+(const std::string&) const;
71  t_string_base operator+(const char*) const;
72 
75  t_string_base& operator+=(const char*);
76 
77  bool operator==(const t_string_base &) const;
78  bool operator==(const std::string &) const;
79  bool operator==(const char* string) const;
80 
81  bool operator!=(const t_string_base &that) const
82  { return !operator==(that); }
83  bool operator!=(const std::string &that) const
84  { return !operator==(that); }
85  bool operator!=(const char *that) const
86  { return !operator==(that); }
87 
88  bool operator<(const t_string_base& string) const;
89 
90  bool empty() const { return value_.empty(); }
91  std::string::size_type size() const { return str().size(); }
92 
93  operator const std::string&() const { return str(); }
94  const std::string& str() const;
95  const char* c_str() const { return str().c_str(); }
96  bool translatable() const { return translatable_; }
97  // Warning: value() may contain platform dependent prefix bytes !
98  // Consider base_str() for a more reliable untranslated string
99  const std::string& value() const { return value_; }
100  std::string base_str() const;
101 
102  size_t hash_value() const;
103 private:
106  mutable unsigned translation_timestamp_;
108 };
109 
110 inline size_t hash_value(const t_string_base& str) { return str.hash_value(); }
111 std::ostream& operator<<(std::ostream&, const t_string_base&);
112 
113 class t_string
114 {
115 public:
118 
119  /** Default implementation, but defined out-of-line for efficiency reasons. */
120  t_string();
121  /** Default implementation, but defined out-of-line for efficiency reasons. */
122  ~t_string();
123  /** Default implementation, but defined out-of-line for efficiency reasons. */
124  t_string(const t_string &);
125  /** Default implementation, but defined out-of-line for efficiency reasons. */
126  t_string &operator=(const t_string &);
127 
128  t_string(const base &);
129  t_string(const char *);
130  t_string(const std::string &);
131  t_string(const std::string &str, const std::string &textdomain);
132 
133  t_string &operator=(const char *o);
134 
135  static t_string from_serialized(const std::string& string) { return t_string(base::from_serialized(string)); }
136  std::string to_serialized() const { return get().to_serialized(); }
137 
138  operator const t_string_base &() const { return get(); }
139 
140  t_string operator+(const t_string& o) const { return get() + o.get(); }
141  t_string operator+(const std::string& o) const { return get() + o; }
142  t_string operator+(const char* o) const { return get() + o; }
143 private:
144  template<typename T>
145  void increase_impl(const T& other)
146  {
147  base * nw = new base(get());
148  *nw += other;
149  val_.reset(nw);
150  }
151 public:
152  t_string& operator+=(const t_string& o) { increase_impl(o.get()); return *this; }
153  t_string& operator+=(const std::string& o) { increase_impl(o); return *this; }
154  t_string& operator+=(const char* o) { increase_impl(o); return *this; }
155 
156  bool operator==(const t_string& o) const { return get() == o.get(); }
157  bool operator==(const std::string& o) const { return get() == o; }
158  bool operator==(const char* o) const { return get() == o; }
159 
160  bool operator!=(const t_string& o) const { return !operator==(o); }
161  bool operator!=(const std::string& o) const { return !operator==(o); }
162  bool operator!=(const char* o) const { return !operator==(o); }
163 
164  bool operator<(const t_string& o) const { return get() < o.get(); }
165 
166  bool empty() const { return get().empty(); }
167  std::string::size_type size() const { return get().size(); }
168 
169  operator const std::string&() const { return get(); }
170  const std::string& str() const { return get().str(); }
171  const char* c_str() const { return get().c_str(); }
172  bool translatable() const { return get().translatable(); }
173  const std::string& value() const { return get().value(); }
174  std::string base_str() const { return get().base_str(); }
175 
176  static void add_textdomain(const std::string &name, const std::string &path);
177  static void reset_translations();
178 
179  const t_string_base& get() const { return *val_; }
180  void swap(t_string& other) { val_.swap(other.val_); }
181 private:
182  //never null
184 };
185 inline std::ostream& operator<<(std::ostream& os, const t_string& str) { return os << str.get(); }
186 inline bool operator==(const std::string &a, const t_string &b) { return b == a; }
187 inline bool operator==(const char *a, const t_string &b) { return b == a; }
188 inline bool operator!=(const std::string &a, const t_string &b) { return b != a; }
189 inline bool operator!=(const char *a, const t_string &b) { return b != a; }
190 inline t_string operator+(const std::string &a, const t_string &b) { return t_string(a) + b; }
191 inline t_string operator+(const char *a, const t_string &b) { return t_string(a) + b; }
192 #endif
193 
t_string & operator+=(const t_string &o)
Definition: tstring.hpp:152
bool translatable() const
Definition: tstring.hpp:36
t_string_base base
Definition: tstring.hpp:116
bool operator!=(const char *that) const
Definition: tstring.hpp:85
bool operator!=(const std::string &o) const
Definition: tstring.hpp:161
bool translatable() const
Definition: tstring.hpp:172
static void reset_translations()
Definition: tstring.cpp:506
t_string()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:457
bool operator!=(const t_string &o) const
Definition: tstring.hpp:160
t_string operator+(const std::string &a, const t_string &b)
Definition: tstring.hpp:190
bool translatable() const
Definition: tstring.hpp:96
std::string to_serialized() const
Definition: tstring.cpp:253
void increase_impl(const T &other)
Definition: tstring.hpp:145
~t_string_base()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:159
bool last() const
Definition: tstring.hpp:35
t_string & operator+=(const std::string &o)
Definition: tstring.hpp:153
size_t hash_value(const t_string_base &str)
Definition: tstring.hpp:110
const std::string & str() const
Definition: tstring.cpp:433
void swap(t_string &other)
Definition: tstring.hpp:180
bool operator<(const t_string_base &string) const
Definition: tstring.cpp:428
std::string value_
Definition: tstring.hpp:104
t_string_base operator+(const t_string_base &) const
Definition: tstring.cpp:310
std::string::size_type end_
Definition: tstring.hpp:45
bool operator!=(const std::string &that) const
Definition: tstring.hpp:83
const std::string & string_
Definition: tstring.hpp:43
const std::string & value() const
Definition: tstring.hpp:173
bool last_untranslatable_
Definition: tstring.hpp:107
std::string base_str() const
Definition: tstring.cpp:244
std::string textdomain_
Definition: tstring.hpp:46
t_string_base & operator=(const t_string_base &)
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:277
unsigned translation_timestamp_
Definition: tstring.hpp:106
std::string translated_value_
Definition: tstring.hpp:105
std::string::const_iterator begin() const
Definition: tstring.hpp:38
boost::shared_ptr< const t_string_base > val_
Definition: tstring.hpp:183
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
std::string::size_type begin_
Definition: tstring.hpp:44
bool empty() const
Definition: tstring.hpp:90
std::ostream & operator<<(std::ostream &, const t_string_base &)
Definition: tstring.cpp:511
GLsizei const char ** path
Definition: glew.h:4654
static void add_textdomain(const std::string &name, const std::string &path)
Definition: tstring.cpp:498
bool operator==(const t_string &o) const
Definition: tstring.hpp:156
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
t_string operator+(const t_string &o) const
Definition: tstring.hpp:140
const t_string_base & get() const
Definition: tstring.hpp:179
walker(const t_string_base &string)
Definition: tstring.cpp:61
const char * c_str() const
Definition: tstring.hpp:171
t_string & operator=(const t_string &)
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:485
bool operator!=(const t_string_base &that) const
Definition: tstring.hpp:81
size_t hash_value() const
Definition: tstring.cpp:53
bool operator!=(const char *o) const
Definition: tstring.hpp:162
std::string base_str() const
Definition: tstring.hpp:174
~t_string()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: tstring.cpp:461
bool eos() const
Definition: tstring.hpp:34
static t_string from_serialized(const std::string &string)
Definition: tstring.hpp:135
const std::string & value() const
Definition: tstring.hpp:99
std::string to_serialized() const
Definition: tstring.hpp:136
bool operator==(const std::string &a, const t_string &b)
Definition: tstring.hpp:186
GLuint const GLchar * name
Definition: glew.h:1782
t_string & operator+=(const char *o)
Definition: tstring.hpp:154
bool operator==(const std::string &o) const
Definition: tstring.hpp:157
static map_location::DIRECTION nw
const std::string & textdomain() const
Definition: tstring.hpp:37
t_string_base::walker walker
Definition: tstring.hpp:117
t_string operator+(const char *o) const
Definition: tstring.hpp:142
t_string_base & operator+=(const t_string_base &)
Definition: tstring.cpp:331
bool operator==(const t_string_base &) const
Definition: tstring.cpp:413
bool operator!=(const std::string &a, const t_string &b)
Definition: tstring.hpp:188
const std::string & str() const
Definition: tstring.hpp:170
const char * c_str() const
Definition: tstring.hpp:95
t_string operator+(const std::string &o) const
Definition: tstring.hpp:141
std::string::size_type size() const
Definition: tstring.hpp:167
std::string::const_iterator end() const
Definition: tstring.hpp:39
bool operator==(const char *o) const
Definition: tstring.hpp:158
bool operator<(const t_string &o) const
Definition: tstring.hpp:164
bool translatable_
Definition: tstring.hpp:107
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool empty() const
Definition: tstring.hpp:166
static t_string_base from_serialized(const std::string &string)
Definition: tstring.cpp:219
std::string::size_type size() const
Definition: tstring.hpp:91