From d5dfd654863461219cb1928ef65e788c69ca17af Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Fri, 3 Jun 2011 08:55:08 +0100 Subject: tweak the odk example to be equivalent to the standard template --- .../Components/CppComponent/service1_impl.cxx | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx b/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx index 1c5e49a0..80be3361 100644 --- a/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx +++ b/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -172,24 +173,34 @@ Sequence< Type > MyService1Impl::getTypes() seq[ 2 ] = ::cppu::UnoType< Reference< ::my_module::XSomething > >::get(); return seq; } -Sequence< sal_Int8 > MyService1Impl::getImplementationId() - throw (RuntimeException) +namespace { - static Sequence< sal_Int8 > * s_pId = 0; - if (! s_pId) + // class to create an unique id + class UniqueIdInit { - // create unique id - Sequence< sal_Int8 > id( 16 ); - ::rtl_createUuid( (sal_uInt8 *)id.getArray(), 0, sal_True ); - // guard initialization with some mutex - ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() ); - if (! s_pId) + private: + ::com::sun::star::uno::Sequence< sal_Int8 > m_aSeq; + public: + UniqueIdInitIdInit() : m_aSeq(16) { - static Sequence< sal_Int8 > s_id( id ); - s_pId = &s_id; + rtl_createUuid( (sal_uInt8*)m_aSeq.getArray(), 0, sal_True ); } - } - return *s_pId; + const ::com::sun::star::uno::Sequence< sal_Int8 >& getSeq() const { return m_aSeq; } + }; + //A multi-thread safe UniqueIdInitIdInit singleton wrapper + class theService1ImplImplementationId + : public rtl::Static< UniqueIdInitIdInit, + theService1ImplImplementationId > + { + }; +} +Sequence< sal_Int8 > MyService1Impl::getImplementationId() + throw (RuntimeException) +{ + //create a singleton that generates a unique id on + //first initialization and returns the same one + //on subsequent calls. + return theService1ImplImplementationId::get().getSeq(); } // XSomething implementation -- cgit v1.2.3