|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
7 W- P; Z1 | \/ {! y$ |( C6 E$ `$ w: h: }2 V8 j
_$ M% Q6 s5 y1 变量声明
6 c/ {: K& {( m8 f5 ^% M' d6 Y
5 M" B4 X, t1 Z6 D" \. B" U
! P: d+ E- q5 s- c% U- u8 a% y( to Old way/ o s) ~/ M6 b" [/ b
// Declare pointers for an Item, Form and vector of ModelObjects
7 C0 ~, j' h" v/ [ b6 p% x// initialize to NULL* U8 P9 I' r/ o$ x" E5 w
Item *theItem = NULL;0 c7 M; C, S$ q: v
Form *theForm = NULL;
, F. P9 y( T0 A" \' | [vector< ModelObject *> objects;% D7 e5 L" Z6 f3 g* g% c \( r# B- ^
...9 k2 _" E6 l- x! }8 j
// Use the Item. p8 V- W" Y4 H% Q. S8 P6 O/ ]
theForm = theItem->get_item_master_tag();0 v9 f: _- I+ m7 F% G% n: [
objects = theItem->get_revision_list();9 H' O" ]0 q8 N/ m# _
o New way/ O$ S4 B T( H! G; l/ J6 J! b
// Declare AutoPtr for an Item, Form and vector of ModelObjects
0 y& ` R4 n7 @9 W f% O' A// all AutoPtrs are instantiated as NULL
7 ~$ Z% F+ q( x @1 g6 RTeamcenter::Soa::Common::AutoPtr<Item> theItem;
9 Q5 k! O! h) x+ s4 J Y( iTeamcenter::Soa::Common::AutoPtr<Form> theForm;% {' J; q7 ?3 ]4 f# U4 o% z
vector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;
3 U9 {9 |2 |) A, v6 H# p1 Q2 U// Use of the ModelObject variables remains unchanged8 Z' _' v9 S5 N6 w9 X
theForm = theItem->get_item_master_tag();* y/ [1 x% j* S- F; _
objects = theItem->get_revision_list();
; M6 ^: j5 o+ S' z+ g$ E0 ?" l) M8 Y
( W; b3 F2 D7 x: {
2 类型转换
0 e$ \3 G' M4 s7 W
1 `! c: i8 D' Z; N2 ro Old way2 {8 v% }8 W0 j6 G" V
// Declare the variables, P( g/ p& m0 `4 ` Z$ s& b( k
User *user;
$ |+ _% }# y1 XModelObject *anObj;, N' V6 X/ t: v0 P# Z$ ?& H( `0 a8 U
Folder *folder;
7 r: O5 _8 ^0 U( Zuser = sessionService->login(name,pass, “”,””, descrim ).user;8 T2 r- l, d3 @/ \8 x! g
// Cast to the base ModelObject class. s& N1 d. ~& C4 j1 T, }
anObj = (ModelObject*)user;! d0 [4 K6 H4 M9 m% g
// Cast to a speicfic sub-type, if not NULL$ t7 {& d6 V% H+ `+ s) h, ^
// do something with the new pointer' S1 S8 e: U) C( G! V# l, @% }, a* o
user = dynamic_cast<User*>(anObj);
" k9 k, q; s L2 F) cif(user != NULL)
' q, L0 j0 |4 P( H; N0 K{( z. @8 `5 m6 T- ] I
folder = user->get_home_folder();
/ B" d$ q+ O0 ? ^; e}& m8 |, c. q, M J' K8 Z& S, S! j
o New way" |' t5 o9 y* l% ]7 g& M
// Declare the variables, X& o/ }+ }% g6 X$ t& F7 v" s$ @
Teamcenter::Soa::Common::AutoPtr<User> user;
8 k, z4 L# x) S* A7 K. K* UTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;$ Q7 x8 a/ u2 Z
Teamcenter::Soa::Common::AutoPtr<Folder> folder;' a1 D; m7 J. b* L1 e3 ]' C
user = sessionService->login(name,pass, “”,””, descrim ).user;
0 O) q5 p8 p$ L6 }$ H# H9 K// Cast to the base ModelObject class
+ a5 s, R5 n% a, C! }4 u; AanObj = user.cast<ModelObject>();
) S' a% L+ ]5 a. F// Put the cast to a speicfic sub-type, in a try/caTCh block: ]7 K3 x/ L- n" ]
// if std::bad_cast not thrown, then cast was successful1 Q. X+ p0 W O7 i) U
try
E$ A7 S/ a% _8 K( m0 f* a{
+ u: [' ~" _* P& E( F# h6 zuser = anObj.dyn_cast< User >();
^" [% u0 E- R+ v# |+ Efolder = user->get_home_folder();
) v+ s- |' P b, T0 F4 B3 B+ x8 |}" i! y; M+ r- _ ]) `
catch(std::bad_cast&){}
. V s% w* T9 W D: u% h# v9 I* ^# s) j& h' i- |/ Q' D' [1 N l& t: X* o
: D$ m, k1 U. |4 d, u
3 关于NULL 赋值0 G, F ?* N/ \* l
: z7 _5 ?; f, ]" y* F9 Z5 }
o Old way' M" P! ]; N/ o/ c
// Declare the variables
" v1 I$ @4 y$ `+ E( h& ~) ~9 ~1 _( `$ HLoginResponse response;
2 r' d' _; W3 AUser * user;$ u) g2 w/ O) T4 K% m& W
// Call a service that returns a ModelObject (User)
- o, w6 ~0 h: l% g# r4 {1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );( Y+ p& n6 |0 D8 E, E9 z" z+ Q
user = response.user;
, C3 I% }) p. I8 F// Test that instnace against NULL* X7 _9 k$ U8 c8 j7 D; c
if( user == NULL)& Q8 x$ V" [) ?- K; r+ } L
{
9 T3 w6 P, r+ B; z# g! d& k.... ^+ |7 K: c1 Z) A+ B
}
3 h+ r& Q' E: z$ Lelse1 [/ f% a5 i+ C F/ @4 J/ b1 [( I0 _
{
' Y9 T5 \1 B/ D// Assign NULL to the pointer5 E; U# ~/ F: I" h; f
user = NULL;
8 N0 a* E* s; y5 E! U: t}
8 q- o4 W4 Y0 n& w eo New way
! e' n4 ~, l0 h// Declare the variables, y) u+ J) \, l3 L3 \ b0 P# v
// The service data structures do not change, only* M$ Q5 G# w' [6 n, m
// references to ModelObjects3 |9 K3 \; ^% q w
LoginResponse response;
# }) P6 O7 h2 C) e5 N. V: CTeamcenter::Soa::Common::AutoPtr<User> user;7 l1 G5 D5 J1 Q8 \# }
// Call a service that returns a ModelObject (User)
: v# h5 Z1 C: T- Xresponse = sessionService->login(name,pass, “”,””, descrim );/ ]; k$ F8 h5 I2 t# j1 d# I* G$ S
user = response.user;: O+ [+ \8 ^; N: W! ~: |6 Y
// Since we are not dealing directly with pointers,. \) q, M5 ^- j3 N) x
// NULL does not make sense here, use the isNull method from the. z# w* x2 v5 _
// AutoPtr template class.% g+ T2 K6 w3 k# J" h" p
if( user.isNull())
5 w: |4 T/ I( Q1 G8 [5 i{
! H( i, f. ]% W' L. `. \...
5 k) L' ]0 E" y}- T" e/ J3 W. A" f( R+ k
else _4 B: j& `" R: N$ @8 v$ h
{
8 B8 r) `8 s% o9 J) q/ ]2 U4 C// Release the instance and
, u. P) c8 S+ }" cuser.release();, _" n! }" F3 n
}
7 L, t0 `' F7 [9 R, O9 E: _6 [ _2 Y Y- E; S( j+ \
) w+ a8 ^/ [* {) {( o0 V |
|