Make Format class almost thread-safe

This is needed since all formats are stored in a global list which is shared
between threads, but never modfified except from the main thread.
The only missing bit is extension_list_, which is not so easy to do.
This commit is contained in:
Georg Baum 2014-12-21 21:40:25 +01:00
parent 90b1f084bf
commit 0af021878b
5 changed files with 91 additions and 17 deletions

View File

@ -13,6 +13,7 @@
#define FORMAT_H #define FORMAT_H
#include "support/docstring.h" #include "support/docstring.h"
#include "support/trivstring.h"
#include "OutputParams.h" #include "OutputParams.h"
@ -54,11 +55,11 @@ public:
/// Name fo the parent format /// Name fo the parent format
std::string const parentFormat() const; std::string const parentFormat() const;
/// ///
std::string const & name() const { return name_; } std::string const name() const { return name_; }
/// ///
void setName(std::string const & v) { name_ = v; } void setName(std::string const & v) { name_ = v; }
/// ///
std::string const & extension() const std::string const extension() const
{ {
return extension_list_.empty() ? empty_string() : extension_list_[0]; return extension_list_.empty() ? empty_string() : extension_list_[0];
} }
@ -67,23 +68,23 @@ public:
/// ///
void setExtensions(std::string const & v); void setExtensions(std::string const & v);
/// ///
std::string const & prettyname() const { return prettyname_; } std::string const prettyname() const { return prettyname_; }
/// ///
void setPrettyname(std::string const & v) { prettyname_ = v; } void setPrettyname(std::string const & v) { prettyname_ = v; }
/// ///
std::string const & shortcut() const { return shortcut_; } std::string const shortcut() const { return shortcut_; }
/// ///
void setShortcut(std::string const & v) { shortcut_ = v; } void setShortcut(std::string const & v) { shortcut_ = v; }
/// ///
std::string const & viewer() const { return viewer_; } std::string const viewer() const { return viewer_; }
/// ///
void setViewer(std::string const & v) { viewer_ = v; } void setViewer(std::string const & v) { viewer_ = v; }
/// ///
std::string const & editor() const { return editor_; } std::string const editor() const { return editor_; }
/// ///
void setEditor(std::string const & v) { editor_ = v; } void setEditor(std::string const & v) { editor_ = v; }
/// ///
std::string const & mime() const { return mime_; } std::string const mime() const { return mime_; }
/// ///
void setMime(std::string const & m) { mime_ = m; } void setMime(std::string const & m) { mime_ = m; }
/// ///
@ -101,13 +102,13 @@ public:
private: private:
/// Internal name. Needs to be unique. /// Internal name. Needs to be unique.
std::string name_; trivstring name_;
/// Filename extensions, the first one being the default /// Filename extensions, the first one being the default
std::vector<std::string> extension_list_; std::vector<std::string> extension_list_;
/// Name presented to the user. Needs to be unique. /// Name presented to the user. Needs to be unique.
std::string prettyname_; trivstring prettyname_;
/// Keyboard shortcut for the View and Export menu. /// Keyboard shortcut for the View and Export menu.
std::string shortcut_; trivstring shortcut_;
/*! /*!
* Viewer for this format. Needs to be in the PATH or an absolute * Viewer for this format. Needs to be in the PATH or an absolute
* filename. * filename.
@ -115,9 +116,9 @@ private:
* If it is \c auto the default viewer of the OS for this format is * If it is \c auto the default viewer of the OS for this format is
* used. * used.
*/ */
std::string viewer_; trivstring viewer_;
/// Editor for this format. \sa viewer_. /// Editor for this format. \sa viewer_.
std::string editor_; trivstring editor_;
/*! /*!
* Full MIME type, e.g. "text/x-tex". * Full MIME type, e.g. "text/x-tex".
* Only types listed by the shared MIME database of freedesktop.org * Only types listed by the shared MIME database of freedesktop.org
@ -125,7 +126,7 @@ private:
* This field may be empty, but it must be unique across all formats * This field may be empty, but it must be unique across all formats
* if it is set. * if it is set.
*/ */
std::string mime_; trivstring mime_;
/// ///
int flags_; int flags_;
}; };

View File

@ -68,6 +68,13 @@ void test_trivstring()
cout << (c == a) << ' ' << (a == c) << endl; // equal strings cout << (c == a) << ' ' << (a == c) << endl; // equal strings
cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length
cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length
// operator[]
cout << d[1] << d[0] << endl;
// substr()
cout << d.substr(1) << endl; // default argument
cout << d.substr(1, 1) << endl; // maximum length
cout << d.substr(1, 2) << endl; // length larger than max
cout << d.substr(2) << endl; // maximum pos
} }
void test_trivdocstring() void test_trivdocstring()
@ -129,6 +136,13 @@ void test_trivdocstring()
cout << (c == a) << ' ' << (a == c) << endl; // equal strings cout << (c == a) << ' ' << (a == c) << endl; // equal strings
cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length
cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length
// operator[]
cout << static_cast<char>(d[1]) << static_cast<char>(d[0]) << endl;
// substr()
cout << to_ascii(d.substr(1)) << endl; // default argument
cout << to_ascii(d.substr(1, 1)) << endl; // maximum length
cout << to_ascii(d.substr(1, 2)) << endl; // length larger than max
cout << to_ascii(d.substr(2)) << endl; // maximum pos
} }
int main() int main()

View File

@ -57,6 +57,11 @@ something which does not fit into sso
0 0 0 0
0 0 0 0
0 0 0 0
24
2
2
2
empty 0 empty 0
@ -116,3 +121,8 @@ something which does not fit into sso
0 0 0 0
0 0 0 0
0 0 0 0
24
2
2
2

View File

@ -16,6 +16,7 @@
#ifdef STD_STRING_USES_COW #ifdef STD_STRING_USES_COW
#include <algorithm> #include <algorithm>
#include <ostream> #include <ostream>
#include <stdexcept>
using namespace std; using namespace std;
@ -31,8 +32,27 @@ trivial_string<Char>::trivial_string(trivial_string const & that) : size_(that.s
else if (size_ > 0) { else if (size_ > 0) {
data_ = new Char[size_ + 1]; data_ = new Char[size_ + 1];
copy(that.data_, that.data_ + size_ + 1, data_); copy(that.data_, that.data_ + size_ + 1, data_);
} else {
// Happens only for really big Char types
data_ = 0;
}
}
template<typename Char>
trivial_string<Char>::trivial_string(Char const * that, size_t n) : size_(n)
{
if (use_sso()) {
copy(that, that + size_, data_sso());
data_sso()[size_] = '\0';
} else if (size_ > 0) {
data_ = new Char[size_ + 1];
copy(that, that + size_, data_);
data_[size_] = '\0';
} else {
// Happens only for really big Char types
data_ = 0;
} }
// else Happens only for really big Char types
} }
@ -50,8 +70,10 @@ trivial_string<Char>::trivial_string(
data_ = new Char[size_ + 1]; data_ = new Char[size_ + 1];
copy(that.begin(), that.end(), data_); copy(that.begin(), that.end(), data_);
data_[size_] = '\0'; data_[size_] = '\0';
} else {
// Happens only for really big Char types
data_ = 0;
} }
// else Happens only for really big Char types
} }
@ -138,6 +160,20 @@ int trivial_string<Char>::compare(trivial_string const & other) const
} }
template trivial_string<char> trivial_string<char>::substr(size_t, size_t) const;
template trivial_string<char_type> trivial_string<char_type>::substr(size_t, size_t) const;
template<typename Char>
trivial_string<Char> trivial_string<Char>::substr(size_t pos, size_t n) const
{
if (pos > length())
throw out_of_range("trivial_string::substr");
if (n == basic_string<Char, char_traits<Char>, allocator<Char> >::npos)
n = length() - pos;
size_t const l = min(pos + n, length());
return trivial_string(c_str() + pos, l - pos);
}
template trivial_string<char>::operator string() const; template trivial_string<char>::operator string() const;
template trivial_string<char_type>::operator docstring() const; template trivial_string<char_type>::operator docstring() const;
template<typename Char> template<typename Char>
@ -154,8 +190,6 @@ trivial_string<Char>::operator basic_string<Char, char_traits<Char>, allocator<C
} }
template char const * trivial_string<char>::c_str() const;
template char_type const * trivial_string<char_type>::c_str() const;
template<typename Char> Char const * trivial_string<Char>::c_str() const template<typename Char> Char const * trivial_string<Char>::c_str() const
{ {
if (use_sso()) if (use_sso())
@ -168,6 +202,14 @@ template<typename Char> Char const * trivial_string<Char>::c_str() const
} }
template char trivial_string<char>::operator[](size_t) const;
template char_type trivial_string<char_type>::operator[](size_t) const;
template <typename Char> Char trivial_string<Char>::operator[](size_t i) const
{
return c_str()[i];
}
template bool operator<(trivial_string<char> const &, template bool operator<(trivial_string<char> const &,
trivial_string<char> const &); trivial_string<char> const &);
template bool operator<(trivial_string<char_type> const &, template bool operator<(trivial_string<char_type> const &,

View File

@ -44,6 +44,8 @@ public:
/// Construct a string from a copy of \p that /// Construct a string from a copy of \p that
trivial_string(trivial_string const & that); trivial_string(trivial_string const & that);
/// Construct a string from a copy of \p that /// Construct a string from a copy of \p that
trivial_string(Char const * that, size_t n);
/// 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(std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > const & that);
/// ///
~trivial_string() { if (!use_sso()) delete[] data_; } ~trivial_string() { if (!use_sso()) delete[] data_; }
@ -59,11 +61,16 @@ public:
bool empty() const { return size_ == 0; } bool empty() const { return size_ == 0; }
/// Is this string ordered before, at the same position or after \p other? /// Is this string ordered before, at the same position or after \p other?
int compare(trivial_string const & other) const; 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 = std::basic_string<Char,
std::char_traits<Char>, std::allocator<Char> >::npos) const;
/// Create a copy as std::basic_string /// Create a copy as std::basic_string
operator std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> >() const; operator std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> >() const;
/// Return a C-compatible string, terminated by a 0 character. /// 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. /// This is never a copy and only valid for the life time of the trivial_string instance.
Char const * c_str() const; Char const * c_str() const;
/// Return character at position \p i (validity of \i is not checked)
Char operator[](size_t i) const;
private: private:
/** /**
* Whether short string optimization is used. * Whether short string optimization is used.