Add a Buffer that stores initial data on stack.

This commit is contained in:
Victor Zverovich
2012-12-10 17:16:08 -08:00
parent 31a507034e
commit 198ebe9cf6
2 changed files with 125 additions and 40 deletions
+100 -19
View File
@@ -25,11 +25,89 @@ class ArgFormatter;
template <typename Callback>
class ArgFormatterWithCallback;
// A buffer with the first SIZE elements stored in the object itself.
template <typename T, std::size_t SIZE>
class Buffer {
private:
std::size_t size_;
std::size_t capacity_;
T *ptr_;
T data_[SIZE];
void Grow(std::size_t size) {
capacity_ = std::max(size, capacity_ + capacity_ / 2);
T *p = new T[capacity_];
std::copy(ptr_, ptr_ + size_, p);
if (ptr_ != data_)
delete [] ptr_;
ptr_ = p;
}
// Do not implement!
Buffer(const Buffer &);
void operator=(const Buffer &);
public:
Buffer() : size_(0), capacity_(SIZE), ptr_(data_) {}
~Buffer() {
if (ptr_ != data_) delete [] ptr_;
}
std::size_t size() const { return size_; }
std::size_t capacity() const { return capacity_; }
void resize(std::size_t size) {
if (size > capacity_)
Grow(size);
size_ = size;
}
void reserve(std::size_t capacity) {
if (capacity < capacity_)
Grow(capacity - capacity_);
}
void push_back(const T &value) {
if (size_ == capacity_)
Grow(size_ + 1);
ptr_[size_++] = value;
}
void append(const T *begin, const T *end) {
std::ptrdiff_t size = end - begin;
if (size_ + size > capacity_)
Grow(size);
std::copy(begin, end, ptr_ + size_);
size_ += size;
}
T &operator[](std::size_t index) { return ptr_[index]; }
const T &operator[](std::size_t index) const { return ptr_[index]; }
const T &back() const { return ptr_[size_ - 1]; }
T &back() { return ptr_[size_ - 1]; }
void clear() { size_ = 0; }
void Take(Buffer &other) {
if (ptr_ != data_)
delete [] ptr_;
size_ = other.size_;
if (other.ptr_ != data_) {
ptr_ = other.ptr_;
other.ptr_ = 0;
} else {
ptr_ = data_;
std::copy(other.ptr_, other.ptr_ + size_, data_);
}
}
};
// A sprintf-like formatter that automatically allocates enough storage to
// fit all the output.
class Formatter {
private:
std::vector<char> buffer_; // Output buffer.
Buffer<char, 500> buffer_; // Output buffer.
enum Type {
// Numeric types should go first.
@@ -40,7 +118,8 @@ class Formatter {
typedef void (Formatter::*FormatFunc)(const void *arg, int width);
// An argument.
public:
// A format argument.
struct Arg {
Type type;
union {
@@ -50,11 +129,9 @@ class Formatter {
long long_value;
unsigned long ulong_value;
long double long_double_value;
const void *pointer_value;
struct {
union {
const char *string_value;
const void *pointer_value;
};
const char *string_value;
std::size_t size;
};
struct {
@@ -63,6 +140,7 @@ class Formatter {
};
};
Arg() {}
explicit Arg(int value) : type(INT), int_value(value) {}
explicit Arg(unsigned value) : type(UINT), uint_value(value) {}
explicit Arg(long value) : type(LONG), long_value(value) {}
@@ -78,15 +156,13 @@ class Formatter {
: type(CUSTOM), custom_value(value), format(f) {}
};
std::vector<Arg> args_; // Format arguments.
Buffer<Arg, 10> args_; // Format arguments.
const char *format_; // Format string.
friend class ArgFormatter;
void Add(const Arg &arg) {
if (args_.empty())
args_.reserve(10);
args_.push_back(arg);
}
@@ -105,6 +181,11 @@ class Formatter {
void Format();
void Take(Formatter &f) {
buffer_.Take(f.buffer_);
args_.Take(f.args_);
}
public:
Formatter() : format_(0) {}
@@ -113,14 +194,10 @@ class Formatter {
template <typename Callback>
ArgFormatterWithCallback<Callback> FormatWithCallback(const char *format);
const char *c_str() const { return &buffer_[0]; }
const char *data() const { return &buffer_[0]; }
std::size_t size() const { return buffer_.size(); }
void Swap(Formatter &f) {
buffer_.swap(f.buffer_);
args_.swap(f.args_);
}
const char *data() const { return &buffer_[0]; }
const char *c_str() const { return &buffer_[0]; }
};
class ArgFormatter {
@@ -255,14 +332,17 @@ void Formatter::FormatCustomArg(const void *arg, int width) {
std::ostringstream os;
os << value;
std::string str(os.str());
buffer_.reserve(buffer_.size() + std::max<std::size_t>(width, str.size()));
buffer_.insert(buffer_.end(), str.begin(), str.end());
std::size_t offset = buffer_.size();
buffer_.resize(offset + std::max<std::size_t>(width, str.size()));
char *out = &buffer_[offset];
std::copy(str.begin(), str.end(), out);
if (width > str.size())
buffer_.resize(buffer_.size() + width - str.size(), ' ');
std::fill_n(out + str.size(), width - str.size(), ' ');
}
inline ArgFormatter Formatter::operator()(const char *format) {
format_ = format;
args_.clear();
return ArgFormatter(*this);
}
@@ -270,6 +350,7 @@ template <typename Callback>
ArgFormatterWithCallback<Callback>
Formatter::FormatWithCallback(const char *format) {
format_ = format;
args_.clear();
return ArgFormatterWithCallback<Callback>(*this);
}
@@ -286,7 +367,7 @@ class FullFormat : public ArgFormatter {
}
FullFormat(const FullFormat& other) : ArgFormatter(other) {
formatter_.Swap(other.formatter_);
formatter_.Take(other.formatter_);
}
~FullFormat() {