12#include <initializer_list>
31 using difference_type = ptrdiff_t;
32 using iterator_category = std::bidirectional_iterator_tag;
34 iterator(
const TclObject& obj_,
unsigned i_)
35 : obj(&obj_), i(i_) {}
37 [[nodiscard]]
bool operator==(
const iterator& other)
const {
38 assert(obj == other.obj);
43 return obj->getListIndexUnchecked(i).getString();
46 iterator& operator++() {
50 iterator operator++(
int) {
51 iterator result = *
this;
55 iterator& operator--() {
59 iterator operator--(
int) {
60 iterator result = *
this;
73 template<
typename T>
explicit TclObject(T
t) { init(newObj(
t)); }
78 template<
typename... Args>
80 init(newList({newObj(std::forward<Args>(args))...}));
84 template<
typename... Args>
86 init(Tcl_NewDictObj());
95 Tcl_DecrRefCount(obj);
101 if (&other !=
this) {
102 Tcl_DecrRefCount(obj);
113 if (Tcl_IsShared(obj)) {
114 Tcl_DecrRefCount(obj);
115 obj = newObj(std::forward<T>(
t));
116 Tcl_IncrRefCount(obj);
118 assign(std::forward<T>(
t));
130 addListElementsImpl(first, last,
131 typename std::iterator_traits<ITER>::iterator_category());
137 addListElementsImpl({newObj(std::forward<Args>(args))...});
141 template<
typename Key,
typename Value>
155 [[nodiscard]] std::span<const uint8_t>
getBinary()
const;
159 template<
typename Key>
167 [[nodiscard]]
unsigned size()
const {
return getListLengthUnchecked(); }
168 [[nodiscard]]
bool empty()
const {
return size() == 0; }
169 [[nodiscard]] iterator
begin()
const {
return {*
this, 0}; }
170 [[nodiscard]] iterator
end()
const {
return {*
this,
size()}; }
192 void init(Tcl_Obj* obj_)
noexcept {
194 Tcl_IncrRefCount(obj);
197 [[nodiscard]]
static Tcl_Obj* newObj(std::string_view s) {
198 return Tcl_NewStringObj(s.data(),
int(s.size()));
200 [[nodiscard]]
static Tcl_Obj* newObj(
const char* s) {
201 return Tcl_NewStringObj(s,
int(strlen(s)));
203 [[nodiscard]]
static Tcl_Obj* newObj(
bool b) {
204 return Tcl_NewBooleanObj(b);
206 [[nodiscard]]
static Tcl_Obj* newObj(
int i) {
207 return Tcl_NewIntObj(i);
209 [[nodiscard]]
static Tcl_Obj* newObj(
unsigned u) {
210 return Tcl_NewIntObj(narrow_cast<int>(u));
212 [[nodiscard]]
static Tcl_Obj* newObj(
float f) {
213 return Tcl_NewDoubleObj(
double(f));
215 [[nodiscard]]
static Tcl_Obj* newObj(
double d) {
216 return Tcl_NewDoubleObj(d);
218 [[nodiscard]]
static Tcl_Obj* newObj(std::span<const uint8_t> buf) {
219 return Tcl_NewByteArrayObj(buf.data(),
int(buf.size()));
221 [[nodiscard]]
static Tcl_Obj* newObj(
const TclObject& o) {
224 [[nodiscard]]
static Tcl_Obj* newList(std::initializer_list<Tcl_Obj*> l) {
225 return Tcl_NewListObj(
int(l.size()), l.begin());
228 void assign(std::string_view s) {
229 Tcl_SetStringObj(obj, s.data(),
int(s.size()));
231 void assign(
const char* s) {
232 Tcl_SetStringObj(obj, s,
int(strlen(s)));
234 void assign(
bool b) {
235 Tcl_SetBooleanObj(obj, b);
238 Tcl_SetIntObj(obj, i);
240 void assign(
unsigned u) {
241 Tcl_SetIntObj(obj, narrow_cast<int>(u));
243 void assign(
float f) {
244 Tcl_SetDoubleObj(obj,
double(f));
246 void assign(
double d) {
247 Tcl_SetDoubleObj(obj, d);
249 void assign(std::span<const uint8_t> b) {
250 Tcl_SetByteArrayObj(obj, b.data(),
int(b.size()));
253 template<
typename ITER>
254 void addListElementsImpl(ITER first, ITER last, std::input_iterator_tag) {
255 for (ITER it = first; it != last; ++it) {
259 template<
typename ITER>
260 void addListElementsImpl(ITER first, ITER last, std::random_access_iterator_tag) {
261 auto objc = last - first;
262 if (objc == 0)
return;
263 VLA(Tcl_Obj*, objv, objc);
264 std::transform(first, last, objv.data(), [](
const auto&
t) { return newObj(t); });
265 addListElementsImpl(narrow<int>(objc), objv.data());
269 void addListElementsImpl(
int objc, Tcl_Obj*
const* objv);
270 void addListElementsImpl(std::initializer_list<Tcl_Obj*> l);
272 [[nodiscard]]
unsigned getListLengthUnchecked()
const;
273 [[nodiscard]]
TclObject getListIndexUnchecked(
unsigned index)
const;
280static_assert(
sizeof(TclObject) ==
sizeof(Tcl_Obj*));
282template<
typename... Args>
288template<
typename... Args>
295 [[nodiscard]] uint32_t
operator()(std::string_view str)
const {
bool getBoolean(Interpreter &interp) const
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
TclObject & operator=(const TclObject &other)
unsigned getListLength(Interpreter &interp) const
friend bool operator==(const TclObject &x, const TclObject &y)
TclObject getListIndex(Interpreter &interp, unsigned index) const
bool evalBool(Interpreter &interp) const
TclObject(const TclObject &o)
double getDouble(Interpreter &interp) const
TclObject(TclObject &&o) noexcept
void addListElement(Args &&... args)
float getFloat(Interpreter &interp) const
TclObject & operator=(TclObject &other)
void addListElements(Range &&range)
TclObject(MakeListTag, Args &&... args)
void addListElement(const T &t)
std::span< const uint8_t > getBinary() const
void addListElements(ITER first, ITER last)
TclObject getDictValue(Interpreter &interp, const Key &key) const
Tcl_Obj * getTclObjectNonConst() const
int getInt(Interpreter &interp) const
TclObject & operator=(T &&t)
TclObject(MakeDictTag, Args &&... args)
friend bool operator==(const TclObject &x, std::string_view y)
void addDictKeyValue(const Key &key, const Value &value)
void addDictKeyValues(Args &&... args)
TclObject getDictValue(Interpreter &interp, const TclObject &key) const
std::optional< int > getOptionalInt() const
zstring_view getString() const
TclObject & operator=(TclObject &&other) noexcept
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
TclObject makeTclList(Args &&... args)
TclObject makeTclDict(Args &&... args)
auto transform(InputRange &&range, OutputIter out, UnaryOperation op)
void swap(openmsx::MemBuffer< T > &l, openmsx::MemBuffer< T > &r) noexcept
uint32_t operator()(std::string_view str) const
uint32_t operator()(const TclObject &obj) const
constexpr uint128 operator*(const uint128 &a, const uint128 &b)
#define VLA(TYPE, NAME, LENGTH)
uint32_t xxhash(std::string_view key)
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)