4 ms·
The number of errors there is frightening. I only noticed the use of void-pointer arithmetic and lack of alignment processing. I would say the code doesn't go
by efaref 11y ago
The number of errors there is frightening. I only noticed the use of void-pointer arithmetic and lack of alignment processing.
I would say the code doesn't go far enough. Instead of using `std::unique_ptr<char[]>`, it should define some kind of memory_block abstraction (using `unique_ptr` internally), where the copy constructor and `operator=` both do a byte-wise copy of the data. Then alongside `make_contiguous`, you would have an analogous `copy_contiguous`, where the base case of `make_contiguous_helper` would instead call `memory_block.resize(total_size)` and the base case of `copy_contiguous_helper` would call memory_block = other_memory_block. This would remove the boiler-plate code for the copy constructor in Mesh, which can instead be:
Mesh::Mesh(const Mesh& other)
: memory_block(copy_contiguous(other.memory_block,
positions, other.positions,
indices, other.indices,
vertices, other.vertices))
{
}
and similar for `operator=`.
- quicknir 11y agoI considered that. The problem is that memory_block would need an extra pointer to know its size, which seemed like pure waste at that point. And ultimately it doesn't relieve you of writing copy constructors etc which should be the real goal; it scales much better if you have a class with other members that are not a part of all this contiguous stuff. I hope to solve that in the next post. I think your approach is good as well, just giving you my reasoning for not putting it in.
- efaref 11y agoRather than two pointers I would probably have pointer + len. Storing the length of your arbitrary memory block is no bad thing, imo. In any case, having a sensible copy constructor on the memory_block (and any necessary associated data) would at least make the copy constructors in the client classes (assuming you have more than one user of this pattern) cleaner. It's a really good pattern, though. Excellent blog post.
- quicknir 11y agoThanks, I appreciate that. I welcome your feedback on the follow-up as well.