]> git.lyx.org Git - lyx.git/blobdiff - src/support/trivstring.h
Backport one more deprecation fix
[lyx.git] / src / support / trivstring.h
index 4123d32ed14d96855962afd19d93996b16c60c1c..6b935eee02eb325ad6cbe200de1334ba29b91cf5 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "support/strfwd.h"
 
+#ifdef STD_STRING_USES_COW
 #include <cstdlib>
 
 namespace lyx {
@@ -31,22 +32,29 @@ namespace lyx {
  * This class should not be used for anything else than providing thread-safety.
  * It should be removed as soon as LyX requires C++11, and all supported STL
  * implementations provide a C++11 conformant std::basic_string.
+ *
+ * If you change anything in this class please ensure that the unit test
+ * tests/check_trivstring.cpp still tests 100% of the public interface.
  */
 template <typename Char> class trivial_string
 {
 public:
+       /// Corresponding std::basic_string
+       typedef std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > _stdstring;
        /// Construct an empty string
        trivial_string() : size_(0), data_(0) {}
        /// Construct a string from a copy of \p that
        trivial_string(trivial_string const & that);
        /// Construct a string from a copy of \p that
-       trivial_string(std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > const & that);
+       trivial_string(Char const * that, size_t n);
+       /// Construct a string from a copy of \p that
+       trivial_string(_stdstring const & that);
        ///
        ~trivial_string() { if (!use_sso()) delete[] data_; }
        /// Assign a copy of \p that
        trivial_string & operator=(trivial_string const & that);
        /// Assign a copy of \p that
-       trivial_string & operator=(std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > const & that);
+       trivial_string & operator=(_stdstring const & that);
        /// Exchange contents with contents of \p that
        void swap(trivial_string & that);
        /// The length of the string, excluding the final 0 character
@@ -55,11 +63,15 @@ public:
        bool empty() const { return size_ == 0; }
        /// Is this string ordered before, at the same position or after \p other?
        int compare(trivial_string const & other) const;
+       /// Return substring of length \p n starting at \p pos
+       trivial_string substr(size_t pos = 0, size_t n = _stdstring::npos) const;
        /// Create a copy as std::basic_string
-       std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > str() const;
+       operator _stdstring() const;
        /// Return a C-compatible string, terminated by a 0 character.
        /// This is never a copy and only valid for the life time of the trivial_string instance.
        Char const * c_str() const;
+       /// Return character at position \p i (validity of \i is not checked)
+       Char operator[](size_t i) const;
 private:
        /**
         * Whether short string optimization is used.
@@ -80,8 +92,25 @@ private:
        /// The character storage
        Char * data_;
 };
-template <typename Char> bool operator<(trivial_string<Char> const & lhs, trivial_string<Char> const &rhs);
 
 
+/// Comparison operator (needed for std::set etc)
+template <typename Char> bool operator<(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs);
+
+
+/// Equality operator
+template <typename Char> bool operator==(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs);
+template <typename Char> bool operator==(trivial_string<Char> const & lhs, Char const * rhs);
+template <typename Char> bool operator==(Char const * lhs, trivial_string<Char> const & rhs);
+
+
+/// Stream output operator
+template <typename Char>
+std::basic_ostream<Char, std::char_traits<Char> > &
+operator<<(std::basic_ostream<Char, std::char_traits<Char> > &, trivial_string<Char> const &);
+
 } // namespace lyx
+#else
+#include <string>
+#endif
 #endif