Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combining similar functions into one common function involving passing function pointers as parameters

Tags:

c++

I am trying to combine the following two functions into one portable function:

void NeedleUSsim::FindIdxRho()
{
    searchTmp = &ninfo->rho;
    double *p = std::find_if(tplRho_deg, tplRho_deg+sampleDim[2], &NeedleUSsim::GreaterThanOrEqualTo);
    while(p != tplRho_deg+sampleDim[2])
    {
        idxRho = p - tplRho_deg;
        p = std::find_if(p+1, tplRho_deg+sampleDim[2], &NeedleUSsim::GreaterThanOrEqualTo);
    }
}

void NeedleUSsim::FindIdxDepth()
{
    searchTmp = &ninfo->l;
    double *p = std::find_if(tplL, tplL+sampleDim[1], &NeedleUSsim::LessThanOrEqualTo);
    while(p != tplL+sampleDim[1])
    {
        idxL = p - tplL;
        p = std::find_if(p+1, tplL+sampleDim[1], &NeedleUSsim::LessThanOrEqualTo);
    }
}

Ideally, I want the parameters of the function to have tpl member to be passed as a pointer, with the size and rho/l passed as value. searchTmp is a file scope double precision pointer. Is there any easy way of passing &NeedleUSsim::GreaterThanOrEqualTo function as a parameter of the function that I'm trying to write easily?

Thanks in advance for the advice.

like image 949
stanigator Avatar asked May 27 '09 17:05

stanigator


3 Answers

The simplest way to make your code a bit more generic is the following :

template<typename ComparisonType>
double* NeedleUSsim::FindIdx(double* containerBegin, double* containerEnd, ComparisonType comparison) {
    double* p = std::find_if(containerBegin, containerEnd, comparison);
    double* idx = 0;
    while(p != containerEnd)
    {
        idx = p - containerBegin;
        p = std::find_if(p+1, containerEnd, comparison);
    }
    return idx;
}

void NeedleUSsim::FindIdxRho()
{
  searchTmp = &ninfo->rho;
  double* idx = FindIdx(tplRho_deg, tplRho_deg+sampleDim[2], &NeedleUSsim::GreaterThanOrEqualTo);
  if( idx != 0 )
  {
    idxL = idx;
  }

}

void NeedleUSsim::FindIdxDepth()
{
  searchTmp = &ninfo->l;
  double* idx = FindIdx(tplL, tplL+sampleDim[1], &NeedleUSsim::LessThanOrEqualTo);
  if( idx != 0 )
  {
    idxRho = idx;
  }
}
like image 91
Benoît Avatar answered Oct 07 '22 03:10

Benoît


Is there any easy way of passing &NeedleUSsim::GreaterThanOrEqualTo function as a parameter of the function that I'm trying to write easily?

There's a couple ways to do this.

  1. You can pass the function pointer around
  2. You can create and pass a function object using run-time polymorphism
  3. You can make your common function templated on a function object (compile time polymorphism)

The first method is covered above by eJames.

Option 2

The second method involves wrapping your comparison functions in some function-object hierarchy. A function object is an instance of a class with the () operator overloaded. This makes the instance callable:

 class IComparator
 {
 public:
    virtual bool operator()(lhs, rhs) = 0;
 }


 class CComparatorLessThan : public IComparator
 {
 public:
    virtual bool operator()(lhs, rhs) {...}
 }


 class CComparatorGreaterThan : public IComparator
 {
 public:
    virtual bool operator()(lhs, rhs) {...}
 }

Your common function would take an ICompatator reference and the behavior would be dynamically bound at runtime.

Option 3

The third method involves templatizing instead of creating an object hierarchy

template <class Comparator>
void foo(...)
{
   ...
   Comparator comparer;
   std::find_if(..., comparer);
}

then calling foo would involve:

foo<CComparatorGreaterThan>(...);

This eliminates a lot of the runtime overhead of the second solution. Here you don't have to define the base class. You only have to have some kind of class that has operator() overloaded and will return bool.

like image 25
Doug T. Avatar answered Oct 07 '22 04:10

Doug T.


It is possible to pass a member function pointer to a function as follows:

typedef bool (NeedleUSsim::*compFunctionPtr)(NeedleUSsim &x, NeedleUSsim &y);

void NeedleUSsim::FindIdxRho(compFunctionPtr comparison)
{
    //..
    p = std::find_if(tplRho_deg, tplRho_deg+sampleDim[2], comparison);
    //..
}

Which can then be called like so:

//..
someNeedleUSsim.FindIdxRho(&NeedleUSsim::LessThanOrEqualTo);
//..

Have a look at this C++ FAQ Lite article for more information.

like image 41
e.James Avatar answered Oct 07 '22 04:10

e.James