8[[noreturn]]
static void throwException(Tcl_Interp* interp)
10 std::string_view message = interp ? Tcl_GetStringResult(interp)
12 throw CommandException(message);
15static void unshare(Tcl_Obj*& obj)
17 if (Tcl_IsShared(obj)) {
18 Tcl_DecrRefCount(obj);
19 obj = Tcl_DuplicateObj(obj);
20 Tcl_IncrRefCount(obj);
34 Tcl_Interp* interp =
nullptr;
35 if (Tcl_ListObjAppendElement(interp, obj, element) != TCL_OK) {
36 throwException(interp);
40void TclObject::addListElementsImpl(std::initializer_list<Tcl_Obj*> l)
42 addListElementsImpl(
int(l.size()), l.begin());
45void TclObject::addListElementsImpl(
int objc, Tcl_Obj*
const* objv)
48 Tcl_Interp* interp =
nullptr;
49 if (Tcl_ListObjReplace(interp, obj, INT_MAX, 0, objc, objv) != TCL_OK) {
50 throwException(interp);
56 assert((keyValuePairs.size() % 2) == 0);
58 Tcl_Interp* interp =
nullptr;
59 auto it = keyValuePairs.begin(), et = keyValuePairs.end();
62 Tcl_Obj* value = *it++;
63 if (Tcl_DictObjPut(interp, obj, key, value) != TCL_OK) {
64 throwException(interp);
71 auto* interp = interp_.interp;
73 if (Tcl_GetIntFromObj(interp, obj, &result) != TCL_OK) {
74 throwException(interp);
82 if (Tcl_GetIntFromObj(
nullptr, obj, &result) != TCL_OK) {
90 auto* interp = interp_.interp;
92 if (Tcl_GetBooleanFromObj(interp, obj, &result) != TCL_OK) {
93 throwException(interp);
101 if (Tcl_GetBooleanFromObj(
nullptr, obj, &result) != TCL_OK) {
112 return narrow_cast<float>(
getDouble(interp_));
117 return narrow_cast<float>(*d);
124 auto* interp = interp_.interp;
126 if (Tcl_GetDoubleFromObj(interp, obj, &result) != TCL_OK) {
127 throwException(interp);
135 if (Tcl_GetDoubleFromObj(
nullptr, obj, &result) != TCL_OK) {
144 const char* buf = Tcl_GetStringFromObj(obj, &length);
145 return {buf, size_t(length)};
151 const auto* buf = Tcl_GetByteArrayFromObj(obj, &length);
152 return {buf, size_t(length)};
157 auto* interp = interp_.interp;
159 if (Tcl_ListObjLength(interp, obj, &result) != TCL_OK) {
160 throwException(interp);
164unsigned TclObject::getListLengthUnchecked()
const
167 if (Tcl_ListObjLength(
nullptr, obj, &result) != TCL_OK) {
175 auto* interp = interp_.interp;
177 if (Tcl_ListObjIndex(interp, obj, narrow<int>(index), &element) != TCL_OK) {
178 throwException(interp);
185 if (Tcl_ListObjIndex(
nullptr, obj, narrow<int>(index), &element) != TCL_OK) {
194 auto* interp = interp_.interp;
195 if (Tcl_ListObjReplace(interp, obj, narrow<int>(index), 1, 0,
nullptr) != TCL_OK) {
196 throwException(interp);
203 auto* interp = interp_.interp;
204 if (Tcl_DictObjPut(interp, obj, key.obj, value.obj) != TCL_OK) {
205 throwException(interp);
211 auto* interp = interp_.interp;
213 if (Tcl_DictObjGet(interp, obj, key.obj, &value) != TCL_OK) {
214 throwException(interp);
222 if ((Tcl_DictObjGet(
nullptr, obj, key.obj, &value) != TCL_OK) || !value) {
230 auto* interp = interp_.interp;
232 if (Tcl_ExprBooleanObj(interp, obj, &result) != TCL_OK) {
233 throwException(interp);
240 auto* interp = interp_.interp;
242 if (Tcl_ExprObj(interp, obj, &result) != TCL_OK) {
243 throwException(interp);
250 auto* interp = interp_.interp;
251 if (
int flags = compile ? 0 : TCL_EVAL_DIRECT;
252 Tcl_EvalObjEx(interp, obj, flags) != TCL_OK) {
255 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 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)
double getDouble(Interpreter &interp) const
float getFloat(Interpreter &interp) const
TclObject eval(Interpreter &interp) const
void addListElement(const T &t)
std::span< const uint8_t > getBinary() const
std::optional< float > getOptionalFloat() const
int getInt(Interpreter &interp) const
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
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: