ra***********@gmail.com wrote:
# Dear All
#
# As I was going through the code, I find the below piece of code,
#
# #define FILEMAPPING \
# char *data; \
# int size; \
# char *position; \
# int (*open)(struct FileMapping *self, const char *filename ); \
# int (*close) (struct FileMapping *self ); \
# int (*destroy) (struct FileMapping *self ); \
# int (*isYourFile)(struct FileMapping *self, const char *filename, u32
# *outSameFile );
#
# typedef struct FileMapping
# {
# FILEMAPPING
# } FileMapping, *FileMapping;
#
# Over above they have declared the Macro Named FILEMAPPING but I am not
# able to understand what does it really mean ?, they have declared the
# macro which is going to be replaced in the structure FileMapping, But
# why they want to go in the above format. ?
Subtyping. If you want a FileMapping object, use struct {FILEMAPPING}. If
you want to derive a subtype that allows the segment length to be set,
#define EXTENDEDABLE_FILEMAPPING \
FILEMAPPING \
int (*setlength)(struct ExtendableFileMapping *self,long long);
typedef struct ExtendableFileMapping {
EXTENDEDABLE_FILEMAPPING
} ExtendableFileMapping;
If I remember aright you are guarenteed that if
ExtendableFileMapping *e = (plugh);
FileMapping *m = (FileMapping*)e;
then
&(m->data) == &(e->data)
&(m->size) == &(e->size)
. . .
&(m->isYourFile) == &(e->isYourFile)
so that you can cast a subtyped object to a supertype and access supertype
fields and methods either way.
The less tricky way to do this is
/*supertype*/
typedef struct FileMapping
char *data;
int size;
char *position;
int (*open)(struct FileMapping *self, const char *filename );
int (*close) (struct FileMapping *self );
int (*destroy) (struct FileMapping *self );
int (*isYourFile)(struct FileMapping *self, const char *filename, u32
*outSameFile );
} FileMapping;
/*subtype*/
typedef struct ExtendableFileMapping
FileMapping super;
int (*setlength)(struct ExtendableFileMapping *self,long long);
} ExtendableFileMapping;
ExtendableFileMapping *e = (plugh);
FileMapping *m = &(e->super);
--
SM Ryan
http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilirating pointing out the shortcomings of others, is there?