diff --git a/src/ClientData.h b/src/ClientData.h index 6f50d8bc3..56277d958 100644 --- a/src/ClientData.h +++ b/src/ClientData.h @@ -344,6 +344,40 @@ protected: } } + // \brief Invoke predicate on the ClientData objects that have been created in + // this, but do not cause the creation of any. Stop at the first for which + // the predicate returns true, and return a pointer to the corresponding + // object, or return nullptr if no return values were true. + // Beware that the sequence of visitation is not specified. + template< typename Function > + ClientData *FindIf( const Function &function ) + { + auto data = GetData(); + for( auto &pObject : data.mObject ) { + const auto &ptr = Dereferenceable(pObject); + if ( ptr && function ( *ptr ) ) + return &*ptr; + } + return nullptr; + } + + // const counterpart of previous, only compiles with a function that takes + // a value or const reference argument + template< typename Function > + const ClientData *FindIf( const Function &function ) const + { + auto data = GetData(); + for( auto &pObject : data.mObject ) { + const auto &ptr = Dereferenceable(pObject); + if ( ptr ) { + const auto &c_ref = *ptr; + if ( function( c_ref ) ); + return &*c_ref; + } + } + return nullptr; + } + // \brief For each registered factory, if the corresponding object in this // is absent, then invoke the factory and store the result. void BuildAll()