[Libreoffice-commits] .: odk/examples

Caolán McNamara caolan at kemper.freedesktop.org
Fri Jun 3 01:41:58 PDT 2011


 odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx |   39 ++++++----
 1 file changed, 25 insertions(+), 14 deletions(-)

New commits:
commit d5dfd654863461219cb1928ef65e788c69ca17af
Author: Caolán McNamara <caolanm at redhat.com>
Date:   Fri Jun 3 08:55:08 2011 +0100

    tweak the odk example to be equivalent to the standard template

diff --git a/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx b/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx
index 1c5e49a..80be336 100644
--- a/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx
+++ b/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx
@@ -36,6 +36,7 @@
 #include <osl/interlck.h>
 #include <osl/mutex.hxx>
 #include <rtl/uuid.h>
+#include <rtl/instance.hpp>
 #include <cppuhelper/factory.hxx>
 
 #include <com/sun/star/lang/XServiceInfo.hpp>
@@ -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


More information about the Libreoffice-commits mailing list