C++ pointer to class member and methods question - tables of
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++ pointer to class member and methods question - tables of
All:
This is a C++ question. Sorry it's a little long, but I'm looking for specific answer.. read on.
I'm wondering if anyone has any good solution for what I'm trying to do below. And just to set the expectations, I've done this before in a more limited sense in C, but I'd like to do it in C++ as that language has other features (like STL) that I need for the rest of what I'm trying to do.
Any proposed solution has to be saleable to 1000's of different classes, millions of instances, and perhaps 100's of unique millions member variables in a distributed environment of 100's or more servers. Actually that scale-ability has been achieved, as I'm generating all the C++ code from very well defined but malleable generic definitions.
So here's the problem: I need to access all the members and methods of any class instance in at least two ways:
1) As a full C++ class, meaning fast access and C++ goodies
2) As a table of pointers to all class members and methods, for reports, cli's and marshaling, ect..
Take a class like this - it contains a collection of related data, and their
access functions. Note that this class will single-instance inherit other classes in a hierarchy that contain more and more common fields. Total depth of may be 10 or more classes.
I figure about 10% of these fields will be direct hardware accesses, though the class user doesn't need to (or want to) care. The rest provide meta-information about those types: names, descriptions, limits, access rights, etc...
Here's the question -- How do I set up a generic table of pointers to 1) member variables and 2) pointers to methods for this class. Again, I plan to use the C++ class for the hard specific work, but the table is used for general cli's, queries and reports, etc..
This is the point I need help on -- The idea with this table is to be able to enumerate through and point at the member variables, and class dependent get/set methods for each of those members. There is to be one of these tables PER CLASS.
As per implementation, I would take a pointer to the class instance (the raw data) and another to this table that defines all the members/methods to the class (could be in the class itself). Add a little bit of enumeration code, stir a bit, and out pops CLI's, marshaling code, report generators, GUI interfaces and even converters to other language interfaces like JAVA, HTML or even God forbid and I was brain dead, COBOL. The goal: Guts in C++, interfaces possible to most anything.
In C, I had "offsetof()" that at least gave me structure dependent offsets of each any fields (and function pointers) that I cared to implement. I could generate the names/types as needed. Worked very very well.
C++ gives me a lot more (I really want to be able to use STL), but offsetof() is not allowed.
I'm looking for a good solution here. These objects ultimately need small, very fast, persistent, client/server-able and transferable over a distributed network to other machines. But more than that, they need to be capable of being self defining.
As I'm generating all this code, I'm not sure I need to use C++ templates, but that's possible. I've looked at Goggle protocol buffers and Apache thrift, but they are too heavyweight, and appear more concerned with network issues than fast hardware access, which at the bottom layer is exactly what I'm doing.
In summary, what is the necessary structure and syntax that would allow me to create a access controllable table of functions to allow name/type/get/set/.. access to each member of class in a common way. (if your ready to suggest parsing the symbol table, I love it, but it's not fast or portable)
Feel free to propose constructive ideas.
Last edited by pbradstr; 01-09-2017 at 02:33 AM.
Reason: formatting
C++ is supposed to insulate you from subtle bugs like the typo that I see in your first code snippet.
But, to obtain the language's strengths, you must work with them. If you start poking-around their guts, and/or building stuff external to them that depends on them, it will all fall down in a pile of poo.
If someone needs to know what a particular class exposes, then IMHO that class(!) should provide the service. Let each class (or its ancestor) provide the property-access that is called for, using code within the class implementation, so that the class continues to be opaque. Let each class be, and continue to be, "the master of its own kingdom."
I'm wondering if anyone has any good solution for what I'm trying to do below. And just to set the expectations, I've done this before in a more limited sense in C, but I'd like to do it in C++ as that language has other features (like STL) that I need for the rest of what I'm trying to do.
if you did it with C ic can be done with C++ since C++ extends C (in some kind)
so whatever you have available in C is available in C++
Quote:
Originally Posted by pbradstr
Any proposed solution has to be saleable to 1000's of different classes, millions of instances, and perhaps 100's of unique millions member variables in a distributed environment of 100's or more servers. Actually that scale-ability has been achieved, as I'm generating all the C++ code from very well defined but malleable generic definitions.
sounds horrible, you are embedded dev, right?
Quote:
Originally Posted by pbradstr
So here's the problem: I need to access all the members and methods of any class instance in at least two ways:
1) As a full C++ class, meaning fast access and C++ goodies
2) As a table of pointers to all class members and methods, for reports, cli's and marshaling, ect..
Take a class like this - it contains a collection of related data, and their
access functions. Note that this class will single-instance inherit other classes in a hierarchy that contain more and more common fields. Total depth of may be 10 or more classes.
I figure about 10% of these fields will be direct hardware accesses, though the class user doesn't need to (or want to) care. The rest provide meta-information about those types: names, descriptions, limits, access rights, etc...
Here's the question -- How do I set up a generic table of pointers to 1) member variables and 2) pointers to methods for this class. Again, I plan to use the C++ class for the hard specific work, but the table is used for general cli's, queries and reports, etc..
a member consists of the type, and the address of the datamember (or function) and the address of the actual class instance
Code:
class FOO
{
public: // note this public
int data;
};
int main()
{
int Foo::*dataaptr = &Foo::data;
Foo f;
f.*dataaptr = 2;
}
C++ is typed, if you want to spare this info in a table it needs to be void* and you need your own conversion functions
Quote:
Originally Posted by pbradstr
This is the point I need help on -- The idea with this table is to be able to enumerate through and point at the member variables, and class dependent get/set methods for each of those members. There is to be one of these tables PER CLASS.
As per implementation, I would take a pointer to the class instance (the raw data) and another to this table that defines all the members/methods to the class (could be in the class itself). Add a little bit of enumeration code, stir a bit, and out pops CLI's, marshaling code, report generators, GUI interfaces and even converters to other language interfaces like JAVA, HTML or even God forbid and I was brain dead, COBOL. The goal: Guts in C++, interfaces possible to most anything.
In C, I had "offsetof()" that at least gave me structure dependent offsets of each any fields (and function pointers) that I cared to implement. I could generate the names/types as needed. Worked very very well.
C++ gives me a lot more (I really want to be able to use STL), but offsetof() is not allowed.
I'm looking for a good solution here. These objects ultimately need small, very fast, persistent, client/server-able and transferable over a distributed network to other machines. But more than that, they need to be capable of being self defining.
As I'm generating all this code, I'm not sure I need to use C++ templates, but that's possible. I've looked at Goggle protocol buffers and Apache thrift, but they are too heavyweight, and appear more concerned with network issues than fast hardware access, which at the bottom layer is exactly what I'm doing.
In summary, what is the necessary structure and syntax that would allow me to create a access controllable table of functions to allow name/type/get/set/.. access to each member of class in a common way. (if your ready to suggest parsing the symbol table, I love it, but it's not fast or portable)
Feel free to propose constructive ideas.
solve the problem, than write code, what do you want do do?
as mentioned , it sound that you want to reinvent reflections, but in C++ and without additional costs.
Qt does add reflections via macros and the moc to C++, but of course, not without additional costs.
you can also look into the implementations of, say python or ruby, and figure out why some features are bounden to costs.
(ruby is btw an excellent lanugage for greating a DSL and code generators, look at rails, but this is of course not famous for runtime speed, just for generating things fast)
but what I really think is the case, you started with C++, and are on the point of learning something new where a lot of confusion and chaos pops up in the head. no panic, if you try to learn a new language every year, you will get used to this state.
just sleep, learn, rethink, try, throw away, just sleep, learn, rethink, try, throw away .... recursion :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.