Add std::basic_string allocator support to StringRef, StringBuffer and relatives (#441)

* - added basic_string allocator support to BasicStringRef, BasicCStringRef, BasicWriter::str, StringBuffer and BasicStringWriter

* - removed templated str() and to_string() function
- code style fixes
This commit is contained in:
Andrey Glebov 2016-12-26 19:36:56 +03:00 committed by Victor Zverovich
parent fac5546321
commit db780cb119
2 changed files with 30 additions and 17 deletions

View File

@ -440,7 +440,8 @@ class BasicFormatter;
/**
\rst
A string reference. It can be constructed from a C string or ``std::string``.
A string reference. It can be constructed from a C string or
``std::basic_string``.
You can use one of the following typedefs for common character types:
@ -483,10 +484,12 @@ class BasicStringRef {
/**
\rst
Constructs a string reference from an ``std::string`` object.
Constructs a string reference from a ``std::basic_string`` object.
\endrst
*/
BasicStringRef(const std::basic_string<Char> &s)
template <typename Allocator>
BasicStringRef(
const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
: data_(s.c_str()), size_(s.size()) {}
/**
@ -539,7 +542,7 @@ typedef BasicStringRef<wchar_t> WStringRef;
/**
\rst
A reference to a null terminated string. It can be constructed from a C
string or ``std::string``.
string or ``std::basic_string``.
You can use one of the following typedefs for common character types:
@ -572,10 +575,13 @@ class BasicCStringRef {
/**
\rst
Constructs a string reference from an ``std::string`` object.
Constructs a string reference from a ``std::basic_string`` object.
\endrst
*/
BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {}
template <typename Allocator>
BasicCStringRef(
const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
: data_(s.c_str()) {}
/** Returns the pointer to a C string. */
const Char *c_str() const { return data_; }

View File

@ -16,11 +16,14 @@ namespace fmt {
namespace internal {
// A buffer that stores data in ``std::string``.
template <typename Char>
// A buffer that stores data in ``std::basic_string``.
template <typename Char, typename Allocator = std::allocator<Char> >
class StringBuffer : public Buffer<Char> {
public:
typedef std::basic_string<Char, std::char_traits<Char>, Allocator> StringType;
private:
std::basic_string<Char> data_;
StringType data_;
protected:
virtual void grow(std::size_t size) FMT_OVERRIDE {
@ -30,8 +33,11 @@ class StringBuffer : public Buffer<Char> {
}
public:
explicit StringBuffer(const Allocator &allocator = Allocator())
: data_(allocator) {}
// Moves the data to ``str`` clearing the buffer.
void move_to(std::basic_string<Char> &str) {
void move_to(StringType &str) {
data_.resize(this->size_);
str.swap(data_);
this->capacity_ = this->size_ = 0;
@ -43,8 +49,8 @@ class StringBuffer : public Buffer<Char> {
/**
\rst
This class template provides operations for formatting and writing data
into a character stream. The output is stored in ``std::string`` that grows
dynamically.
into a character stream. The output is stored in a ``std::basic_string``
that grows dynamically.
You can use one of the following typedefs for common character types
and the standard allocator:
@ -68,13 +74,13 @@ class StringBuffer : public Buffer<Char> {
The answer is 42
The output can be moved to an ``std::string`` with ``out.move_to()``.
The output can be moved to a ``std::basic_string`` with ``out.move_to()``.
\endrst
*/
template <typename Char>
template <typename Char, typename Allocator = std::allocator<Char> >
class BasicStringWriter : public BasicWriter<Char> {
private:
internal::StringBuffer<Char> buffer_;
internal::StringBuffer<Char, Allocator> buffer_;
public:
/**
@ -82,14 +88,15 @@ class BasicStringWriter : public BasicWriter<Char> {
Constructs a :class:`fmt::BasicStringWriter` object.
\endrst
*/
BasicStringWriter() : BasicWriter<Char>(buffer_) {}
explicit BasicStringWriter(const Allocator &allocator = Allocator())
: BasicWriter<Char>(buffer_), buffer_(allocator) {}
/**
\rst
Moves the buffer content to *str* clearing the buffer.
\endrst
*/
void move_to(std::basic_string<Char> &str) {
void move_to(std::basic_string<Char, std::char_traits<Char>, Allocator> &str) {
buffer_.move_to(str);
}
};