14#include <initializer_list>
33 using difference_type = ptrdiff_t;
34 using iterator_category = std::bidirectional_iterator_tag;
36 iterator() : obj(
nullptr), i(0) {}
37 iterator(
const TclObject& obj_,
unsigned i_)
38 : obj(&obj_), i(i_) {}
40 [[nodiscard]]
bool operator==(
const iterator& other)
const {
41 assert(obj == other.obj);
46 return obj->getListIndexUnchecked(i).getString();
49 iterator& operator++() {
53 iterator operator++(
int) {
54 iterator result = *
this;
58 iterator& operator--() {
62 iterator operator--(
int) {
63 iterator result = *
this;
71 static_assert(std::bidirectional_iterator<iterator>);
76 template<
typename T>
explicit TclObject(T
t) { init(newObj(
t)); }
81 template<
typename... Args>
83 init(newList({newObj(std::forward<Args>(args))...}));
87 template<
typename... Args>
89 init(Tcl_NewDictObj());
98 Tcl_DecrRefCount(obj);
104 if (&other !=
this) {
105 Tcl_DecrRefCount(obj);
111 std::swap(obj, other.obj);
116 if (Tcl_IsShared(obj)) {
117 Tcl_DecrRefCount(obj);
118 obj = newObj(std::forward<T>(
t));
119 Tcl_IncrRefCount(obj);
121 assign(std::forward<T>(
t));
133 addListElementsImpl(first, last,
134 typename std::iterator_traits<ITER>::iterator_category());
140 addListElementsImpl({newObj(std::forward<Args>(args))...});
144 template<
typename Key,
typename Value>
158 [[nodiscard]] std::span<const uint8_t>
getBinary()
const;
165 template<
typename Key>
177 [[nodiscard]]
unsigned size()
const {
return getListLengthUnchecked(); }
178 [[nodiscard]]
bool empty()
const {
return size() == 0; }
179 [[nodiscard]] iterator
begin()
const {
return {*
this, 0}; }
180 [[nodiscard]] iterator
end()
const {
return {*
this,
size()}; }
203 void init(Tcl_Obj* obj_)
noexcept {
205 Tcl_IncrRefCount(obj);
208 [[nodiscard]]
static Tcl_Obj* newObj(std::string_view s) {
209 return Tcl_NewStringObj(s.data(),
int(s.size()));
211 [[nodiscard]]
static Tcl_Obj* newObj(
const char* s) {
212 return Tcl_NewStringObj(s,
int(strlen(s)));
214 [[nodiscard]]
static Tcl_Obj* newObj(
bool b) {
215 return Tcl_NewBooleanObj(b);
217 [[nodiscard]]
static Tcl_Obj* newObj(
int i) {
218 return Tcl_NewIntObj(i);
220 [[nodiscard]]
static Tcl_Obj* newObj(
unsigned u) {
221 return Tcl_NewIntObj(narrow_cast<int>(u));
223 [[nodiscard]]
static Tcl_Obj* newObj(
float f) {
224 return Tcl_NewDoubleObj(
double(f));
226 [[nodiscard]]
static Tcl_Obj* newObj(
double d) {
227 return Tcl_NewDoubleObj(d);
229 [[nodiscard]]
static Tcl_Obj* newObj(std::span<const uint8_t> buf) {
230 return Tcl_NewByteArrayObj(buf.data(),
int(buf.size()));
232 [[nodiscard]]
static Tcl_Obj* newObj(
const TclObject& o) {
235 [[nodiscard]]
static Tcl_Obj* newList(std::initializer_list<Tcl_Obj*> l) {
236 return Tcl_NewListObj(
int(l.size()), l.begin());
239 void assign(std::string_view s) {
240 Tcl_SetStringObj(obj, s.data(),
int(s.size()));
242 void assign(
const char* s) {
243 Tcl_SetStringObj(obj, s,
int(strlen(s)));
245 void assign(
bool b) {
246 Tcl_SetBooleanObj(obj, b);
249 Tcl_SetIntObj(obj, i);
251 void assign(
unsigned u) {
252 Tcl_SetIntObj(obj, narrow_cast<int>(u));
254 void assign(
float f) {
255 Tcl_SetDoubleObj(obj,
double(f));
257 void assign(
double d) {
258 Tcl_SetDoubleObj(obj, d);
260 void assign(std::span<const uint8_t> b) {
261 Tcl_SetByteArrayObj(obj, b.data(),
int(b.size()));
264 template<
typename ITER>
265 void addListElementsImpl(ITER first, ITER last, std::input_iterator_tag) {
266 for (ITER it = first; it != last; ++it) {
270 template<
typename ITER>
271 void addListElementsImpl(ITER first, ITER last, std::random_access_iterator_tag) {
272 auto objc = last - first;
273 if (objc == 0)
return;
274 VLA(Tcl_Obj*, objv, objc);
275 std::transform(first, last, objv.data(), [](
const auto&
t) { return newObj(t); });
276 addListElementsImpl(narrow<int>(objc), objv.data());
280 void addListElementsImpl(
int objc, Tcl_Obj*
const* objv);
281 void addListElementsImpl(std::initializer_list<Tcl_Obj*> l);
283 [[nodiscard]]
unsigned getListLengthUnchecked()
const;
290static_assert(
sizeof(TclObject) ==
sizeof(Tcl_Obj*));
292template<
typename... Args>
298template<
typename... Args>
305 [[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 getListIndexUnchecked(unsigned index) const
TclObject getListIndex(Interpreter &interp, unsigned index) const
bool evalBool(Interpreter &interp) const
std::optional< TclObject > getOptionalDictValue(const TclObject &key) const
void removeListIndex(Interpreter &interp, unsigned index)
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)
TclObject eval(Interpreter &interp) const
void addListElements(Range &&range)
TclObject(MakeListTag, Args &&... args)
void addListElement(const T &t)
std::span< const uint8_t > getBinary() const
std::optional< float > getOptionalFloat() 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)
std::optional< bool > getOptionalBool() const
TclObject getDictValue(Interpreter &interp, const TclObject &key) const
std::optional< int > getOptionalInt() const
zstring_view getString() const
TclObject & operator=(TclObject &&other) noexcept
std::optional< double > getOptionalDouble() const
void setDictValue(Interpreter &interp, const TclObject &key, const TclObject &value)
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)
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)