|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式- t' k. H, I0 j+ r5 O& A: w; e
: a% v& C. i O- z3 E; X
) K; l3 m1 U' M. Y1 变量声明1 z- w [. P+ l/ K1 v
+ ^5 i" X+ ^) C9 V1 H
) `/ d$ r3 q: L
o Old way
/ `: t) y& l# X. k# P1 M// Declare pointers for an Item, Form and vector of ModelObjects
1 W1 R" o- S& t/ T- E4 L// initialize to NULL p9 E u C- b4 e& @
Item *theItem = NULL;- m3 S X5 E! E5 o R7 W: r/ \
Form *theForm = NULL;& c5 g' U0 Z; c( k
vector< ModelObject *> objects;
) W! p+ [& ? N2 V8 A...
! Z; r: U' o! h( i( P" `6 s9 e// Use the Item
4 {( z; O/ D! ]5 q$ l8 \theForm = theItem->get_item_master_tag();
% _+ h9 N8 E% j' J, nobjects = theItem->get_revision_list();
+ m- j2 @9 ]8 ?6 `, Qo New way
9 u3 t, q% n5 o s// Declare AutoPtr for an Item, Form and vector of ModelObjects5 @4 C, x% @: V6 ~+ V
// all AutoPtrs are instantiated as NULL
) W! `6 Q: F" VTeamcenter::Soa::Common::AutoPtr<Item> theItem;4 W. _4 f( ?# b2 n+ a
Teamcenter::Soa::Common::AutoPtr<Form> theForm;
& {4 x$ K4 b5 R" n# \# \vector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;/ G- D: f, [, J' ?
// Use of the ModelObject variables remains unchanged
5 U/ y" h& b3 p; p1 J% l. {theForm = theItem->get_item_master_tag();; V% d8 ?! e$ [ `
objects = theItem->get_revision_list();2 L: [ w4 L" s2 D. |
/ J% p% t6 h! m+ _
3 ?* _0 C- h7 C/ @
2 类型转换
+ x+ K+ M3 y& |2 \5 c
: k+ r8 N3 }2 n# S; ^8 j" uo Old way& ^7 y' S& u( I( O" ]
// Declare the variables- i, }3 f3 b% m& E9 K, B6 D" Z
User *user;( H$ N# b0 v) A4 {# k
ModelObject *anObj;, i# g( s, X7 k I" j" N
Folder *folder;
' F E9 K& V8 {1 P; G" {user = sessionService->login(name,pass, “”,””, descrim ).user;3 g# b; J3 O/ Y
// Cast to the base ModelObject class
2 d5 y& q9 ?9 D) A/ j" K+ {anObj = (ModelObject*)user;4 @4 X$ D8 U F' f& g& O
// Cast to a speicfic sub-type, if not NULL' W; n* d% [6 t6 _$ N8 V! s
// do something with the new pointer
+ A. y. f& G1 huser = dynamic_cast<User*>(anObj);
6 c: o: }2 ~5 ]+ W! w4 `7 z$ o1 Q" mif(user != NULL)2 C; `. ?6 |6 }( T
{
9 T; q# B. C. W9 C; Zfolder = user->get_home_folder();
$ ?4 q1 g0 y$ p5 S1 N}
& A9 K$ Q/ i8 K. `- K" Lo New way/ F% k' C) U9 T6 u9 b
// Declare the variables
( O0 o/ m& T H4 }; Y7 sTeamcenter::Soa::Common::AutoPtr<User> user;
' W, z% s1 h2 W0 k" QTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;
: P3 t% S* S% z: Q: g6 f+ WTeamcenter::Soa::Common::AutoPtr<Folder> folder;; d+ M. B, f; h1 A8 ~& Y3 j
user = sessionService->login(name,pass, “”,””, descrim ).user;
; X8 L6 h* [/ a5 t5 Z: x// Cast to the base ModelObject class. o6 D& }7 l m2 u
anObj = user.cast<ModelObject>();
/ o6 i$ }8 T i1 t* v// Put the cast to a speicfic sub-type, in a try/caTCh block6 [6 X' c5 g* \
// if std::bad_cast not thrown, then cast was successful
# n: a o" p% |try
% O& c9 I4 j: W+ a* ?. T3 @( p{
3 l0 K$ g) R8 g; R; X2 J7 L+ _- vuser = anObj.dyn_cast< User >();: s! x1 m% P3 m. c
folder = user->get_home_folder();; c: L( D5 ?9 C3 d- i
}
1 g0 Q- g) G, o Y1 S- ^1 ]catch(std::bad_cast&){}
2 ?# y5 D' u) B( p% S! Y2 A1 V& u
! R% d& p& J% k& @
3 关于NULL 赋值
" Z+ U# A0 f+ C0 c4 n) X& I! O- f2 ~
; F3 K- c& ^5 o1 S% Zo Old way1 `$ z: v6 H v O9 T5 U% m
// Declare the variables7 M1 y" r5 q5 u6 I& i8 E
LoginResponse response;
( I# \, }' T! S0 z0 h4 v" u5 DUser * user;) F: L1 }* T' }( \- l& O$ {# X
// Call a service that returns a ModelObject (User)
& `' P- ?: r: f1 r* A- B e% ~1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );7 ~, O* D8 O5 |5 f t1 w. N
user = response.user;7 g1 p6 O, X1 Y/ ?1 a% V
// Test that instnace against NULL
" ?0 t2 D& ]- i- cif( user == NULL)
j/ q" L* w1 N3 ^+ K* g; P' @{6 x, }; G4 ]9 f7 X, X( c" S
...2 T T. P- [/ C' |' k- B K
}
* a" A- X6 K! u( @5 ielse
5 J+ r* A7 Z& \ v/ \8 G. d; E3 ^, j{
% O7 O6 V. @2 d) H5 T; I/ Y// Assign NULL to the pointer
! M5 k* u+ p/ n7 ]$ [# Q; {2 s7 U3 N& juser = NULL;
! N1 U9 | I: K- p# u& o X}9 p, a4 R5 p! P! v* z3 w
o New way
" A# G& ?3 s0 W// Declare the variables
( v3 r6 P7 t& r// The service data structures do not change, only
. }' R: h3 E& R- H& a// references to ModelObjects
. W4 G- |& H9 R; j+ `LoginResponse response;
: m, l7 x) y% p i s$ b3 l' bTeamcenter::Soa::Common::AutoPtr<User> user;
1 [- |8 D/ g+ V& O) i# o4 j) B1 s// Call a service that returns a ModelObject (User); w3 h5 F% O3 Y: X- q8 [: |
response = sessionService->login(name,pass, “”,””, descrim );
, G' i) a( F0 |( Yuser = response.user;
. }) t+ l0 q$ f4 ~5 t// Since we are not dealing directly with pointers,/ z+ _. k: b/ U) }' i
// NULL does not make sense here, use the isNull method from the
! Z( T, y; x; p4 T// AutoPtr template class.2 J5 _# M& r6 ?1 A: x* I
if( user.isNull())+ m5 ^3 N: U( i
{ j/ Q9 H- q! o; s2 @) A4 \$ q. [4 ?8 l
...
: q6 I7 E; s5 s}
, I4 s N" R! v6 Y+ _else% P- ^2 c- j9 D7 w, A N
{
4 g8 \+ @% Q; q% R& Y4 |6 ~9 c// Release the instance and& @3 e1 C7 P: ], ~ _& F
user.release();/ ^$ L1 m5 u: A! D
}7 y& \6 ]( ~; z) v
: a( A) H3 c8 h$ r* ?/ w3 T4 b8 I3 o$ ]/ Q$ u* z
|
|