8static void throwException(Tcl_Interp* interp)
10 std::string_view message = interp ? Tcl_GetStringResult(interp)
12 throw CommandException(message);
24 Tcl_Interp* interp =
nullptr;
25 if (Tcl_IsShared(obj)) {
26 Tcl_DecrRefCount(obj);
27 obj = Tcl_DuplicateObj(obj);
28 Tcl_IncrRefCount(obj);
30 if (Tcl_ListObjAppendElement(interp, obj, element) != TCL_OK) {
31 throwException(interp);
35void TclObject::addListElementsImpl(std::initializer_list<Tcl_Obj*> l)
37 Tcl_Obj*
const* objv = l.begin();
38 addListElementsImpl(
int(l.size()), objv);
41void TclObject::addListElementsImpl(
int objc, Tcl_Obj*
const* objv)
43 Tcl_Interp* interp =
nullptr;
44 if (Tcl_IsShared(obj)) {
45 Tcl_DecrRefCount(obj);
46 obj = Tcl_DuplicateObj(obj);
47 Tcl_IncrRefCount(obj);
49 if (Tcl_ListObjReplace(interp, obj, INT_MAX, 0, objc, objv) != TCL_OK) {
50 throwException(interp);
56 assert((keyValuePairs.size() % 2) == 0);
57 Tcl_Interp* interp =
nullptr;
58 if (Tcl_IsShared(obj)) {
59 Tcl_DecrRefCount(obj);
60 obj = Tcl_DuplicateObj(obj);
61 Tcl_IncrRefCount(obj);
63 auto it = keyValuePairs.begin(), et = keyValuePairs.end();
66 Tcl_Obj* value = *it++;
67 if (Tcl_DictObjPut(interp, obj, key, value) != TCL_OK) {
68 throwException(interp);
75 auto* interp = interp_.interp;
77 if (Tcl_GetIntFromObj(interp, obj, &result) != TCL_OK) {
78 throwException(interp);
86 if (Tcl_GetIntFromObj(
nullptr, obj, &result) != TCL_OK) {
94 auto* interp = interp_.interp;
96 if (Tcl_GetBooleanFromObj(interp, obj, &result) != TCL_OK) {
97 throwException(interp);
107 return narrow_cast<float>(
getDouble(interp_));
112 auto* interp = interp_.interp;
114 if (Tcl_GetDoubleFromObj(interp, obj, &result) != TCL_OK) {
115 throwException(interp);
123 char* buf = Tcl_GetStringFromObj(obj, &
length);
124 return {buf, size_t(
length)};
130 auto* buf = Tcl_GetByteArrayFromObj(obj, &
length);
131 return {buf, size_t(
length)};
136 auto* interp = interp_.interp;
138 if (Tcl_ListObjLength(interp, obj, &result) != TCL_OK) {
139 throwException(interp);
143unsigned TclObject::getListLengthUnchecked()
const
146 if (Tcl_ListObjLength(
nullptr, obj, &result) != TCL_OK) {
154 auto* interp = interp_.interp;
156 if (Tcl_ListObjIndex(interp, obj, narrow<int>(index), &element) != TCL_OK) {
157 throwException(interp);
161TclObject TclObject::getListIndexUnchecked(
unsigned index)
const
164 if (Tcl_ListObjIndex(
nullptr, obj, narrow<int>(index), &element) != TCL_OK) {
172 auto* interp = interp_.interp;
174 if (Tcl_DictObjGet(interp, obj, key.obj, &value) != TCL_OK) {
175 throwException(interp);
182 auto* interp = interp_.interp;
184 if (Tcl_ExprBooleanObj(interp, obj, &result) != TCL_OK) {
185 throwException(interp);
192 auto* interp = interp_.interp;
193 int flags = compile ? 0 : TCL_EVAL_DIRECT;
194 int success = Tcl_EvalObjEx(interp, obj, flags);
195 if (success != TCL_OK) {
198 return TclObject(Tcl_GetObjResult(interp));
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
TclObject getListIndex(Interpreter &interp, unsigned index) const
bool evalBool(Interpreter &interp) const
double getDouble(Interpreter &interp) const
float getFloat(Interpreter &interp) const
void addListElement(const T &t)
std::span< const uint8_t > getBinary() const
int getInt(Interpreter &interp) const
void addDictKeyValues(Args &&... args)
TclObject getDictValue(Interpreter &interp, const TclObject &key) const
std::optional< int > getOptionalInt() const
zstring_view getString() const
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
T length(const vecN< N, T > &x)
This file implemented 3 utility functions: