几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   DirectDWG (http://www.dimcax.com/hust/forumdisplay.php?f=89)
-   -   【转帖】dispatching according to an entity type (http://www.dimcax.com/hust/showthread.php?t=15740)

yang686526 2009-05-04 07:49 PM

【转帖】dispatching according to an entity type
 
dispatching according to an entity type.
dispatching according to an entity type.
hello,
i would like to understand how to call different functions on different entities. in the read example, there is the following code:
void dbdumper: umpentity(oddbobjectid id,
ostream& os)
{
oddbentityptr pent = id.safeopenobject();
....
}
i would like to know how i can call different function according to the "real" entity type of pent. is there a visitor pattern, or is there a way to check the real type of pent ?
thanks,

hi renoo,
i'm not sure what you really want to do, but if you know what kind you need you can use iskindof or simply to convert from an anonymous pointer (odrxobjectptr). in the second case an exception should be thrown - enotthatkind or something like this;
try {
oddblineptr pent = id.safeopenobject();
} catch(oderror& err) {
}
best regards
chudomir

thanks a lot for your help ! that's exactly what i wanted to do. in fact, i wanted to know is how to choose a function according to the real type of the entity.
visitor pattern was simply another way to do the same thing, allowing to group operation.
struct visitor {
virtual void visitline(line *) {};
virtual void visitcurve(curve *) {};
...
};
struct entity {
virtual void accept(visitor &);
};
struct line : public entity {
void accept(visitor & v) { v.visitline(*this); }
};
thanks !

i have seen in the reader example (odreadex), that it is also possible to call some functions according to the type of an entity using the addx function.
something like ..
oddbellipse: esc()->addx(oddbentity_dumper: esc(), m_pellipsedumper);
however, i can't understand how it works, can someone help me ?
thanks,

this example in odreadex allows you to associate an extension object with a particular class. for example, this line associates the extension object (m_p2dpolylinedumper) with the oddb2dpolyline class. the oddbentity_dumper: esc() argument indicates the parent type of the extension object.
oddb2dpolyline: esc()->addx(oddbentity_dumper: esc(), m_p2dpolylinedumper);
later in dbdumper: umpentity(), the following call allows you to retrieve the particular extension object that is associated with the current entity type by the previous addx call:
odsmartptr<oddbentity_dumper> pentdumper = pent;
the effect of this in odreadex is that you can register and easily execute a dumper object for each entity type. this technique can be used in other contexts as well, to perform any type-specific operations on entities.
[ november 22, 2002: message edited by: ]</p>


所有的时间均为北京时间。 现在的时间是 03:40 PM.