Counting Observers of a Reference Target
 
 
 

The following code demonstrates how to count the number of reference makers that are directly observing a reference target.

unsigned int GetImmediateDependencyCount(ReferenceTarget* rtarg)
{
  unsigned int count = 0;
  DependentIterator di(rtarg);
  while (di.Next() != NULL) 
  {
    count++;
  }
  return count;
}
               

To count all references that are dependent on an object, directly or indirectly you would use ReferenceTarget::DoEnumDependents(). This would be done as follows:

unsigned int GetAllDependencyCount(ReferenceTarget* rtarg)
{  
  struct CallBack : DependentEnumProc {
    int count;
    CallBack() : count(0) { }
    virtual int proc(ReferenceMaker* rmaker) { 
      ++count;
    }
  };
 
  CallBack cb;
  rtarg->DoEnumDependents(&cb);
  return cb.count();  
}