All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Phillip Wood <phillip.wood123@gmail.com>
Cc: "René Scharfe" <l.s.r@web.de>, "Jeff King" <peff@peff.net>,
	"Oswald Buddenhagen" <oswald.buddenhagen@gmx.de>,
	"Git List" <git@vger.kernel.org>
Subject: Re: [PATCH 2/2] parse-options: use and require int pointer for OPT_CMDMODE
Date: Mon, 18 Sep 2023 10:11:45 -0700	[thread overview]
Message-ID: <xmqqedivl832.fsf@gitster.g> (raw)
In-Reply-To: <0bf56c65-e59f-4290-8160-cce141f692d5@gmail.com> (Phillip Wood's message of "Mon, 18 Sep 2023 14:33:56 +0100")

Phillip Wood <phillip.wood123@gmail.com> writes:

>> -	resume->mode = RESUME_SHOW_PATCH;
>> +	resume->mode_int = RESUME_SHOW_PATCH;
>>   	resume->sub_mode = new_value;
>>   	return 0;
>>   }
>
> Having "mode" and "mode_int" feels a bit fragile as only "mode_int" is
> valid while parsing the options but then we want to use "mode". I
> wonder if we could get Oswald's idea of using callbacks working in a
> reasonably ergonomic way with a couple of macros. We could add an new
> OPTION_SET_ENUM member to "enum parse_opt_type" that would take a
> setter function as well as the usual void *value. To set the value it
> would pass the value pointer and an integer value to the setter
> function. We could change OPT_CMDMODE to use OPTION_SET_ENUM and take
> the name of the enum as well as the integer value we want to set for
> that option. The name of the enum would be used to generate the name
> of the setter callback which would be defined with another macro. The
> macro to generate the setter would look like
>
> #define MAKE_CMDMODE_SETTER(name) \
> 	static void parse_cmdmode_ ## name (void * var, int value) {
> 		enum name *p = var;
> 		*p = value;
> 	}

Ah, OK.  So that's how you defeat "how the size and alignment of an
enum mixes well with int is not known and depends on particular enum
type".  It is a tad sad that this relies on "void *", which means
that the caller of parse_cmdmode_resume_type cannot be forced by the
compilers to pass "enum resume_type *" to the function, though.  And
that is probably inevitable with the design as .enum_setter needs to
be of a single type, and the member in the "struct option" that
points at the destination variable must be "void *" as it has to
be capable of pointing at various different enum types.

> ...
> Then in builtin/am.c at the top level we'd add
>
> MAKE_CMDMODE_SETTER(resume_type)
>
> and change the option definitions to look like
>
> OPT_CMDMODE(0, "continue", resume_type, &resume.mode, ...)

Yup, that is ergonomic and corrects "The shape of a particular enum
may not match 'int'" issue nicely.  I do not know how severe the
problem is that it is not quite type safe that we cannot enforce
resume_type is the same as typeof(resume.mode) here, though.

Thanks.


  reply	other threads:[~2023-09-18 17:11 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-09 21:10 [PATCH 1/2] parse-options: add int value pointer to struct option René Scharfe
2023-09-09 21:14 ` [PATCH 2/2] parse-options: use and require int pointer for OPT_CMDMODE René Scharfe
2023-09-10 10:18   ` Oswald Buddenhagen
2023-09-11 20:11     ` René Scharfe
2023-09-12  8:40       ` Jeff King
2023-09-16 17:45         ` Junio C Hamano
2023-09-18  9:28           ` René Scharfe
2023-09-18 10:10             ` Oswald Buddenhagen
2023-09-19  7:41               ` René Scharfe
2023-09-21 11:07                 ` [PATCH] am: fix error message in parse_opt_show_current_patch() Oswald Buddenhagen
2023-09-21 19:09                   ` Junio C Hamano
2023-09-21 19:28                     ` Oswald Buddenhagen
2023-09-18 13:33             ` [PATCH 2/2] parse-options: use and require int pointer for OPT_CMDMODE Phillip Wood
2023-09-18 17:11               ` Junio C Hamano [this message]
2023-09-18 19:48                 ` Phillip Wood
2023-10-03  8:49                   ` René Scharfe
2023-10-03 17:15                     ` Junio C Hamano
2023-09-19  7:47               ` René Scharfe
2023-09-11 19:12   ` Junio C Hamano
2023-09-11 20:11     ` René Scharfe
2023-09-19  9:40   ` Oswald Buddenhagen
2023-09-20  8:18     ` René Scharfe
2023-09-21 10:40       ` Oswald Buddenhagen
2023-10-03  8:49         ` René Scharfe
2023-10-03  9:38           ` Oswald Buddenhagen
2023-10-03 17:54             ` René Scharfe
2023-10-03 18:24               ` Oswald Buddenhagen
2023-09-10 18:40 ` [PATCH 1/2] parse-options: add int value pointer to struct option Taylor Blau
2023-09-11 19:19   ` Junio C Hamano
2023-09-11 22:28     ` Oswald Buddenhagen
2023-09-18 11:34       ` Kristoffer Haugsbakk
2023-09-18  9:53     ` René Scharfe
2023-09-18 10:28       ` Oswald Buddenhagen
2023-09-18 16:17       ` Junio C Hamano
2023-09-20 11:34         ` René Scharfe
2023-09-11 20:12   ` René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqedivl832.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=oswald.buddenhagen@gmx.de \
    --cc=peff@peff.net \
    --cc=phillip.wood123@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.