|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
$ `0 F% Q7 w7 O& l$ ?) D* e' h9 h1 e# ]& c4 F7 y6 i3 ^
7 Z" q9 i9 ?: }/ q; h
1 变量声明
z$ f! I8 e) @4 h
* h: V4 H, |) `$ v9 w2 p0 c% e O& t0 d6 `
o Old way& m) G9 R' u5 @1 s% D5 ~1 e$ I1 @
// Declare pointers for an Item, Form and vector of ModelObjects
" P1 `$ B; x0 l/ P$ j// initialize to NULL
5 f+ S7 T' P( f" v+ V9 kItem *theItem = NULL;
& B& p# q- q4 G/ f6 fForm *theForm = NULL;) t- T! E; k3 d" e' t! u
vector< ModelObject *> objects;
* c# [- [. c: j: F...
( b. `' [0 Y4 z4 O Y8 y# w. V// Use the Item& E; }: O: x6 A# }1 p
theForm = theItem->get_item_master_tag();1 q9 {7 g; [% d3 e1 ^% x! T1 |
objects = theItem->get_revision_list();5 c, g/ ?6 a; f. P# {7 l% @
o New way
& }2 n# Y4 d8 |$ `# h9 m" [" u// Declare AutoPtr for an Item, Form and vector of ModelObjects" |7 l" G/ V9 ?( c
// all AutoPtrs are instantiated as NULL I! D1 U3 m' K7 {- Y8 E) v1 x0 M
Teamcenter::Soa::Common::AutoPtr<Item> theItem;
2 P6 b2 K, r9 A3 c& FTeamcenter::Soa::Common::AutoPtr<Form> theForm;( s, H& n. n/ w4 d C% L
vector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;" O4 j2 J- V* R: p, G
// Use of the ModelObject variables remains unchanged
, a: X* H" k. ItheForm = theItem->get_item_master_tag();
& a1 _* @# a2 h6 {- G, d+ c3 Uobjects = theItem->get_revision_list();
0 d2 I8 w4 ^& j" I" F* }! C+ \; g$ w
/ _4 y0 |, M3 N2 类型转换 / Q' l% G7 L+ F5 y. g
" m; H, }9 K$ q$ i/ T/ F) Ao Old way; V, O' V. a* g
// Declare the variables
( K8 ]9 I8 C/ g/ O. i2 {User *user;
U& B4 S' m( W4 D! u5 X6 NModelObject *anObj;
$ y' b9 v. V; [- m1 y4 KFolder *folder;
' x) G% b3 D8 vuser = sessionService->login(name,pass, “”,””, descrim ).user;2 Q' v: N+ }. ^- _. e% Y; D
// Cast to the base ModelObject class; q9 T, {7 }0 A) ?( x+ H
anObj = (ModelObject*)user;
! M1 n6 W1 Y b' c+ y5 A* U// Cast to a speicfic sub-type, if not NULL
" _6 D5 n- U! B' x3 M// do something with the new pointer
$ F1 \) z/ h! V$ F) C. T) ?" Zuser = dynamic_cast<User*>(anObj);
5 G. s" x! T3 |5 T- c6 Qif(user != NULL)
0 I' X* D0 w: X{
8 k$ m& d/ ~4 z% Z2 Y! lfolder = user->get_home_folder();
. [. P, i: C" z4 L: H. G}
8 b( r0 |/ s1 t. \' V ^o New way
1 p$ t; z* N; u( U' b) `2 s9 T// Declare the variables
! D$ \7 l5 {) V1 ^Teamcenter::Soa::Common::AutoPtr<User> user;
' @% u: O; H' j: i. [6 `Teamcenter::Soa::Common::AutoPtr<ModelObject> anObj;% H. M0 M* F% d. J' ]/ A7 A
Teamcenter::Soa::Common::AutoPtr<Folder> folder;+ l& q: G/ {: M0 h% k
user = sessionService->login(name,pass, “”,””, descrim ).user;5 Z T4 d. v" P8 Z2 K* U& h9 q
// Cast to the base ModelObject class' L& n) S V3 a& c9 x( Q, T! q
anObj = user.cast<ModelObject>();# ]1 h0 C& t& b3 |9 i% t
// Put the cast to a speicfic sub-type, in a try/caTCh block& v- Q6 _8 g1 A1 L" G
// if std::bad_cast not thrown, then cast was successful( Y @$ E) t* f- k1 ]
try k6 h; @$ {" z1 n9 w2 `( M
{
+ n+ U o. g' ~" e3 C+ h" K+ B2 ~user = anObj.dyn_cast< User >();
! [ t. J( @. a# `7 v. @- J" t5 `folder = user->get_home_folder();9 k! q! V" r6 q. N
}) K, q/ L2 Q: n4 a3 p2 ~
catch(std::bad_cast&){}" v6 f2 @- o; g1 E" L( x! |7 C
* ?8 ]: j) G P% v- m9 \! ^8 f
! a; {$ J- s* p6 X- _3 关于NULL 赋值
6 S8 E/ ]: }9 q8 x& E. C8 z* u0 `
: X/ x" u9 |( Z+ e5 lo Old way9 C$ J* ~' J' f
// Declare the variables
4 W6 I5 U* _. f# i# e$ L5 }LoginResponse response;) U: K2 y7 Y0 B! X f& J
User * user;
- Q# f V4 x$ E- W// Call a service that returns a ModelObject (User)
+ c2 d% R; p6 w( Z0 ^1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
# }+ A; r. A3 x1 d! G2 E0 quser = response.user;- L' Y' d4 H% j& i
// Test that instnace against NULL
& m- f1 w5 Y% v; u( n9 ?if( user == NULL); V$ x* f1 P% z0 w/ R, C
{
+ j6 g6 Y. J' f0 _1 N9 f..." M0 ^! X( I% f5 `
}
+ L+ D1 D+ ]/ jelse' {" O- o4 R! Y m
{% o9 j* a- P9 d% K
// Assign NULL to the pointer
2 h; M3 `$ I2 B, h2 [user = NULL;* p/ `; ~) b- O, s
}
" q1 q6 e" R' i6 d2 x' m9 Vo New way" T, M" a; |' i( ~
// Declare the variables
. S" W" b# p1 h( ?// The service data structures do not change, only2 U" H8 R2 p: }' d+ n- N1 @7 f: v
// references to ModelObjects/ S6 r) }2 K6 t; Z! E3 O
LoginResponse response;
7 l2 E+ J" a$ q) {. ~: A8 f: cTeamcenter::Soa::Common::AutoPtr<User> user;
1 G% ?% f9 F8 l5 m// Call a service that returns a ModelObject (User)
& I, K8 |* P9 G' Z: G6 a mresponse = sessionService->login(name,pass, “”,””, descrim );# y2 ^0 ]! U! J/ |; R
user = response.user;
8 z- A5 H. F$ ^2 ^, ]( \5 T( ^// Since we are not dealing directly with pointers,
# \4 f( ]& Q, J! ?// NULL does not make sense here, use the isNull method from the# d/ O4 J5 i) t" H; V8 H$ M
// AutoPtr template class.3 O4 z2 U- j- k8 I- }+ T: E; S
if( user.isNull())
0 w- g1 ^) T( D7 v7 q5 N3 P{
4 [$ h" J- N6 {...: e1 f2 s- p- ]: }
}
" q6 _4 s2 k. C, Y o3 welse
) z/ g" A2 y: j! A, }3 ^ ]{% \; s- R* |8 |+ G$ I/ t4 _
// Release the instance and- [: N4 k/ T" x l# v1 O* o ^) O4 x8 e
user.release();
' Y5 X5 ~1 T$ ?}9 n/ r1 b9 O8 z+ f& ~1 i. b% c6 `
3 [* D3 J6 }3 z! n; d
. ]: `. j: r6 U6 t. f* r4 { |
|