Class mutable_string_view
Synopsis
#include <include/sajson.h>
class mutable_string_view
Description
A pointer to a mutable buffer, its size in bytes, and strong ownership of any copied memory.
Mentioned in
- Examples / main.cpp
- Examples / zero-allocation.cpp
Methods
mutable_string_view overload | Creates an empty, zero-sized view. | |
mutable_string_view overload | Given a length in bytes and a pointer, constructs a view that does not allocate a copy of the data or maintain its life | |
mutable_string_view overload | Allocates a copy of the given literal string and exposes a mutable view into it | |
mutable_string_view overload | Allocates a copy of the given string and exposes a mutable view into it | |
mutable_string_view overload | Copies a mutable_string_view | |
mutable_string_view overload | Move constructor - neuters the old mutable_string_view. | |
get_data | ||
length | ||
operator= overload |
Source
Lines 280-360 in include/sajson.h.
class mutable_string_view {
public:
/// Creates an empty, zero-sized view.
mutable_string_view()
: length_(0)
, data(0)
, buffer() {}
/// Given a length in bytes and a pointer, constructs a view
/// that does not allocate a copy of the data or maintain its life.
/// The given pointer must stay valid for the duration of the parse and the
/// resulting \ref document's life.
mutable_string_view(size_t length, char* data_)
: length_(length)
, data(data_)
, buffer() {}
/// Allocates a copy of the given \ref literal string and exposes a
/// mutable view into it. Throws std::bad_alloc if allocation fails.
mutable_string_view(const literal& s)
: length_(s.length())
, buffer(length_) {
data = buffer.get_data();
memcpy(data, s.data(), length_);
}
/// Allocates a copy of the given \ref string and exposes a mutable view
/// into it. Throws std::bad_alloc if allocation fails.
mutable_string_view(const string& s)
: length_(s.length())
, buffer(length_) {
data = buffer.get_data();
memcpy(data, s.data(), length_);
}
/// Copies a mutable_string_view. If any backing memory has been
/// allocated, its refcount is incremented - both views can safely
/// use the memory.
mutable_string_view(const mutable_string_view& that)
: length_(that.length_)
, data(that.data)
, buffer(that.buffer) {}
/// Move constructor - neuters the old mutable_string_view.
mutable_string_view(mutable_string_view&& that)
: length_(that.length_)
, data(that.data)
, buffer(std::move(that.buffer)) {
that.length_ = 0;
that.data = 0;
}
mutable_string_view& operator=(mutable_string_view&& that) {
if (this != &that) {
length_ = that.length_;
data = that.data;
buffer = std::move(that.buffer);
that.length_ = 0;
that.data = 0;
}
return *this;
}
mutable_string_view& operator=(const mutable_string_view& that) {
if (this != &that) {
length_ = that.length_;
data = that.data;
buffer = that.buffer;
}
return *this;
}
size_t length() const { return length_; }
char* get_data() const { return data; }
private:
size_t length_;
char* data;
internal::allocated_buffer buffer; // may not be allocated
};