Code quality: Add type-safe C++ functions to cast ID types #111920
No reviewers
Labels
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
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
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#111920
Loading…
Reference in New Issue
No description provided.
Delete Branch "JulianEisel/blender:temp-id-cast"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
reinterpret_cast()
s are annoying to type, contain redundantinformation (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-pointerdereferences when
some_id
is null (null may still be a valid value forthe 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:
actually be used as ID type (e.g.
id_cast<Sequence>(...)
orid_cast<Bone>(...)
won't compile, unlikereinterpret_cast
).const
correctness implicitly.in case we switch to C++ style inheritance, for example).
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.
@ -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);
Note that
id_for_copy
is const.id_cast<>()
preserves this, without needing to repeatconst
in the template parameter.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) \
Think this can also just be another inline function instead of a macro.
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)
andid_cast<Bar>(foo)
look so similar but do opposite things. Something likecast_to_id
andcast_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 exactid_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
It allows to use the macro in the conditions, like:
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.
This is a function instead of a macro now, so this can be avoided.
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 thePOINTER_FROM_INT()
/POINTER_AS_INT()
macros because I can never remember the naming pattern we use for them (e.g. could also beINT_TO_POINTER()
andPOINTER_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.
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 addconst
(e.g. to force calling aconst
overload), depending on context.BTW my reply would be simple: It is a cast that works both ways. If you pass it a "derived" ID type (like
Object *
orMaterial *
) it casts it to anID
pointer/reference. If you pass it anID
pointer/reference, it casts it to the derived ID type you request in the template parameter (e.g.id_cast<Object>()
casts to anObject
pointer/reference).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.
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 ofid_cast(foo)
you'll doid_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 aboutid_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.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.Checkout
From your project repository, check out a new branch and test the changes.