|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式3 J3 h; h3 m- l/ I% F6 U+ |
( M7 v% k- Q* [ B* d
( W% N4 A# D; ^0 _! q: v2 ~) x& |1 变量声明6 j. P/ p; }8 H& v/ ^
( h' H* f! d2 p% m/ }' _4 H
# P, ^$ f' x" n1 Ko Old way
. Q3 K: ^* {3 C6 Z7 N. a* ^// Declare pointers for an Item, Form and vector of ModelObjects3 d9 t) ~2 L6 T7 a" V
// initialize to NULL
3 f/ n# m& r) a6 CItem *theItem = NULL;, @; q' s$ v$ Z: V) ]3 g. W9 A
Form *theForm = NULL;; ^9 l. U: R4 ]
vector< ModelObject *> objects;
" R: W# D/ S: i3 B...$ P0 A( U) l6 ^9 p
// Use the Item. N, Y( Y/ X) m; I9 Y. T
theForm = theItem->get_item_master_tag();% W, K- {; R. E+ p: l! ~7 i+ w, u
objects = theItem->get_revision_list();- m( P8 d2 j0 Y2 M/ I. G# Z5 X0 q
o New way& W3 j0 J* D' |
// Declare AutoPtr for an Item, Form and vector of ModelObjects
& `$ C: ?& W# G0 G) {+ I// all AutoPtrs are instantiated as NULL/ G' W* a, i% s) U1 j- a8 x3 N
Teamcenter::Soa::Common::AutoPtr<Item> theItem;
5 p) C2 D. @9 e" STeamcenter::Soa::Common::AutoPtr<Form> theForm;
: e* J& q% `) }* H4 O$ q/ uvector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;
/ A5 k* b* U/ N' o// Use of the ModelObject variables remains unchanged
4 ~. w1 q, I( a: H5 {: StheForm = theItem->get_item_master_tag();7 c9 l, @% F, O1 Z: k: U3 D
objects = theItem->get_revision_list();
1 K4 C9 Y( J; Q" q! H T* Y& l" _! L4 Y- @1 R* j" N
f' g/ y( ~ Y q }2 类型转换 2 ?8 X$ Z( f& L5 [
: I4 } g3 q z+ d% L/ A# Qo Old way
8 T* V; j7 V9 U// Declare the variables* B" D8 r( H' S
User *user;
: d. v) K4 }! S8 Y+ } KModelObject *anObj;
; ?, I" }" F4 e- eFolder *folder;! S/ L& m6 m& k# f: D$ q
user = sessionService->login(name,pass, “”,””, descrim ).user;/ u0 l# a" X$ }
// Cast to the base ModelObject class
7 c4 b* J: D8 j" H( VanObj = (ModelObject*)user;
9 l6 c& s! q7 I, m, j) ^1 c// Cast to a speicfic sub-type, if not NULL
8 U! c2 E! d- Q; a$ X: E! r// do something with the new pointer! B( `$ ^% m# ~
user = dynamic_cast<User*>(anObj);
3 y* P9 @1 z' h& X3 M5 \5 sif(user != NULL)
; U% u3 ~. g P p/ I) e{ s4 A% P# ]: l+ T2 k
folder = user->get_home_folder();
" w" ~+ g5 C: P0 S: }6 `& j}1 P0 ]3 J4 g) Z7 N4 o9 b
o New way5 p7 Z: ]6 u9 a2 |; n7 r
// Declare the variables
" i/ ]& \8 r1 N3 s: t1 C' a& {Teamcenter::Soa::Common::AutoPtr<User> user;% L2 X& W/ |% V
Teamcenter::Soa::Common::AutoPtr<ModelObject> anObj;3 }7 N5 ~9 q F5 ^. {0 b2 Z
Teamcenter::Soa::Common::AutoPtr<Folder> folder;: M3 w. p# q+ A6 Y
user = sessionService->login(name,pass, “”,””, descrim ).user;. i! \1 D9 t( }* g }" j
// Cast to the base ModelObject class
3 b) ?+ X2 U& c% I# _anObj = user.cast<ModelObject>();
8 i' T8 M* u: l! C// Put the cast to a speicfic sub-type, in a try/caTCh block
0 ]1 E$ S- F3 h" ]" b/ }4 Q// if std::bad_cast not thrown, then cast was successful
, K: G% ~$ _% |" y9 jtry+ b9 ?+ J8 Q. T* W, e# a: J
{$ ^" l. B5 k" h, x- U1 |7 p8 ?9 g( }
user = anObj.dyn_cast< User >();
- g+ @1 S! O. `& s8 I) ^2 Dfolder = user->get_home_folder();
T2 y0 f/ q2 ?$ j6 x* O4 Q4 O}
9 C- W: J7 Z8 T1 icatch(std::bad_cast&){}$ G( D9 Y9 G; A$ }$ j) j
9 u3 K4 t' v# H- D6 y, _
/ N% F7 J, @2 {8 }. s& Z% O9 `3 关于NULL 赋值' m% k7 q2 [% w9 l6 e2 F r9 \3 a
0 |- u9 }7 C: _. ^1 Y/ Eo Old way
, O$ x( ^! N( K' r! p& n// Declare the variables8 C( C8 d; u1 r, x+ b
LoginResponse response;
% H; r# h9 c- Z0 Z* CUser * user;
H) x) }1 Y4 E, k6 A# M+ a0 Z// Call a service that returns a ModelObject (User)4 T5 `, K% P( U# Y2 h# d; B
1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );4 @( y5 F8 N+ v* H
user = response.user;' h1 {* a8 S: y8 n; K
// Test that instnace against NULL; [- A# z! U) O c
if( user == NULL)
( K7 c" I: _/ h, ?{
* n. O3 R; q1 T- {. w; T...
8 e. G) m6 z4 Z2 A& @ h! U' @}4 R8 w) S7 L, h3 W a/ s
else+ q6 T" M" C& Z
{
q& P" {% f9 o// Assign NULL to the pointer) C$ I- x/ K* X) F2 ^% ]* I2 k% }
user = NULL;1 ]0 w, k* H1 r: _) [
}
/ K! ]$ ?& i9 K# d8 V5 S5 C- Yo New way0 y# h- ]8 ?' g( M: W
// Declare the variables/ Y$ _% P9 y5 j: _
// The service data structures do not change, only0 C% G3 [% p8 F7 s' m& S
// references to ModelObjects- i) w7 J, `6 u2 K% b( P
LoginResponse response;" C; q: l+ K1 h
Teamcenter::Soa::Common::AutoPtr<User> user;- m% T- c) }% j
// Call a service that returns a ModelObject (User)
$ ~8 u$ b4 c6 F9 H. a: u0 Cresponse = sessionService->login(name,pass, “”,””, descrim );
+ u, @1 [9 ~- p/ puser = response.user;
7 j$ } v" ?7 B9 G0 a// Since we are not dealing directly with pointers, r/ R [+ Y! ^# B: n
// NULL does not make sense here, use the isNull method from the
: I; ?4 k( }# D0 Z! n// AutoPtr template class.
9 O& @+ z6 `( H) V, w; qif( user.isNull())
3 d; `* D0 ?. D{+ w! _; [# P; N3 {$ \" W
...% l+ u6 A1 ]; H4 e, a+ ]& W, ?- e; z
}& I9 v6 b# p; D% k# I- Y
else* l! A, B5 t) u K, @/ S
{, Q8 Z/ @% ]: G7 ^' ^; r
// Release the instance and
! h1 m, j8 m, g! u/ Y# t8 Zuser.release();8 U' P. E0 `/ ^& _. i
}, ~& \+ Z; n' |1 x1 ]+ g( {
% d7 X0 e, i9 J( h
5 |, \7 x7 s" H, I |
|