Tcl_IntObj(3tcl)
π NAME π
Tcl_NewIntObj, Tcl_NewLongObj, Tcl_NewWideIntObj, Tcl_NewWideUIntObj, Tcl_SetIntObj, Tcl_SetLongObj, Tcl_SetWideIntObj, Tcl_SetWideUIntObj, Tcl_GetIntFromObj, Tcl_GetIntForIndex, Tcl_GetLongFromObj, Tcl_GetSizeIntFromObj, Tcl_GetWideIntFromObj, Tcl_GetWideUIntFromObj, Tcl_NewBignumObj, Tcl_SetBignumObj, Tcl_GetBignumFromObj, Tcl_TakeBignumFromObj β manipulate Tcl values as integers
okay so this whole page lives in the Tcl Library Procedures manual, and its little tag is Tcl_IntObj(3tcl). it's the group chat where every single Tcl C function that turns numbers into Tcl values (and Tcl values back into numbers) hangs out. bestie, grab a snack, there's a LOT of them and they're all lowkey iconic. β¨
πΈ SYNOPSIS πΈ
#include <tcl.h>
Tcl_Obj *
Tcl_NewIntObj(intValue)
Tcl_Obj *
Tcl_NewLongObj(longValue)
Tcl_Obj *
Tcl_NewWideIntObj(wideValue)
Tcl_Obj *
Tcl_NewWideUIntObj(uwideValue)
Tcl_SetIntObj(objPtr, intValue)
Tcl_SetLongObj(objPtr, longValue)
Tcl_SetWideIntObj(objPtr, wideValue)
Tcl_SetWideUIntObj(objPtr, uwideValue)
int
Tcl_GetIntFromObj(interp, objPtr, intPtr)
int
Tcl_GetIntForIndex(interp, objPtr, endValue, indexPtr)
int
Tcl_GetLongFromObj(interp, objPtr, longPtr)
int
Tcl_GetWideIntFromObj(interp, objPtr, widePtr)
int
Tcl_GetWideUIntFromObj(interp, objPtr, uwidePtr)
int
Tcl_GetSizeIntFromObj(interp, objPtr, sizePtr)
#include <tclTomMath.h>
Tcl_Obj *
Tcl_NewBignumObj(bigValue)
Tcl_SetBignumObj(objPtr, bigValue)
int
Tcl_GetBignumFromObj(interp, objPtr, bigValue)
int
Tcl_TakeBignumFromObj(interp, objPtr, bigValue)
int
Tcl_InitBignumFromDouble(interp, doubleValue, bigValue)
π ARGUMENTS π
Tcl_Size endValue(in) β when the value you hand toTcl_GetIntForIndexis literally"end", this is what it hands back to you. the answer to "what even is end", basically. πint intValue(in) β the integer value you use to initialize or set a Tcl value.long longValue(in) β the long integer value you use to initialize or set a Tcl value.Tcl_WideInt wideValue(in) β the wide integer value you use to initialize or set a Tcl value.Tcl_WideUInt uwideValue(in) β the unsigned wide integer value you use to initialize or set a Tcl value.Tcl_Obj *objPtr(in/out) β forTcl_SetIntObj,Tcl_SetLongObj,Tcl_SetWideIntObj,Tcl_SetWideUIntObj, andTcl_SetBignumObj, this points at the value you're storing an integral value into. forTcl_GetIntFromObj,Tcl_GetLongFromObj,Tcl_GetWideIntFromObj,Tcl_GetBignumFromObj, andTcl_TakeBignumFromObj, it's the value you're pulling an integral value out of. read vs write, bestie, that's the whole difference.Tcl_Interp *interp(in/out) β when it's notNULL, and the retrieval of an integral value fails, the error message gets dropped right here. π«Άint *intPtr(out) β points at the spot where the integer value retrieved fromobjPtrgoes to live.long *longPtr(out) β points at the spot where the long integer value retrieved fromobjPtrgoes to live.Tcl_Size *indexPtr(out) β points at the spot where theTcl_Sizevalue retrieved fromobjPtrgoes to live.Tcl_WideInt *widePtr(out) β points at the spot where the wide integer value retrieved fromobjPtrgoes to live.Tcl_WideUInt *uwidePtr(out) β points at the spot where the unsigned wide integer value retrieved fromobjPtrgoes to live.Tcl_Size *sizePtr(out) β points at the spot where theTcl_Sizeinteger value retrieved fromobjPtrgoes to live.mp_int *bigValue(in/out) β points at a multi-precision integer structure, declared by the LibTomMath library. big math energy. π₯double doubleValue(in) β the double value whose integer part gets determined and then used to initialize a multi-precision integer value.
π¦ DESCRIPTION π¦
so these procedures are the whole toolkit for creating, modifying, and reading Tcl values that hold integral values. that's the vibe.
the reason there are so many of them is that C has a bunch of different integral types, and values might need to be exchanged through any of them. the C integral types Tcl gives you value-exchange routines for are int, long int, Tcl_WideInt, and mp_int. the int and long int types are just handed to us by the C language standard, no drama. the Tcl_WideInt type is a typedef defined to be whatever signed integral type covers at least the 64-bit integer range (-9223372036854775808 to 9223372036854775807). depending on the platform and the C compiler, the actual type might be long long int, or something else entirely β she's flexible like that. π
the Tcl_Size typedef is a signed integer type that can hold the maximum permitted lengths of Tcl values like strings and lists. and the preprocessor constant TCL_SIZE_MAX defines the maximum value that can be stored in a variable of this type. so that's the ceiling, bestie.
the mp_int type is a multiple-precision integer type defined by the LibTomMath multiple-precision integer library.
β¨ making new ones β¨
Tcl_NewIntObj, Tcl_NewLongObj, Tcl_NewWideIntObj, Tcl_NewWideUIntObj, and Tcl_NewBignumObj each create and return a brand new Tcl value initialized to the integral value of the argument. the returned Tcl value is unshared. fresh out the box, no cap. π
β¨ setting existing ones β¨
Tcl_SetIntObj, Tcl_SetLongObj, Tcl_SetWideIntObj, Tcl_SetWideUIntObj, and Tcl_SetBignumObj each set the value of an existing Tcl value pointed to by objPtr to the integral value provided by the other argument.
β οΈ the objPtr argument must point to an unshared Tcl value. any attempt to set the value of a shared Tcl value violates Tcl's copy-on-write policy. don't do it. π
any existing string representation or internal representation in the unshared Tcl value will be freed as a consequence of setting the new value. out with the old, in with the new. π
β¨ getting an index β¨
Tcl_GetIntForIndex tries to retrieve an index value from the Tcl value objPtr. if the attempt succeeds, then TCL_OK is returned, and the value gets written to the storage the caller provided. the attempt might fail if objPtr does not hold an index value. if the attempt fails, then TCL_ERROR is returned, and if interp is non-NULL, an error message is left in interp. the Tcl_ObjType of objPtr may be changed to make subsequent calls to the same routine more efficient. glow-up for speed. β
β¨ getting integers out β¨
Tcl_GetIntFromObj, Tcl_GetLongFromObj, Tcl_GetWideIntFromObj, Tcl_GetSizeIntFromObj, Tcl_GetBignumFromObj, and Tcl_TakeBignumFromObj all try to retrieve an integral value of the appropriate type from the Tcl value objPtr.
if it works, TCL_OK comes back and the value lands in the storage the caller provided. it might fail if objPtr does not hold an integral value, or if the value exceeds the range of the target type. if it fails, TCL_ERROR comes back, and if interp is non-NULL, an error message is left in interp. the Tcl_ObjType of objPtr may be changed to make subsequent calls to the same routine more efficient.
π unlike the other functions,Tcl_TakeBignumFromObjmay set the content of the Tcl valueobjPtrto an empty string while it's in the middle of retrieving the multiple-precision integer value. it takes, bestie. that's the whole point of the name.
the choice between Tcl_GetBignumFromObj and Tcl_TakeBignumFromObj is governed by how the caller will keep using objPtr afterwards:
- if, after the
mp_intvalue is retrieved fromobjPtr, the caller makes no more use ofobjPtr, then usingTcl_TakeBignumFromObjlets Tcl detect when an unsharedobjPtrpermits the value to be moved instead of copied, which should be more efficient. slay, no wasted work. π₯ - if anything later in the caller needs
objPtrto keep holding the same value, thenTcl_GetBignumFromObjmust be chosen. periodt.
β¨ the double-to-bignum little helper β¨
Tcl_InitBignumFromDouble is a utility procedure that extracts the integer part of doubleValue and stores that integer value in the mp_int value bigValue. tiny, but she ate. π
π REFERENCE COUNT MANAGEMENT π
okay this part is important, don't skip it.
Tcl_NewIntObj, Tcl_NewLongObj, Tcl_NewWideIntObj, and Tcl_NewBignumObj always return a zero-reference object, much like Tcl_NewObj. brand new, refcount zero.
Tcl_SetIntObj, Tcl_SetLongObj, Tcl_SetWideIntObj, and Tcl_SetBignumObj do not modify the reference count of their objPtr arguments, but they do require that the object be unshared.
Tcl_GetIntFromObj, Tcl_GetIntForIndex, Tcl_GetLongFromObj, Tcl_GetWideIntFromObj, Tcl_GetSizeIntFromObj, Tcl_GetBignumFromObj, and Tcl_TakeBignumFromObj do not modify the reference count of their objPtr arguments; they only read.
β οΈ note however that this function may set the interpreter result; if that is the only place that is holding a reference to the object, it will be deleted. π also note that if Tcl_TakeBignumFromObj is given an unshared value, the value of that object may be modified; it is intended to be used when the value is βconsumedβ by the operation at this point.
π SEE ALSO π
Tcl_NewObjTcl_DecrRefCountTcl_IncrRefCountTcl_GetObjResult
π KEYWORDS π
integer, integer value, integer type, internal representation, value, value type, string representation
and that's the whole page, bestie!! this is from Tcl 8.5, and yes the page tag at the bottom is Tcl_IntObj(3tcl) β go forth and manipulate integers, you absolute icon. π«Άβ¨π
π¦ where this page comes from
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1996-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms