Richard Heck wrote:
Abdelrazak Younes wrote:
rgheck wrote:

Attached is my attempt at using type safety to make Layout objects safer to use. I tried several things, and this may well not be best. But other efforts---going back to a vector<Layout *>, e.g.---all had their own problems, usually, that they make iterators behave badly. (In that case, e.g., you have to doubly dereference to use the iterator. E.g.: (*it)->name().) This one simply makes LayoutPtr a thin wrapper around a Layout * and then (a) requires that LayoutPtr objects be passed to Paragraph::setLayout() and (b) makes the LayoutPtr constructor private, so that only TextClass can construct these objects.

By far most of the patch is e.g.: s/layout./layout->/. All the action is in LayoutPtr.h and TextClass.{h,cpp}.

Comments most welcome.

Quite frankly, I don't see the benefit. Type safety is nice to have if possible but it is not mandatory. AFAICS there isn't hundreds of problematic cases so, I'd rather see the problematic cases properly documented in source rather than going this way. Just my humble opinion, I am fine with whatever solution you choose as long as you are comfortable with it ;-)

This got started because there was a crash that involved passing a temporary to Paragraph::setLayout(), and I added a comment to the effect that one ought not to do that. Then Andre suggested there must be a better way, in particular, that maybe we could make Layout's constructor private, etc. Unfortunately, you can't do that, because we have a std::vector of Layout's.

What's wrong with a vector<Layout *> and a private Layout ctor? You just need to declare TextClass as friend in the layout class, that's all. I probably missed something...

Abdel.

Reply via email to