|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
5 T7 W5 |$ Q! h- l. R5 j
: Q/ I! W/ u! t/ t( ?4 l3 i/ q/ l6 |. j: L
1 变量声明
* B4 a2 }8 |& N6 v! ^, j9 t8 O/ d9 y' v) Q
0 A2 B, t' Y+ y" K1 Po Old way
1 o' P6 J- x) z$ ^1 E% Y6 B$ {0 h// Declare pointers for an Item, Form and vector of ModelObjects# ]# O' h& c% B3 Z7 x9 P0 c5 a
// initialize to NULL
# u W* |6 @4 W6 c. oItem *theItem = NULL;
# m6 M( u, }7 J0 a+ n( WForm *theForm = NULL;
r4 H- s2 S: [* F8 \* qvector< ModelObject *> objects;
+ q7 n9 q0 }& z% D0 ]! C...* N& [3 E9 ~3 P- _7 ]: O
// Use the Item1 U0 J0 U- w* O4 g1 T$ b! n- }
theForm = theItem->get_item_master_tag();
" A, V' y1 N- Z1 Nobjects = theItem->get_revision_list();/ I/ S- R4 i6 N6 ?
o New way
1 Y' l9 u- [7 j, O s4 p// Declare AutoPtr for an Item, Form and vector of ModelObjects
6 t9 [. |# R3 Q/ a. Y9 M3 J// all AutoPtrs are instantiated as NULL
! b" d( ?9 b6 ^- FTeamcenter::Soa::Common::AutoPtr<Item> theItem;
3 w. t( N; x4 R4 G2 _3 lTeamcenter::Soa::Common::AutoPtr<Form> theForm;
& O8 S( a( B, @: M, g5 svector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;
J; b: _( y0 H- C// Use of the ModelObject variables remains unchanged1 E9 M6 W9 I4 G# V% U) t% V. {
theForm = theItem->get_item_master_tag();7 f- ]+ _) K' R
objects = theItem->get_revision_list();
) M9 Y, Z v" p; v$ V. V9 h |
7 j) f- v% i: k) v6 I1 B) N6 ]; s& _" z
2 类型转换
) U2 z/ T- u" v( Z7 Q
. e7 k- [3 D! o) zo Old way
* R/ ~) L T$ X% s( G// Declare the variables
7 s1 u# x8 K0 E) J, @User *user;
. ]' F- {- |' x qModelObject *anObj;# h/ k$ W$ @' v J' ~" j2 _
Folder *folder;
F. J7 n( m2 r5 F d. Q xuser = sessionService->login(name,pass, “”,””, descrim ).user;8 h) I7 E; L R- v1 x+ w5 N% p
// Cast to the base ModelObject class
" |0 J% {* J1 Q- s- U" X0 u! wanObj = (ModelObject*)user;
) W0 s# c% R& D0 _1 \5 C. C W// Cast to a speicfic sub-type, if not NULL
6 I3 G- Y0 K: Q( i, j$ M- D7 g// do something with the new pointer
0 t- L* h& Y0 m8 t4 Nuser = dynamic_cast<User*>(anObj);
6 |- j' x. {1 B8 f2 D2 R+ yif(user != NULL)8 k, u* V: u/ r7 U
{
4 v- f3 }8 g- u* E* i: Y0 wfolder = user->get_home_folder();, u$ F6 s4 P3 M
}: K0 o' K" U" j4 M) D8 b
o New way7 S, F$ b( p/ @+ a6 o% g
// Declare the variables
1 ~8 {) X8 L% S" E# HTeamcenter::Soa::Common::AutoPtr<User> user;
2 w2 p) o' I9 N) @! a3 P- XTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;) Q6 h: Z/ q) M7 y
Teamcenter::Soa::Common::AutoPtr<Folder> folder;7 S2 ~- i: s. D5 B1 P
user = sessionService->login(name,pass, “”,””, descrim ).user;
2 [5 L2 l; t( }; H2 E5 o// Cast to the base ModelObject class# F4 D- b6 \( k9 R$ @7 B' K
anObj = user.cast<ModelObject>();
! N: S2 V9 v8 c% q( g; W// Put the cast to a speicfic sub-type, in a try/caTCh block0 t7 s0 q5 r, G3 V! [2 |% M
// if std::bad_cast not thrown, then cast was successful" ]& b- o! Y0 G% \ W4 C
try
/ Q8 Q- w8 A6 P9 m{+ `2 W5 s% K0 \* `/ k6 e$ S
user = anObj.dyn_cast< User >();) H) e% J A h: T' q
folder = user->get_home_folder();
- `+ n8 c1 D. X$ ?4 u, L}1 v$ E# y& M1 A- h. F
catch(std::bad_cast&){}
; s& k# G2 V1 |
- u& @9 I" f# B+ Y4 A
) q+ [4 Q, x8 s5 a- q3 关于NULL 赋值
- t: g% G l! c: I( k# _% K
, y- a' D8 _ w9 R. V- L& \1 Oo Old way
9 l4 |- l0 @5 `* Y" e* T$ M// Declare the variables
, c# J. a' F0 V$ ?6 {LoginResponse response;
' j, |% B; Q" }User * user;/ `3 _9 D) n% D9 P z
// Call a service that returns a ModelObject (User)4 M, @$ z! I/ r8 j# H' I: m
1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );' F) D3 G: g7 {' l/ E8 X
user = response.user;. o7 X) g; _% P4 V R6 T
// Test that instnace against NULL
T0 c- I: B" j3 @" g! T, Rif( user == NULL)
* p3 q" @ p9 r" N9 g{4 a1 } i' c. } X+ {5 ^) m5 m
...
, m0 t# }6 e! j$ {+ u& |}
/ e- S% u+ T7 ?% i; C. l Velse0 ]: e3 }( A$ i5 N F0 @. u, |% @
{$ R8 N/ F& Z* F4 a5 ?
// Assign NULL to the pointer
% W6 ~: ~5 {, P Vuser = NULL;; H, `/ d5 A/ @ d. \" x( d, O
}
" _' j4 n' {% W- @+ v6 }o New way, N+ Y& i+ i! B( f B8 j
// Declare the variables" s c1 F8 q$ Q$ k
// The service data structures do not change, only' ^' e+ j3 ~2 ^6 @* B" y1 [
// references to ModelObjects! Y/ y( d; |0 M* z
LoginResponse response;
" H$ s8 V' E. E1 t8 rTeamcenter::Soa::Common::AutoPtr<User> user;# A' K P# v! |. }+ w9 U
// Call a service that returns a ModelObject (User)
# l, v) t8 h4 wresponse = sessionService->login(name,pass, “”,””, descrim );
0 L9 ?9 c H. S- H/ {9 t1 n" Y% uuser = response.user;
" A; k$ ^1 j8 t4 m7 q1 v9 z8 ]) D// Since we are not dealing directly with pointers,: \2 h" A) W4 e
// NULL does not make sense here, use the isNull method from the2 g# x8 M, j' O; ^
// AutoPtr template class.# l, Q7 Y" f% S+ D; g+ h5 W
if( user.isNull())
9 j1 L/ \6 Z- j* ?# U$ j- c{, z6 }% `( `& q; P
...
8 Y6 J2 c6 i. A) k. H; G( V4 L/ i}5 @ j- O/ c1 ]3 ], I- P
else
& g$ R6 T: L' l# d' z{6 R$ o/ I: v: \; D' A, \
// Release the instance and
7 h. U3 F3 ?8 { d2 X: H7 _3 ]$ T1 Uuser.release();
. f& W1 b3 H5 Y, Q}
( T" _% E) z4 t7 P3 a7 Y/ @" h u' s) n/ B0 ~' O5 J& }0 f# N
# N0 l8 N$ r4 H& H5 T8 K |
|