Difference between revisions of "Uno/Cpp/Snippet/Function always returning an appropriate object"
From Apache OpenOffice Wiki
m (Splitted categories.) |
SergeMoutou (Talk | contribs) m |
||
Line 1: | Line 1: | ||
Callee: | Callee: | ||
− | < | + | <source lang="cpp"> |
// This function is environment specialized on "c++<purpose>*". | // This function is environment specialized on "c++<purpose>*". | ||
uno::Reference<uno::XInterface> create_appropriateObject(void) { | uno::Reference<uno::XInterface> create_appropriateObject(void) { | ||
Line 33: | Line 33: | ||
return result_Obj; | return result_Obj; | ||
} | } | ||
− | </ | + | </source> |
Caller: | Caller: | ||
− | < | + | <source lang="cpp"> |
... | ... | ||
{ | { | ||
Line 46: | Line 46: | ||
} | } | ||
... | ... | ||
− | </ | + | </source><noinclude>[[Category:Cpp]][[Category:Snippet]][[Category:Uno]]</noinclude> |
Latest revision as of 17:38, 23 February 2008
Callee:
// This function is environment specialized on "c++<purpose>*". uno::Reference<uno::XInterface> create_appropriateObject(void) { cppu::FreeReference<uno::XInterface> result_Obj; // We may want to open a new scope, to ensure that "result_Obj" does // not get destructed while "c++:unsafe" is active. { // We need to remember the callers environment, to "map-out/in" // the parameters and return values properly. uno::Environment outerEnv(uno::getCurrent()); // We activate (enter) the "c++:unsafe" environment. // Note: Any other environment suiteable for "MyUnsafeObject" would work as well. cppu::EnvGuard unsafeGuard(uno::Environment(rtl::OUString(RTL_CONSTASCII_UPARAM("c++:unsafe")))); // This reference points to a "thread-unsafe" object. Reference<uno::XInterface> unsafeEnv_Obj(new MyUnsafeObject()); // We may do some activations on "unsafeEnv_Obj". unsafeEnv_Obj->doThis(); unsafeEnv_Obj->doThat(); // We assign it to "result_Obj". // Using "result_obj" is "safe" anywhere. result_Obj.set(unsafeEnv_Obj, SAL_NO_ACQUIRE); // The unsafeEnv_Obj reference gets destructed here, actually calling the "release" method in the right environment. // The unsafeGuard gets destructed here, deactivating the "c++:unsafe" environment. } return result_Obj; }
Caller:
... { // Whatever "c++<purpose>*" we enter, the result of "create_appropriateObject" will // always match. cppu::EnvGuard cppDebug_Guard(rtl::OUString(RTL_CONSTASCII_PARAM("c++:debug"))); uno::Reference obj(create_appropriateObject()); } ...