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!

Andrew



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to