Dawoodoz / DFPSR

Fast realtime softare rendering library for C++14 using SSE/AVX/NEON. 2D, 3D and isometric rendering with minimal system dependencies.
https://dawoodoz.com/dfpsr.html
78 stars 6 forks source link

How to handle loading of empty files #54

Closed Dawoodoz closed 1 year ago

Dawoodoz commented 1 year ago

Buffers currently don't allow empty allocations in order to mimic closely how a raw memory allocation works. This can however seem strange on the higher level, if loading an empty file as text raises an exception. One can instead return a null handle when creating a buffer of length zero, and then treat null buffers just like buffers of length zero where it makes sense. This would however prevent someone from porting algorithms back and forth between a C allocation and the Buffer object, and one would not be able to distinguish between not having anything and having something empty.

Another option would be to allocate the Buffer head but let it contain a null data pointer when the requested size is zero. This can break algorithms that rely on catching length zero as an exception with try-catch, but also reduce the number of special cases one needs to handle, by working almost exactly like an unused padded allocation.

Returning a headless null handle would avoid causing a confusing distinction between empty and non-existing buffers, because currently one can see the buffer handle as the buffer itself when not shared. On the other hand, an empty Buffer could be treated just like a padded allocation where no data is used, by pretending that the head has an allocation.

Dawoodoz commented 1 year ago

Decided to go with the empty head solution, because then you can just wrap an if statement checking the size around buffer constructors if you want a null handle instead of an empty head.