|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
# p2 X" H) }# I' c
! J$ K/ l; a" s) J8 o" U3 z8 l p0 R8 {! S8 q& a
1 变量声明+ J9 \; c0 _0 ^7 e+ T& c
& Y, L9 |$ ~) }0 }3 @' f. N
6 Z2 e2 j0 p0 x/ |; jo Old way
8 |$ L1 l3 l5 Q, Z' a) V// Declare pointers for an Item, Form and vector of ModelObjects) w! `' v$ g U, e- }4 P
// initialize to NULL* d* {/ V, \1 R: V5 h
Item *theItem = NULL;! W# i# N: a# w5 b
Form *theForm = NULL;
# f/ [) s; d: Kvector< ModelObject *> objects;
0 @9 `/ K5 ~8 E...
' V$ x/ j; k- t// Use the Item
4 w8 r) v* R8 f- DtheForm = theItem->get_item_master_tag();6 J- J1 E( D3 H1 l% \7 H
objects = theItem->get_revision_list();
; u1 k1 ?, w, f) b+ ~5 n+ ~o New way
5 i% f! n7 }% k9 y9 j// Declare AutoPtr for an Item, Form and vector of ModelObjects
8 o8 i; n. E0 f) h* O5 A& j// all AutoPtrs are instantiated as NULL8 M! o( s' d" ]3 F* @1 X
Teamcenter::Soa::Common::AutoPtr<Item> theItem;
! l! l) _' c8 r) L! eTeamcenter::Soa::Common::AutoPtr<Form> theForm;
" m3 n0 o$ F4 U- ]* t) y& mvector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;( ?1 w' ]. j7 j% U8 T
// Use of the ModelObject variables remains unchanged: i4 y# Y) W9 T+ g9 a1 j" ]" k
theForm = theItem->get_item_master_tag();# |2 X1 V. f' z1 X8 F
objects = theItem->get_revision_list();" d# }! Z$ w/ z" C* c5 k$ P
2 t6 t! }3 F T8 F/ K4 A# \( y7 M% S( I5 K& W
2 类型转换
3 V7 C. v' f. j, V' e- Z% B& K, }* a* s; [
o Old way
) W/ M% X2 `- y4 B+ G7 c// Declare the variables
4 P7 g2 ~" D, M( [/ U$ T) gUser *user;, H: f6 p# ]' N1 V* q, s' D
ModelObject *anObj;
3 b/ a0 Y1 e9 xFolder *folder;
) d9 s& u3 v: e3 ~- t; ?user = sessionService->login(name,pass, “”,””, descrim ).user;+ {% t8 a7 h( H
// Cast to the base ModelObject class, h* L& w$ W) K/ c/ S
anObj = (ModelObject*)user;
9 N3 Z+ t0 S# \/ `& d// Cast to a speicfic sub-type, if not NULL
* m( Q& v* k2 n* H// do something with the new pointer
4 r* F$ ^. ~8 j( L' r7 B z2 G& Wuser = dynamic_cast<User*>(anObj);
m* ` D$ z5 Y1 J- M2 j- ~if(user != NULL)
' P; Z, G7 d, o{5 y, G) g3 p" d3 q P$ p
folder = user->get_home_folder();
" J6 t, A; k2 D0 x' C3 ~( W, P3 [0 y}; V5 o8 ]1 T p9 [. d0 o
o New way. N4 j/ F6 e6 n8 p
// Declare the variables
" M o5 E+ U" e+ B: WTeamcenter::Soa::Common::AutoPtr<User> user;( x1 l' M0 W! R* ~
Teamcenter::Soa::Common::AutoPtr<ModelObject> anObj;
# g- k! t2 x$ r/ R7 n$ U& ~2 mTeamcenter::Soa::Common::AutoPtr<Folder> folder;1 P( k5 v- `9 }0 \: Q
user = sessionService->login(name,pass, “”,””, descrim ).user;* n. M6 `) M. E8 q( m
// Cast to the base ModelObject class8 D+ ?, U: K8 i. c1 @! d
anObj = user.cast<ModelObject>();* W$ G5 H: Q' c. q' X4 z" A
// Put the cast to a speicfic sub-type, in a try/caTCh block
7 o. @7 {7 N7 _( U* D J// if std::bad_cast not thrown, then cast was successful O" M5 Y; A( J7 j- x0 s0 }; Q
try) T. N, z# V/ E3 f" }
{9 x+ \0 o1 S7 i$ c0 y" F7 \% ?/ ]" B' ~
user = anObj.dyn_cast< User >();: B, Q" {0 i& B
folder = user->get_home_folder();
* v4 v. l& H: O% b}
8 G8 N/ c8 O0 scatch(std::bad_cast&){}: w! }6 Y% N8 S2 s5 X& R
* K# N$ [8 V" K: g2 j
, C1 k/ P1 {6 p3 关于NULL 赋值
% [$ r2 p7 Z" m$ h0 f, S- I: W5 S' q2 M% l' ?2 F' A( C# r
o Old way" ^* u+ @4 R8 F$ W+ x
// Declare the variables* b! ?7 T' b7 A2 c
LoginResponse response;
9 j8 m; ~, N2 Z4 i3 MUser * user;0 r0 `. F# s0 R
// Call a service that returns a ModelObject (User)
4 H, S0 j" Z( j5 w1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
# T! o5 L% {1 [user = response.user;8 T3 B7 i% D3 N/ Y# D, a3 u
// Test that instnace against NULL( w& o' U) a! G7 f
if( user == NULL) @& \. m% b8 M: A6 P
{
6 [( a4 L0 t! p, |& R...
% w( a. ~9 @' E: P9 v7 S1 U, k}1 u; u3 g: [1 }3 n8 W
else; V9 T- s% Y( `! s( e* _
{2 r0 \8 n1 l0 t _( X0 ?; @
// Assign NULL to the pointer2 V! B& z- P8 r5 R( F( m- [3 R
user = NULL;
' n: N/ u& y$ _. S# F. Y}
( U3 }: v6 d& f/ |3 Lo New way
2 X+ u: ~! m# h3 ?; d9 O/ |+ B// Declare the variables
: Q/ u4 l+ V( q// The service data structures do not change, only Q2 l. f, [+ i6 @# h
// references to ModelObjects) Q! B: R9 r& B' n6 n' A& V
LoginResponse response;
6 X8 _0 Q; W3 L7 ~6 Z% x( e- ITeamcenter::Soa::Common::AutoPtr<User> user;2 G Z7 p' L/ y: K: P
// Call a service that returns a ModelObject (User): L4 P6 p' M0 ?
response = sessionService->login(name,pass, “”,””, descrim );# R3 A4 y/ z4 L! p# W
user = response.user;
/ s/ J7 _. j% z# k% r// Since we are not dealing directly with pointers,' S7 R1 Q! c- v' `! p4 t
// NULL does not make sense here, use the isNull method from the
) Q9 v: I6 D$ Y: P# J// AutoPtr template class.
4 q" ^# w! }7 z, A( A+ Bif( user.isNull())
/ c/ G% a" ~9 A9 l( s{/ T! d. Q! j7 E: R
...4 v9 k8 u5 b7 p" B4 E8 S
}
+ T1 p6 e, Z4 ~* melse( d4 T7 \- R4 J6 q
{8 y0 A, y5 Q5 c, a6 [
// Release the instance and# ?% F! O* l% M/ j) N) Z
user.release();
+ I! _6 K o4 d" l# F}( r: X2 s! n h: K6 m
( o' Y; a G; }1 w* d0 v
$ h+ F+ N) x; g7 Y |
|