11 #include <initializer_list>
14 #include <string_view>
29 using difference_type = ptrdiff_t;
30 using iterator_category = std::bidirectional_iterator_tag;
32 iterator(
const TclObject& obj_,
unsigned i_)
33 : obj(&obj_), i(i_) {}
35 [[nodiscard]]
bool operator==(
const iterator& other)
const {
36 assert(obj == other.obj);
39 [[nodiscard]]
bool operator!=(
const iterator& other)
const {
40 return !(*
this == other);
44 return obj->getListIndexUnchecked(i).getString();
47 iterator& operator++() {
51 iterator operator++(
int) {
52 iterator result = *
this;
56 iterator& operator--() {
60 iterator operator--(
int) {
61 iterator result = *
this;
74 template<
typename T>
explicit TclObject(T
t) { init(newObj(
t)); }
79 template<
typename... Args>
81 init(newList({newObj(std::forward<Args>(args))...}));
85 template<
typename... Args>
87 init(Tcl_NewDictObj());
96 Tcl_DecrRefCount(obj);
102 if (&other !=
this) {
103 Tcl_DecrRefCount(obj);
109 std::swap(obj, other.obj);
114 if (Tcl_IsShared(obj)) {
115 Tcl_DecrRefCount(obj);
116 obj = newObj(std::forward<T>(
t));
117 Tcl_IncrRefCount(obj);
119 assign(std::forward<T>(
t));
131 addListElementsImpl(first, last,
132 typename std::iterator_traits<ITER>::iterator_category());
138 addListElementsImpl({newObj(std::forward<Args>(args))...});
142 template<
typename Key,
typename Value>
159 template<
typename Key>
167 [[nodiscard]]
unsigned size()
const {
return getListLengthUnchecked(); }
168 [[nodiscard]]
bool empty()
const {
return size() == 0; }
169 [[nodiscard]]
auto begin()
const {
return iterator(*
this, 0); }
170 [[nodiscard]]
auto end()
const {
return iterator(*
this,
size()); }
188 return x.getString() == y;
199 void init(Tcl_Obj* obj_) noexcept {
201 Tcl_IncrRefCount(obj);
204 [[nodiscard]]
static Tcl_Obj* newObj(std::string_view s) {
205 return Tcl_NewStringObj(s.data(),
int(s.size()));
207 [[nodiscard]]
static Tcl_Obj* newObj(
const char* s) {
208 return Tcl_NewStringObj(s,
int(strlen(s)));
210 [[nodiscard]]
static Tcl_Obj* newObj(
bool b) {
211 return Tcl_NewBooleanObj(b);
213 [[nodiscard]]
static Tcl_Obj* newObj(
int i) {
214 return Tcl_NewIntObj(i);
216 [[nodiscard]]
static Tcl_Obj* newObj(
unsigned u) {
217 return Tcl_NewIntObj(u);
219 [[nodiscard]]
static Tcl_Obj* newObj(
float f) {
220 return Tcl_NewDoubleObj(
double(f));
222 [[nodiscard]]
static Tcl_Obj* newObj(
double d) {
223 return Tcl_NewDoubleObj(d);
226 return Tcl_NewByteArrayObj(buf.
data(),
int(buf.
size()));
228 [[nodiscard]]
static Tcl_Obj* newObj(
const TclObject& o) {
231 [[nodiscard]]
static Tcl_Obj* newList(std::initializer_list<Tcl_Obj*> l) {
232 return Tcl_NewListObj(
int(l.size()), l.begin());
235 void assign(std::string_view s) {
236 Tcl_SetStringObj(obj, s.data(),
int(s.size()));
238 void assign(
const char* s) {
239 Tcl_SetStringObj(obj, s,
int(strlen(s)));
241 void assign(
bool b) {
242 Tcl_SetBooleanObj(obj, b);
245 Tcl_SetIntObj(obj, i);
247 void assign(
unsigned u) {
248 Tcl_SetIntObj(obj, u);
250 void assign(
float f) {
251 Tcl_SetDoubleObj(obj,
double(f));
253 void assign(
double d) {
254 Tcl_SetDoubleObj(obj, d);
257 Tcl_SetByteArrayObj(obj, b.
data(),
int(b.
size()));
260 template<
typename ITER>
261 void addListElementsImpl(ITER first, ITER last, std::input_iterator_tag) {
262 for (ITER it = first; it != last; ++it) {
266 template<
typename ITER>
267 void addListElementsImpl(ITER first, ITER last, std::random_access_iterator_tag) {
268 auto objc = last - first;
269 if (objc == 0)
return;
270 VLA(Tcl_Obj*, objv, objc);
272 addListElementsImpl(objc, objv);
276 void addListElementsImpl(
int objc, Tcl_Obj*
const* objv);
277 void addListElementsImpl(std::initializer_list<Tcl_Obj*> l);
279 [[nodiscard]]
unsigned getListLengthUnchecked()
const;
280 [[nodiscard]]
TclObject getListIndexUnchecked(
unsigned index)
const;
287 static_assert(
sizeof(TclObject) ==
sizeof(Tcl_Obj*));
289 template<
typename... Args>
295 template<
typename... Args>
302 [[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)