|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
* u" J6 D+ x7 Q1 U+ u2 g4 H% p
4 L/ W# t0 N# x" M( \+ ?
1 c/ L6 I8 P, [! P: c1 变量声明' s. x5 T4 M' Q3 M9 J, c
* I; h2 J, n; S/ _
- p' f1 G3 J. b, p2 T7 r5 h$ g9 c! u: ]o Old way- t. x8 ^0 K8 J8 q
// Declare pointers for an Item, Form and vector of ModelObjects
$ a7 h$ W7 {3 r8 i/ M3 q// initialize to NULL) W& b. r. ]' p% |
Item *theItem = NULL;- f6 i- R, H! P5 J# C
Form *theForm = NULL;
( t* X. e5 p: evector< ModelObject *> objects;8 ^ n) r6 H4 _/ @$ e
...
, r0 [3 A Q F9 `" e) ]// Use the Item6 ]; f6 e/ N3 J1 K r/ o
theForm = theItem->get_item_master_tag();
9 ?% x$ v W7 ?% L% N% C8 Jobjects = theItem->get_revision_list();
3 y& Z2 e7 z+ ?3 s& e! h7 M3 s; Lo New way
. \1 o- V0 _' ]8 v% k8 [8 }// Declare AutoPtr for an Item, Form and vector of ModelObjects
" X+ n c0 g! Y [8 B3 q/ O: d// all AutoPtrs are instantiated as NULL- L$ f2 M5 B+ N7 I6 z
Teamcenter::Soa::Common::AutoPtr<Item> theItem;4 u; ]9 J% |3 W" p
Teamcenter::Soa::Common::AutoPtr<Form> theForm;; j1 }- J9 V$ U* S7 p* L% M$ k
vector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;4 j9 Y% P+ T5 B; U& K1 p
// Use of the ModelObject variables remains unchanged
0 ~$ t4 \) m: l8 ztheForm = theItem->get_item_master_tag();& ]9 W6 P, k, l! e- M" L9 |2 L
objects = theItem->get_revision_list();
" b- y+ q# {8 \; u; w, l' F* @) j' U$ c* J! J
5 @& p( f- P s. o% J- {2 类型转换
& ?* \* w/ Z4 ^9 K; G$ }# C) _. E3 N6 |( i9 H% i% _
o Old way/ N9 U+ ^+ H' ^* M, \7 B, O) K9 O
// Declare the variables6 C# j% X( O' M2 N1 B5 a
User *user;
U' ?6 |6 Q/ X. E. zModelObject *anObj;
m: v- J" O) ?; e! ]( u9 BFolder *folder;
' \. J0 U& ], ]1 Puser = sessionService->login(name,pass, “”,””, descrim ).user;/ B3 A/ d. w: O; {/ m
// Cast to the base ModelObject class$ a; \0 r8 r) v) }7 }" A
anObj = (ModelObject*)user;
8 j+ L, w- }! v+ p, I" B& n0 P// Cast to a speicfic sub-type, if not NULL9 K2 j0 |* i+ s m) Q; q v. x! Y! Y
// do something with the new pointer8 f9 y1 x! @) \( _+ i5 w" f. d6 a
user = dynamic_cast<User*>(anObj);2 t. ~0 n( h6 p' {- Q" a2 H, P
if(user != NULL)
: ?! Y% Q7 c% r3 ?+ Q{
' \1 C- @7 x( H! ?5 _folder = user->get_home_folder();
8 R, |$ x! X! I4 t. N+ U}6 e+ c6 N$ a3 {. t# q" j* I1 ^' N" g2 D
o New way( z; q. H. C# T- G
// Declare the variables l* m$ [( R, U
Teamcenter::Soa::Common::AutoPtr<User> user;" ^7 d% O5 k. G6 s
Teamcenter::Soa::Common::AutoPtr<ModelObject> anObj;6 P9 `: f. \: l. I' A7 H8 q
Teamcenter::Soa::Common::AutoPtr<Folder> folder;+ f1 b& K3 n _9 l3 w
user = sessionService->login(name,pass, “”,””, descrim ).user;/ T' |9 O! R- B# L) E0 J
// Cast to the base ModelObject class, @8 T, C8 _1 D- V, L
anObj = user.cast<ModelObject>();
0 n- ^7 @+ {% h* @, H// Put the cast to a speicfic sub-type, in a try/caTCh block3 V# b. e; A4 V4 F
// if std::bad_cast not thrown, then cast was successful
/ v+ Y" D) r9 g' Etry
- _+ B4 z" I! M& b2 @9 z{
- E/ Z; n( g% ?: euser = anObj.dyn_cast< User >();2 S3 g0 y0 t2 {. J
folder = user->get_home_folder();2 H% ^8 o6 e2 S& d
}
0 e. q8 m3 `, C" pcatch(std::bad_cast&){}
) E$ @# }0 P3 G' c; c7 m! N9 O. Z
0 X7 B- N% m" B% k
3 关于NULL 赋值% \3 W7 @4 L+ g& `, v# ^4 i0 w
) Z+ j$ {. V# ]) \
o Old way- Q5 X% k k+ g2 ?4 \, F
// Declare the variables
, _" o' Q7 q. L5 VLoginResponse response;5 p* J$ Q' }1 T3 C* `/ g, @2 w) d
User * user;
1 Q/ j' D( g) w F// Call a service that returns a ModelObject (User)# `* |' e8 o: v2 w) x
1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
( N% g0 J/ {3 u4 G4 f& K% \1 ouser = response.user;6 ^3 D$ H$ ]$ a- ~
// Test that instnace against NULL
& B. n4 ~+ \- w- Y. c1 \if( user == NULL)# a3 h" ^% |$ T- b# t! s
{
+ ~! Q0 X$ c" Y. r; P3 @; ^! S...6 u/ h$ h: [! j5 H! A* H
}
" {$ i, g* l3 K* Eelse) l* }/ M) Y" d% I
{/ _' ^! l1 V( W+ @3 ?
// Assign NULL to the pointer
0 Q; C! F/ q" Q; {user = NULL;3 s0 |: H6 w, T* `* u( c/ k
}' o( ^+ i& b7 g* `4 S7 B. d
o New way
5 G* I# P( ^7 _2 ?: _5 U2 ^// Declare the variables1 d a% N" v h" b3 G
// The service data structures do not change, only0 z) V+ V! z9 d5 w( u! f
// references to ModelObjects
M' t0 |9 o6 u% o2 `LoginResponse response;% D- M& i8 j4 U2 X/ I
Teamcenter::Soa::Common::AutoPtr<User> user;% j$ f: h/ l) o% R3 v/ [6 \$ R
// Call a service that returns a ModelObject (User)! g \$ T ?/ S( B3 a. d) U
response = sessionService->login(name,pass, “”,””, descrim );
) H; P; T2 k1 ^user = response.user;( _" l& y& t+ \7 M" T% g
// Since we are not dealing directly with pointers,4 {" d: E3 t9 c7 e6 f1 t9 l" t
// NULL does not make sense here, use the isNull method from the
$ [( q, x3 Z- v& [' C// AutoPtr template class.
9 X* j+ a' n; o, s- i9 |8 Bif( user.isNull())
9 X: r- Y5 v# v* t6 B, a) t1 Y H) x{
5 \* r$ B5 C8 i) B% v; y, [...
+ X* s; @: ?; L0 W& @}
$ ]1 _6 c/ N+ H2 [% V Relse( D; ?! ~8 ]! C) _) p# _
{0 v) P! X {* C) w
// Release the instance and( n9 C% @+ Y& w- t5 b/ ]
user.release();
# g9 _+ Q. a9 g}% V& r4 S! g0 C2 }) S
& O* ?/ @! L5 T6 p; @
$ Y2 f4 a! x8 e, r |
|