Why is the value of "Instance" whacked in the following code. Note this
only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be
BredthFirst and BredthFirstPrint and make both approriate func ptrs it works
fine.
void print(void *data);
#define R 'r'-'r'
#define S 's'-'r'
#define T 't'-'r'
#define U 'u'-'r'
#define V 'v'-'r'
#define W 'w'-'r'
#define X 'x'-'r'
#define Y 'y'-'r'
int main()
{
int i;
Graph *graph;
char *s="rstuvwxy";
graph=graph->init();
for(i=0;i<8;i++)
graph->addNode(&s[i]);
graph->addEdge(&s[R],&s[S]);
graph->addEdge(&s[R],&s[V]);
graph->addEdge(&s[S],&s[W]);
graph->addEdge(&s[W],&s[S]);
graph->addEdge(&s[W],&s[T]);
graph->addEdge(&s[T],&s[X]);
graph->addEdge(&s[T],&s[u]);
graph->addEdge(&s[u],&s[Y]);
graph->addEdge(&s[X],&s[Y]);
graph->walk();
graph->print(print,&s[R],&s[Y]);
}
void print(void *data)
{
cout<<*RI_CAST<char *>(data)<<endl;
}
class Graph {
public:
Graph *init(unsigned long walkfunc=0,unsigned long printfunc=0);
void addNode(void *data);
void addEdge(void *d1, void *d2);
GNode *findNode(void *data);
void walk();
void print(void (*func)(void *data),void *d1,void *d2);
static void BredthFirstWrapper();
static void
BredthFirstPrintWrapper(void (*func)(void *data),GNode
*node,GNode *node2);
void Dump();
//protected:
Graph(unsigned long walkfunc=0,unsigned long printfunc=0);
private:
void BredthFirst();
void BredthFirstPrint(void (*func)(void *data),GNode *node,GNode
*node2);
void reset();
static Graph *Instance;
LinkedList *Nodes;
unsigned long WalkFunc;
unsigned long PrintFunc;
};
Graph *Graph::Instance=0;
Graph *Graph::init(unsigned long walkfunc,unsigned long printfunc)
{
return new Graph(walkfunc,printfunc);
}
void Graph::addNode(void *data)
{
GNode *node;
node=node->init(data);
Nodes->addNode(node);
}
void Graph::addEdge(void *d1,void *d2)
{
GNode *node;
GNode *node2;
node=findNode(d1);
node2=findNode(d2);
node->addEdge(node2);
node2->addEdge(node);
}
GNode *Graph::findNode(void *data)
{
GNode *node;
Nodes->reset(); // paranoia
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0)
if(node->getData()==data) {
Nodes->reset();
return node;
}
Nodes->reset();
return 0;
}
void Graph::walk()
{
Instance=this;
(RI_CAST<void (*)(Graph *)>(WalkFunc))(this);
}
void Graph::print(void (*func)(void *data),void *d1,void *d2)
{
GNode *node;
GNode *node2;
node=findNode(d1);
node2=findNode(d2);
if(node==0||node2==0) {
cout<<"Can't find one of the path ends for this graph print"<<endl;
exit(1);
}
Instance=this;
(RI_CAST<void (*)(Graph *,void (*)(void *),GNode *, GNode *)>(PrintFunc))
(this,func,node,node2);
}
void Graph::BredthFirstWrapper()
{
if(Instance==0)
return;
Instance->BredthFirst();
}
void Graph::BredthFirstPrintWrapper(void (*func)(void *data),GNode
*node,GNode *node2)
{
if(Instance==0)
return;
cout<<node<<" "<<node2<<endl;
Instance->BredthFirstPrint(func,node,node2);
}
void Graph::Dump()
{
GNode *node;
cout<<"Nodes: "<<endl;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0)
node->Dump();
Nodes->reset();
}
Graph::Graph(unsigned long walkfunc,unsigned long printfunc)
{
Nodes=Nodes->init();
if(walkfunc==0)
walkfunc=RI_CAST<unsigned long>(&Graph::BredthFirstWrapper);
WalkFunc=(walkfunc);
if(printfunc==0)
printfunc=RI_CAST<unsigned long>(&Graph::BredthFirstPrintWrapper);
PrintFunc=printfunc;
}
void Graph::BredthFirst()
{
GNode *node;
GNode *tmp;
Queue *queue;
LinkedList *edges;
reset();
node=RI_CAST<GNode *>(Nodes->getNext());
if(node==0)
return;
node->setColor(GNodeColorGrey);
queue=queue->init();
queue->insert(node);
node->setDepth(0);
node->setPrev(0);
do {
node=RI_CAST<GNode *>(queue->peek());
cout<<"processing node "<<node<<endl;
edges=node->getEdges();
while((tmp=RI_CAST<GNode *>(edges->getNext()))!=0)
if(tmp->getColor()==GNodeColorWhite) {
tmp->setColor(GNodeColorGrey);
tmp->setDepth(node->getDepth()+1);
tmp->setPrev(node);
queue->insert(tmp);
}
edges->reset();
node=RI_CAST<GNode *>(queue->get());
if(node==0)
break;
node->setColor(GNodeColorBlack);
} while(queue->getSize()>0);
}
void Graph::BredthFirstPrint(void (*func)(void *data),GNode *node, GNode
*node2)
{
if(node==node2) {
func(node->getData());
return;
}
if(node2->getPrev()==0) {
cout<<"No path from "<<node<<" to "<<node2<<endl;
exit(1);
}
BredthFirstPrint(func,node,node2->getPrev());
func(node2->getData());
}
void Graph::reset()
{
GNode *node;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0)
node->reset();
Nodes->reset();
} 3 1522
You might want to shorten and simplify your code, to pinpoint what fails.
This way you will either
1. find the bug
2. be able to post a message which more people might go over
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message
news:Wd***************@fe10.lga... Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and BredthFirstPrint and make both approriate func ptrs it
works fine. void print(void *data);
#define R 'r'-'r' #define S 's'-'r' #define T 't'-'r' #define U 'u'-'r' #define V 'v'-'r' #define W 'w'-'r' #define X 'x'-'r' #define Y 'y'-'r'
int main() { int i; Graph *graph; char *s="rstuvwxy";
graph=graph->init();
for(i=0;i<8;i++) graph->addNode(&s[i]);
graph->addEdge(&s[R],&s[S]); graph->addEdge(&s[R],&s[V]); graph->addEdge(&s[S],&s[W]); graph->addEdge(&s[W],&s[S]); graph->addEdge(&s[W],&s[T]); graph->addEdge(&s[T],&s[X]); graph->addEdge(&s[T],&s[u]); graph->addEdge(&s[u],&s[Y]); graph->addEdge(&s[X],&s[Y]);
graph->walk(); graph->print(print,&s[R],&s[Y]); }
void print(void *data) { cout<<*RI_CAST<char *>(data)<<endl; } class Graph { public: Graph *init(unsigned long walkfunc=0,unsigned long printfunc=0);
void addNode(void *data); void addEdge(void *d1, void *d2);
GNode *findNode(void *data); void walk(); void print(void (*func)(void *data),void *d1,void *d2);
static void BredthFirstWrapper(); static void BredthFirstPrintWrapper(void (*func)(void *data),GNode *node,GNode *node2);
void Dump();
//protected: Graph(unsigned long walkfunc=0,unsigned long printfunc=0);
private: void BredthFirst(); void BredthFirstPrint(void (*func)(void *data),GNode *node,GNode *node2); void reset();
static Graph *Instance;
LinkedList *Nodes; unsigned long WalkFunc; unsigned long PrintFunc; };
Graph *Graph::Instance=0;
Graph *Graph::init(unsigned long walkfunc,unsigned long printfunc) { return new Graph(walkfunc,printfunc); }
void Graph::addNode(void *data) { GNode *node;
node=node->init(data); Nodes->addNode(node); }
void Graph::addEdge(void *d1,void *d2) { GNode *node; GNode *node2;
node=findNode(d1); node2=findNode(d2);
node->addEdge(node2); node2->addEdge(node); }
GNode *Graph::findNode(void *data) { GNode *node;
Nodes->reset(); // paranoia while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0) if(node->getData()==data) { Nodes->reset();
return node; }
Nodes->reset();
return 0; }
void Graph::walk() { Instance=this; (RI_CAST<void (*)(Graph *)>(WalkFunc))(this); }
void Graph::print(void (*func)(void *data),void *d1,void *d2) { GNode *node; GNode *node2;
node=findNode(d1); node2=findNode(d2);
if(node==0||node2==0) { cout<<"Can't find one of the path ends for this graph print"<<endl; exit(1); }
Instance=this; (RI_CAST<void (*)(Graph *,void (*)(void *),GNode *, GNode *)>(PrintFunc)) (this,func,node,node2); }
void Graph::BredthFirstWrapper() { if(Instance==0) return;
Instance->BredthFirst(); }
void Graph::BredthFirstPrintWrapper(void (*func)(void *data),GNode *node,GNode *node2) { if(Instance==0) return;
cout<<node<<" "<<node2<<endl; Instance->BredthFirstPrint(func,node,node2); }
void Graph::Dump() { GNode *node;
cout<<"Nodes: "<<endl;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0) node->Dump();
Nodes->reset(); }
Graph::Graph(unsigned long walkfunc,unsigned long printfunc) { Nodes=Nodes->init();
if(walkfunc==0) walkfunc=RI_CAST<unsigned long>(&Graph::BredthFirstWrapper);
WalkFunc=(walkfunc);
if(printfunc==0) printfunc=RI_CAST<unsigned long>(&Graph::BredthFirstPrintWrapper);
PrintFunc=printfunc; }
void Graph::BredthFirst() { GNode *node; GNode *tmp; Queue *queue; LinkedList *edges;
reset();
node=RI_CAST<GNode *>(Nodes->getNext()); if(node==0) return;
node->setColor(GNodeColorGrey);
queue=queue->init(); queue->insert(node);
node->setDepth(0); node->setPrev(0);
do { node=RI_CAST<GNode *>(queue->peek()); cout<<"processing node "<<node<<endl; edges=node->getEdges();
while((tmp=RI_CAST<GNode *>(edges->getNext()))!=0) if(tmp->getColor()==GNodeColorWhite) { tmp->setColor(GNodeColorGrey); tmp->setDepth(node->getDepth()+1); tmp->setPrev(node);
queue->insert(tmp); }
edges->reset();
node=RI_CAST<GNode *>(queue->get());
if(node==0) break;
node->setColor(GNodeColorBlack); } while(queue->getSize()>0); }
void Graph::BredthFirstPrint(void (*func)(void *data),GNode *node, GNode *node2) { if(node==node2) { func(node->getData()); return; }
if(node2->getPrev()==0) { cout<<"No path from "<<node<<" to "<<node2<<endl; exit(1); }
BredthFirstPrint(func,node,node2->getPrev()); func(node2->getData()); }
void Graph::reset() { GNode *node;
while((node=RI_CAST<GNode *>(Nodes->getNext()))!=0) node->reset();
Nodes->reset(); }
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message
news:Wd***************@fe10.lga... Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and BredthFirstPrint and make both approriate func ptrs it
works fine.
You'd have to ask the programmer why it was done that way. It could be that
one or both static functions were intended to called from somewhere external
that didn't have access to the necessary instance, so the static Instance
was used. Or maybe there's only ever supposed to be one instance. There's a
lot of code and I don't have time to analyse it properly..
It all looks pretty horrible to me. I would want to get rid of 'Instance'
altogether if possible.
DW
"David White" <no@email.provided> wrote in message
news:Ak*****************@nasal.pacific.net.au... "Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message news:Wd***************@fe10.lga... Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper.
Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and BredthFirstPrint and make both approriate func ptrs it works fine.
You'd have to ask the programmer why it was done that way. It could be that
Ok I will ask myself why I did it this way ;-).... btw I found the issue
later which was
a junk arguement.
one or both static functions were intended to called from somewhere external that didn't have access to the necessary instance, so the static Instance was used. Or maybe there's only ever supposed to be one instance. There's a lot of code and I don't have time to analyse it properly..
It all looks pretty horrible to me. I would want to get rid of 'Instance' altogether if possible.
The reason is if your going to pass a non-default method to the factory
(Graph::init) the compiler barfs unless it is static. And "this" is
avaible from inside a static method. Thus Instance=this then call the
static wrapper and do Instance->real
--Aryeh This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J |
last post by:
I'm wondering why the form created in the code below stays "alive". I would
have thought that because the form variable is local to the method, that
garbage collection would get rid of it. Makes...
|
by: KathyB |
last post by:
Hi,
Using the following in an asp.net procedure. I get the error "The
expression passed to this method should result in a NodeSet".
Dim xDoc As New Document()
The line causing the error is:...
|
by: Marty |
last post by:
Hi,
I have a class that I modified to be static. It is now a public sealed
class and all function are static, no more constructor but a init()
function to do the constructor job.
This class...
|
by: ChrisB |
last post by:
Hello:
An object that is a field in another object has a constructor that requires
a reference to the containing object:
// object fields
ChildObject childObject = new ChildObject(this);
...
|
by: relient |
last post by:
Question: Why can't you access a private inherited field from a base
class in a derived class? I have a *theory* of how this works, of
which, I'm not completely sure of but makes logical sense to...
|
by: dhnriverside |
last post by:
Hi peeps
I'm just following this HOW-TO from MSDN..
http://support.microsoft.com/default.aspx?scid=kb;en-us;306355
But I've got a problem. I've adding the #using System.Diagnostics; line to...
|
by: danny.dion |
last post by:
Hi ! I have a question about JScript :
I have an object class wich dynamically creates a control in the page.
Then it binds an event to that control, pointing on one of its methods
(the...
|
by: psp |
last post by:
I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-to the method being called,
i.e.
class X::methodA()
{
this->methodB();
}
And they...
|
by: DamienS |
last post by:
Hi,
I have a static method in a class and I need to be able to return a
reference to "this". Googling around, I found a heap of discussions of
the pros/cons of "abstract static" etc. It was...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |