]> git.lyx.org Git - lyx.git/blob - src/support/trivstring.h
First version of trivstring class (bug #9336)
[lyx.git] / src / support / trivstring.h
1 // -*- C++ -*-
2 /**
3  * \file trivstring.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Georg Baum
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_TRIVSTRING_H
13 #define LYX_TRIVSTRING_H
14
15 #include "support/strfwd.h"
16
17 #include <cstdlib>
18
19 namespace lyx {
20
21 /**
22  * Trivial string class with almost no features.
23  * The public interface is a subset of the std::basic_string interface.
24  * The only important feature is that any read-only access does not need
25  * synchronization between multiple threads, i.e. it is thread-safe without
26  * locking.
27  * Therefore you can safely use a const trivial_string object in multiple
28  * threads at the same time. This is not the case for std::basic_string in some
29  * STL implementations (e. g. GNU libcstd++, see bug 9336 and
30  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=21334.
31  * This class should not be used for anything else than providing thread-safety.
32  * It should be removed as soon as LyX requires C++11, and all supported STL
33  * implementations provide a C++11 conformant std::basic_string.
34  */
35 template <typename Char> class trivial_string
36 {
37 public:
38         /// Construct an empty string
39         trivial_string() : size_(0), data_(0) {}
40         /// Construct a string from a copy of \p that
41         trivial_string(trivial_string const & that);
42         /// Construct a string from a copy of \p that
43         trivial_string(std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > const & that);
44         ///
45         ~trivial_string() { if (!use_sso()) delete[] data_; }
46         /// Assign a copy of \p that
47         trivial_string & operator=(trivial_string const & that);
48         /// Assign a copy of \p that
49         trivial_string & operator=(std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > const & that);
50         /// Exchange contents with contents of \p that
51         void swap(trivial_string & that);
52         /// The length of the string, excluding the final 0 character
53         size_t length() const { return size_; }
54         /// Is this string empty?
55         bool empty() const { return size_ == 0; }
56         /// Is this string ordered before, at the same position or after \p other?
57         int compare(trivial_string const & other) const;
58         /// Create a copy as std::basic_string
59         std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > str() const;
60         /// Return a C-compatible string, terminated by a 0 character.
61         /// This is never a copy and only valid for the life time of the trivial_string instance.
62         Char const * c_str() const;
63 private:
64         /**
65          * Whether short string optimization is used.
66          * Short string optimization is a technique where no additional memory
67          * needs to be allocated to store the string contents.
68          * Instead, the memory which would be used to store the pointer to the
69          * character buffer is reinterpreted to be a Char * buffer.
70          * On most 64 bit systems and with Char == char this allows to store
71          * strings of up to 7 characters without allocating additional memory.
72          */
73         bool use_sso() const { return (size_ + 1) * sizeof(Char) <= sizeof(Char *); }
74         /// The character storage if sso is used
75         Char       * data_sso()       { return reinterpret_cast<Char *      >(&data_); }
76         /// The character storage if sso is used
77         Char const * data_sso() const { return reinterpret_cast<Char const *>(&data_); }
78         /// The length of the string, excluding the final 0 character
79         size_t size_;
80         /// The character storage
81         Char * data_;
82 };
83 template <typename Char> bool operator<(trivial_string<Char> const & lhs, trivial_string<Char> const &rhs);
84
85
86 } // namespace lyx
87 #endif