473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

An excursion to pseudo-polymorphism

We have a system of small objects in the lines of

typedef struct {
...
} text_object;

And we have a "object master" that serves as a container for
several of these small objects, in the lines of

typedef struct {
...
text_object *text;
media_object *media;
...
} object_master;

Now, we need a generic/polymorphic way to initialise the small objects
as the initialisation is similar for all of them. I would like to use
inlined functions instead of macros for that, but currently don't see
any natural, clean way for it. A pseudo-implementation with a macro
(ALLOC_OBJ) is provided below.

Any ideas for converting that macro to an inline function?

my_error alloc_text(object_master *om, params* pms)
{
my_errcode err = 0;
text_object *to;
ALLOC_OBJ(om, to, textobj, TEXT_OBJECT_TYPE, pms);
return err;
}

my_error alloc_text(object_master *om, params* pms)
{
my_errcode err = 0;
media_object *mo;
ALLOC_OBJ(om, mo, mediaobj, MEDIA_OBJECT_TYPE, pms);
return err;
}

#define ALLOC_OBJ(objmaster, gen_obj, gen_obj_field, gen_obj_type,
params)
{
my_ASSERT(obj != NULL);
my_ASSERT(params != NULL);

gen_obj = my_malloc(sizeof(*gen_obj));
if (! gen_obj)
return ERR_OOM;

my_memset(gen_obj, 0, sizeof(*gen_obj));

gen_obj->module = my_module_find(params);
if (! gen_obj->module) {
err = ERR_MISSING_MODULE;
goto out_free_gen_obj;
}
if (my_typeof(gen_obj->module) != gen_obj_type) {
err = ERR_WRONG_MODULE;
goto out_free_gen_obj;
}

err = gen_obj->module->mod_init(gen_obj, params);
if (err)
goto out_free_gen_obj;

goto out;

out_free_gen_obj:
my_memset(gen_obj, 0, sizeof(*gen_obj));
my_free(gen_obj);
gen_obj = NULL;

out:
/* especially rigorous w/o a macro */
objmaster->gen_obj_field = gen_obj;
}

Jul 18 '06 #1
15 1994

gu********@gmail.com wrote:
We have a system of small objects in the lines of

typedef struct {
...
} text_object;

You're trying to emulate C++, the best way is to use a plugin model.
Prepare structures that have the address of functions that do init,
deinit, etc. Then just call through them.

Tom

Jul 18 '06 #2

Tom St Denis wrote:
gu********@gmail.com wrote:
We have a system of small objects in the lines of

typedef struct {
...
} text_object;


You're trying to emulate C++, the best way is to use a plugin model.
Prepare structures that have the address of functions that do init,
deinit, etc. Then just call through them.

Tom
I'm sorry, but this is too vague. I can't see how function pointers can
help in this case. ALLOC_OBJECT() is the problem, as it's generic. No
use of making it a "member function" by a function pointer as there is
nothing object-specifc going on, only identifiers/types change.

Can you perhaps bring an example?

Jul 18 '06 #3
gu********@gmail.com wrote:
I'm sorry, but this is too vague. I can't see how function pointers can
help in this case. ALLOC_OBJECT() is the problem, as it's generic. No
use of making it a "member function" by a function pointer as there is
nothing object-specifc going on, only identifiers/types change.

Can you perhaps bring an example?
http://libtomcrypt.com

That entire library is pluggable. For instance, my HMAC message
authentication code ... er source code [hehehe] doesn't know what hash
you are using. Doesn't know how the hash even works. Just knows it
can call certain functions to handle the message being hashed.

Similarly my Public Key code has no clue how your bignum math is done
[I support three different math libraries, each with their own bignum
data types, init/deinit functions, etc].

etc, etc, etc.

You're just giving up too easily.

Tom

Jul 18 '06 #4

Tom St Denis wrote:
gu********@gmail.com wrote:
I'm sorry, but this is too vague. I can't see how function pointers can
help in this case. ALLOC_OBJECT() is the problem, as it's generic. No
use of making it a "member function" by a function pointer as there is
nothing object-specifc going on, only identifiers/types change.

Can you perhaps bring an example?

http://libtomcrypt.com

That entire library is pluggable. For instance, my HMAC message
authentication code ... er source code [hehehe] doesn't know what hash
you are using. Doesn't know how the hash even works. Just knows it
can call certain functions to handle the message being hashed.

Similarly my Public Key code has no clue how your bignum math is done
[I support three different math libraries, each with their own bignum
data types, init/deinit functions, etc].

etc, etc, etc.

You're just giving up too easily.

Tom
This is all elementary run-time polymorphism. This case is a little bit
different and I'm afraid you fail to see the point. Please look at the
initial post and give your own function version of OBJECT_ALLOCATE()
that works both with text_objects and media_objects if you want to help.

Jul 18 '06 #5
guillem...@gmail.com wrote:
This is all elementary run-time polymorphism. This case is a little bit
different and I'm afraid you fail to see the point. Please look at the
initial post and give your own function version of OBJECT_ALLOCATE()
that works both with text_objects and media_objects if you want to help.
Are you saying you're adding methods that aren't known at compile time?

Then just do variadic definitions and lookup by name...

func_int_t insert_int = myplugin->lookup(methodname);
insert_int(ID, blah, blah, blah);

I don't care to look at your example because I know that you can
emulate all basic functions of C++ with C structures.

You just have to rethink the approach. It won't always be as nice as
C++ [that's why they invented C++] but you can get the same job done
regardless.

Tom

Jul 18 '06 #6

Tom St Denis wrote:
I don't care to look at your example because I know that you can
emulate all basic functions of C++ with C structures.
Tom, why did you answer if you have nothing to say? I'll open another
thread, please don't answer there.

Jul 19 '06 #7
gu********@gmail.com schrieb:
We have a system of small objects in the lines of

typedef struct {
...
} text_object;

And we have a "object master" that serves as a container for
several of these small objects, in the lines of

typedef struct {
...
text_object *text;
media_object *media;
...
} object_master;

Now, we need a generic/polymorphic way to initialise the small objects
as the initialisation is similar for all of them. I would like to use
inlined functions instead of macros for that, but currently don't see
any natural, clean way for it. A pseudo-implementation with a macro
(ALLOC_OBJ) is provided below.
You want templates for this, don't you? In C, you could use a structure,
that contains the common parts:

/* "common" or "base" object */
typedef struct
{
void* module;
} gen_object;

/* specific object */
typedef struct
{
void* module;
void* media_data;
} media_object;

my_error alloc_media(object_master *om, params* pms)
{
my_errcode err = 0;
media_object *mo;
/* ALLOC_OBJ(om, to, textobj, MEDIA_OBJECT_TYPE, pms); */
alloc_obj((gen_object**)&mo, sizeof *mo, MEDIA_OBJECT_TYPE, pms);
om->media = mo;

return err;
}

void alloc_obj(gen_object** obj, size_t size, int type, params* pms)
{
/* no error checking done */
*obj = my_malloc(size);

my_memset(*obj, 0, size);

(*obj)->module = my_module_find(params);

/* ... */
}

--
Thomas
Jul 19 '06 #8

Thomas J. Gritzan wrote:
You want templates for this, don't you?
Exactly. I should have explicitly pointed that out.
In C, you could use a structure,
that contains the common parts:
Well, yes, but it is a little fragile. It seems it is safest to just
stick to the macro. Looks like templates are best emulated with macros,
even though macros are infamous. Like goto that is notoriously
ill-famed, but still useful for try/catch-like error handling. Any
objections?

Thanks for the example, it looks nice and does exactly what I wanted.
However, it seems the macro provides better compile-time safety as no
casts are involved and pointers retain their "identity". I hoped that a
function can be generically used without loosing pointer identity (i.e.
compiler knows the pointer type and hence the pointed-to struct size),
postponing the obvious though in the back of my mind --- it is
impossible. Well, it is impossible and I should not have started this
discussion at all.

Jul 21 '06 #9

gu********@gmail.com wrote:
Thomas J. Gritzan wrote:
You want templates for this, don't you?

Exactly. I should have explicitly pointed that out.
In C, you could use a structure,
that contains the common parts:

Well, yes, but it is a little fragile. It seems it is safest to just
stick to the macro. Looks like templates are best emulated with macros,
even though macros are infamous. Like goto that is notoriously
ill-famed, but still useful for try/catch-like error handling. Any
objections?

Thanks for the example, it looks nice and does exactly what I wanted.
However, it seems the macro provides better compile-time safety as no
casts are involved and pointers retain their "identity". I hoped that a
function can be generically used without loosing pointer identity (i.e.
compiler knows the pointer type and hence the pointed-to struct size),
postponing the obvious though in the back of my mind --- it is
impossible. Well, it is impossible and I should not have started this
discussion at all.
NO! You absolutely should have started the discussion. I enjoyed
reading the examples, so even if you didn't get exactly what you
wanted, me (and undoubtedly other thread lurkers) reaped some
benefit.

I was recently playing around with some similar ideas and
had hoped that typedef'ing the void * would generate a
compiler warning, but was unable to coerce any warnings. eg

typedef void * foo;
typedef void * bar;

void f(foo a) {return;}
void g(bar a) {return;}

int main(void)
{
foo a;
bar b;
f(b);
g(a);
return 0;
}

Does anyone know a compiler that will warn here? Better
yet, can I coerce a warning out of gcc?

Jul 22 '06 #10
On 21 Jul 2006 22:42:50 -0700, "Bill Pursell" <bi**********@gmail.com>
wrote:
<snip>
I was recently playing around with some similar ideas and
had hoped that typedef'ing the void * would generate a
compiler warning, but was unable to coerce any warnings. eg
Nope. typedef in C is strictly an alias, not a distinct type.

The only way to create a distinct new type in C is as a struct or
union, or which the former is more familiar and usually more useful.
(Chris Torek likes to retroform 'struct' as a humorous acronym for
'STRange spelling for User Constructed Type'.)

<OTIn C++ enum types are also distinct, although they still silently
convert to but not from integers; and all class/struct, union, and
enum tags are automatically usable as typenames without being
explicitly declared as typedefs; but typedefs are still aliases.

And of course class/struct types already have true, builtin,
hierarchical polymorphism. If that's what you want. </>

- David.Thompson1 at worldnet.att.net
Jul 31 '06 #11
Dave Thompson <da*************@worldnet.att.netwrites:
On 21 Jul 2006 22:42:50 -0700, "Bill Pursell" <bi**********@gmail.com>
wrote:
<snip>
>I was recently playing around with some similar ideas and
had hoped that typedef'ing the void * would generate a
compiler warning, but was unable to coerce any warnings. eg

Nope. typedef in C is strictly an alias, not a distinct type.

The only way to create a distinct new type in C is as a struct or
union, or which the former is more familiar and usually more useful.
(Chris Torek likes to retroform 'struct' as a humorous acronym for
'STRange spelling for User Constructed Type'.)

<OTIn C++ enum types are also distinct, although they still silently
convert to but not from integers; and all class/struct, union, and
enum tags are automatically usable as typenames without being
explicitly declared as typedefs; but typedefs are still aliases.

And of course class/struct types already have true, builtin,
hierarchical polymorphism. If that's what you want. </>
Actually, enumerated types are distinct in C as well -- but the
enumeration constants are of type int, not of the enum type. For
example:

enum foo { zero, one, two };
enum foo *fp;
int *ip;
ip = fp; /* illegal */

For that matter, a declaration of a pointer, array, or function type
creates a distinct new type. (The language has no facility for
declaring new numeric types.) But structures are the most flexible
form of new types, since they can contain whatever else you need them
to.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #12
Keith Thompson wrote:
Dave Thompson <da*************@worldnet.att.netwrites:
>>On 21 Jul 2006 22:42:50 -0700, "Bill Pursell" <bi**********@gmail.com>
wrote:
<snip>
>>>I was recently playing around with some similar ideas and
had hoped that typedef'ing the void * would generate a
compiler warning, but was unable to coerce any warnings. eg

Nope. typedef in C is strictly an alias, not a distinct type.

The only way to create a distinct new type in C is as a struct or
union, or which the former is more familiar and usually more useful.
(Chris Torek likes to retroform 'struct' as a humorous acronym for
'STRange spelling for User Constructed Type'.)

<OTIn C++ enum types are also distinct, although they still silently
convert to but not from integers; and all class/struct, union, and
enum tags are automatically usable as typenames without being
explicitly declared as typedefs; but typedefs are still aliases.

And of course class/struct types already have true, builtin,
hierarchical polymorphism. If that's what you want. </>


Actually, enumerated types are distinct in C as well -- but the
enumeration constants are of type int, not of the enum type. For
example:

enum foo { zero, one, two };
enum foo *fp;
int *ip;
ip = fp; /* illegal */
But they are effectively a type in name only in C, by virtue of

enum foo f;

f = 42;

--
Ian Collins.
Jul 31 '06 #13
Ian Collins <ia******@hotmail.comwrites:
Keith Thompson wrote:
>Dave Thompson <da*************@worldnet.att.netwrites:
[...]
>Actually, enumerated types are distinct in C as well -- but the
enumeration constants are of type int, not of the enum type. For
example:

enum foo { zero, one, two };
enum foo *fp;
int *ip;
ip = fp; /* illegal */
But they are effectively a type in name only in C, by virtue of

enum foo f;

f = 42;
That's merely because there's an implicit conversion between int and
enum foo. C is full of implicit conversions: between void* and any
pointer-to-object type, between any two numeric types, from a null
pointer constant to any pointer type.

A *value* of type enum foo is pretty much interchangeable with any
integer type. An *object* of type enum foo is not. In that sense,
it's a distinct type.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #14
Keith Thompson wrote:
Ian Collins <ia******@hotmail.comwrites:
>>Keith Thompson wrote:
>>>Dave Thompson <da*************@worldnet.att.netwrites:

[...]
>>>Actually, enumerated types are distinct in C as well -- but the
enumeration constants are of type int, not of the enum type. For
example:

enum foo { zero, one, two };
enum foo *fp;
int *ip;
ip = fp; /* illegal */

But they are effectively a type in name only in C, by virtue of

enum foo f;

f = 42;


That's merely because there's an implicit conversion between int and
enum foo. C is full of implicit conversions: between void* and any
pointer-to-object type, between any two numeric types, from a null
pointer constant to any pointer type.

A *value* of type enum foo is pretty much interchangeable with any
integer type. An *object* of type enum foo is not. In that sense,
it's a distinct type.
Can you flesh out "An *object* of type enum foo is not" with an example?
I'm not quite sure of the context.

--
Ian Collins.
Jul 31 '06 #15
Ian Collins <ia******@hotmail.comwrites:
Keith Thompson wrote:
>A *value* of type enum foo is pretty much interchangeable with any
integer type. An *object* of type enum foo is not. In that sense,
it's a distinct type.
Can you flesh out "An *object* of type enum foo is not" with an example?
I'm not quite sure of the context.
If we have:
enum foo { zero, one, two } enum_object;
int int_object;
then &object is not compatible with &int_object. In fact,
enum_object and int_object might not have the same size.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
Jul 31 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andrew Thompson | last post by:
I would like to create a menu that uses the 'active' pseudo-class to highlight the current page, but I cannot get it to work. The URL http://www.lensescapes.com/tst/nav/1.jsp shows the attempts...
4
by: Stephen Poley | last post by:
The issue of the focus pseudo-class came up a few weeks ago, and I finally got around to trying it out (better late than never ...) The recommended order given for the pseudo-classes is link,...
8
by: Kerberos | last post by:
As you might know, IE doesn't support the :hover pseudo-class on every tag (just the "a" tag AFAIK) Is there a simple JavaScript that would solve the problem? I have images that have a light blue...
1
by: Steven T. Hatton | last post by:
ISO/IEC 14882:2003: "5.2.4 Pseudo destructor call The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class...
6
by: Fungii | last post by:
Hello, I have a stylesheet that sets p:first-letter to a certain size and colour. I was playing around with Javascript to change paragraph stylesheets using an array like this: var paras =...
1
by: rudderstick | last post by:
Hi there all, I have an interesting problem.... I work for a company that develops software for the building industry and would like to distribute one of our software products via the web. ...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
37
by: pochartrand | last post by:
Hello, Is there a way to manipulate or at least read the pseudo-class of an element ? I know the style attribute can be accessed and gives the CSS properties for a particular element....
0
by: Kevin Frey | last post by:
WebControl contains properties such as ControlStyle and Style for controlling the style of a web-control. In particular the Style property is a CssStyleCollection that simply comprises key-value...
3
by: bytehunter | last post by:
Hello, I hope this is the best place to ask my question. I was wondering if there was such a thing as a pseudo terminal in Windows. For those unfamiliar, in Linux/Unix one can create a pseudo...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.