use gi.invalid_obj instead of cg.zeroobj

cg.zeroobj was originally added (under its previous unprefixed name)
for providing a one-line way to zero out the fields of a struct obj.

    struct obj tempobj;
    tempobj = cg.zeroobj;

    initfn(struct obj *otmp)
    {
        if (otmp)
            *otmp = cg.zeroobj;
    }

More recently, the address of cg.zeroobj began to be used as a return
flag to indicate some things, but the 'const struct obj zeroobj' wasn't
an ideal fit for the purpose and required a number of casts, including
casting away const.

Provide a better fitting variable (gi.invalid_obj) and eliminate a
number of casts.
This commit is contained in:
nhmall
2023-11-10 11:07:49 -05:00
parent 17dd51d922
commit 314a2a9489
14 changed files with 35 additions and 30 deletions

View File

@@ -1527,13 +1527,13 @@ any_obj_ok(struct obj *obj)
* getobj returns:
* struct obj *xxx: object to do something with.
* (struct obj *) 0 error return: no object.
* &cg.zeroobj explicitly no object (as in w-).
* &gi.invalid_obj explicitly no object (as in w-).
* The obj_ok callback should not have side effects (apart from
* abnormal-behavior things like impossible calls); it can be called multiple
* times on the same object during the execution of this function.
* Callbacks' argument is either a valid object pointer or a null pointer,
* which represents the validity of doing that action on HANDS_SYM. getobj
* won't call it with &cg.zeroobj, so its behavior can be undefined in that
* won't call it with &gi.invalid_obj, so its behavior can be undefined in that
* case.
*/
struct obj *
@@ -1579,7 +1579,7 @@ getobj(
/* check whether the hands/self choice is suitable */
v = (*obj_ok)((struct obj *) 0);
if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY)
otmp = (struct obj *) &cg.zeroobj;
otmp = &gi.invalid_obj;
} else {
/* there could be more than one match if key is '#';
take first one which passes the obj_ok callback */
@@ -1742,7 +1742,7 @@ getobj(
if (ilet == HANDS_SYM) { /* '-' */
if (!allownone)
mime_action(word);
return (allownone ? (struct obj *) &cg.zeroobj : (struct obj *) 0);
return (allownone ? &gi.invalid_obj : (struct obj *) 0);
}
redo_menu:
/* since gold is now kept in inventory, we need to do processing for
@@ -1778,7 +1778,7 @@ getobj(
continue;
}
if (ilet == HANDS_SYM)
return (struct obj *) &cg.zeroobj; /* cast away 'const' */
return &gi.invalid_obj;
if (ilet == '\033') {
if (flags.verbose)
pline1(Never_mind);