class A
{
public:
std::string &GetName() const;
const int GetThreshold() const;
...
};
typedef vector<A *> APtrContainer;
class B
{
public:
void Update();
private:
Control *GetControlByName(std::string);
Graphic &GetGraphic() const;
...
APtrContainer m_APCollection;
};
Still, my desire to use std::for_each was strong, pah! to for-loops I thought, there must be a way!
I thought back to how the old, ugly code would have looked, something like this I guessed...
void B::Update()
{
Graphic &graphic = GetGraphic();
for(APtrContainer::iterator it = m_APCollection.begin(); it!=
m_APCollection.end(); ++it)
{
std::string &name = it->GetName();
Control *control = GetControlByName(name);
// can return NULL
if(control)
{
bool value =
control->GetCurrentValue() > it->GetThreshold();
graphic.doSomething(value);
}
}
}
So, now to refactor using std::for_each, I thought. I know I need to pass it a function object so let’s write that first. Perhaps I was being rash but fools rush in where angels fear to tread, so in I rushed …
Witness the resulting mess in part II coming soon...
No comments:
Post a Comment