|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
6 q2 _4 U8 @' a: h/ I" [* w
( q' \. ?% D) d0 X5 p4 }3 N6 T5 g% t1 |
1 变量声明! I% s1 Z% v V1 o
, s( v( Z- w* w* D: r
4 E: P" r/ |8 }9 k$ |+ g0 ro Old way: v+ h+ C+ ]3 h8 {' B
// Declare pointers for an Item, Form and vector of ModelObjects0 C2 @# g8 }6 t1 M
// initialize to NULL9 V# J0 M0 Y/ Q: v
Item *theItem = NULL;
$ L1 }* k( T8 Z. C/ ?* f! BForm *theForm = NULL;7 B' n% l& Q% s' d
vector< ModelObject *> objects;) W8 B6 s* P$ D, A& W r
...
8 z" @! q2 m7 r; i, ]5 B' A// Use the Item8 @. X9 l/ {" [) M& S
theForm = theItem->get_item_master_tag();1 [ Z* V, h1 k/ v. g) {; S
objects = theItem->get_revision_list();5 P. `( K4 t6 F3 t8 C
o New way! O* Q- i- Y$ A
// Declare AutoPtr for an Item, Form and vector of ModelObjects1 w. v7 k& w2 }" w6 F7 }
// all AutoPtrs are instantiated as NULL
6 _* X9 D0 X; @1 e& yTeamcenter::Soa::Common::AutoPtr<Item> theItem;
; L$ w9 Z2 r- E1 z( a tTeamcenter::Soa::Common::AutoPtr<Form> theForm;
0 v5 d; ~& s: D, e& Ivector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;6 b% H4 v) a. o7 O' M$ J2 r* [ w) M
// Use of the ModelObject variables remains unchanged; |8 u4 Q0 E5 D3 I6 [ F
theForm = theItem->get_item_master_tag();
1 r; I' h( J2 fobjects = theItem->get_revision_list();6 l4 V8 i3 n3 L( U1 C- I. D
S4 j; Q' @' d% O
, F& [4 `/ K9 U3 V. t) F* C" D
2 类型转换
( k- v) m- N) B2 W! `4 i, Z$ N; n5 B2 q
o Old way
4 r7 J( u( Z& i1 h// Declare the variables
1 W7 e8 w) H+ q. ^$ iUser *user;
% K7 s0 Z( S1 _8 R5 [" vModelObject *anObj;- D% f. M9 ]. @* a Q2 `. U
Folder *folder;6 _% f6 H8 g% f+ H9 f
user = sessionService->login(name,pass, “”,””, descrim ).user;
, z0 m9 N- V: ]1 I// Cast to the base ModelObject class' j0 m5 T+ Q( ]+ d8 q
anObj = (ModelObject*)user;, F# d. s! V, E% M0 N
// Cast to a speicfic sub-type, if not NULL% A) |* w# s( f& A! O
// do something with the new pointer- C& E: k6 {9 w, I
user = dynamic_cast<User*>(anObj);" D9 H9 Z1 W2 l8 |# \# \- H
if(user != NULL)# \0 @$ E# t) N9 s( k
{
, H( H+ Y Q7 U5 F! ?+ Lfolder = user->get_home_folder();4 C3 Y3 f9 R8 T3 Q+ v% \6 G0 {
}
4 s- m9 L5 X+ @5 w( Q% L6 Go New way
. _( g6 D9 T) m* Q2 ^/ F// Declare the variables
; V3 i, p0 A6 D. L% |; CTeamcenter::Soa::Common::AutoPtr<User> user;
7 V% o6 S# o# q3 ZTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;
4 x7 k5 w; k! s. u* ~Teamcenter::Soa::Common::AutoPtr<Folder> folder;
7 r% F. o( S6 ^& B( C3 j/ y) huser = sessionService->login(name,pass, “”,””, descrim ).user;0 n4 P. h2 ~% H+ q' z
// Cast to the base ModelObject class0 J0 }+ V Q: b9 o( f
anObj = user.cast<ModelObject>();
6 {! v, e4 W! @" E! [" ^// Put the cast to a speicfic sub-type, in a try/caTCh block, n7 S, ?, c7 ?2 s& A! c
// if std::bad_cast not thrown, then cast was successful
7 x3 d3 U9 h. s) Qtry; m, }1 \- x: r Q/ n& ?$ z9 M
{
# _: H4 F6 w9 A& u- L7 Ouser = anObj.dyn_cast< User >();
1 h0 `% P ^) Z3 ]/ }, efolder = user->get_home_folder();" q1 F6 w& U! w h" q
}
; d( C* B0 W1 F. M2 vcatch(std::bad_cast&){}
4 h) e" b( D9 Q5 c- ]& C. @
2 F% m% p7 i g7 v( q
# l2 O) ]5 Y9 S: E7 F1 h! L3 关于NULL 赋值
* q) p7 ~* w7 J' A) J8 O9 ]( k4 q' C# c( Y: L% O9 Q8 d
o Old way+ t3 a- k8 V9 f! r. H6 ]/ Y- l
// Declare the variables. U( P9 b5 s" Y3 N/ Q
LoginResponse response;" d8 T3 s! M n4 i
User * user;+ _# S& @4 l0 Z
// Call a service that returns a ModelObject (User)
4 X x$ d& c: y1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
; s6 R) ?0 A/ F- \" K/ D, S! ?user = response.user;- p' h' q7 W: k9 o1 x! [
// Test that instnace against NULL
4 D ~ T" L/ T% rif( user == NULL)% T D: y" O2 x& W3 r E
{
! F3 l2 i7 Y) `+ e...* [9 I: H" B5 ]6 n* B; u4 ?+ J: \! M
}
$ |' ]: k0 W: \* B. |4 Celse
& |, b5 ]. {3 P5 D{
b* K: |7 @7 h& j// Assign NULL to the pointer
. d2 ?; R, s. r/ f. j" b% W9 Iuser = NULL;: I, Y3 J/ s( a4 B0 V5 L" K( p% `$ Q: U( ]
}
- [: g( o$ k( S [, c7 \o New way/ K: j$ k( y, |1 K
// Declare the variables& t _5 ]/ E- o Z& m
// The service data structures do not change, only7 D# E, a, I' \) l: ?: X' n
// references to ModelObjects
7 M* k! i( q% v1 m$ _" cLoginResponse response;
+ j& j. N; g0 M6 `& B: c' l, HTeamcenter::Soa::Common::AutoPtr<User> user;
$ Y0 y/ p# M) o8 ^: H* W// Call a service that returns a ModelObject (User)
. |0 W; U1 a9 H: ~- Yresponse = sessionService->login(name,pass, “”,””, descrim );* h; ~5 {. g# n6 z$ Q2 O3 n9 r
user = response.user;6 A% b& O! v3 p2 r m# U( o7 n
// Since we are not dealing directly with pointers,
. r! W$ E& `2 c2 o3 f" ~8 G// NULL does not make sense here, use the isNull method from the$ v. \/ `' H5 \' F
// AutoPtr template class.
: R4 L j% \- N) g# tif( user.isNull())
, f! I8 F' V6 m, Q) q{: j5 ^( n6 T) v' Z4 `8 ^! N5 J' d
...
. ]% B9 v* @8 k% X: y2 s}4 I6 q5 F6 q+ y* R% U% k9 x. K
else
# s% r6 S- x( u) v" n- }$ c8 Q, S{0 q7 g: u" C* Z! ?- m) Y
// Release the instance and
0 h* T( a! Z* f( S; j: e. R! Tuser.release();; R% J3 q5 f/ L Y s
}7 E6 i* R5 z2 H
6 S9 B5 `7 h5 O4 F
2 ~0 W) P7 a" j- l- z$ @% I |
|