|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
" f9 P' f/ G8 j3 k+ e# n: s' g1 r- d5 [ F9 J5 N, ]3 s
- W. C7 N& h- D0 b: l" b# O
1 变量声明
. I" b/ M/ D7 p0 J( J2 A; @! h b5 T6 A$ L
. b2 h- E2 e, W+ x9 @o Old way9 K) ^7 l. V$ [1 C
// Declare pointers for an Item, Form and vector of ModelObjects. @5 i! c w" A8 n
// initialize to NULL
4 R: m) ` M- d- p k% Z4 q$ iItem *theItem = NULL;
+ ^0 t9 a4 W1 H! I, R4 V) jForm *theForm = NULL;
* i2 |# a3 }4 S8 M& c0 I+ n- }vector< ModelObject *> objects;' i- u, K8 t( K% L6 g% e
...
5 Z# U% F s8 I, z2 g; w// Use the Item( J; \+ B2 x% W, U# S& O
theForm = theItem->get_item_master_tag();# I0 _$ V' e2 `5 Q
objects = theItem->get_revision_list();
" G0 |6 s: L P/ Bo New way
1 [. W+ M4 q( c& `9 s2 X// Declare AutoPtr for an Item, Form and vector of ModelObjects7 Q8 U0 v5 q: T
// all AutoPtrs are instantiated as NULL' \. C b0 M/ e4 }6 t" b
Teamcenter::Soa::Common::AutoPtr<Item> theItem;
- a0 r: G, h0 JTeamcenter::Soa::Common::AutoPtr<Form> theForm;
+ N0 H( }- B1 z/ A6 ^& F( A$ Lvector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;
& E7 `. b& F' f# l/ T3 p+ K5 T6 e// Use of the ModelObject variables remains unchanged( s4 E: J+ G P4 P
theForm = theItem->get_item_master_tag(); f; E3 J4 Y1 _, ^& W0 d, z2 g8 [1 B- a
objects = theItem->get_revision_list();2 z6 C$ Q, _ W/ f
9 J% X1 Y* U! \9 a0 N. V' h5 L2 j9 D1 O
2 类型转换
5 j* G& z8 w1 d) X0 z& e: M( ]/ _, j2 X- e5 M! h8 O# s
o Old way
y; _1 j$ }8 i- k& D4 m+ W// Declare the variables8 F6 \" U3 h A) ]! j
User *user;4 ]3 G2 L& @) R1 j) H, `5 o
ModelObject *anObj;6 c. Q! e! z0 T& Y' }( l, I
Folder *folder;% v- C, A" W2 j1 t- |) \ y* Q
user = sessionService->login(name,pass, “”,””, descrim ).user;7 Z0 Y! l( A; M$ T8 a+ x7 i0 _
// Cast to the base ModelObject class8 b6 l/ w. w8 R* |4 Z( v/ Y
anObj = (ModelObject*)user; I% |6 }2 B3 }0 w$ Z4 f
// Cast to a speicfic sub-type, if not NULL
5 ]7 A3 W. o/ a+ c// do something with the new pointer
( N) V' R) F: A! M% t5 ^0 Tuser = dynamic_cast<User*>(anObj);
0 _' o3 j0 M; H/ N; b5 k5 f/ U( n, }/ gif(user != NULL)6 [2 L6 r( u) R2 C
{
5 P3 H8 A& n0 o6 Mfolder = user->get_home_folder();
( ^2 u( S8 {$ G1 T}
, s7 p) O- C5 D" S4 Z& eo New way: b/ ?" k1 G1 F( _
// Declare the variables
. e7 f# O" ^8 n8 k; lTeamcenter::Soa::Common::AutoPtr<User> user;! u4 U% k0 E) {* M$ _
Teamcenter::Soa::Common::AutoPtr<ModelObject> anObj;
* f% q7 v- [2 `, c, LTeamcenter::Soa::Common::AutoPtr<Folder> folder;9 N& J& [. q1 f4 A9 `; _+ t. x2 r
user = sessionService->login(name,pass, “”,””, descrim ).user;
' T' E" f" W( S0 b# e* E// Cast to the base ModelObject class k- u y! W# B
anObj = user.cast<ModelObject>();
W/ T/ U; x5 `* u( K& M7 B; w// Put the cast to a speicfic sub-type, in a try/caTCh block
! }4 e" W( y! Z4 [& t// if std::bad_cast not thrown, then cast was successful
% C* {6 e" |% W. ]7 p$ V: S6 htry" y. m$ Y: }; B$ c) ?
{6 ^: P% d; E: |9 M
user = anObj.dyn_cast< User >();, \- U. W5 @7 D% F3 i
folder = user->get_home_folder();
' f0 D+ D/ G" L; h6 G. x' K}
* k+ m1 w0 G% d5 k- F2 @! lcatch(std::bad_cast&){}, a& i9 h& E* Y) n4 `0 `+ b
R( z4 J7 M6 F! `( @
- ]1 d9 I! F5 X2 Z+ c6 _+ u4 M2 u3 关于NULL 赋值- d7 d% s) C5 `; ~- f. |4 c
+ z5 s9 z" m1 \6 v% xo Old way
# B+ R" |8 \# k// Declare the variables
4 R2 K# W) p' [3 vLoginResponse response;
/ r: m' u8 n: `+ g, TUser * user;' _, [( n) x. b' N1 p. D
// Call a service that returns a ModelObject (User)4 r2 H4 d$ I7 V; V, Q# R/ ~
1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
( x! V6 y- M( Puser = response.user;+ W3 x; P# l4 p4 q# Q
// Test that instnace against NULL
7 J$ ?: y' O8 T" a% m' d! Sif( user == NULL)* Z: i! w& e! @/ ~5 M; J; i
{
. L/ a# I$ a$ P9 ]1 w...
! h, N) Y& z0 N* ]} X* O5 |- B5 [6 q) d
else
, h) H1 q) ^* c$ H! r) f{
' l; h6 b& X8 b; R// Assign NULL to the pointer
3 c& Z: _* R1 Z/ h! p5 ` Kuser = NULL;8 }1 a7 p7 ]! L2 _
}
3 A( E2 d; P n U. Mo New way
8 U* H5 T8 s3 R. k. d+ o/ `// Declare the variables
5 q5 E4 _9 Y/ p. B3 K4 T8 w& \// The service data structures do not change, only
' X y/ U& j# X1 C# V// references to ModelObjects& h% Y2 w5 @ o7 X1 W3 m; s
LoginResponse response;
1 O8 a' m* o, [3 c8 xTeamcenter::Soa::Common::AutoPtr<User> user;
\( S& D8 H7 u+ W// Call a service that returns a ModelObject (User)
% t* _$ |4 ]! P3 y2 jresponse = sessionService->login(name,pass, “”,””, descrim );! y* y: |* N0 `% A0 w$ o! P
user = response.user;
4 J. d+ }6 p4 A6 V) H* i0 n// Since we are not dealing directly with pointers,; l8 _ \4 `0 v) x
// NULL does not make sense here, use the isNull method from the
9 \! H; a& `' Z# `( N% Z8 p( y9 ]// AutoPtr template class.- y- W- s, ^- t4 p$ C& q9 t
if( user.isNull())# Q4 P' b( w n5 K( \
{/ ?7 p7 j! F; }8 W2 g% J
...4 r5 W. k. @ H8 c
}# B, d5 {; K3 ~
else
7 w! q+ h" \. M. ~( H7 ~{% B9 R$ Z) [3 e
// Release the instance and
Y) e* `9 D8 b) Guser.release();
, Z; @( E+ i9 `1 V, E T7 A6 o}
. L) G7 X1 b7 @) Q1 P. Z/ _% u u7 {* K; c% U
* u! N- { E1 k1 U5 K |
|