12 #include <initializer_list>
15 #include <string_view>
30 using difference_type = ptrdiff_t;
31 using iterator_category = std::bidirectional_iterator_tag;
33 iterator(
const TclObject& obj_,
unsigned i_)
34 : obj(&obj_), i(i_) {}
36 [[nodiscard]]
bool operator==(
const iterator& other)
const {
37 assert(obj == other.obj);
40 [[nodiscard]]
bool operator!=(
const iterator& other)
const {
41 return !(*
this == other);
45 return obj->getListIndexUnchecked(i).getString();
48 iterator& operator++() {
52 iterator operator++(
int) {
53 iterator result = *
this;
57 iterator& operator--() {
61 iterator operator--(
int) {
62 iterator result = *
this;
75 template<
typename T>
explicit TclObject(T
t) { init(newObj(
t)); }
80 template<
typename... Args>
82 init(newList({newObj(std::forward<Args>(args))...}));
86 template<
typename... Args>
88 init(Tcl_NewDictObj());
97 Tcl_DecrRefCount(obj);
103 if (&other !=
this) {
104 Tcl_DecrRefCount(obj);
110 std::swap(obj, other.obj);
115 if (Tcl_IsShared(obj)) {
116 Tcl_DecrRefCount(obj);
117 obj = newObj(std::forward<T>(
t));
118 Tcl_IncrRefCount(obj);
120 assign(std::forward<T>(
t));
132 addListElementsImpl(first, last,
133 typename std::iterator_traits<ITER>::iterator_category());
139 addListElementsImpl({newObj(std::forward<Args>(args))...});
143 template<
typename Key,
typename Value>
160 template<
typename Key>
168 [[nodiscard]]
unsigned size()
const {
return getListLengthUnchecked(); }
169 [[nodiscard]]
bool empty()
const {
return size() == 0; }
170 [[nodiscard]]
auto begin()
const {
return iterator(*
this, 0); }
171 [[nodiscard]]
auto end()
const {
return iterator(*
this,
size()); }
189 return x.getString() == y;
200 void init(Tcl_Obj* obj_) noexcept {
202 Tcl_IncrRefCount(obj);
205 [[nodiscard]]
static Tcl_Obj* newObj(std::string_view s) {
206 return Tcl_NewStringObj(s.data(),
int(s.size()));
208 [[nodiscard]]
static Tcl_Obj* newObj(
const char* s) {
209 return Tcl_NewStringObj(s,
int(strlen(s)));
211 [[nodiscard]]
static Tcl_Obj* newObj(
bool b) {
212 return Tcl_NewBooleanObj(b);
214 [[nodiscard]]
static Tcl_Obj* newObj(
int i) {
215 return Tcl_NewIntObj(i);
217 [[nodiscard]]
static Tcl_Obj* newObj(
unsigned u) {
218 return Tcl_NewIntObj(u);
220 [[nodiscard]]
static Tcl_Obj* newObj(
float f) {
221 return Tcl_NewDoubleObj(
double(f));
223 [[nodiscard]]
static Tcl_Obj* newObj(
double d) {
224 return Tcl_NewDoubleObj(d);
227 return Tcl_NewByteArrayObj(buf.
data(),
int(buf.
size()));
229 [[nodiscard]]
static Tcl_Obj* newObj(
const TclObject& o) {
232 [[nodiscard]]
static Tcl_Obj* newList(std::initializer_list<Tcl_Obj*> l) {
233 return Tcl_NewListObj(
int(l.size()), l.begin());
236 void assign(std::string_view s) {
237 Tcl_SetStringObj(obj, s.data(),
int(s.size()));
239 void assign(
const char* s) {
240 Tcl_SetStringObj(obj, s,
int(strlen(s)));
242 void assign(
bool b) {
243 Tcl_SetBooleanObj(obj, b);
246 Tcl_SetIntObj(obj, i);
248 void assign(
unsigned u) {
249 Tcl_SetIntObj(obj, u);
251 void assign(
float f) {
252 Tcl_SetDoubleObj(obj,
double(f));
254 void assign(
double d) {
255 Tcl_SetDoubleObj(obj, d);
258 Tcl_SetByteArrayObj(obj, b.
data(),
int(b.
size()));
261 template<
typename ITER>
262 void addListElementsImpl(ITER first, ITER last, std::input_iterator_tag) {
263 for (ITER it = first; it != last; ++it) {
267 template<
typename ITER>
268 void addListElementsImpl(ITER first, ITER last, std::random_access_iterator_tag) {
269 auto objc = last - first;
270 if (objc == 0)
return;
271 VLA(Tcl_Obj*, objv, objc);
273 addListElementsImpl(objc, objv);
277 void addListElementsImpl(
int objc, Tcl_Obj*
const* objv);
278 void addListElementsImpl(std::initializer_list<Tcl_Obj*> l);
280 [[nodiscard]]
unsigned getListLengthUnchecked()
const;
281 [[nodiscard]]
TclObject getListIndexUnchecked(
unsigned index)
const;
288 static_assert(
sizeof(TclObject) ==
sizeof(Tcl_Obj*));
290 template<
typename... Args>
296 template<
typename... Args>
303 [[nodiscard]] uint32_t
operator()(std::string_view str)
const {
friend bool operator==(std::string_view x, const TclObject &y)
bool getBoolean(Interpreter &interp) const
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
unsigned getListLength(Interpreter &interp) const
friend bool operator==(const TclObject &x, const TclObject &y)
Tcl_Obj * getTclObjectNonConst() const
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)
void addListElements(Range &&range)
TclObject(MakeListTag, Args &&... args)
void addListElement(const T &t)
TclObject & operator=(const TclObject &other)
void addListElements(ITER first, ITER last)
TclObject & operator=(TclObject &&other) noexcept
friend bool operator!=(const TclObject &x, const TclObject &y)
TclObject getDictValue(Interpreter &interp, const Key &key) const
int getInt(Interpreter &interp) const
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 & operator=(TclObject &other)
span< const uint8_t > getBinary() const
friend bool operator!=(std::string_view x, const TclObject &y)
friend bool operator!=(const TclObject &x, std::string_view y)
TclObject getDictValue(Interpreter &interp, const TclObject &key) const
TclObject & operator=(T &&t)
std::optional< int > getOptionalInt() const
zstring_view getString() const
constexpr pointer data() const noexcept
constexpr index_type size() const noexcept
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
constexpr KeyMatrixPosition x
Keyboard bindings.
TclObject makeTclList(Args &&... args)
TclObject makeTclDict(Args &&... args)
auto transform(InputRange &&range, OutputIter out, UnaryOperation op)
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)