|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
9 p9 e& R( @! v( Q/ K
' K6 E9 d$ {9 W! w( K( [ ~$ K( g5 B: M2 V# t
1 变量声明
: y. o1 ~/ M1 H0 [4 u4 R/ W
- @ ~. f6 M+ H$ C% D
/ D8 b& ~. O- Z- U. a* yo Old way
7 A" ] H, {' U% B5 Y// Declare pointers for an Item, Form and vector of ModelObjects
3 Z9 H3 { S y" |5 V* u+ s// initialize to NULL3 D. E5 W7 e M; U! H6 U* \
Item *theItem = NULL;
" v6 V# r; m+ o7 I" O& s* D, TForm *theForm = NULL;
% E# b" S! o5 jvector< ModelObject *> objects;
0 w# J& M: b+ ]6 e1 c! K...
2 \ r) \$ l7 w* H$ \: x- r. s4 c// Use the Item( R8 z8 {8 v: M! O/ i2 L2 r
theForm = theItem->get_item_master_tag();
( {0 a- v5 _' N$ M4 ~! \/ U* k( `objects = theItem->get_revision_list();6 b8 H( R Y3 a; M a0 A q
o New way
5 T' p; z& \ U// Declare AutoPtr for an Item, Form and vector of ModelObjects
6 r1 X5 V# G6 {5 c2 ^// all AutoPtrs are instantiated as NULL
8 e7 S. w- ~; @- Q, pTeamcenter::Soa::Common::AutoPtr<Item> theItem;
9 s# J! P# B" k5 oTeamcenter::Soa::Common::AutoPtr<Form> theForm;
! m1 H9 `; W) J! Ivector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;- H2 z0 }8 J3 Y7 T9 }' r! N7 B
// Use of the ModelObject variables remains unchanged
6 E) e; t4 W6 N0 a9 D7 xtheForm = theItem->get_item_master_tag(); O! _# u% u, H/ v' `8 K( c
objects = theItem->get_revision_list();
5 A8 G+ s6 k9 N" i# F. |
. A- X" \! u( ~$ G$ |
5 D# s! Y1 [# X. N2 类型转换 1 u/ o* [5 m& O% h7 r8 [9 _, o
8 R. f% S1 C, S6 m$ i+ H' q
o Old way
: {) M3 A. W) g/ y$ ~' w+ p8 t' E. }// Declare the variables
3 H; Y G# X! w- oUser *user;, G2 K% ^+ e" A7 b# W; U
ModelObject *anObj;4 k& N" [' V0 M( ~
Folder *folder;
/ B! J( @" R+ C# x! Nuser = sessionService->login(name,pass, “”,””, descrim ).user;
7 m1 ?( m8 m7 j: b* k% _" p# s& D0 v// Cast to the base ModelObject class
: A' N# U4 m, L8 j; e8 T9 canObj = (ModelObject*)user;
: { X; D/ d, |5 A( P3 O- _// Cast to a speicfic sub-type, if not NULL- Q4 `$ i8 b; F1 P9 Q
// do something with the new pointer
7 |# j- U4 @) b8 B* o( l# Guser = dynamic_cast<User*>(anObj);$ u$ L7 d# I0 } q% i# q
if(user != NULL)
* @; F. ~0 `$ U5 s" L{( e: L) [+ \; D' R' ?$ {8 L
folder = user->get_home_folder();& e. f+ h1 X% h/ S
}
( w: ^9 B( ~: m0 h" @ u. Fo New way
! E6 `) g/ n! ~- u* F0 \$ N// Declare the variables
% l2 _* x: D; J% pTeamcenter::Soa::Common::AutoPtr<User> user;
( [, x$ x9 b5 K0 w/ Z& n, JTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;; i( }5 U! z* d/ `/ Z
Teamcenter::Soa::Common::AutoPtr<Folder> folder;
* @6 E- t: X6 ]( }user = sessionService->login(name,pass, “”,””, descrim ).user;
- O/ T: Z9 u1 x4 E// Cast to the base ModelObject class
% Y& n" t$ R% k. SanObj = user.cast<ModelObject>();' Y$ o! l7 X# a7 b" u
// Put the cast to a speicfic sub-type, in a try/caTCh block1 {2 {7 |# }& Y# r4 O% c5 `( L- l
// if std::bad_cast not thrown, then cast was successful
2 n% x3 x% J/ k% d) ctry
) }2 D" f; z5 t- {. ]' t{5 ]9 ~/ t- g6 R, R e
user = anObj.dyn_cast< User >();
) h7 A( c, A1 ?: g( ^9 H& K) R2 tfolder = user->get_home_folder();
5 ?& X2 c* \. u) w. C# C}
* `! X% z. |6 b# z1 k- Zcatch(std::bad_cast&){}/ }' d- t5 `4 z4 r
$ I S _5 Y3 r' C& i. t) }$ ?+ v# r8 I/ m& ~
3 关于NULL 赋值. S! B F5 `3 u8 a
9 p2 U# d3 N! m2 R8 r7 ?o Old way7 `) u1 ?* p( e' v% l* [/ L
// Declare the variables
2 g0 w9 h# P6 J( hLoginResponse response;
6 E$ ?& F# ]: X6 e& KUser * user;
% ^. T) ]! J+ f0 R// Call a service that returns a ModelObject (User)$ o3 {; s# a& s: d: L: v, ?4 t
1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
( k/ D2 S e$ q$ c, j, |( k7 Quser = response.user;! F3 c+ o' |8 g2 `, H
// Test that instnace against NULL
% n, |& H( h1 T& G2 {if( user == NULL)
# W- Z( t8 G) s/ {+ r( I0 L{
5 ] h, M- q" B( j2 j; D8 i.../ `0 g7 _8 W m3 @
}
6 j6 e7 k: Q9 \" W( Selse0 y# b+ X" b" K, b$ P* P. D9 ^
{
* v$ w) D4 ?. C' n, F$ P7 t8 T// Assign NULL to the pointer
3 G8 S' o$ M* y6 c+ Auser = NULL;4 R! X' y6 @% @4 n1 C; {
}2 T E3 j+ q( m v5 \( W
o New way9 L6 g9 D! a7 Q$ ]% a; r1 p
// Declare the variables
6 R9 U4 w5 a2 y7 p// The service data structures do not change, only" u! E. o( m8 M' K
// references to ModelObjects
" A8 V7 L$ Z2 m- G9 c% _# mLoginResponse response;
! r6 Q! \8 X/ T8 D1 g/ a) S& V- `Teamcenter::Soa::Common::AutoPtr<User> user;8 @" h, ?; c" Q$ J! d
// Call a service that returns a ModelObject (User)/ i3 A% w8 S% M& p: m+ C" b* w
response = sessionService->login(name,pass, “”,””, descrim );- D+ Z* c9 e- g* v1 V
user = response.user;
9 f% i. H! i2 W0 Z// Since we are not dealing directly with pointers,) e5 l% Z6 l3 E. U: n
// NULL does not make sense here, use the isNull method from the
/ M& W. j( L8 L6 C// AutoPtr template class.2 |7 N4 l* b. \8 Z$ s/ P
if( user.isNull()); D- M, A( Z, J$ A2 d3 d, T! n+ \ Q
{
9 `" S1 f: U/ D. ^* G; T8 m...
/ L* i, E- H0 U. u" z5 O, I6 o}/ ~' x: H, S; _ r( s" e" M
else2 _# `4 L h& I$ m0 Z
{
j" E6 U X5 W |# t) E/ v// Release the instance and2 v* R" B3 n& n; u
user.release();# t8 `: `. O% p. R3 o/ n
}' g/ [3 Z" [1 }0 c) e" i
9 a3 {3 ~+ I7 w A; I) r( Y$ [) n5 m
. D3 U9 \+ z& q) I% J |
|