add cs_valbuf

master
Daniel Kolesa 2021-03-19 22:40:11 +01:00
parent 2e725771e4
commit 0ca8561d5d
1 changed files with 24 additions and 18 deletions

View File

@ -194,25 +194,39 @@ struct cs_strman {
> counts; > counts;
}; };
struct cs_charbuf { template<typename T>
cs_charbuf() = delete; struct cs_valbuf {
cs_valbuf() = delete;
cs_charbuf(cs_shared_state &cs): cs_valbuf(cs_shared_state &cs):
buf{cs_shared_state::allocator<char>{&cs}} buf{cs_shared_state::allocator<T>{&cs}}
{} {}
cs_charbuf(cs_state &cs): cs_valbuf(cs_state &cs):
buf{cs_shared_state::allocator<char>{cs_get_sstate(cs)}} buf{cs_shared_state::allocator<T>{cs_get_sstate(cs)}}
{} {}
using size_type = std::size_t; using size_type = std::size_t;
using value_type = char; using value_type = T;
using reference = char &; using reference = T &;
using const_reference = char const &; using const_reference = T const &;
void reserve(std::size_t s) { buf.reserve(s); } void reserve(std::size_t s) { buf.reserve(s); }
void push_back(char c) { buf.push_back(c); } void push_back(T const &v) { buf.push_back(v); }
size_t size() const { return buf.size(); }
bool empty() const { return buf.empty(); }
void clear() { buf.clear(); }
std::vector<T, cs_shared_state::allocator<T>> buf;
};
struct cs_charbuf: cs_valbuf<char> {
cs_charbuf(cs_shared_state &cs): cs_valbuf<char>(cs) {}
cs_charbuf(cs_state &cs): cs_valbuf<char>(cs) {}
void append(char const *beg, char const *end) { void append(char const *beg, char const *end) {
buf.insert(buf.end(), beg, end); buf.insert(buf.end(), beg, end);
@ -229,14 +243,6 @@ struct cs_charbuf {
ostd::string_range str_term() { ostd::string_range str_term() {
return ostd::string_range{buf.data(), buf.data() + buf.size() - 1}; return ostd::string_range{buf.data(), buf.data() + buf.size() - 1};
} }
size_t size() const { return buf.size(); }
bool empty() const { return buf.empty(); }
void clear() { buf.clear(); }
std::vector<char, cs_shared_state::allocator<char>> buf;
}; };
} /* namespace cscript */ } /* namespace cscript */