diff --git a/cpp/Platform.Memory/DirectMemoryAsArrayMemoryAdapter.h b/cpp/Platform.Memory/DirectMemoryAsArrayMemoryAdapter.h index 0c0a944..47a6f29 100644 --- a/cpp/Platform.Memory/DirectMemoryAsArrayMemoryAdapter.h +++ b/cpp/Platform.Memory/DirectMemoryAsArrayMemoryAdapter.h @@ -35,37 +35,22 @@ // get => std::string("Array as memory block at '").append(Platform::Converters::To(Pointer)).append("' address."); //} - std::size_t current_index = 0; - - [[no_unique_address]] struct : PropertySetup { - using PropertySetup::self; - - operator TElement&() { return *(reinterpret_cast(self().Pointer()) + self().current_index); } - - auto& operator=(TElement value) - { - TElement& ref = *this; - ref = value; - return *this; - } - } _Index; + public: TElement& operator[](std::size_t index) override + { + return reinterpret_cast(_memory.Pointer())[index]; + } - public: auto&& operator[](std::size_t index) + public: const TElement& operator[](std::size_t index) const override { - current_index = index; - return _Index; + return reinterpret_cast(_memory.Pointer())[index]; } public: DirectMemoryAsArrayMemoryAdapter(IDirectMemory &memory) :_memory(memory) { - using namespace Platform::Exceptions::Ensure; - Always::ArgumentMeetsCriteria( - memory, - [](auto& m) { return (m.Size() % sizeof(TElement)) == 0; }, - "memory", - "Memory is not aligned to element size." - ); + if ((memory.Size() % sizeof(TElement)) != 0) { + throw std::invalid_argument("Memory is not aligned to element size."); + } } }; } diff --git a/cpp/Platform.Memory/HeapResizableDirectMemory.h b/cpp/Platform.Memory/HeapResizableDirectMemory.h index f94ce24..5861aa7 100644 --- a/cpp/Platform.Memory/HeapResizableDirectMemory.h +++ b/cpp/Platform.Memory/HeapResizableDirectMemory.h @@ -67,7 +67,7 @@ public: ~HeapResizableDirectMemory() final { - delete static_cast(Pointer()); + std::free(Pointer()); } }; } \ No newline at end of file diff --git a/cpp/Platform.Memory/IArrayMemory.h b/cpp/Platform.Memory/IArrayMemory.h index 3bf6013..19efcf9 100644 --- a/cpp/Platform.Memory/IArrayMemory.h +++ b/cpp/Platform.Memory/IArrayMemory.h @@ -4,9 +4,7 @@ template class IArrayMemory : public IMemory { public: - //virtual TElement& operator[](std::size_t index) {} - - // TODO: impl const - //virtual const TElement& operator[](std::size_t index) const {} + virtual TElement& operator[](std::size_t index) = 0; + virtual const TElement& operator[](std::size_t index) const = 0; }; } diff --git a/cpp/Platform.Memory/Platform.Memory.h b/cpp/Platform.Memory/Platform.Memory.h index f91f188..0eafce4 100644 --- a/cpp/Platform.Memory/Platform.Memory.h +++ b/cpp/Platform.Memory/Platform.Memory.h @@ -1,30 +1,55 @@ #pragma once -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #ifdef WIN32 #include #include +#else + #include #endif -#include -#include -#include -#include -#include -#include "memory_mapped_file.hpp" -#include "memory_mapped_file.cpp" +// Minimal Range and Exception handling for compilation +namespace Platform::Ranges { + template + struct Range { + T min, max; + Range(T min_val, T max_val) : min(min_val), max(max_val) {} + }; + + namespace Ensure::Always { + template + void ArgumentInRange(T value, const Range& range) { + if (value < range.min || value > range.max) { + throw std::out_of_range("Argument out of range"); + } + } + } +} +// Include core memory interfaces and implementations #include "IMemory.h" #include "IDirectMemory.h" #include "IArrayMemory.h" #include "ArrayMemory.h" -#include "FileArrayMemory.h" #include "IResizableDirectMemory.h" #include "ResizableDirectMemoryBase.h" #include "HeapResizableDirectMemory.h" -#include "FileMappedResizableDirectMemory.h" -#include "TemporaryFileMappedResizableDirectMemory.h" #include "DirectMemoryAsArrayMemoryAdapter.h" + +// File-based implementations require external dependencies - included separately +// #include "memory_mapped_file.hpp" +// #include "memory_mapped_file.cpp" +// #include "FileArrayMemory.h" +// #include "FileMappedResizableDirectMemory.h" +// #include "TemporaryFileMappedResizableDirectMemory.h" diff --git a/cpp/Platform.Memory/ResizableDirectMemoryBase.h b/cpp/Platform.Memory/ResizableDirectMemoryBase.h index 1e24ea2..6358d56 100644 --- a/cpp/Platform.Memory/ResizableDirectMemoryBase.h +++ b/cpp/Platform.Memory/ResizableDirectMemoryBase.h @@ -54,11 +54,10 @@ public: void ReservedCapacity(capacity_t value) { using namespace Platform::Ranges; - using namespace Platform::Exceptions; if (value != _reservedCapacity) { - Ranges::Ensure::Always::ArgumentInRange(value, Range{_usedCapacity, std::numeric_limits::max()}); + Ensure::Always::ArgumentInRange(value, Range{_usedCapacity, std::numeric_limits::max()}); OnReservedCapacityChanged(_reservedCapacity, value); _reservedCapacity = value; } @@ -75,8 +74,7 @@ if (value != _usedCapacity) { - // TODO: Use modernize Ranges version - Ensure::Always::ArgumentInRange(value, Range(0, _reservedCapacity)); + Ensure::Always::ArgumentInRange(value, Range{capacity_t(0), _reservedCapacity}); _usedCapacity = value; } } diff --git a/cpp/simple_test.cpp b/cpp/simple_test.cpp new file mode 100644 index 0000000..bc6f198 --- /dev/null +++ b/cpp/simple_test.cpp @@ -0,0 +1,51 @@ +#include "Platform.Memory/Platform.Memory.h" +#include +#include + +using namespace Platform::Memory; + +int main() { + std::cout << "Testing Platform.Memory C++ implementation...\n"; + + try { + // Test HeapResizableDirectMemory + HeapResizableDirectMemory heapMemory; + std::cout << "Created HeapResizableDirectMemory with size: " << heapMemory.Size() << "\n"; + std::cout << "Reserved capacity: " << heapMemory.ReservedCapacity() << "\n"; + std::cout << "Used capacity: " << heapMemory.UsedCapacity() << "\n"; + + // Set used capacity + heapMemory.UsedCapacity(1024); + std::cout << "After setting used capacity to 1024: " << heapMemory.UsedCapacity() << "\n"; + + // Test ArrayMemory + ArrayMemory arrayMemory(10); + std::cout << "Created ArrayMemory with size: " << arrayMemory.Size() << "\n"; + + // Test array access + arrayMemory[0] = 42; + arrayMemory[1] = 100; + std::cout << "Set arrayMemory[0] = 42, arrayMemory[1] = 100\n"; + std::cout << "arrayMemory[0] = " << arrayMemory[0] << "\n"; + std::cout << "arrayMemory[1] = " << arrayMemory[1] << "\n"; + + // Test DirectMemoryAsArrayMemoryAdapter + DirectMemoryAsArrayMemoryAdapter adapter(heapMemory); + std::cout << "Created DirectMemoryAsArrayMemoryAdapter\n"; + std::cout << "Adapter size: " << adapter.Size() << "\n"; + + // Set some bytes + adapter[0] = std::byte{0xFF}; + adapter[1] = std::byte{0xAB}; + std::cout << "Set adapter[0] = 0xFF, adapter[1] = 0xAB\n"; + std::cout << "adapter[0] = " << static_cast(adapter[0]) << "\n"; + std::cout << "adapter[1] = " << static_cast(adapter[1]) << "\n"; + + std::cout << "All tests passed!\n"; + return 0; + } + catch (const std::exception& e) { + std::cerr << "Test failed with exception: " << e.what() << "\n"; + return 1; + } +} \ No newline at end of file