|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
8 D+ |1 W) R) k! X8 B4 m' k7 R
/ v& r+ k9 h1 U$ I% T( {4 G9 @. a' \! O+ S; A' g
1 变量声明 N1 z( C& _* |) }! `1 N" d
% f' i1 \. y. j z+ e3 T: R* b8 D( K
, E" C o; C8 k" l4 O9 Y
o Old way
. {7 ~/ @! {5 I$ p// Declare pointers for an Item, Form and vector of ModelObjects
$ x% z" W: [" x3 w% P// initialize to NULL" ]. x( z6 z4 q5 E4 w
Item *theItem = NULL;
" q. N2 C, T, o3 rForm *theForm = NULL;% N- s$ A3 f" E+ ^' `' M. J
vector< ModelObject *> objects;2 b5 T; H+ L' s9 H- B8 x R) S
..., j0 s7 |4 o$ \3 G
// Use the Item
7 B3 r% o+ X( i" BtheForm = theItem->get_item_master_tag();' D( f, T/ h8 x4 i
objects = theItem->get_revision_list();7 l- p, r: D3 E* _+ x/ A7 J) M/ L
o New way! {: O, Z6 f* e$ O* n' F
// Declare AutoPtr for an Item, Form and vector of ModelObjects
( k6 `$ C4 u/ k% o: E# Y// all AutoPtrs are instantiated as NULL& o5 p/ i9 u* K2 h& p2 g, @# o& f
Teamcenter::Soa::Common::AutoPtr<Item> theItem;
# L2 s/ T4 Q, R+ g% H+ HTeamcenter::Soa::Common::AutoPtr<Form> theForm;, S% A* S( S- H( F. c r
vector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;' [- ]1 ?9 I1 F) ]3 c p
// Use of the ModelObject variables remains unchanged
# i- G# ^$ u3 ]9 l6 RtheForm = theItem->get_item_master_tag();
: s' S5 L$ {/ c. d5 iobjects = theItem->get_revision_list();! d/ P( m/ m, S7 L2 g, B5 Y! y
( Q" ~) [, J0 G0 L
( j0 C+ G) S1 \& m$ }" z2 类型转换 8 ]; H5 i' H( s5 J+ l
v7 W" v8 r- b! z
o Old way2 X7 L' f; o& Z) D6 l b
// Declare the variables
/ J! ~2 {$ W/ t* V4 Q6 y' NUser *user;
! u1 Q& U8 A1 S* U5 @( q. S: yModelObject *anObj;
# }- v4 B2 ~1 `2 b8 \8 m8 X6 CFolder *folder;
+ w! D( i5 u. u. P7 ?1 duser = sessionService->login(name,pass, “”,””, descrim ).user;: A m# j1 E: O
// Cast to the base ModelObject class3 a4 S) q/ O7 u e" L
anObj = (ModelObject*)user;7 }; G. y; U4 {* C D# J* B8 {6 D
// Cast to a speicfic sub-type, if not NULL" B" F$ l* f% J
// do something with the new pointer
+ `* G' e" J" `; `' ]2 h' \9 Q& Huser = dynamic_cast<User*>(anObj);0 {1 L4 W" O2 L9 R4 S* O- A4 z
if(user != NULL)
0 k1 G* h- r& K1 @+ _7 i{& x- a6 i4 @! Q I
folder = user->get_home_folder();
' w5 r8 e5 S: E}
, b. _0 l& R1 Q; _o New way( A$ f5 {+ }* O# Y: q4 d% J
// Declare the variables
* i! z7 j2 h4 Q* x3 l2 d& ETeamcenter::Soa::Common::AutoPtr<User> user;
1 N- n1 L7 [* m, m# NTeamcenter::Soa::Common::AutoPtr<ModelObject> anObj;* |7 `7 _1 i" k5 y; g
Teamcenter::Soa::Common::AutoPtr<Folder> folder;9 P2 {; Y2 S A5 J
user = sessionService->login(name,pass, “”,””, descrim ).user; G, y5 x; Q; v
// Cast to the base ModelObject class
4 h, w! a: F5 j' ]' N, r- p* H& UanObj = user.cast<ModelObject>();
9 l) L$ m2 y* ]* H5 X// Put the cast to a speicfic sub-type, in a try/caTCh block j8 O9 |7 X! Z) a
// if std::bad_cast not thrown, then cast was successful! C B! @9 y# p
try7 z4 z4 [# ?" a7 Y: Y) A
{ X* N% e3 m9 O0 e/ e8 E3 j
user = anObj.dyn_cast< User >();2 m5 \% G' \" Q2 t5 s S) N; n1 D
folder = user->get_home_folder();% l6 z( { w* d
}
- i! G: w. U1 ?4 S& K& q% L4 lcatch(std::bad_cast&){}
# n* W6 o% r( n9 [, y3 u4 t- X
* u4 [" I, D0 ?+ `4 o2 [
7 ]8 D1 D4 W: ?3 关于NULL 赋值
9 ^2 J4 N# A) l. @7 z) i2 b7 ^; d
' K- w. R+ r1 O* eo Old way
, F! R9 A) H7 R# D// Declare the variables
. H. d: M, `. I' G0 ~, d' r* k& wLoginResponse response;
1 z: }. |. d' Q8 Y, h) X. zUser * user;; l: s2 r. P9 O* _
// Call a service that returns a ModelObject (User)
7 X7 ?1 f. e9 p. N* \% _1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
( A1 n' Z) @1 h, N8 @6 Auser = response.user;
( w3 [# w. J. S. N" \) w2 L f// Test that instnace against NULL- d! n4 t* F% i( e Q$ S
if( user == NULL)
# V. S: |1 L# B; f$ |{# Z2 w. J! n7 K/ E5 B7 o9 C2 c) k
...
; y; P4 _) k/ w9 a% I}& H4 s; v) \: }. q( a
else
5 }+ g" N$ y( E$ ] G5 y" d{! c0 E2 p! U( V' t6 a
// Assign NULL to the pointer/ Y5 Y6 m" k5 s3 Y
user = NULL;
7 R" V& E- [* w- f}" e. j8 ~$ G: L
o New way: U4 n) C) h9 T
// Declare the variables ?9 t9 ~$ I" X) w, P- } E* N
// The service data structures do not change, only
8 E9 Q% J" x% q: d// references to ModelObjects& f% B$ l- T% |
LoginResponse response;, W) y, ~( L" M0 n. R' L
Teamcenter::Soa::Common::AutoPtr<User> user;0 F/ m; E2 x. s3 v4 {
// Call a service that returns a ModelObject (User)6 }+ l$ q; G; u J
response = sessionService->login(name,pass, “”,””, descrim );2 V1 { B/ k( |8 n
user = response.user;! e! f4 [" {/ D( ~" q8 a
// Since we are not dealing directly with pointers,
: u% ^$ B. q0 x d; \' \2 ?// NULL does not make sense here, use the isNull method from the) Y' L- z% {1 I) {1 @
// AutoPtr template class.6 u6 P; |" e2 ]* T3 s/ b( `
if( user.isNull())
5 G! f/ }4 M$ q8 L$ p1 |{; }* v0 ~( ~7 W7 S; C; e
... ` a5 ^. E+ y' s! f8 ?- t0 g
}
2 ~ f7 Z* W3 c+ C2 h/ ?else
8 x3 S( @2 e$ `9 b{+ R4 k) z/ [& [9 w' V! R
// Release the instance and
4 M9 D% e3 B% [/ }/ W; fuser.release();/ O! E R( j" [* J, z
}
, T# C T9 N* e0 |# ~7 j+ ]
1 g- b3 g1 l( W) H
0 i" }" x+ x2 e- P6 u2 \$ s, t |
|