Re: typedef struct RTTI/Reflection
Giovanni Dicanio wrote:
"David Lowndes"
Is it possible in MFC to wrap a C typedef struct with a class so I can
programmatically access the struct member name and type information?
You don't need to wrap it as a class to use RTTI - it'll work with a
struct.
In fact, in C++, <struct> == <class with all public fields>.
Right, that wasn't the question.
How do you programmatically access the fields here? Or better how can
enumerate the class memebers RTTI?
For example, in .NET you have the reflection library. Like this VB.NET
function I wrote to dump any object field members, if any.72
Sub dumpObject(ByVal o As Object)
Console.WriteLine("Type: {0} Fields: {1}",o.GetType(),
o.GetType().GetFields().Length)
For Each s As System.Reflection.FieldInfo In
o.GetType().GetFields()
Console.WriteLine("- {0,-25} {1}", s.Name, s.FieldType)
Next
End Sub
In javascript, you similar have prototype iteration capabilities.
For std C++, I found a set of classes, but it requires you to
describes the members using macros which defeats the automation purpose:
class A {
public:
int a;
char* b;
private:
unsigned short c;
long l[10];
public:
RTTI_DESCRIBE_STRUCT((RTTI_FIELD(a, 0),
RTTI_PTR(b, 0),
RTTI_FIELD(c, 0),
RTTI_ARRAY(l, 0)));
};
I am wondering if MFC with RTTI, if it has any sort of reflection
concept. I don't see myself.
For example, I have this struct
typedef struct tagTFileRecord {
DWORD Status;
DWORD Area;
char Name[SIZE_LONG_FILE_NAME];
char Description[SIZE_FILE_DESCRIPTION];
char Password[SIZE_PASSWORD];
TUserInfo Uploader;
FILETIME PostTime;
FILETIME FileTime;
FILETIME LastAccessed;
DWORD FileFlags;
DWORD FileSize;
DWORD Downloads;
DWORD Cost;
} TFileRecord;
Using reflection, I would like to generate source code such a class
like so:
class CTFileRecord {
public:
CTFileRecord(const TFileRecord *pfr = NULL)
{
ZeroMemory(&rec,sizeof(rec));
if (pfr) rec = *pfr;
}
const TFileRecord & Get() {return rec;}
void Put(const TFileRecord & fr) { rec = fr;}
public:
//-------------------------------------------------
// write/read properties for each struct field:
_declspec(property(get=getArea, put=putArea))DWORD Area;
virtual DWORD getArea() { return rec.Area;}
virtual void putArea(DWORD a) { rec.Area = a; }
_declspec(property(get=getName, put=putName)) CString Name;
virtual CString getName() { return rec.Name;}
virtual void putName(const char *s) {
strncpy(rec.Name,s,sizeof(rec.Name)); }
// ......... more properties per field ........
//-------------------------------------------------
private:
TFileRecord rec;
};
and in addition, using reflection also create ODBC CTFileRecordSet
class with the proper DoFieldExchange() member functions.
Currently, the way we do this is with a C/C++ header files parser and
converter that creates the interfaces for other languages. I am
wondering if its possible to use MFC/RTTI do automatic this for me for
C/C++.
Thanks
--
HLS