|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
8 E9 H1 Z0 x9 l1 G& a" ]
& U9 n0 o- L" a5 z2 N/ R' }$ F' z9 O) l- z
1 变量声明
* v, D( g. H% ] O
) l) m9 E! `9 G! q+ G, q! f6 L+ E$ e+ v& V2 p
o Old way
- K- D g; Z; o& c7 N) s1 f; l( o ^// Declare pointers for an Item, Form and vector of ModelObjects
% ~. S% e# i8 T' Q' \. R// initialize to NULL
: t1 n' y* B1 Z, c7 Z; U1 y9 C1 cItem *theItem = NULL;9 p5 L5 P$ A! G& Z
Form *theForm = NULL;
5 T, W9 _ U. ~9 O9 x+ n$ Rvector< ModelObject *> objects;
; _( m; y6 l; z0 J! U4 E! Y...
! ?) j; U8 @" p5 R5 t- M7 L6 {- p// Use the Item* c7 ~$ W# N+ V7 [% M
theForm = theItem->get_item_master_tag();
* t6 t4 X5 ?9 nobjects = theItem->get_revision_list();
/ R+ G. S. t0 U) J9 H: q$ To New way2 {* h! y" G( U4 H! m1 i; c. k8 B
// Declare AutoPtr for an Item, Form and vector of ModelObjects# H! t, S- {: d
// all AutoPtrs are instantiated as NULL
Q, ?. s. }' c+ g; }Teamcenter::Soa::Common::AutoPtr<Item> theItem;- `( N7 q9 k, N% K. S; z& D
Teamcenter::Soa::Common::AutoPtr<Form> theForm;
) l8 j$ s; [, j8 H5 g' ~6 E# Xvector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;8 g3 C& `: }9 ?" k0 M3 G" L
// Use of the ModelObject variables remains unchanged
% W. Z# b# q) `theForm = theItem->get_item_master_tag();1 n* ^6 q9 {& z
objects = theItem->get_revision_list();
. |* T; J* ]; u$ }8 C l
: X7 p8 b- Y, ]# p2 ]/ M: B9 U5 A5 h: o4 y4 Q/ C v3 i$ t
2 类型转换 4 l7 n6 a( C: N8 ~, M
3 b" u# F9 O( m3 b: e5 yo Old way
: J3 A1 [( Q& }, z$ n( T/ k8 S// Declare the variables
3 u O4 \+ L; M0 L, G v; c6 E6 iUser *user;+ u8 |) M& }% W, {+ \
ModelObject *anObj;: S0 j: W. S# {. [* f
Folder *folder;$ g* E U0 M V* y; k6 ?
user = sessionService->login(name,pass, “”,””, descrim ).user;8 Q9 x( K8 a0 Q' v7 J" s1 f+ R
// Cast to the base ModelObject class! @* W9 v$ }, V, ^. W5 v3 c8 U, ~2 }
anObj = (ModelObject*)user;
3 F- K5 o: l9 t9 M U// Cast to a speicfic sub-type, if not NULL
# x" B' v/ ]- @: n/ i1 A; I// do something with the new pointer
7 b, p" B+ w7 Juser = dynamic_cast<User*>(anObj);( ?5 Q( B8 }) n) [
if(user != NULL)! y6 E0 ^3 e; y3 r
{* C9 x5 V; z+ {% l
folder = user->get_home_folder();3 B: [- Q! k ?
}) F2 n+ P/ Q( D6 e4 W
o New way
4 G2 |7 \( B! F2 {2 o// Declare the variables' y% v; @2 Y, i7 l
Teamcenter::Soa::Common::AutoPtr<User> user;
( i; Q( K0 r& V6 MTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;& |8 J6 A8 e4 h4 f; K6 ?
Teamcenter::Soa::Common::AutoPtr<Folder> folder;
8 }8 B! N9 e3 ~: Wuser = sessionService->login(name,pass, “”,””, descrim ).user;
: O+ J% A& s( s9 d, `) z// Cast to the base ModelObject class$ E' e3 j6 R+ B
anObj = user.cast<ModelObject>();9 k/ d2 C5 X8 M, @& E. A8 }' {
// Put the cast to a speicfic sub-type, in a try/caTCh block4 ]- W9 @' R! H6 b' H) `, p9 [
// if std::bad_cast not thrown, then cast was successful8 }+ l v+ S5 M2 z. B
try, X: r& }0 n2 U7 s
{; T$ h% Q- w7 q
user = anObj.dyn_cast< User >();
& I: ]" e$ P0 Cfolder = user->get_home_folder();, j3 W, ?3 s3 s
}" H) b1 D! |% D, m7 b3 Q
catch(std::bad_cast&){}
/ r/ }( F5 t) G% y9 ?( v# I# D0 V6 r1 V1 ~8 e! ^* Q4 \3 g
' [+ y) z8 ?7 P$ {$ i2 K/ f( C3 关于NULL 赋值9 g( v; B/ r. N. R0 z
# H7 s- q$ l4 z7 I+ x: C( O
o Old way
6 l+ |, Q% D* h! g// Declare the variables( f' {. y- j+ a( h3 t# x
LoginResponse response;2 U9 e ?" O# j1 x' x
User * user;, S: D8 d4 ]: D1 [
// Call a service that returns a ModelObject (User)
6 L* V: M) Z. L, _8 S5 S9 a( t: Q1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );1 e6 ]$ D! ~/ t: u8 ^4 z* \( f+ N
user = response.user;
' u/ l, c5 b7 {7 F! ^// Test that instnace against NULL7 o1 C& }& Q$ u8 ]
if( user == NULL)
! s/ G) k# ~, j3 `{, S( G5 a; ]9 W: j6 r9 T
...% R& W2 F# `/ u9 K' o( \, q6 m% b
}
* j5 r' p# H7 ~5 V$ H- @: a2 Helse( ~* j+ \: V, ?9 l
{
3 K& K, b& Q$ {. O: l// Assign NULL to the pointer0 ?0 f* G$ i4 L' U; x
user = NULL;
; m# j- x( s( I8 W0 t% y# v}
9 U5 |! i% h3 f2 I h0 Yo New way
2 N6 Q0 d" E1 S! g4 Q, d// Declare the variables
+ C0 z% j' x9 V. U// The service data structures do not change, only
$ i7 ? B* z; v" S \// references to ModelObjects
, ?5 D9 `7 c: p' N0 z$ c9 mLoginResponse response;
7 J6 ? {1 I: _ o/ Q! mTeamcenter::Soa::Common::AutoPtr<User> user;
1 {9 l: H8 Y& y9 }+ s8 _// Call a service that returns a ModelObject (User)
! _$ G% ]3 V" [# lresponse = sessionService->login(name,pass, “”,””, descrim );- ^3 x0 R& ]+ L& Q
user = response.user;
& a8 j% }1 ~5 j7 x' Y5 o( B% Q// Since we are not dealing directly with pointers,' q" C8 ^2 D# g( i3 x) w1 Q. P* O
// NULL does not make sense here, use the isNull method from the1 d1 ?, \1 V; }# _! M
// AutoPtr template class.1 F3 f4 s w. E5 ]& G5 P3 V. }2 O
if( user.isNull()): \! E/ Z' I3 Z I- A3 e0 l9 b
{5 Z( R: ` R/ l8 E* T# p V
...
% _; l/ `( i6 o: d# E/ o9 L}
2 V. M# K8 l5 B* ]3 q3 felse
9 W$ I5 x& a) X2 }8 F* }4 K7 ^ g{; X# _- M6 X- d. h
// Release the instance and
- S- F/ k0 R4 S) ^- W4 R, c, L muser.release();9 ~1 R8 k/ g1 K( v2 g
}
! l4 i1 {' u. I- N3 f3 L% l# i
( N; m0 c6 k! c% \1 z, }4 f5 b' R, S u4 `/ |2 G0 r+ z
|
|