Software design: Wrapping valarray, or not.
Please excuse my ramblings here. For a hobby project, I'm working on a
simple ray-tracer. I've gone through a few iterations, and I wanted some
opinions on my next idea for an iteration...
My first iteration, I went all out and created a generic
template<typename component, unsigned dimensions> class vector;
I used template meta-programming to do all of the operations as
efficiently as possible, and enforce dimensions at runtime.
It was an interesting exercise, but I something I did wasn't quite
cross-platform. I scrapped the whole thing and created a simpler
vector3d class, which used explicit x, y, z members. While this isn't
as efficient, it was a lot easier to write, and I don't need that much
efficiency at this point.
Then I discovered valarray :-). I wish I had known about that earlier.
So, my next iteration will probably utilize valarray some how.
My question is a "elegance in design" one. My first thought is that I
should wrap the valarray in a template vector class. I might go the
route having the type *and* dimensionality of the vector be template
parameters. That way, there is compile-time safety for operations that
require same-length vectors. cross-product for instance needs exactly 3
dimensions. Of course, my application only needs 3 dimensions, so
perhaps making it generic at this point is a little premature. Then
again, it is an exercise, and I can go the route of premature
generalization if I choose :-)
One alternative is to use valarray directly, and not wrap it at all.
This seems to defy good OO practices though.
The other alternative is to keep the dimensionality a runtime parameter,
and use assert to verify the correct dimensionality.
So, what opinions are out there? I'm looking for more of a "why" than a
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|All times are GMT. The time now is 10:01 AM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.