Tcl_BooleanObj(3)
π NAME π
ok so bestie, meet the girlies of the boolean Tcl_Obj world β Tcl_NewBooleanObj, Tcl_SetBooleanObj, Tcl_GetBooleanFromObj, and Tcl_GetBoolFromObj. their whole job? storing and retrieving a boolean value in a Tcl_Obj. that's it. that's the vibe. π
this page lives in the Tcl Library Procedures section of the manual (section 3, iykyk).
β¨ SYNOPSIS β¨
#include <tcl.h>
Tcl_Obj *
Tcl_NewBooleanObj(intValue)
Tcl_SetBooleanObj(objPtr, intValue)
int
Tcl_GetBooleanFromObj(interp, objPtr, boolPtr)
int
Tcl_GetBoolFromObj(interp, objPtr, flags. charPtr)
ok yes, that flags. charPtr is literally what the page says, don't come for me, I'm just the messenger π
πΈ ARGUMENTS πΈ
int intValue(in) β the integer value that's getting stored as a boolean inside aTcl_Obj.Tcl_Obj *objPtr(in/out) β points at theTcl_Objyou're storing into, or the one you're retrieving from.Tcl_Interp *interp(in/out) β if a boolean value can't be retrieved, an error message gets parked in the interpreter's result valueβ¦ unlessinterpisNULL, in which case, silence. πbool | int *boolPtr(out) β points to the spot whereTcl_GetBooleanFromObjstores the boolean value (0or1) it pulled out ofobjPtr.char *charPtr(out) β points to the spot whereTcl_GetBoolFromObjstores the boolean value (0or1) it pulled out ofobjPtr.int flags(in) β0orTCL_NULL_OK. if you passTCL_NULL_OK, then the empty string orNULLwill makeTcl_GetBoolFromObjreturnTCL_OK, with*charPtrfilled in with the value'\xFF'.
π¦ DESCRIPTION π¦
these procedures are how boolean values get passed to and from Tcl as Tcl_Obj's. here's the tea: when you store a boolean into a Tcl_Obj, any non-zero integer in intValue counts as boolean 1, and the integer 0 counts as boolean 0. simple as that, no cap. β¨
Tcl_NewBooleanObj creates a brand new Tcl_Obj, stashes the boolean value intValue inside it, and hands you back a pointer to that new Tcl_Obj. the new Tcl_Obj has a reference count of zero.
Tcl_SetBooleanObj takes objPtr β a pointer to an existing Tcl_Obj β and stores the boolean value intValue into the Tcl_Obj *objPtr. this is a write operation on *objPtr, so objPtr must be unshared. attempting to write to a shared Tcl_Obj will panic. not cute. not slay. don't do it. π a successful write of intValue into *objPtr means any former value stored in *objPtr gets freed β out with the old, in with the new π
.
Tcl_GetBooleanFromObj tries to retrieve a boolean value from whatever's stored in *objPtr. here's how it goes down:
- if
objPtrholds a string value thatTcl_GetBooleanrecognizes, then the recognized boolean value gets written at the address given byboolPtr. - if
objPtrholds any value that Tcl recognizes as a number, then: if that value is zero, a0is written at the address given byboolPtr; if that value is non-zero, a1is written at the address given byboolPtr. - in all cases where a value gets written at the address given by
boolPtr,Tcl_GetBooleanFromObjreturnsTCL_OK. - if the value of
objPtrdoesn't meet any of those conditions, thenTCL_ERRORis returned and an error message gets left in the interpreter's result β unlessinterpisNULL.
also, Tcl_GetBooleanFromObj may make changes to the internal fields of *objPtr so that future calls to Tcl_GetBooleanFromObj on the same objPtr can run more efficiently. it's optimizing. let her cook. π
Tcl_GetBoolFromObj does almost exactly what Tcl_GetBooleanFromObj does, except it has that extra flags parameter, which lets you say whether the empty string or NULL is accepted as valid. glow-up behavior honestly. π«Ά
β οΈ big important note bestie:Tcl_GetBooleanFromObjandTcl_GetBooleanare not functional equivalents. the set of values whereTcl_GetBooleanFromObjreturnsTCL_OKis strictly larger than the set whereTcl_GetBooleandoes the same. for example: the value β5β passed toTcl_GetBooleanFromObjgives you aTCL_OKreturn (and the boolean value1), while that same value passed toTcl_GetBooleangives you aTCL_ERRORreturn. same input, different energy. that's the whole point.
π REFERENCE COUNT MANAGEMENT π
keep track of the refs, bestie, it's giving responsible:
Tcl_NewBooleanObjalways returns a zero-reference object, a lot likeTcl_NewObj.Tcl_SetBooleanObjdoes not modify the reference count of itsobjPtrargument, but it does require that the object be unshared.Tcl_GetBooleanFromObjdoes not modify the reference count of itsobjPtrargument β it only reads. but heads up: this function may set the interpreter result, and if that's the only place holding a reference to the object, it will be deleted. π
π SEE ALSO π
Tcl_NewObj, Tcl_IsShared, Tcl_GetBoolean
β KEYWORDS β
boolean, value
ok that's the whole page, you ate it, periodt π β¨ this was the Tcl manual, version 8.5, and you were reading Tcl_BooleanObj(3) β go forth and store those booleans responsibly, bestie π¦πΈ
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- 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