LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2017, 02:06 AM   #1
pbradstr
LQ Newbie
 
Registered: May 2016
Posts: 3

Rep: Reputation: Disabled
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.

Code:
class foo : bar
{
     int           var1;
     float         var2;
     std::string   var3;

     int           get_var1()             { return(var1); );
     error_t       set_var1(int v)        { var1 = v; return(ERR_OK); };
     float         get_var2()             { return(var2); );
     error_t       set_var2(float v)      { var2 = v; return(ERR_OK); };
     std::string   get_var3()             { return(var1); );
     error_t       set_var3(std::string v){ var3 = v; return(ERR_OK); };
};
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..

Code:
some_type  foo_info[] =         // THIS DOESN'T WORK
{
    { "var1",  int,          &foo::var1, &foo::get_var1, &foo::set_var1  },
    { "var2",  float,        &foo::var2, &foo::get_var2, &foo::set_var2  },
    { "var3",  std::string,  &foo::var3, &foo::get_var3, &foo::set_var3  },
};
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
 
Old 01-09-2017, 01:54 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
If you need reflection, why use C++?
 
1 members found this post helpful.
Old 01-09-2017, 07:10 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
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."
 
Old 01-17-2017, 12:04 AM   #4
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by pbradstr View Post
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 View Post
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 View Post
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.

Code:
class foo : bar
{
     int           var1;
     float         var2;
     std::string   var3;

     int           get_var1()             { return(var1); );
     error_t       set_var1(int v)        { var1 = v; return(ERR_OK); };
     float         get_var2()             { return(var2); );
     error_t       set_var2(float v)      { var2 = v; return(ERR_OK); };
     std::string   get_var3()             { return(var1); );
     error_t       set_var3(std::string v){ var3 = v; return(ERR_OK); };
};
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..

Code:
some_type  foo_info[] =         // THIS DOESN'T WORK
{
    { "var1",  int,          &foo::var1, &foo::get_var1, &foo::set_var1  },
    { "var2",  float,        &foo::var2, &foo::get_var2, &foo::set_var2  },
    { "var3",  std::string,  &foo::var3, &foo::get_var3, &foo::set_var3  },
};
of course not, and it will not work in C either.

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 View Post
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.
of course it is
http://en.cppreference.com/w/cpp/types/offsetof


Quote:
Originally Posted by pbradstr View Post
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 :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can't we access a protected member in a derived class by a base class's object? Aquarius_Girl Programming 5 02-04-2012 10:35 PM
[SOLVED] C++ Class object can't find class methods TheCrow33 Programming 4 01-29-2012 05:04 PM
Does making a class a 'friend' make the class' member classes 'friends'? Jusctsch Programming 7 11-17-2011 07:58 PM
[SOLVED] C++ class member pointer changes after function returns Snark1994 Programming 5 03-30-2011 06:46 PM
c++ question, how to define a member array, and it's size, outside of the class dec.. Winter Knight Programming 2 01-23-2007 07:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration