Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式
Teamcenter SOA开发中关于变量声明,转换等代码的新处理方式1 变量声明
o Old way
// Declare pointers for an Item, Form and vector of ModelObjects
// initialize to NULL
Item *theItem = NULL;
Form *theForm = NULL;
vector< ModelObject *> objects;
...
// Use the Item
theForm = theItem->get_item_master_tag();
objects = theItem->get_revision_list();
o New way
// Declare AutoPtr for an Item, Form and vector of ModelObjects
// all AutoPtrs are instantiated as NULL
Teamcenter::Soa::Common::AutoPtr<Item> theItem;
Teamcenter::Soa::Common::AutoPtr<Form> theForm;
vector< Teamcenter::Soa::Common::AutoPtr <ModelObject> > objects;
// Use of the ModelObject variables remains unchanged
theForm = theItem->get_item_master_tag();
objects = theItem->get_revision_list();
2 类型转换
o Old way
// Declare the variables
User *user;
ModelObject *anObj;
Folder *folder;
user = sessionService->login(name,pass, “”,””, descrim ).user;
// Cast to the base ModelObject class
anObj = (ModelObject*)user;
// Cast to a speicfic sub-type, if not NULL
// do something with the new pointer
user = dynamic_cast<User*>(anObj);
if(user != NULL)
{
folder = user->get_home_folder();
}
o New way
// Declare the variables
Teamcenter::Soa::Common::AutoPtr<User> user;
Teamcenter::Soa::Common::AutoPtr<ModelObject> anObj;
Teamcenter::Soa::Common::AutoPtr<Folder> folder;
user = sessionService->login(name,pass, “”,””, descrim ).user;
// Cast to the base ModelObject class
anObj = user.cast<ModelObject>();
// Put the cast to a speicfic sub-type, in a try/catch block
// if std::bad_cast not thrown, then cast was successful
try
{
user = anObj.dyn_cast< User >();
folder = user->get_home_folder();
}
catch(std::bad_cast&){}
3 关于NULL 赋值
o Old way
// Declare the variables
LoginResponse response;
User * user;
// Call a service that returns a ModelObject (User)
1-28 Services Guide PLM00076 Jresponse = sessionService->login(name,pass, “”,””, descrim );
user = response.user;
// Test that instnace against NULL
if( user == NULL)
{
...
}
else
{
// Assign NULL to the pointer
user = NULL;
}
o New way
// Declare the variables
// The service data structures do not change, only
// references to ModelObjects
LoginResponse response;
Teamcenter::Soa::Common::AutoPtr<User> user;
// Call a service that returns a ModelObject (User)
response = sessionService->login(name,pass, “”,””, descrim );
user = response.user;
// Since we are not dealing directly with pointers,
// NULL does not make sense here, use the isNull method from the
// AutoPtr template class.
if( user.isNull())
{
...
}
else
{
// Release the instance and
user.release();
}
页:
[1]