On 12/23/2009 11:23 AM, Andrew Stitcher wrote:
On Wed, 2009-12-23 at 09:14 -0500, Alan Conway wrote:
...
How about a general-purpose modify function like this:
// t takes a const vector<T>& , m takes a non-const vector<T>&
template<class Test, class Modifier>
bool modify_if(Test t, Modifier m) {
Mutex::ScopedLock l(lock);
if (array&& t(*array)) {
ArrayPtr copy(new std::vector<T>(*array));
m(*copy);
array = copy;
return true;
}
return false;
}
That will let you do anything you like to the array - you could rewrite all the
modifiers in terms of this one (although they'd be a little less efficient
because the current ones use the results of the test part in the modify part.)
Just a cautionary note: I wouldn't necessarily think that you always
know better than the optimiser (unless you customarily look at generated
assembler! life is too short). I'm saying that if t and m can actually
be expanded inline then the code wouldn't necessarily be worse. Except
to understand of course, but then my brain always starts to glaze over
when there is more than one template parameter and I didn't write the
code!
Agreed in general, in this particular case though the existing methods look like
(T part) iterator i = find(...); ...
(M part) do_something_with(i)
But if you want to re-implement this using a generic modify_if you have no way
in the M part to refer to the iterator computed in the T part, so you'd have to
do the search again.
---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project: http://qpid.apache.org
Use/Interact: mailto:[email protected]