tstring.hpp

Go to the documentation of this file.
00001 /* $Id: tstring.hpp 26083 2008-04-25 01:55:51Z soliton $ */
00002 /*
00003    Copyright (C) 2004 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 #ifndef TSTRING_H_INCLUDED
00016 #define TSTRING_H_INCLUDED
00017 
00018 #include <string>
00019 
00020 class t_string
00021 {
00022 public:
00023     class walker
00024     {
00025     public:
00026         walker(const t_string& string);
00027         walker(const std::string&);
00028 
00029         void next()                               { begin_ = end_; update(); }
00030         bool eos() const                          { return begin_ == string_.size(); }
00031         bool last() const                         { return end_ == string_.size(); }
00032         bool translatable() const                 { return translatable_; }
00033         const std::string& textdomain() const     { return textdomain_; }
00034         std::string::const_iterator begin() const { return string_.begin() + begin_; }
00035         std::string::const_iterator end() const   { return string_.begin() + end_; }
00036     private:
00037         void update(void);
00038 
00039         const std::string& string_;
00040         std::string::size_type begin_;
00041         std::string::size_type end_;
00042         std::string textdomain_;
00043         bool translatable_;
00044     };
00045 
00046     friend class walker;
00047 
00048     t_string();
00049     t_string(const t_string&);
00050     t_string(const std::string& string);
00051     t_string(const std::string& string, const std::string& textdomain);
00052     t_string(const char* string);
00053 
00054     static t_string from_serialized(const std::string& string);
00055     std::string to_serialized() const;
00056 
00057     t_string& operator=(const t_string&);
00058     t_string& operator=(const std::string&);
00059     t_string& operator=(const char*);
00060 
00061     t_string operator+(const t_string&) const;
00062     t_string operator+(const std::string&) const;
00063     t_string operator+(const char*) const;
00064 
00065     t_string& operator+=(const t_string&);
00066     t_string& operator+=(const std::string&);
00067     t_string& operator+=(const char*);
00068 
00069     bool operator==(const t_string& string) const    { return string.translatable_ == translatable_ && string.value_ == value_; }
00070     bool operator==(const std::string& string) const { return !translatable_ && value_ == string; }
00071     bool operator==(const char* string) const        { return !translatable_ && value_ == string; }
00072     bool operator!=(const t_string& string) const    { return !(*this == string); }
00073     bool operator!=(const std::string& string) const { return !(*this == string); }
00074     bool operator!=(const char* string) const        { return !(*this == string); }
00075 
00076     bool operator<(const t_string& string) const     { return value_ < string.value_; }
00077 
00078     bool empty() const                               { return value_.empty(); }
00079     std::string::size_type size() const              { return str().size(); }
00080 
00081     operator const std::string&() const              { return str(); }
00082     const std::string& str() const;
00083     const char* c_str() const                        { return str().c_str(); }
00084 
00085     // Warning: value() may contain platform dependant prefix bytes !
00086     // Consider base_str() for a more reliable untranslated string
00087     const std::string& value() const                 { return value_; }
00088     const std::string base_str() const;
00089 
00090     void reset_translation() const                   { translated_value_ = ""; }
00091 
00092     static void add_textdomain(const std::string& name, const std::string& path);
00093 private:
00094     std::string value_;
00095     mutable std::string translated_value_;
00096     bool translatable_, last_untranslatable_;
00097 };
00098 
00099 std::ostream& operator<<(std::ostream&, const t_string&);
00100 inline bool operator==(const std::string& a, const t_string& b)    { return a == b.str(); }
00101 inline bool operator==(const char* a, const t_string& b)           { return b == a; }
00102 inline bool operator!=(const std::string& a, const t_string& b)    { return a != b.str(); }
00103 inline bool operator!=(const char* a, const t_string& b)           { return b != a; }
00104 inline t_string operator+(const std::string& a, const t_string& b) { return t_string(a + b.str()); }
00105 inline t_string operator+(const char* a, const t_string& b)        { return t_string(a) + b; }
00106 
00107 #endif
00108 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs