473,785 Members | 3,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with function pointers

I have two classes:
class OntologyParser
{
...

protected:

virtual void startElement(vo id *userData, const char *name, const
char **atts);
virtual void endElement(void *userData, const char *name);
virtual void charData( void *userData, const XML_Char *s, int len );

....
public:
void initXMLParse(co nst string& fname);
.....
};

This class is a base class for

class ProtegeOntology Parser : public OntologyParser
{
public:
inline ProtegeOntology Parser(const string& strFileName)
{
initXMLParse(st rFileName);
}

};

I am using G++ on RedHat 9.0 Linux with Expat library.

The initXMLParse function is common to all (here just
ProtegeOntology Parser) derived classes hence I want to keep that in
parent class. The functions startElement, endElement and charData must
be implemented separately by all derived classes.

However the problem is expat needs call backs setup for startElement,
endElement and charData functions from within initXMLParse like this:

void OntologyParser: :initXMLParse(c onst string& fname)
{
char buffer[BUFSIZ];
int len;
int done;
int depth = 0;

//Get the Parser
XML_Parser parser = XML_ParserCreat e(NULL);

//User Data Handler
XML_SetUserData (parser, &depth);

//Element Level Handler
XML_SetElementH andler(parser, startElement, endElement);

//Set What to do with the data
XML_SetCharacte rDataHandler( parser, charData );

....
}

But the functions that I pass there as argument must be the functions
of derived classes and right function addresses should be passed based
upon which object was responsible for invoking initXMLParse().

As you know ISO C++ forbids somthing like &(this->charData)
Secondly i do not want to make those 3 function static. and they must
be "virtual" in the base class so that all derived classes are forced
to implement them.

How can I pass those arguments?

Jul 23 '05 #1
1 1686
"TheOne" <su************ ****@gmail.com> wrote...
I have two classes:
class OntologyParser
{
...

protected:

virtual void startElement(vo id *userData, const char *name, const
char **atts);
virtual void endElement(void *userData, const char *name);
virtual void charData( void *userData, const XML_Char *s, int len );

....
public:
void initXMLParse(co nst string& fname);
.....
};

This class is a base class for

class ProtegeOntology Parser : public OntologyParser
{
public:
inline ProtegeOntology Parser(const string& strFileName)
{
initXMLParse(st rFileName);
}

};

I am using G++ on RedHat 9.0 Linux with Expat library.

The initXMLParse function is common to all (here just
ProtegeOntology Parser) derived classes hence I want to keep that in
parent class. The functions startElement, endElement and charData must
be implemented separately by all derived classes.

However the problem is expat needs call backs setup for startElement,
endElement and charData functions from within initXMLParse like this:

void OntologyParser: :initXMLParse(c onst string& fname)
{
char buffer[BUFSIZ];
int len;
int done;
int depth = 0;

//Get the Parser
XML_Parser parser = XML_ParserCreat e(NULL);

//User Data Handler
XML_SetUserData (parser, &depth);

//Element Level Handler
XML_SetElementH andler(parser, startElement, endElement);

//Set What to do with the data
XML_SetCharacte rDataHandler( parser, charData );

....
}

But the functions that I pass there as argument must be the functions
of derived classes and right function addresses should be passed based
upon which object was responsible for invoking initXMLParse().

As you know ISO C++ forbids somthing like &(this->charData)
Secondly i do not want to make those 3 function static.
But how would expat call them? It requres them to be callbacks of some
particular signature which probably does not have anything to do with
your classes. You have no choice but to make them static.
and they must
be "virtual" in the base class so that all derived classes are forced
to implement them.
The derived classes are not going to be forced unless those functions
are declared pure.
How can I pass those arguments?


Make the three functions static. Let expat pass the 'userdata' as
void* and static_cast it to your base class in each function. Then
call your "real" processing functions, which should be virtual, using
that base class pointer. The derived function should be called.

---------------------------------------
struct Caller { // your expat emulator
void (*int_callback) (void*,int);
void (*double_callba ck)(void*,doubl e);
void* ptr;

void init_ptr(void* p) { ptr = p; }
void init_int_cb(voi d (*icb)(void*,in t)) { int_callback = icb; }
void init_dbl_cb(voi d (*dcb)(void*,do uble)) { double_callback =
dcb; }

void do_something() {
int_callback(pt r, 666);
double_callback (ptr, 3.1415926);
int_callback(pt r, 42);
}
};

struct Base {
Caller *caller;

Base() : caller(new Caller) { caller->init_ptr(this) ; }
virtual ~Base() { delete caller; }
Base(const Base& b) : caller(new Caller(*b.calle r)) {}

void init_caller();

static void foo_callback(vo id*, int);
static void bar_callback(vo id*, double);
virtual void foo(int) = 0;
virtual void bar(double) = 0;

private:
Base& operator =(const Base&);
};

#include <iostream>
using std::cout;

struct Derived : Base {
virtual void foo(int a) { cout << "Derived::f oo(" << a << ")\n"; }
virtual void bar(double d) { cout << "Derived::b ar(" << d <<
")\n"; }
};

void Base::foo_callb ack(void* ptr, int a) {
Base *p = static_cast<Bas e*>(ptr);
p->foo(a);
}

void Base::bar_callb ack(void* ptr, double d) {
Base *p = static_cast<Bas e*>(ptr);
p->bar(d);
}

void Base::init_call er() {
caller->init_int_cb(fo o_callback);
caller->init_dbl_cb(ba r_callback);

caller->do_something() ;
}

int main() {
Base *pb = new Derived;
pb->init_caller( );
delete pb;
}
---------------------------------------
V
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2976
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version seem to work. I would appreciate someone to comment on this. Version 1 (crashes on deallocating) #include <iostream>
13
2154
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================ class Buyer{ void start(void); friend void Buyer_run(Buyer *buyer);
3
3341
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to substitute the set<Data*> with a hash_set<Data*>: typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet; // typedef set<Data*> DataPointerHashSet; // (see complete code below) But it doesn't work! Everything is fine...
8
2090
by: Mantorok Redgormor | last post by:
I have ran into a problem where I have a struct that has a member which contains a pointer to function and is initialized to a function in the initializer list. With my array of structs of this type, I have some elements of this array, thats function pointer member does not need to be initialized to a function. I can't simply initialize it to NULL and I'm not sure if casting NULL(which can be 0 or (void *)0) to the function pointer type
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
39
19647
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
11
1704
by: Michael | last post by:
Hi, I am trying to get an idea of how function pointers work. I have the following: #include <stdio.h> void do_stuff(int*,int,void*); void getInt(int*); void showInt(int*);
57
5674
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p expects type 'void *; but argument 2 has type 'void
14
2090
by: Frank | last post by:
Hello everyone, I am having trouble overloading the < operator for an assignment. I use a struct that contains information and I would like to sort this structure using STL sort with my own criteria of sorting. Basically, I would like to sort on visitor count of the Attraction structure. However, it never uses the < overloaded operator with my code. Handler.h:
15
2683
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it then adds values to "jd" 3.) in the end it prints values of "jd".
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.