Files
Juliet/AgentData/StdlibAndCMigrationAnalysis.md

6.2 KiB

Workload Analysis: Removing C++ Stdlib & Migrating to Pure C

This analysis outlines the required workload, technical impact, and migration strategy for:

  1. Removing all standard C++ library dependencies (std::*) from the Juliet codebase.
  2. 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:
    1. Containers: Replace container implementations (such as Juliet/include/Core/Container/Vector.h, which currently includes <vector>) with custom dynamically resizing arrays / buffer allocators backed by MemoryArena.
    2. Type Traits & Concepts: Replace <type_traits> and <concepts> usage in files like Game/Entity/Entity.h with compiler intrinsics (e.g. __is_enum, __is_base_of) or internal lightweight trait structures.
    3. Algorithms & Math Utilities: Implement custom Min, Max, Clamp, Sort, and Swap functions in CoreUtils.h or custom inline header helpers.

B. Threading & Synchronization

  • Current Usages: <thread>, <mutex>, std::thread, std::mutex, std::lock_guard.
  • Workload:
    1. Replace <thread> in Thread.h and Romeo/src/main.cpp with platform abstraction wrappers using Win32 CreateThread / CloseHandle.
    2. Replace <mutex> in Mutex.h with SRWLOCK or CRITICAL_SECTION.

C. Time & File I/O

  • Current Usages: <chrono>, <cstdio>, <fstream>, <ios>, <iosfwd>, <cstdlib>.
  • Workload:
    1. Replace <chrono> in LogManager.cpp and SystemEvent.cpp with QueryPerformanceCounter / QueryPerformanceFrequency on Windows.
    2. Standardize filesystem I/O on Win32 API (CreateFileW, ReadFile, WriteFile) or minimal C runtime headers (<stdio.h>, <stdlib.h>, <string.h>).

D. Memory Allocators & RTTI

  • Current Usages: <new>, <typeinfo> (typeid in MemoryArena.h), <memory>.
  • Workload:
    1. Replace placement new with custom inline placement functions or custom explicit initialization methods.
    2. Replace typeid / type_info usages with custom type hashing or enum type IDs.

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.h and 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.h to cimgui (C-compatible wrappers for ImGui) or maintain a minimal C++ bridge file specifically for ImGui.

C. Codebase Refactoring (Language Paradigm)

  1. Classes to Structs: Convert classes in Juliet core, Game, and Romeo to C structs.
  2. Virtual Functions: Replace polymorphism with explicit void* context pointers and function pointer tables (vtables).
  3. Namespaces & Function Overloading: Prefix function names (e.g., Juliet_Log_Info(...)) as C does not support namespaces or function overloading.
  4. Member Functions to Free Functions: Convert obj.Method(args) to Method(&obj, args).
  5. No Reference Arguments (&): Replace all pass-by-reference parameters with explicit pointers (*).
  6. Casting Rules: Remove static_cast and reinterpret_cast in favor of standard C explicit casts (Type)val.

D. Build System (Romeo.bff / fbuild.bff)

  • Update compiler settings in FastBuild (.bff files) and Visual Studio project settings from /std:c++20 to C compiler flags (/std:c11 or /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

  1. 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 custom Juliet engine primitives.
  2. 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++).