Skip to content
Tcl Source Code Artifact [169cd3df15]
Login

Artifact [169cd3df15]

Artifact 169cd3df15b7dfa664f0068b78edc33105de4af2e0d2ff2531850f814c9d7faa:


---
CommandName: Tcl_BooleanObj
title: Tcl_BooleanObj
ManualSection: 3
Version: 8.5
TclPart: Tcl
TclDescription: Tcl Library Procedures
Links:
 - Tcl_NewObj
 - Tcl_IsShared
 - Tcl_GetBoolean
Keywords:
 - boolean
 - value
Copyright:
 - Copyright (c) 1996-1997 Sun Microsystems, Inc.
---

# Name

Tcl\_NewBooleanObj, Tcl\_SetBooleanObj, Tcl\_GetBooleanFromObj, Tcl\_GetBoolFromObj - store/retrieve boolean value in a [Tcl\_Obj][Object]

# Synopsis

::: {.synopsis} :::
**#include <tcl.h>**
[Tcl\_Obj \*]{.ret} [Tcl\_NewBooleanObj]{.ccmd} [intValue]{.cargs}
[Tcl\_SetBooleanObj]{.ccmd} [objPtr, intValue]{.cargs}
[int]{.ret} [Tcl\_GetBooleanFromObj]{.ccmd} [interp, objPtr, boolPtr]{.cargs}
[int]{.ret} [Tcl\_GetBoolFromObj]{.ccmd} [interp, objPtr, flags, charPtr]{.cargs}
:::

# Arguments

::: {.arguments} :::

[intValue]{.carg .in type="int"}
: Integer value to be stored as a boolean value in a [Tcl\_Obj][Object].

[\*objPtr]{.carg .in/out type="Tcl_Obj"}
: Points to the [Tcl\_Obj][Object] in which to store, or from which to retrieve a boolean value.

[\*interp]{.carg .in/out type="Tcl_Interp"}
: If a boolean value cannot be retrieved, an error message is left in the interpreter's result value unless *interp* is NULL.

[\*boolPtr]{.carg .out type="bool &| int"}
: Points to place where **Tcl\_GetBooleanFromObj** stores the boolean value (0 or 1) obtained from *objPtr*.

[\*charPtr]{.carg .out type="char"}
: Points to place where **Tcl\_GetBoolFromObj** stores the boolean value (0 or 1) obtained from *objPtr*.

[flags]{.carg .in type="int"}
: 0 or TCL\_NULL\_OK. If TCL\_NULL\_OK is used, then the empty string or NULL will result in **Tcl\_GetBoolFromObj** return [TCL\_OK][catch], the \*charPtr filled with the value **'\\xFF'**;

:::

# Description

These procedures are used to pass boolean values to and from Tcl as [Tcl\_Obj][Object]'s.  When storing a boolean value into a [Tcl\_Obj][Object], any non-zero integer value in *intValue* is taken to be the boolean value **1**, and the integer value **0** is taken to be the boolean value **0**.

**Tcl\_NewBooleanObj** creates a new [Tcl\_Obj][Object], stores the boolean value *intValue* in it, and returns a pointer to the new [Tcl\_Obj][Object]. The new [Tcl\_Obj][Object] has reference count of zero.

**Tcl\_SetBooleanObj** accepts *objPtr*, a pointer to an existing [Tcl\_Obj][Object], and stores in the [Tcl\_Obj][Object] *\*objPtr* the boolean value *intValue*.  This is a write operation on *\*objPtr*, so *objPtr* must be unshared.  Attempts to write to a shared [Tcl\_Obj][Object] will panic.  A successful write of *intValue* into *\*objPtr* implies the freeing of any former value stored in *\*objPtr*.

**Tcl\_GetBooleanFromObj** attempts to retrieve a boolean value from the value stored in *\*objPtr*. If *objPtr* holds a string value recognized by [Tcl\_GetBoolean][GetInt], then the recognized boolean value is written at the address given by *boolPtr*. If *objPtr* holds any value recognized as a number by Tcl, then if that value is zero a 0 is written at the address given by *boolPtr* and if that value is non-zero a 1 is written at the address given by *boolPtr*. In all cases where a value is written at the address given by *boolPtr*, **Tcl\_GetBooleanFromObj** returns [TCL\_OK][catch]. If the value of *objPtr* does not meet any of the conditions above, then [TCL\_ERROR][catch] is returned and an error message is left in the interpreter's result unless *interp* is NULL. **Tcl\_GetBooleanFromObj** may also make changes to the internal fields of *\*objPtr* so that future calls to **Tcl\_GetBooleanFromObj** on the same *objPtr* can be performed more efficiently.

**Tcl\_GetBoolFromObj** functions almost the same as **Tcl\_GetBooleanFromObj**, but it has an additional parameter **flags**, which can be used to specify whether the empty string or NULL is accepted as valid.

Note that the routines **Tcl\_GetBooleanFromObj** and [Tcl\_GetBoolean][GetInt] are not functional equivalents. The set of values for which **Tcl\_GetBooleanFromObj** will return [TCL\_OK][catch] is strictly larger than the set of values for which [Tcl\_GetBoolean][GetInt] will do the same. For example, the value "5" passed to **Tcl\_GetBooleanFromObj** will lead to a [TCL\_OK][catch] return (and the boolean value 1), while the same value passed to [Tcl\_GetBoolean][GetInt] will lead to a [TCL\_ERROR][catch] return. 

# Reference count management

**Tcl\_NewBooleanObj** always returns a zero-reference object, much like [Tcl\_NewObj][Object].

**Tcl\_SetBooleanObj** does not modify the reference count of its *objPtr* argument, but does require that the object be unshared.

**Tcl\_GetBooleanFromObj** does not modify the reference count of its *objPtr* argument; it only reads. 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.


[catch]: ../Tcl/catch.md
[GetInt]: GetInt.md
[Object]: ../Tcl/Object.md