Refactoring #1527
Feature #1290: === Core: framework ===
simplify containsMagneticMaterial, printSampleTree, genPyScript
Status: | Archived | Start date: | 22 Jul 2016 | |
---|---|---|---|---|
Priority: | Normal | Due date: | ||
Assignee: | wuttke | % Done: | 0% | |
Category: | - | |||
Target version: | Sprint32 |
Description
Try a lighter way to iterate through the children of a Sample. The current implementation of ISample::containsMagneticMaterial() involves all the following:
// ISample.cpp: bool ISample::containsMagneticMaterial() const { SampleMaterialVisitor material_vis; VisitSampleTreePreorder(*this, material_vis); return material_vis.containsMagneticMaterial(); } // ISampleVisitor.cpp: void VisitSampleTreePreorder(const ISample& sample, ISampleVisitor& visitor) { SampleTreeIterator<SampleIteratorPreorderStrategy> it(&sample); it.first(); while (!it.isDone()) { visitor.setLevel(it.getLevel()); const ISample *child = it.getCurrent(); child->accept(&visitor); it.next(); } } // SampleTreeIterator.h: class IteratorMemento { public: IteratorMemento() {} virtual ~IteratorMemento() {} void push_state(const IteratorState& state) { m_state_stack.push(state); } void pop_state() { m_state_stack.pop(); } IteratorState& get_state() { return m_state_stack.top(); } bool empty() const { return m_state_stack.empty(); } void reset() { while(!m_state_stack.empty()) m_state_stack.pop(); } const ISample* getCurrent() { return m_state_stack.top().getCurrent(); } void next() { m_state_stack.top().next(); } size_t size() const { return m_state_stack.size(); } protected: std::stack<IteratorState > m_state_stack; }; template <class Strategy> class BA_CORE_API_ SampleTreeIterator { ... } // ISampleIteratorStrategy.h: class BA_CORE_API_ SampleIteratorPreorderStrategy : public ISampleIteratorStrategy { public: SampleIteratorPreorderStrategy(); virtual SampleIteratorPreorderStrategy* clone() const; virtual ~SampleIteratorPreorderStrategy(); virtual IteratorMemento first(const ISample* p_root); virtual void next(IteratorMemento &iterator_stack) const; virtual bool isDone(IteratorMemento &iterator_stack) const; }; // ISampleIteratorStrategy.cpp: IteratorMemento SampleIteratorPreorderStrategy::first(const ISample *p_root) { IteratorMemento iterator_stack; iterator_stack.push_state( IteratorState(p_root) ); return iterator_stack; } void SampleIteratorPreorderStrategy::next(IteratorMemento &iterator_stack) const { const ISample *p_sample = iterator_stack.getCurrent(); if( !p_sample ) { throw Exceptions::NullPointerException("CompositeIteratorPreorderStrategy::next(): " "Error! Null object in the tree of objects"); } std::vector<const ISample *> children = p_sample->getChildren(); if (children.size()>0) { iterator_stack.push_state( IteratorState(children) ); return; } iterator_stack.next(); while ( !iterator_stack.empty() && iterator_stack.get_state().isEnd() ) { iterator_stack.pop_state(); if ( !iterator_stack.empty() ) iterator_stack.next(); } } bool SampleIteratorPreorderStrategy::isDone(IteratorMemento &iterator_stack) const { return iterator_stack.empty(); } // plus lots of boilerplate code in ISampleVisitor.h/cpp: void SampleMaterialVisitor::visit(const ParticleDistribution*) {} void SampleMaterialVisitor::visit(const ParticleComposition*) {} void SampleMaterialVisitor::visit(const MesoCrystal*) {} void SampleMaterialVisitor::visit(const ParticleCoreShell*) {} void SampleMaterialVisitor::visit(const IFormFactor*) {} void SampleMaterialVisitor::visit(const FormFactorDWBAPol*) {} void SampleMaterialVisitor::visit(const FormFactorWeighted*) {} .....
History
#1 Updated by wuttke over 4 years ago
For containsMagneticMaterial, reimplementation went smoothly in no more than 20 lines, cff2abd.
#2 Updated by wuttke over 4 years ago
- Status changed from Sprint to Resolved
Resolved in 9e708f4af; ISampleVisitor no longer needed in Core.
#3 Updated by herck about 4 years ago
- Status changed from Resolved to Archived