2021-03-23 00:11:21 +00:00
|
|
|
#ifndef LIBCUBESCRIPT_STD_HH
|
|
|
|
#define LIBCUBESCRIPT_STD_HH
|
|
|
|
|
2021-03-23 00:45:35 +00:00
|
|
|
#include <cubescript/cubescript.hh>
|
|
|
|
|
2021-03-23 00:11:21 +00:00
|
|
|
#include <cstddef>
|
2021-03-24 01:42:33 +00:00
|
|
|
#include <new>
|
2021-03-23 00:11:21 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <type_traits>
|
2021-03-23 00:45:35 +00:00
|
|
|
#include <string_view>
|
2021-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
#include "cs_state.hh"
|
|
|
|
|
2021-03-23 22:32:25 +00:00
|
|
|
namespace cubescript {
|
2021-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
/* a value buffer */
|
|
|
|
|
|
|
|
template<typename T>
|
2021-03-23 22:29:32 +00:00
|
|
|
struct valbuf {
|
|
|
|
valbuf() = delete;
|
2021-03-23 00:11:21 +00:00
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
valbuf(internal_state *cs): buf{std_allocator<T>{cs}} {}
|
2021-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
using size_type = std::size_t;
|
|
|
|
using value_type = T;
|
|
|
|
using reference = T &;
|
|
|
|
using const_reference = T const &;
|
|
|
|
|
|
|
|
void reserve(std::size_t s) { buf.reserve(s); }
|
|
|
|
void resize(std::size_t s) { buf.resize(s); }
|
|
|
|
|
2021-03-24 21:37:53 +00:00
|
|
|
void resize(std::size_t s, value_type const &v) {
|
|
|
|
buf.resize(s, v);
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:11:21 +00:00
|
|
|
void append(T const *beg, T const *end) {
|
|
|
|
buf.insert(buf.end(), beg, end);
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:37:53 +00:00
|
|
|
template<typename ...A>
|
|
|
|
reference emplace_back(A &&...args) {
|
|
|
|
return buf.emplace_back(std::forward<A>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:11:21 +00:00
|
|
|
void push_back(T const &v) { buf.push_back(v); }
|
|
|
|
void pop_back() { buf.pop_back(); }
|
|
|
|
|
|
|
|
T &back() { return buf.back(); }
|
|
|
|
T const &back() const { return buf.back(); }
|
|
|
|
|
|
|
|
std::size_t size() const { return buf.size(); }
|
|
|
|
std::size_t capacity() const { return buf.capacity(); }
|
|
|
|
|
|
|
|
bool empty() const { return buf.empty(); }
|
|
|
|
|
|
|
|
void clear() { buf.clear(); }
|
|
|
|
|
|
|
|
T &operator[](std::size_t i) { return buf[i]; }
|
|
|
|
T const &operator[](std::size_t i) const { return buf[i]; }
|
|
|
|
|
|
|
|
T *data() { return &buf[0]; }
|
|
|
|
T const *data() const { return &buf[0]; }
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
std::vector<T, std_allocator<T>> buf;
|
2021-03-23 00:11:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* specialization of value buffer for bytes */
|
|
|
|
|
2021-03-23 22:29:32 +00:00
|
|
|
struct charbuf: valbuf<char> {
|
|
|
|
charbuf(internal_state *cs): valbuf<char>{cs} {}
|
2021-03-25 00:37:13 +00:00
|
|
|
charbuf(state &cs);
|
2021-03-26 01:59:42 +00:00
|
|
|
charbuf(thread_state &ts);
|
2021-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
void append(char const *beg, char const *end) {
|
2021-03-23 22:29:32 +00:00
|
|
|
valbuf<char>::append(beg, end);
|
2021-03-23 00:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void append(std::string_view v) {
|
|
|
|
append(&v[0], &v[v.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view str() {
|
|
|
|
return std::string_view{buf.data(), buf.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view str_term() {
|
|
|
|
return std::string_view{buf.data(), buf.size() - 1};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-23 22:32:25 +00:00
|
|
|
} /* namespace cubescript */
|
2021-03-23 00:11:21 +00:00
|
|
|
|
|
|
|
#endif
|