Click here to Skip to main content
15,887,266 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Compiling DOS Apps With Windows 7 Pin
«_Superman_»18-Oct-11 17:23
professional«_Superman_»18-Oct-11 17:23 
GeneralRe: Compiling DOS Apps With Windows 7 Pin
softwaremonkey18-Oct-11 19:45
softwaremonkey18-Oct-11 19:45 
GeneralRe: run older 16-bit Windows programs on a 64-bit platform Pin
Goto_Label_18-Oct-11 23:23
Goto_Label_18-Oct-11 23:23 
GeneralRe: run older 16-bit Windows programs on a 64-bit platform Pin
softwaremonkey19-Oct-11 7:09
softwaremonkey19-Oct-11 7:09 
Questionc++Safe to use size specifier in enum declaration ? Pin
Maximilien18-Oct-11 4:29
Maximilien18-Oct-11 4:29 
AnswerRe: c++Safe to use size specifier in enum declaration ? Pin
TheGreatAndPowerfulOz18-Oct-11 4:58
TheGreatAndPowerfulOz18-Oct-11 4:58 
GeneralRe: c++Safe to use size specifier in enum declaration ? Pin
Maximilien18-Oct-11 5:03
Maximilien18-Oct-11 5:03 
QuestionC++ Dynamic Data Binding / Reflection Pin
Mortomfod17-Oct-11 9:35
Mortomfod17-Oct-11 9:35 
Sorry about the long post but its a bit of complex subject matter.

I'm trying to do a data-binding system in C++ similar to that found in WPF.

WPF uses the built-in C# reflection mechanism to map an XML data binding tag to a method or field in a object. It can also binding collection classes to list controls.

I want to do the same but in C++. I'm writing the back-end system so I need to provide a data-binding/reflection type system for other programmers to use.

Requirements:
Here is an example usage for the system.

The programmers will define their own classes and somehow expose their classes functions and member variables to the the data binding system. They could then create a collection class, containing instances of those classes.

They specify the UI elements in a XML format, and include the data binding reference for certain widget types.

They would then expose the collection class to the widget, probably using data-binding, which will bind the collection class a list widget. The list widget would then iterate through the list and for each element would invoke a function call, by mapping the binding name to a class function or reading the data directly out of the object structure.

What I have so far:
I've created an interface class (IReflection) which provides a number of pure virtual methods. A programmer wanting to expose their classes methods/member variables would inherit for the interface.

I've managed to expose the member variables by using a static class which takes a static function pointer that belongs to the class. This then creates a temporary instance of the class, which is then used to build a table of byte offsets for each member variable. This static array belongs to the class. One of the IReflection methods, takes a string lookup name, finds it in the array, gets the byte offset back and reads the data at that point. The lookup table also contains the size of the type of data to prevent buffer overruns.

This works OK, except its pretty nasty and feels like a bit of hack. Also it gets around private access
as the code is just reading a piece of memory.

I have a slightly harder problem when it comes to executing member functions. I want to be able to create function pointers to call the methods that are exposed. However I don't want to create member function pointers as I would need to create them for every instance and I think that would be a waste of memory. Therefore I need to provide static functions which as essentially proxy functions that take a pointer to the instance and then call the member function on it.

This is making the code look messy and unmanageable and I'm not happy with any of the solutions I have.

I have found lots of information on-line for providing reflection in C++, but none of them provide the ability to invoke methods or read/write properties, which is essential for a UI to display and change values.

I am starting to think I should provide a method in the IReflection interface that takes a string and returns the type of data required. It would then be up to the programmer to define this function. They could simply just write 'if-else if' code and do the necessary string compares and execute the correct code.

That feels like the easy option for me, but I'm concerned how much work I'm generating for others by taking the easy option.

It would be nice to be able to expose the data binding using a set of macros, similar to RTTI to make it easier for the programmer to define the code. However I'm not a fan of macro code as it is impossible to debug.

I should say this is a UI system for a game engine, so I can't go around allocating lots of memory, I don't want lots of small allocations as this is bad for memory fragmentation and the data cache. It also needs to work quickly. I'll probably eventual turn the strings into hashes and do hash table lookup but that is a implementation detail for performance reasons and doesn't have to be done straight away.

Does anyone know how this could be done or know of any examples of this type of system. Not sure if you would call this problem 'dynamic data-binding' or 'reflection'.

Any advice would be appreciated.

Thanks
AnswerRe: C++ Dynamic Data Binding / Reflection Pin
Fernando A. Gomez F.17-Oct-11 10:09
Fernando A. Gomez F.17-Oct-11 10:09 
GeneralRe: C++ Dynamic Data Binding / Reflection Pin
Mortomfod17-Oct-11 10:20
Mortomfod17-Oct-11 10:20 
GeneralRe: C++ Dynamic Data Binding / Reflection Pin
Fernando A. Gomez F.17-Oct-11 10:53
Fernando A. Gomez F.17-Oct-11 10:53 
AnswerRe: C++ Dynamic Data Binding / Reflection Pin
TheGreatAndPowerfulOz17-Oct-11 10:25
TheGreatAndPowerfulOz17-Oct-11 10:25 
AnswerRe: C++ Dynamic Data Binding / Reflection Pin
TheGreatAndPowerfulOz17-Oct-11 10:47
TheGreatAndPowerfulOz17-Oct-11 10:47 
QuestionGetting a list of SQL Servers Available Pin
jkirkerx17-Oct-11 8:30
professionaljkirkerx17-Oct-11 8:30 
QuestionRe: Getting a list of SQL Servers Available Pin
David Crow17-Oct-11 9:52
David Crow17-Oct-11 9:52 
AnswerRe: Getting a list of SQL Servers Available Pin
jkirkerx17-Oct-11 11:05
professionaljkirkerx17-Oct-11 11:05 
AnswerRe: Getting a list of SQL Servers Available Pin
jkirkerx17-Oct-11 11:42
professionaljkirkerx17-Oct-11 11:42 
GeneralRe: Getting a list of SQL Servers Available Pin
jkirkerx17-Oct-11 12:35
professionaljkirkerx17-Oct-11 12:35 
QuestionSuggest a fast way to do? Pin
john563216-Oct-11 23:06
john563216-Oct-11 23:06 
AnswerRe: Suggest a fast way to do? Pin
Richard MacCutchan16-Oct-11 23:11
mveRichard MacCutchan16-Oct-11 23:11 
GeneralRe: Suggest a fast way to do? Pin
john563216-Oct-11 23:19
john563216-Oct-11 23:19 
GeneralRe: Suggest a fast way to do? Pin
Richard MacCutchan16-Oct-11 23:56
mveRichard MacCutchan16-Oct-11 23:56 
GeneralRe: Suggest a fast way to do? Pin
john563217-Oct-11 0:01
john563217-Oct-11 0:01 
GeneralRe: Suggest a fast way to do? Pin
Richard MacCutchan17-Oct-11 0:06
mveRichard MacCutchan17-Oct-11 0:06 
GeneralRe: Suggest a fast way to do? Pin
enhzflep17-Oct-11 13:35
enhzflep17-Oct-11 13:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.