
On Friday 16 July 2004 05:28, MenTaLguY wrote:
On Thu, 2004-07-15 at 15:44, Aubanel MONNIER wrote:
OK, so I stick with manual delete for the momment. It's just that deleting vector elements ain't fun.
Hmm, a std::vector<SPObject *> or similar?
A std::vector<BBoxSort *> in fact, then using v.sort; FOr the momment, I used a std::vector<BBoxSort > instead, the copy contructor not being too heavy. But I would prefer using pointers, for sure ...
In fact, I'm not used to smart pointers, but to answering one of my questions, I was suggested to do it that way.
It could be one of the less evil uses of auto pointers, though there are still probably better approaches. I would need to know more about the code in question before I could know what to suggest, however.
Here is the function I currently have
virtual void on_button_click() { //Retreive selected objects SPDesktop *desktop = SP_ACTIVE_DESKTOP; if (!desktop) return;
SPSelection *selection = SP_DT_SELECTION(desktop); if (!selection) return;
std::list<SPItem *> selected; selection->list(selected); if (selected.empty()) return;
//Check 2 or more selected objects std::list<SPItem *>::iterator second(++(selected.begin())); if (second == selected.end()) return;
std::vector< BBoxSort > sorted; for (std::list<SPItem *>::iterator it(selected.begin()); it != selected.end(); ++it) { BBoxSort b (*it, _orientation, _kBegin, _kEnd); sorted.push_back(b); } //sort bbox by anchors std::sort(sorted.begin(), sorted.end()); /*[Snipped code using the sorted vector] */
}
with bbox being the following class :
struct BBoxSort { SPItem *item; float anchor; NR::Rect bbox; BBoxSort(SPItem *pItem, NR::Dim2 orientation, double kBegin, double kEnd) : item(pItem), bbox (sp_item_bbox_desktop (pItem)) { anchor = kBegin * bbox.min()[orientation] + kEnd * bbox.max() [orientation]; } BBoxSort(const BBoxSort &rhs): //NOTE : this copy ctor is called O(sort) when sorting the vector //this is bad. The vector should be a vector of pointers. //But I'll wait the bohem GC before doing that item(rhs.item), anchor(rhs.anchor), bbox(rhs.bbox) { } }; bool operator< (const BBoxSort &a, const BBoxSort &b) { return (a.anchor < b.anchor); }
Will the gc be included for 0.4 or is this planned for later ?
Early 0.4.
Do you need some help to do that ?
Not sure yet. I'm still trying to decide how agressively existing code should be converted to use the garbage collector (which will dictate how much new code can use it).
-mental
OK, I keep my code as is for the time being. But this kind of consideration will be quite frequent I'm afraid. Of course,I cans still iterate on the vector of pointers and call delete on each of them....
- obi