6.2 KiB
6.2 KiB
Workload Analysis: Removing C++ Stdlib & Migrating to Pure C
This analysis outlines the required workload, technical impact, and migration strategy for:
- Removing all standard C++ library dependencies (
std::*) from the Juliet codebase. - Moving completely from C++ to pure C.
1. Scope Overview
The repository consists of multiple sub-projects (Juliet Core Engine, JulietApp, Game, Romeo, and External dependencies). Eliminating stdlib dependencies versus migrating completely to pure C represent two distinct milestones:
| Metric / Domain | Task 1: Eliminate C++ stdlib | Task 2: Pure C Migration |
|---|---|---|
| Language Target | C++20 (without standard libraries) | Pure C (C11 / C99 standard) |
| Containers & Math | Custom templates (Vector<T>, HashTable<K,V>, custom algorithms) |
C structs + void* / macro templates + explicit functions |
| Threading & Sync | Win32 / OS APIs directly (CreateThread, SRWLOCK) |
Native OS APIs (CreateThread, SRWLOCK, atomic primitives) |
| External Third-Party | Native Direct3D 12, ImGui | cimgui (C bindings), DirectX C COM interfaces (ID3D12Device*) |
| OOP & Templates | Kept intact | Struct-based vtables, no function overloading, no references (&) |
2. Phase 1: Removing C++ stdlib Dependencies
A. Containers & Utility Replacement
- Current Usages:
<vector>,<queue>,<list>,<forward_list>,<algorithm>,<bit>,<concepts>,<type_traits>,<source_location>. - Workload:
- Containers: Replace container implementations (such as
Juliet/include/Core/Container/Vector.h, which currently includes<vector>) with custom dynamically resizing arrays / buffer allocators backed byMemoryArena. - Type Traits & Concepts: Replace
<type_traits>and<concepts>usage in files likeGame/Entity/Entity.hwith compiler intrinsics (e.g.__is_enum,__is_base_of) or internal lightweight trait structures. - Algorithms & Math Utilities: Implement custom
Min,Max,Clamp,Sort, andSwapfunctions inCoreUtils.hor custom inline header helpers.
- Containers: Replace container implementations (such as
B. Threading & Synchronization
- Current Usages:
<thread>,<mutex>,std::thread,std::mutex,std::lock_guard. - Workload:
- Replace
<thread>inThread.handRomeo/src/main.cppwith platform abstraction wrappers using Win32CreateThread/CloseHandle. - Replace
<mutex>inMutex.hwithSRWLOCKorCRITICAL_SECTION.
- Replace
C. Time & File I/O
- Current Usages:
<chrono>,<cstdio>,<fstream>,<ios>,<iosfwd>,<cstdlib>. - Workload:
- Replace
<chrono>inLogManager.cppandSystemEvent.cppwithQueryPerformanceCounter/QueryPerformanceFrequencyon Windows. - Standardize filesystem I/O on Win32 API (
CreateFileW,ReadFile,WriteFile) or minimal C runtime headers (<stdio.h>,<stdlib.h>,<string.h>).
- Replace
D. Memory Allocators & RTTI
- Current Usages:
<new>,<typeinfo>(typeidinMemoryArena.h),<memory>. - Workload:
- Replace placement
newwith custom inline placement functions or custom explicit initialization methods. - Replace
typeid/type_infousages with custom type hashing or enum type IDs.
- Replace placement
3. Phase 2: Complete Migration to C
Transitioning fully to C requires refactoring away all C++ language constructs.
A. DirectX 12 & Agility SDK Integration
- D3D12 Header Interface: Switch from C++ COM smart pointers / methods (
device->CreateCommandAllocator(...)) to standard C COM interfaces (ID3D12Device_CreateCommandAllocator(device, ...)). - D3DX12 Helper Library:
d3dx12_state_object.hand helper files rely heavily on stdlib (std::vector,std::memory, C++ structures). These must be rewritten in plain C or replaced with native DirectX 12 C struct initializers.
B. UI Framework (ImGui)
- Dear ImGui is natively C++.
- Workload: Switch from standard
imgui.h/imgui_stdlib.hto cimgui (C-compatible wrappers for ImGui) or maintain a minimal C++ bridge file specifically for ImGui.
C. Codebase Refactoring (Language Paradigm)
- Classes to Structs: Convert classes in
Julietcore,Game, andRomeoto C structs. - Virtual Functions: Replace polymorphism with explicit
void*context pointers and function pointer tables (vtables). - Namespaces & Function Overloading: Prefix function names (e.g.,
Juliet_Log_Info(...)) as C does not support namespaces or function overloading. - Member Functions to Free Functions: Convert
obj.Method(args)toMethod(&obj, args). - No Reference Arguments (
&): Replace all pass-by-reference parameters with explicit pointers (*). - Casting Rules: Remove
static_castandreinterpret_castin favor of standard C explicit casts(Type)val.
D. Build System (Romeo.bff / fbuild.bff)
- Update compiler settings in FastBuild (
.bfffiles) and Visual Studio project settings from/std:c++20to C compiler flags (/std:c11or/TC).
4. Estimated Workload Matrix
| Task Module | Complexity | Estimated Relative Effort | Risks / Key Considerations |
|---|---|---|---|
| 1. Stdlib-Free Containers & Utils | Moderate | ~15% | Custom array/hash table implementation. |
| 2. OS Threading & Time Abstraction | Low-Moderate | ~10% | Clean Windows API replacements (QueryPerformanceCounter, SRWLOCK). |
| 3. C Struct & Syntax Refactoring | High | ~40% | Large volume of mechanical refactoring (references, namespaces, overloading, methods). |
| 4. Graphics / D3D12 C Refactoring | High | ~20% | d3dx12 heavily relies on C++ templates/stdlib; rewriting helper structs in C. |
| 5. ImGui / Romeo GUI Integration | Moderate | ~15% | Needs cimgui integration or a C-wrapper boundary. |
5. Recommendation & Phasing
- Step 1 (Stdlib Removal): Keep C++20 language features (classes, templates, member methods), but remove all stdlib inclusions (
<vector>,<thread>,<chrono>,<algorithm>). Replace them with customJulietengine primitives. - Step 2 (Evaluate C Migration): Re-evaluate if moving to pure C provides enough benefit vs. keeping a clean, stdlib-free C++ subset ("C with Classes" / stdlib-free C++).