Manta Interactive Ray Tracer Development Mailing List

Text archives Help


Re: [MANTA] Callback question.


Chronological Thread 
  • From: Steven Parker <sparker@cs.utah.edu>
  • To: Abe Stephens <abe@sci.utah.edu>
  • Cc: "'manta@sci.utah.edu'" <manta@sci.utah.edu>
  • Subject: Re: [MANTA] Callback question.
  • Date: Tue, 21 Jun 2005 07:58:50 -0600


There are two ways to address this. With both of them you must change your addCuttingPlane method to be const, since your create prototype is const.
1. Add explicit template arguments to create:
create<FMantaWindow, Point&, Vector&>(&f, &FMantaWindow::addCuttingPlane, p, n);
This is ugly, but it will work with the create as-is. As a side, this mechanism is also a great way to debug these problems because it will cause the compiler to actually give you an error message. That is how I found the const problem.

2. Overload create with a version that takes references. The callback doesn't need to change.

template<class T, typename Arg1, typename Arg2> static
CallbackBase_0Data*
create(T* ptr, void (T::*pmf)(Arg1&, Arg2&) const, Arg1& arg1, Arg2& arg2) {
return new Callback_0Data_2Arg_const<T, Arg1&, Arg2&>(ptr, pmf, arg1, arg2);
}


Full example code is below.

Steve



#include <iostream>
using namespace std;

class CallbackBase_0Data {
};

   template<class T, typename Arg1, typename Arg2>
   class Callback_0Data_2Arg_const : public CallbackBase_0Data {
   public:
Callback_0Data_2Arg_const(T* ptr, void (T::*pmf)(Arg1, Arg2) const, Arg1 arg1, Arg2 arg2)
       : ptr(ptr), pmf(pmf), arg1(arg1), arg2(arg2)
   {
   }
       virtual ~Callback_0Data_2Arg_const()
   {
   }
       virtual void call()
   {
           (ptr->*pmf)(arg1, arg2);
   }
   private:
       T* ptr;
       void (T::*pmf)(Arg1, Arg2) const;
       Arg1 arg1;
       Arg2 arg2;
   };

template<class T, typename Arg1, typename Arg2> static
CallbackBase_0Data*
create(T* ptr, void (T::*pmf)(Arg1, Arg2) const, Arg1 arg1, Arg2 arg2) {
  cerr << "value version\n";
return new Callback_0Data_2Arg_const<T, Arg1, Arg2>(ptr, pmf, arg1, arg2);
}

template<class T, typename Arg1, typename Arg2> static
CallbackBase_0Data*
create(T* ptr, void (T::*pmf)(Arg1&, Arg2&) const, Arg1& arg1, Arg2& arg2) {
  cerr << "ref version\n";
return new Callback_0Data_2Arg_const<T, Arg1&, Arg2&>(ptr, pmf, arg1, arg2);
}

class Point {
};
class Vector {
};

class FMantaWindow {
public:
  void addCuttingPlane( Point& point, Vector& normal ) const {
  }
};

int main()
{
  FMantaWindow f;
  Point p;
  Vector n;
create<FMantaWindow, Point&, Vector&>(&f, &FMantaWindow::addCuttingPlane, p, n);
  create(&f, &FMantaWindow::addCuttingPlane, p, n);
}


On Jun 21, 2005, at 1:26 AM, Abe Stephens wrote:

Hey

I've got my new const callback class for transactions:

   template<class T, typename Arg1, typename Arg2>
   class Callback_0Data_2Arg_const : public CallbackBase_0Data {
   public:
Callback_0Data_2Arg_const(T* ptr, void (T::*pmf)(Arg1, Arg2) const, Arg1 arg1, Arg2 arg2)
       : ptr(ptr), pmf(pmf), arg1(arg1), arg2(arg2)
   {
   }
       virtual ~Callback_0Data_2Arg_const()
   {
   }
       virtual void call()
   {
           (ptr->*pmf)(arg1, arg2);
   }
   private:
       T* ptr;
       void (T::*pmf)(Arg1, Arg2) const;
       Arg1 arg1;
       Arg2 arg2;
   };

And my create function:

   template<class T, typename Arg1, typename Arg2> static
   CallbackBase_0Data*
create(T* ptr, void (T::*pmf)(Arg1, Arg2) const, Arg1 arg1, Arg2 arg2) {
return new Callback_0Data_2Arg_const<T, Arg1, Arg2>(ptr, pmf, arg1, arg2);
   }

Now I want to create a callback for the method:

FMantaWindow::addCuttingPlane( Point &point, Vector &normal );


Problem is that Arg1 and Arg2 in create don't take references. So the create function doesn't match. (At least I think that's why, but even though this part of manta's code is template nirvana, it makes my head spin. ;-) ) Anyone see a reason not to make Arg1 and Arg2 of create's formal arguments references? (I'm about 65% sure I know what change would do, but maybe someone else can parse this and tell me if it sounds good....)

Abe






Archive powered by MHonArc 2.6.16.

Top of page