From: Hector Santos on
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?

I guess the question is if I can do this via serialization.

I was able to do this in .NET with reflection library, but I would
like to do this in non-.NET C/C++.

--
HLS
From: David Lowndes on
>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.

Dave
From: Giovanni Dicanio on


"David Lowndes" <DavidL(a)example.invalid> ha scritto nel messaggio
news:0kr5p59ncuh1qhs2v74q590bbe89dk74s8(a)4ax.com...
>>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>.

Giovanni


From: Hector Santos on
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
From: Hector Santos on
Hector Santos wrote:

> I am wondering if MFC with RTTI, if it has any sort of reflection
> concept. I don't see myself.
>

> ...
>
> 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;
>
> ...
>

> 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++.
>

Another use will be to create marshalling proxies over the wire RPC
communications , like we have for Java using a Cpp2Java converter.
The java class for TFileRecord is:

//****************************************************
// File: TFileRecord.java
// (c) copyright 2002 Santronics Software Inc.
//
//****************************************************
// DO NOT EDIT THIS FILE!!!
// CPP2JAVA GENERATED FROM SOURCE FILE: wctype.h
//****************************************************

package COM.winserver.wildcat;

import java.io.IOException;

public class TFileRecord extends WcRecord {
public int Status;
public int Area;
public String Name;
public String Description;
public String Password;
public TUserInfo Uploader;
public long PostTime;
public long FileTime;
public long LastAccessed;
public int FileFlags;
public int FileSize;
public int Downloads;
public int Cost;
// Total size
public static final int SIZE =
4+4+MAX_PATH+76+32+TUserInfo.SIZE+8+8+8+4+4+4+4;

// Constructors
public TFileRecord()
{
}

public TFileRecord(byte[] x)
{
fromByteArray(x);
}

// Methods
protected void writeTo(WcOutputStream out) throws IOException
{
super.writeTo(out);
out.writeInt(Status);
out.writeInt(Area);
out.writeString(Name, MAX_PATH);
out.writeString(Description, 76);
out.writeString(Password, 32);
Uploader.writeTo(out);
out.writeLong(PostTime);
out.writeLong(FileTime);
out.writeLong(LastAccessed);
out.writeInt(FileFlags);
out.writeInt(FileSize);
out.writeInt(Downloads);
out.writeInt(Cost);
}

protected void readFrom(WcInputStream in) throws IOException
{
super.readFrom(in);
Status = in.readInt();
Area = in.readInt();
Name = in.readString(MAX_PATH);
Description = in.readString(76);
Password = in.readString(32);
Uploader = new TUserInfo(); Uploader.readFrom(in);
PostTime = in.readLong();
FileTime = in.readLong();
LastAccessed = in.readLong();
FileFlags = in.readInt();
FileSize = in.readInt();
Downloads = in.readInt();
Cost = in.readInt();
}
}

--
HLS