16#include <initializer_list>
35 using difference_type = ptrdiff_t;
36 using iterator_category = std::bidirectional_iterator_tag;
38 iterator() : obj(
nullptr), i(0) {}
39 iterator(
const TclObject& obj_,
unsigned i_)
40 : obj(&obj_), i(i_) {}
42 [[nodiscard]]
bool operator==(
const iterator& other)
const {
43 assert(obj == other.obj);
48 return obj->getListIndexUnchecked(i).getString();
51 iterator& operator++() {
55 iterator operator++(
int) {
56 iterator result = *
this;
60 iterator& operator--() {
64 iterator operator--(
int) {
65 iterator result = *
this;
73 static_assert(std::bidirectional_iterator<iterator>);
78 template<
typename T>
explicit TclObject(T
t) { init(newObj(
t)); }
83 template<
typename... Args>
85 init(newList({newObj(std::forward<Args>(args))...}));
89 template<
typename... Args>
91 init(Tcl_NewDictObj());
100 Tcl_DecrRefCount(obj);
106 if (&other !=
this) {
107 Tcl_DecrRefCount(obj);
113 std::swap(obj, other.obj);
118 if (Tcl_IsShared(obj)) {
119 Tcl_DecrRefCount(obj);
120 obj = newObj(std::forward<T>(
t));
121 Tcl_IncrRefCount(obj);
123 assign(std::forward<T>(
t));
135 addListElementsImpl(first, last,
136 typename std::iterator_traits<ITER>::iterator_category());
142 addListElementsImpl({newObj(std::forward<Args>(args))...});
146 template<
typename Key,
typename Value>
160 [[nodiscard]] std::span<const uint8_t>
getBinary()
const;
167 template<
typename Key>
179 [[nodiscard]]
unsigned size()
const {
return getListLengthUnchecked(); }
180 [[nodiscard]]
bool empty()
const {
return size() == 0; }
181 [[nodiscard]] iterator
begin()
const {
return {*
this, 0}; }
182 [[nodiscard]] iterator
end()
const {
return {*
this,
size()}; }
205 void init(Tcl_Obj* obj_)
noexcept {
207 Tcl_IncrRefCount(obj);
210 [[nodiscard]]
static Tcl_Obj* newObj(std::string_view s) {
211 return Tcl_NewStringObj(s.data(),
int(s.size()));
213 [[nodiscard]]
static Tcl_Obj* newObj(
const char* s) {
214 return Tcl_NewStringObj(s,
int(strlen(s)));
216 [[nodiscard]]
static Tcl_Obj* newObj(
bool b) {
217 return Tcl_NewBooleanObj(b);
219 [[nodiscard]]
static Tcl_Obj* newObj(
int i) {
220 return Tcl_NewIntObj(i);
222 [[nodiscard]]
static Tcl_Obj* newObj(
unsigned u) {
223 return Tcl_NewIntObj(narrow_cast<int>(u));
225 [[nodiscard]]
static Tcl_Obj* newObj(
float f) {
226 return Tcl_NewDoubleObj(
double(f));
228 [[nodiscard]]
static Tcl_Obj* newObj(
double d) {
229 return Tcl_NewDoubleObj(d);
231 [[nodiscard]]
static Tcl_Obj* newObj(std::span<const uint8_t> buf) {
232 return Tcl_NewByteArrayObj(buf.data(),
int(buf.size()));
234 [[nodiscard]]
static Tcl_Obj* newObj(
const TclObject& o) {
237 [[nodiscard]]
static Tcl_Obj* newList(std::initializer_list<Tcl_Obj*> l) {
238 return Tcl_NewListObj(
int(l.size()), l.begin());
241 void assign(std::string_view s) {
242 Tcl_SetStringObj(obj, s.data(),
int(s.size()));
244 void assign(
const char* s) {
245 Tcl_SetStringObj(obj, s,
int(strlen(s)));
247 void assign(
bool b) {
248 Tcl_SetBooleanObj(obj, b);
251 Tcl_SetIntObj(obj, i);
253 void assign(
unsigned u) {
254 Tcl_SetIntObj(obj, narrow_cast<int>(u));
256 void assign(
float f) {
257 Tcl_SetDoubleObj(obj,
double(f));
259 void assign(
double d) {
260 Tcl_SetDoubleObj(obj, d);
262 void assign(std::span<const uint8_t> b) {
263 Tcl_SetByteArrayObj(obj, b.data(),
int(b.size()));
266 template<
typename ITER>
267 void addListElementsImpl(ITER first, ITER last, std::input_iterator_tag) {
268 for (ITER it = first; it != last; ++it) {
272 template<
typename ITER>
273 void addListElementsImpl(ITER first, ITER last, std::random_access_iterator_tag) {
275 [](
const auto&
t) {
return newObj(
t); }));
276 addListElementsImpl(narrow<int>(objv.size()), 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)
constexpr auto transform(Range &&range, UnaryOp op)
uint32_t operator()(std::string_view str) const
uint32_t operator()(const TclObject &obj) const
uint32_t xxhash(std::string_view key)