File C++ Conversion Background #103343

Open
opened 2022-12-19 15:59:10 +01:00 by Hans Goudey · 5 comments
Member

Moving Blender code from C to C++ has many benefits:

  • Less manual memory management with use of C++ data structures like Array, Vector, Map
  • Easier access to better performance through those same data structures
  • Can use existing C++ APIs directly instead of relying on C wrappers
  • Stronger type safety enforced by the compiler
  • Much easier multi-threading with parallel_for, etc.

Common changes when moving C code to C++:

  • NULL -> nullptr
  • Remove UNUSED() macro for unused arguments. Comment name out instead (see style guide).
  • Remove use of designated initializers (not part of C++17)
  • Add casting where necessary (static_cast and reinterpret_cast)
  • Change to functional casts for arithmetic and enum types (from style guide)
  • Change headers <string.h> -> <cstring> (Clang tidy check)
  • Remove typedef
  • Remove unnecessary struct keywords
  • Add ENUM_OPERATORS to used enums in headers.

When converting C to C++ code, do not change old loops to use LISTBASE_FOREACH and the like. That's known to cause behavior changes in subtle ways. Instead just insert casts. A cleanup to use LISTBASE_FOREACH can be done separately afterwards.

Moving Blender code from C to C++ has many benefits: - Less manual memory management with use of C++ data structures like `Array`, `Vector`, `Map` - Easier access to better performance through those same data structures - Can use existing C++ APIs directly instead of relying on C wrappers - Stronger type safety enforced by the compiler - Much easier multi-threading with `parallel_for`, etc. Common changes when moving C code to C++: - `NULL` -> `nullptr` - Remove `UNUSED()` macro for unused arguments. Comment name out instead ([see style guide](https://wiki.blender.org/wiki/Style_Guide/Best_Practice_C_Cpp#Comment_unused_arguments_in_C++_code)). - Remove use of designated initializers (not part of C++17) - Add casting where necessary (`static_cast` and `reinterpret_cast`) - Change to functional casts for arithmetic and enum types (from style guide) - Change headers `<string.h>` -> `<cstring>` (Clang tidy check) - Remove `typedef` - Remove unnecessary `struct` keywords - Add `ENUM_OPERATORS` to used enums in headers. When converting C to C++ code, do **not** change old loops to use `LISTBASE_FOREACH` and the like. That's known to cause behavior changes in subtle ways. Instead just insert casts. A cleanup to use `LISTBASE_FOREACH` can be done separately afterwards.

This issue was referenced by 3d6fd2906b

This issue was referenced by 3d6fd2906be0adac2f42528f55a53e0cac1d5896
Member

Is the idea to move most/all Blender code to C++ I am interested in converting UI files to C++

Is the idea to move most/all Blender code to C++ I am interested in converting UI files to C++
Author
Member

I already converted the editors/interface directory to C++ if that's what you mean.

I already converted the `editors/interface` directory to C++ if that's what you mean.
Member

What Module label is appropriate here?

What Module label is appropriate here?
Hans Goudey added the
Module
Development Management
label 2023-02-20 14:14:10 +01:00
Author
Member

Maybe Development Management? Or Core.

Maybe Development Management? Or Core.
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:40 +02:00
Jacques Lucke referenced this issue from a commit 2023-07-20 09:47:26 +02:00
Jacques Lucke referenced this issue from a commit 2023-07-20 09:49:17 +02:00
Jacques Lucke referenced this issue from a commit 2023-07-20 10:41:46 +02:00
Jacques Lucke referenced this issue from a commit 2023-07-20 22:12:37 +02:00
Hans Goudey referenced this issue from a commit 2023-08-02 15:34:53 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
Asset Browser Project
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#103343
No description provided.