伪多态性的偏移 [英] An excursion to pseudo-polymorphism

查看:58
本文介绍了伪多态性的偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个小对象系统,包括


typedef struct {

...

} text_object ;


我们有一个对象主人

中的几个小对象的容器,用于
typedef struct {

...

text_object * text;

media_object * media;

...

} object_master;


现在,我们需要一种通用/多态的方式初始化小对象

,因为初始化对所有这些都是类似的。我想使用

内联函数代替宏,但目前看不到

任何自然,干净的方式。下面提供了一个带宏宏实现
(ALLOC_OBJ)的伪实现。


将宏转换为内联函数的想法是什么?


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

返回错误;

}

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

返回错误;

}


#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)

返回ERR_OOM;


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


gen_obj-> module = my_module_find(params);

if(! gen_obj->模块){

err = ERR_MISSING_MODULE;

转到out_free_gen_obj;

}

if(my_typeof (gen_obj->模块)!= gen_obj_type){

err = ERR_WRONG_MODULE;

转到out_free_gen_obj;

}


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

if(err)

goto out_free_gen_obj;


转出;


out_free_gen_obj:

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

my_free(gen_obj);

gen_obj = NULL;


out:

/ *特别严格w / oa宏* /

objmaster-> gen_obj_field = gen_obj;

}

解决方案


gu********@gmail.com 写道:


我们有一个小对象系统


typedef struct {

.. 。

} text_objec吨;



您正在尝试模拟C ++,最好的方法是使用插件模型。

准备具有地址的结构执行init的函数,

deinit等等。然后只需通过它们打电话。


Tom




Tom St Denis写道:

gu ******** @ gmail.com 写道:


我们有一个小对象系统/>

typedef struct {

...

} text_object;




你试图模仿C ++,最好的方法是使用插件模型。

准备结构拥有执行init的函数地址,

deinit等等。然后只需通过它们打电话。


Tom



对不起,这太模糊了。在这种情况下,我无法看到函数指针如何帮助
。 ALLOC_OBJECT()是问题,因为它是通用的。否

使用它作为成员函数通过函数指针,因为没有任何对象特定的事情发生,只有标识符/类型发生变化。


你能举一个例子吗?


gu ******** @ gmail。 com 写道:


对不起,但这太模糊了。在这种情况下,我无法看到函数指针如何帮助
。 ALLOC_OBJECT()是问题,因为它是通用的。否

使用它作为成员函数通过函数指针,因为没有任何对象特定的事情发生,只有标识符/类型发生变化。


你能举一个例子吗?

http://libtomcrypt.com

整个库都是可插拔的。例如,我的HMAC消息

认证码......呃源代码[hehehe]不知道你用的是什么哈希
。不知道哈希是如何工作的。只知道它

可以调用某些函数来处理被散列的消息。


同样我的公钥代码也不知道你的bignum数学是如何完成的/>
[我支持三个不同的数学库,每个库都有自己的bignum

数据类型,初始化/取消函数等]。


等等等等。


你只是放弃太容易了。


汤姆


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

解决方案


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



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?


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


这篇关于伪多态性的偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆