1

I have a function which takes an object of a custom class. A member of this object should store an identifier of that function (or a 'reference' to that function) so that the function can determine whether it was called before with this object.

What is a suitable identfier for this purpose?
I am not necessarily talking about the function name, because overloaded functions share the same name, so this doesn't work.
Is the function address (as used für delegates) a proper way? I am pretty sure that this will work, but not 100% sure. Or may the function be moved around by the garbage collector like regular objects?
Is there maybe another, better way to get a 'function ID'?

Edit
Here's an example to demonstrate my requirement (pseudo-code used):

void WorkerFunction (CFuncStep i_oFS)  
{
  if (i_oFS.FunctionID == WorkerFunction.FunctionID)
  {
    // continue work
  }
  else
  {
    // start work
    i_oFS.FunctionID = WorkerFunction.FunctionID;
  }

  if (finished_work)
    i_oFS.FunctionID = null;
}

The purpose is saving the operation state and later continuing an operation in a called function. This makes sense in cases e.g. where the function does network communication and has to wait for the reply. The func returns immediately so that the thread can do other work. Later it comes back to fetch the reply.
I could use a separate thread, but I want to avoid the overhead of thread sync here, because it would not just be 1 add. thread, but quite some.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
  • Without some more context it's hard to say the best way to do this, but in most situations I would suggest not to allow a member of a class to have other knowledge of functions in this way. For what purpose do functions need to know whether they have been called on this object before? – Matt Dalzell Apr 05 '16 at 20:57
  • Looking for an oracle is rather a bad idea. Trivially use an enum instead. – Hans Passant Apr 05 '16 at 22:45
  • The MethodHandle should meet your requirements – Ben Voigt Apr 06 '16 at 00:46
  • @BenVoigt: can I get the handle of the method from inside the method itself? – Tobias Knauss Apr 06 '16 at 06:30

1 Answers1

0

@BenVoigt: Thanks for the hint at MethodHandle!
This is absolutely what I needed. I found a usage example at the SO post 'Can you use reflection to find the name of the currently executing method?'

Here's my solution:

using namespace System::Reflection;

ref class CFuncStep
{
public:
  int               m_iStep;
  MethodBase^       m_oMethod;
  String^           m_sName;
};

void workerfunction (CFuncStep^ i_oFS)
{
  MethodBase^ oMethod = MethodBase::GetCurrentMethod ();

  if (oMethod != i_oFS->m_oMethod)
  {
    i_oFS->m_iStep = 1;
    i_oFS->m_oMethod = MethodBase::GetCurrentMethod ();
    i_oFS->m_sName   = i_oFS->m_oMethod->Name;
  }

  switch (i_oFS->m_iStep)
  {
  case 1:
  case 2:
  case 3:
    i_oFS->m_iStep++;
    break;

  case 4:
    i_oFS->m_iStep = 0;
    i_oFS->m_oMethod = nullptr;
    i_oFS->m_sName   = nullptr;
  }
};

int main()
{
  CFuncStep^ oFS = gcnew CFuncStep;
  do
  {
    workerfunction (oFS);
  }
  while (oFS->m_iStep > 0);

  return 0;
}
Community
  • 1
  • 1
Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45