WIP: Code quality: Add type-safe C++ functions to cast ID types #111920

Draft
Julian Eisel wants to merge 6 commits from JulianEisel/blender:temp-id-cast into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

reinterpret_cast()s are annoying to type, contain redundant
information (e.g. const or pointer/ref) and remove type safety.
Non-obvious issue: Being annoying to type leads people to avoid the
casting and use &some_id->id instead. This can introduce null-pointer
dereferences when some_id is null (null may still be a valid value for
the result of the expression). So making casts easy to type can in fact
prevent bugs.

This adds simple ID-type casting functions from IDs to the "derived"
type and vise versa. Features/benefits:

  • Contain static type checks to ensure the requested derived type can
    actually be used as ID type (e.g. id_cast<Sequence>(...) or
    id_cast<Bone>(...) won't compile, unlike reinterpret_cast).
  • Preserve const correctness implicitly.
  • Works for both pointers and references implicitly.
  • Easy to search for and change unlike all possible other casts (useful
    in case we switch to C++ style inheritance, for example).
  • Easy to write.

To cast an ID pointer/reference to a derived type, the derived type
needs to be passed as template parameter. To cast to an ID
pointer/reference, no template parameter is required.

With this we might want to make it a good-practice or even guideline to
use the cast over alternatives.

`reinterpret_cast()`s are annoying to type, contain redundant information (e.g. `const` or pointer/ref) and remove type safety. Non-obvious issue: Being annoying to type leads people to avoid the casting and use `&some_id->id` instead. This can introduce null-pointer dereferences when `some_id` is null (null may still be a valid value for the result of the expression). So making casts easy to type can in fact prevent bugs. This adds simple ID-type casting functions from IDs to the "derived" type and vise versa. Features/benefits: - Contain static type checks to ensure the requested derived type can actually be used as ID type (e.g. `id_cast<Sequence>(...)` or `id_cast<Bone>(...)` won't compile, unlike `reinterpret_cast`). - Preserve `const` correctness implicitly. - Works for both pointers and references implicitly. - Easy to search for and change unlike all possible other casts (useful in case we switch to C++ style inheritance, for example). - Easy to write. To cast an ID pointer/reference to a derived type, the derived type needs to be passed as template parameter. To cast to an ID pointer/reference, no template parameter is required. With this we might want to make it a good-practice or even guideline to use the cast over alternatives.
Julian Eisel added the
Module
Core
label 2023-09-04 11:51:22 +02:00
Julian Eisel added 1 commit 2023-09-04 11:51:36 +02:00
3b242e18f8 Code quality: Add type-safe C++ functions to cast ID types
`reinterpret_cast()`s are annoying to type, contain redundant
information (e.g. `const` or pointer/ref) and remove type safety.
Non-obvious issue: Being annoying to type leads people to avoid the
casting and use `&some_id->id` instead, which can introduce null-pointer
dereferences when `some_id` is null (null may still be a valid value for
the result of the expression). So making casts easy to type can in fact
prevent bugs.

This adds simple ID-type casting functions from IDs to the "derived"
type and vise versa. Main benefits:
- Contain static type checks to ensure the requested derived type can
  actually be used as ID type (e.g. `id_cast<Sequence>(...)` or
  `id_cast<Bone>(...) won't compile, unlike `reinterpret_cast`).
- Preserve `const` correctness implicitly.
- Works for both pointers and references implicitly.
- Easy to search for and change unlike all possible other casts (useful
  in case we switch to C++ style inheritance, for example).
- Easy to write.

To cast an ID pointer/reference to a derived type, the derived type
needs to be passed as template parameter. To cast to an ID
pointer/reference, no template parameter is required.

With this we might want to make it a good-practice or even guideline to
use the cast over alternatives.
Julian Eisel reviewed 2023-09-04 11:53:15 +02:00
@ -273,3 +273,3 @@
const ID_Type id_type = GS(id_for_copy->name);
if (id_type == ID_OB) {
const Object *object = reinterpret_cast<const Object *>(id_for_copy);
const Object *object = id_cast<Object>(id_for_copy);
Author
Member

Note that id_for_copy is const. id_cast<>() preserves this, without needing to repeat const in the template parameter.

Note that `id_for_copy` is const. `id_cast<>()` preserves this, without needing to repeat `const` in the template parameter.
Julian Eisel added 1 commit 2023-09-04 11:55:00 +02:00
Julian Eisel added 1 commit 2023-09-04 12:03:50 +02:00
Julian Eisel requested review from Sergey Sharybin 2023-09-04 12:10:30 +02:00
Julian Eisel requested review from Bastien Montagne 2023-09-04 12:10:30 +02:00
Julian Eisel requested review from Jacques Lucke 2023-09-04 12:10:30 +02:00
Jacques Lucke approved these changes 2023-09-04 12:25:11 +02:00
Jacques Lucke left a comment
Member

Not necessary for this patch. But it would be nice if this could have a runtime assert that checks whether the type cast is valid when converting to a derived type.

@blender-bot build

Not necessary for this patch. But it would be nice if this could have a runtime assert that checks whether the type cast is valid when converting to a derived type. @blender-bot build
@ -1326,0 +1332,4 @@
#ifdef __cplusplus
# define _ID_TYPE_STATIC_ASSERT(ID_Type) \
Member

Think this can also just be another inline function instead of a macro.

Think this can also just be another inline function instead of a macro.
JulianEisel marked this conversation as resolved
Sergey Sharybin requested changes 2023-09-04 12:45:48 +02:00
Sergey Sharybin left a comment
Owner

There is an inlined comment which needs to be addressed.

I also have a concern on a clarity level of the proposed API: id_cast(foo) and id_cast<Bar>(foo) look so similar but do opposite things. Something like cast_to_id and cast_from_id would be much more clear for reading and explaining.

P.S. Just imagine someone asking in a chat: what does id_cast do. And reply being: well, depends on which exact id_cast you're talking about.

There is an inlined comment which needs to be addressed. I also have a concern on a clarity level of the proposed API: `id_cast(foo)` and `id_cast<Bar>(foo)` look so similar but do opposite things. Something like `cast_to_id` and `cast_from_id` would be much more clear for reading and explaining. P.S. Just imagine someone asking in a chat: what does `id_cast` do. And reply being: well, depends on which exact `id_cast` you're talking about.
@ -1326,0 +1333,4 @@
#ifdef __cplusplus
# define _ID_TYPE_STATIC_ASSERT(ID_Type) \
{ \

More proper way for such constructions is do to

#  define _ID_TYPE_STATIC_ASSERT(ID_Type) \
  do { \
      static_assert(!std::is_same_v<ID_Type, ID>, \
                    "Expected a specific ID type here, not the `ID` base type"); \
      static_assert(std::is_same_v<decltype(ID_Type::id), ID>, \
                    "Invalid ID type: Expected `ID id` member"); \
      static_assert( \
          offsetof(ID_Type, id) == 0, \
          "Invalid ID type: `ID id` member must be at the very beginning of the struct"); \
  } while (false)

It allows to use the macro in the conditions, like:

if (foo)
    _ID_TYPE_STATIC_ASSERT(bar);
else
  baz();

It is not possible to do with the {} (void)0 semantic.

While an argument could be made here that it is not how the macro is intended to be used, and that the "buggy" usecase is against our guidelines, I do not think we should have a bad practice example, especially in such centralized public header.

More proper way for such constructions is do to ``` # define _ID_TYPE_STATIC_ASSERT(ID_Type) \ do { \ static_assert(!std::is_same_v<ID_Type, ID>, \ "Expected a specific ID type here, not the `ID` base type"); \ static_assert(std::is_same_v<decltype(ID_Type::id), ID>, \ "Invalid ID type: Expected `ID id` member"); \ static_assert( \ offsetof(ID_Type, id) == 0, \ "Invalid ID type: `ID id` member must be at the very beginning of the struct"); \ } while (false) ``` It allows to use the macro in the conditions, like: ``` if (foo) _ID_TYPE_STATIC_ASSERT(bar); else baz(); ``` It is not possible to do with the `{} (void)0` semantic. While an argument could be made here that it is not how the macro is intended to be used, and that the "buggy" usecase is against our guidelines, I do not think we should have a bad practice example, especially in such centralized public header.
Author
Member

This is a function instead of a macro now, so this can be avoided.

This is a function instead of a macro now, so this can be avoided.
JulianEisel marked this conversation as resolved
Julian Eisel added 3 commits 2023-09-04 15:42:18 +02:00
Author
Member

I also have a concern on a clarity level of the proposed API: id_cast(foo) and id_cast<Bar>(foo) look so similar but do opposite things. Something like cast_to_id and cast_from_id would be much more clear for reading and explaining.

I realize this is subjective, but I find such API names hard to use. Reason is language is quite ambiguous, I have to pause, think which direction I want to cast in and guesstimate which function name that translates to. There are multiple ways to name the same operation. Like: was it "from ID" or "to ID type"? Or "as ID"? I have that issue every single time I want to save integer values in pointers, I need to open BLI_utildefines.h and scroll to the POINTER_FROM_INT()/POINTER_AS_INT() macros because I can never remember the naming pattern we use for them (e.g. could also be INT_TO_POINTER() and POINTER_TO_INT()). It's just not intuitive for me. Not sure if others have the same issue.

For id_cast there's simply no thinking involved, it does the right thing (or fails to compile when you have to specify the type to cast to), the confusion is avoided.

There's also value in keeping the naming scheme consistent with standard C++ casts.

P.S. Just imagine someone asking in a chat: what does id_cast do. And reply being: well, depends on which exact id_cast you're talking about.

Honestly don't think that's an issue. Also added a comment to each cast clarifying in brief what it does. You have the same with standard C++ casts, like const_cast<>(), which can either remove or add const (e.g. to force calling a const overload), depending on context.

> I also have a concern on a clarity level of the proposed API: `id_cast(foo)` and `id_cast<Bar>(foo)` look so similar but do opposite things. Something like `cast_to_id` and `cast_from_id` would be much more clear for reading and explaining. I realize this is subjective, but I find such API names hard to use. Reason is language is quite ambiguous, I have to pause, think which direction I want to cast in and guesstimate which function name that translates to. There are multiple ways to name the same operation. Like: was it "from ID" or "to ID type"? Or "as ID"? I have that issue every single time I want to save integer values in pointers, I need to open `BLI_utildefines.h` and scroll to the `POINTER_FROM_INT()`/`POINTER_AS_INT()` macros because I can never remember the naming pattern we use for them (e.g. could also be `INT_TO_POINTER()` and `POINTER_TO_INT()`). It's just not intuitive for me. Not sure if others have the same issue. For `id_cast` there's simply no thinking involved, it does the right thing (or fails to compile when you have to specify the type to cast to), the confusion is avoided. There's also value in keeping the naming scheme consistent with standard C++ casts. > P.S. Just imagine someone asking in a chat: what does `id_cast` do. And reply being: well, depends on which exact `id_cast` you're talking about. Honestly don't think that's an issue. Also added a comment to each cast clarifying in brief what it does. You have the same with standard C++ casts, like `const_cast<>()`, which can either remove or add `const` (e.g. to force calling a `const` overload), depending on context.
Author
Member

P.S. Just imagine someone asking in a chat: what does id_cast do. And reply being: well, depends on which exact id_cast you're talking about.

BTW my reply would be simple: It is a cast that works both ways. If you pass it a "derived" ID type (like Object * or Material *) it casts it to an ID pointer/reference. If you pass it an ID pointer/reference, it casts it to the derived ID type you request in the template parameter (e.g. id_cast<Object>() casts to an Object pointer/reference).

> P.S. Just imagine someone asking in a chat: what does `id_cast` do. And reply being: well, depends on which exact `id_cast` you're talking about. BTW my reply would be simple: It is a cast that works both ways. If you pass it a "derived" ID type (like `Object *` or `Material *`) it casts it to an `ID` pointer/reference. If you pass it an `ID` pointer/reference, it casts it to the derived ID type you request in the template parameter (e.g. `id_cast<Object>()` casts to an `Object` pointer/reference).
Julian Eisel requested review from Sergey Sharybin 2023-09-04 16:08:24 +02:00

For id_cast there's simply no thinking involved, it does the right thing (or fails to compile when you have to specify the type to cast to), the confusion is avoided.

Maybe for when you type code it is could be easier, but when you read code (and you read it much more than you type it) the confusion is clearly not avoided.

Honestly don't think that's an issue. Also added a comment to each cast clarifying in brief what it does. You have the same with standard C++ casts, like const_cast<>(), which can either remove or add const (e.g. to force calling a const overload), depending on context.

The comparison is not very valid. const_cast does one thing and does it explicitly.
The current proposed formulation of id_cast does a lot, and implicitly: it tries to be smart about the destination type, and it also tries to be smarter than other casting function w.r.t explictness of the const qualifier.

The former one can be solved by allowing id_cast<Type> to do both up-casting from a type of ID, and down-casting from ID to a type. In practice it would mean that instead of id_cast(foo) you'll do id_cast<ID>(foo). Makes it consistent with other casting functionality provided by the language.

For the latter one I pretty much prefer to the existing semantics of the language. So instead of id_cast<Object> we explicit about id_cast<const Object&>. I do not see a strong enough reason to diverge from the constructions typically used by the language. More smartness and Blender specific difference introduced to our toolbox makes it harder for the new developer to come onboard and be efficient.

> For `id_cast` there's simply no thinking involved, it does the right thing (or fails to compile when you have to specify the type to cast to), the confusion is avoided. Maybe for when you type code it is could be easier, but when you read code (and you read it much more than you type it) the confusion is clearly not avoided. > Honestly don't think that's an issue. Also added a comment to each cast clarifying in brief what it does. You have the same with standard C++ casts, like `const_cast<>()`, which can either remove or add `const` (e.g. to force calling a `const` overload), depending on context. The comparison is not very valid. `const_cast` does one thing and does it explicitly. The current proposed formulation of `id_cast` does a lot, and implicitly: it tries to be smart about the destination type, and it also tries to be smarter than other casting function w.r.t explictness of the const qualifier. The former one can be solved by allowing `id_cast<Type>` to do both up-casting from a type of ID, and down-casting from ID to a type. In practice it would mean that instead of `id_cast(foo)` you'll do `id_cast<ID>(foo)`. Makes it consistent with other casting functionality provided by the language. For the latter one I pretty much prefer to the existing semantics of the language. So instead of `id_cast<Object>` we explicit about `id_cast<const Object&>`. I do not see a strong enough reason to diverge from the constructions typically used by the language. More smartness and Blender specific difference introduced to our toolbox makes it harder for the new developer to come onboard and be efficient.
Sergey Sharybin requested changes 2023-09-04 16:25:18 +02:00
Sergey Sharybin left a comment
Owner

Leaving a comment to allow the "Requested changes" state after re-rewview requested.

Leaving a comment to allow the "Requested changes" state after re-rewview requested.

I would be fine with current patch, but I also tend to agree with @Sergey regarding not being 'too' smart and doing too much automated hidden things under the hood. From personal experience, this very easily can lead to confusion and unexpected results in edge cases, when devs do not fully understand what the 'under the hood black magic' is doing.

So indeed I'd also rather see a slightly more verbose and explicit id_cast<expected type>, even though this somewhat defeats the initial goal of the PR, which was to make conversion to ID as concise as possible.

I would be fine with current patch, but I also tend to agree with @Sergey regarding not being 'too' smart and doing too much automated hidden things under the hood. From personal experience, this very easily can lead to confusion and unexpected results in edge cases, when devs do not fully understand what the 'under the hood black magic' is doing. So indeed I'd also rather see a slightly more verbose and explicit `id_cast<expected type>`, even though this somewhat defeats the initial goal of the PR, which was to make conversion to ID as concise as possible.
Bastien Montagne requested changes 2024-02-12 10:40:27 +01:00
Bastien Montagne left a comment
Owner

Think this needs more work?

Think this needs more work?
Julian Eisel changed title from Code quality: Add type-safe C++ functions to cast ID types to WIP: Code quality: Add type-safe C++ functions to cast ID types 2024-02-14 23:51:04 +01:00
This pull request has changes conflicting with the target branch.
  • source/blender/blenkernel/intern/action.cc
  • source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
  • source/blender/editors/object/object_add.cc
  • source/blender/nodes/intern/geometry_nodes_log.cc

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u temp-id-cast:JulianEisel-temp-id-cast
git checkout JulianEisel-temp-id-cast
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#111920
No description provided.