Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[collectd 6, draft] Add the VALUE_T() generic macro. #4227

Draft
wants to merge 4 commits into
base: collectd-6.0
Choose a base branch
from

Conversation

octo
Copy link
Member

@octo octo commented Jan 10, 2024

VALUE_T(x) is a generic macro that initializes and returns a value_t. Depending on the type of the argument x, it will either initialize the value_t as a counter_t, derive_t, or gauge_t.

This means that the previous expression:

value_t x = (value_t){.derive = y};

can now be written as:

value_t x = VALUE_T(y);

Passing in an incompatible type, such as an int, will cause a compile time error:

src/cpu.c: In function 'module_register':
./src/daemon/metric.h:56:12: error: '_Generic' selector of type 'int' is not compatible with any association
   56 |   _Generic((x),                                                                \
      |            ^
src/cpu.c:929:17: note: in expansion of macro 'VALUE_T'
  929 |   value_t foo = VALUE_T((int){42});
      |                 ^~~~~~~

Draft: I want to gauge how widely supported this C11 feature is.

`VALUE_T(x)` is a generic macro that initializes and returns a `value_t`.
Depending on the type of the argument `x`, it will either initialize the
`value_t` as a `counter_t`, `derive_t`, or `gauge_t`.

This means that the previous expression:

    value_t x = (value_t){.derive = y};

can now be written as:

    value_t x = VALUE_T(y);

Passing in an incompatible type, such as an `int`, will cause a compile time
error:

```
src/cpu.c: In function 'module_register':
./src/daemon/metric.h:56:12: error: '_Generic' selector of type 'int' is not compatible with any association
   56 |   _Generic((x),                                                                \
      |            ^
src/cpu.c:929:17: note: in expansion of macro 'VALUE_T'
  929 |   value_t foo = VALUE_T((int){42});
      |                 ^~~~~~~
```
@octo octo added the Maintenance A pull request without immediate user-observable impact label Jan 10, 2024
@collectd-bot collectd-bot added this to the 6.0 milestone Jan 10, 2024
@octo
Copy link
Member Author

octo commented Jan 10, 2024

Solaris (/opt/solarisstudio12.4/bin/cc) apparently does not implement type-generic macros.

@eero-t
Copy link
Contributor

eero-t commented Jan 12, 2024

Solaris (/opt/solarisstudio12.4/bin/cc) apparently does not implement type-generic macros.

So what's the plan with this PR, give type as another arg, or give up as value of that macro was automatic inferencing of the type to avoid mistakes with it?

@octo
Copy link
Member Author

octo commented Jan 12, 2024

I'm undecided. I think the macro makes a common use case nicer and safer. It may be reasonable to break compatibility with an ancient compiler for this.

However I don't know how commonly the platform is still used and whether any alternative compilers are available. With this lack of knowledge is hard to make an informed decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance A pull request without immediate user-observable impact
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants