tstring.hpp

Go to the documentation of this file.
00001 /* $Id: tstring.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2004 - 2012 by Philippe Plantier <ayin@anathas.org>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef TSTRING_H_INCLUDED
00017 #define TSTRING_H_INCLUDED
00018 
00019 #include "shared_object.hpp"
00020 
00021 #include <string>
00022 
00023 /**
00024  * Helper class for translatable strings.
00025  */
00026 class t_string;
00027 class t_string_base
00028 {
00029 public:
00030     class walker
00031     {
00032     public:
00033         walker(const t_string_base& string);
00034 #ifdef _MSC_VER
00035         /*
00036          * Work around a compiler bug in MSVC 9 see definition for more
00037          * information.
00038          */
00039         explicit walker(const std::string&);
00040         walker(const t_string& string);
00041 #endif
00042         void next()                               { begin_ = end_; update(); }
00043         bool eos() const                          { return begin_ == string_.size(); }
00044         bool last() const                         { return end_ == string_.size(); }
00045         bool translatable() const                 { return translatable_; }
00046         const std::string& textdomain() const     { return textdomain_; }
00047         std::string::const_iterator begin() const { return string_.begin() + begin_; }
00048         std::string::const_iterator end() const   { return string_.begin() + end_; }
00049     private:
00050         void update();
00051 
00052         const std::string& string_;
00053         std::string::size_type begin_;
00054         std::string::size_type end_;
00055         std::string textdomain_;
00056         bool translatable_;
00057     };
00058 
00059     friend class walker;
00060 
00061     t_string_base();
00062     /** Default implementation, but defined out-of-line for efficiency reasons. */
00063     ~t_string_base();
00064     /** Default implementation, but defined out-of-line for efficiency reasons. */
00065     t_string_base(const t_string_base&);
00066     t_string_base(const std::string& string);
00067     t_string_base(const std::string& string, const std::string& textdomain);
00068     t_string_base(const char* string);
00069 
00070     static t_string_base from_serialized(const std::string& string);
00071     std::string to_serialized() const;
00072 
00073     /** Default implementation, but defined out-of-line for efficiency reasons. */
00074     t_string_base& operator=(const t_string_base&);
00075     t_string_base& operator=(const std::string&);
00076     t_string_base& operator=(const char*);
00077 
00078     t_string_base operator+(const t_string_base&) const;
00079     t_string_base operator+(const std::string&) const;
00080     t_string_base operator+(const char*) const;
00081 
00082     t_string_base& operator+=(const t_string_base&);
00083     t_string_base& operator+=(const std::string&);
00084     t_string_base& operator+=(const char*);
00085 
00086     bool operator==(const t_string_base &) const;
00087     bool operator==(const std::string &) const;
00088     bool operator==(const char* string) const;
00089 
00090     bool operator!=(const t_string_base &that) const
00091     { return !operator==(that); }
00092     bool operator!=(const std::string &that) const
00093     { return !operator==(that); }
00094     bool operator!=(const char *that) const
00095     { return !operator==(that); }
00096 
00097     bool operator<(const t_string_base& string) const;
00098 
00099     bool empty() const                               { return value_.empty(); }
00100     std::string::size_type size() const              { return str().size(); }
00101 
00102     operator const std::string&() const              { return str(); }
00103     const std::string& str() const;
00104     const char* c_str() const                        { return str().c_str(); }
00105     bool translatable() const                        { return translatable_; }
00106     // Warning: value() may contain platform dependant prefix bytes !
00107     // Consider base_str() for a more reliable untranslated string
00108     const std::string& value() const                 { return value_; }
00109     std::string base_str() const;
00110 
00111     size_t hash_value() const;
00112 private:
00113     std::string value_;
00114     mutable std::string translated_value_;
00115     mutable unsigned translation_timestamp_;
00116     bool translatable_, last_untranslatable_;
00117 };
00118 
00119 inline size_t hash_value(const t_string_base& str) { return str.hash_value(); }
00120 std::ostream& operator<<(std::ostream&, const t_string_base&);
00121 
00122 class t_string :
00123     private shared_object<t_string_base> {
00124 public:
00125     typedef shared_object<t_string_base> super;
00126     typedef t_string_base base;
00127     typedef t_string_base::walker walker;
00128 
00129     /** Default implementation, but defined out-of-line for efficiency reasons. */
00130     t_string();
00131     /** Default implementation, but defined out-of-line for efficiency reasons. */
00132     ~t_string();
00133     /** Default implementation, but defined out-of-line for efficiency reasons. */
00134     t_string(const t_string &);
00135     /** Default implementation, but defined out-of-line for efficiency reasons. */
00136     t_string &operator=(const t_string &);
00137 
00138     t_string(const base &);
00139     t_string(const char *);
00140     t_string(const std::string &);
00141     t_string(const std::string &str, const std::string &textdomain);
00142 
00143     t_string &operator=(const char *o);
00144 
00145     static t_string from_serialized(const std::string& string) { return t_string(base::from_serialized(string)); }
00146     std::string to_serialized() const { return get().to_serialized(); }
00147 
00148     operator t_string_base() const { return get(); }
00149 
00150     t_string operator+(const t_string& o) const { return get() + o.get(); }
00151     t_string operator+(const std::string& o) const { return get() + o; }
00152     t_string operator+(const char* o) const { return get() + o; }
00153 
00154     t_string& operator+=(const t_string& o) { set(base(get()) += o.get()); return *this; }
00155     t_string& operator+=(const std::string& o) { set(base(get()) += o); return *this; }
00156     t_string& operator+=(const char* o) { set(base(get()) += o); return *this; }
00157 
00158     bool operator==(const t_string& o) const { return get() == o.get(); }
00159     bool operator==(const std::string& o) const { return get() == o; }
00160     bool operator==(const char* o) const { return get() == o; }
00161 
00162     bool operator!=(const t_string& o) const { return !operator==(o); }
00163     bool operator!=(const std::string& o) const { return !operator==(o); }
00164     bool operator!=(const char* o) const { return !operator==(o); }
00165 
00166     bool operator<(const t_string& o) const { return get() < o.get(); }
00167 
00168     bool empty() const { return get().empty(); }
00169     std::string::size_type size() const { return get().size(); }
00170 
00171     operator const std::string&() const { return get(); }
00172     const std::string& str() const { return get().str(); }
00173     const char* c_str() const { return get().c_str(); }
00174     bool translatable() const { return get().translatable(); }
00175     const std::string& value() const { return get().value(); }
00176     std::string base_str() const { return get().base_str(); }
00177 
00178     static void add_textdomain(const std::string &name, const std::string &path);
00179     static void reset_translations();
00180 
00181     const t_string_base& get() const { return super::get(); }
00182 };
00183 inline std::ostream& operator<<(std::ostream& os, const t_string& str) { return os << str.get(); }
00184 inline bool operator==(const std::string &a, const t_string &b)    { return b == a; }
00185 inline bool operator==(const char *a, const t_string &b)           { return b == a; }
00186 inline bool operator!=(const std::string &a, const t_string &b)    { return b != a; }
00187 inline bool operator!=(const char *a, const t_string &b)           { return b != a; }
00188 inline t_string operator+(const std::string &a, const t_string &b) { return t_string(a) + b; }
00189 inline t_string operator+(const char *a, const t_string &b)        { return t_string(a) + b; }
00190 #endif
00191 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:13 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs