Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_COMPHELPER_IDPROPARRAYHELPER_HXX
20 : #define INCLUDED_COMPHELPER_IDPROPARRAYHELPER_HXX
21 :
22 : #include <sal/config.h>
23 :
24 : #include <map>
25 :
26 : #include <cppuhelper/component.hxx>
27 : #include <osl/mutex.hxx>
28 : #include <cppuhelper/interfacecontainer.hxx>
29 : #include <osl/diagnose.h>
30 : #include <rtl/instance.hxx>
31 : #include <cppuhelper/propshlp.hxx>
32 :
33 : namespace cppu { class IPropertyArrayHelper; }
34 :
35 : namespace comphelper
36 : {
37 :
38 : // OIdPropertyArrayUsageHelper
39 :
40 : template <typename TYPE> struct OIdPropertyArrayUsageHelperMutex
41 : : public rtl::Static< ::osl::Mutex, OIdPropertyArrayUsageHelperMutex<TYPE> > {};
42 :
43 : typedef std::map< sal_Int32, ::cppu::IPropertyArrayHelper*, std::less< sal_Int32 > > OIdPropertyArrayMap;
44 : template <class TYPE>
45 : class OIdPropertyArrayUsageHelper
46 : {
47 : protected:
48 : static sal_Int32 s_nRefCount;
49 : static OIdPropertyArrayMap* s_pMap;
50 :
51 : public:
52 : OIdPropertyArrayUsageHelper();
53 7780 : virtual ~OIdPropertyArrayUsageHelper()
54 : {
55 7780 : ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
56 : OSL_ENSURE(s_nRefCount > 0, "OIdPropertyArrayUsageHelper::~OIdPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
57 7780 : if (!--s_nRefCount)
58 : {
59 : // delete the element
60 1820 : for (OIdPropertyArrayMap::iterator i = s_pMap->begin(); i != s_pMap->end(); ++i)
61 650 : delete (*i).second;
62 1170 : delete s_pMap;
63 1170 : s_pMap = NULL;
64 7780 : }
65 15560 : }
66 :
67 : /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
68 : class, which is created if necessary.
69 : */
70 : ::cppu::IPropertyArrayHelper* getArrayHelper(sal_Int32 nId);
71 :
72 : protected:
73 : /** used to implement the creation of the array helper which is shared amongst all instances of the class.
74 : This method needs to be implemented in derived classes.
75 : <BR>
76 : The method gets called with Mutex acquired.
77 : <BR>
78 : as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
79 : assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
80 : @return an pointer to the newly created array helper. Must not be NULL.
81 : */
82 : virtual ::cppu::IPropertyArrayHelper* createArrayHelper(sal_Int32 nId) const = 0;
83 : };
84 :
85 :
86 : template<class TYPE>
87 : sal_Int32 OIdPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
88 :
89 : template<class TYPE>
90 : OIdPropertyArrayMap* OIdPropertyArrayUsageHelper< TYPE >::s_pMap = NULL;
91 :
92 :
93 : template <class TYPE>
94 7830 : OIdPropertyArrayUsageHelper<TYPE>::OIdPropertyArrayUsageHelper()
95 : {
96 7830 : ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
97 : // create the map if necessary
98 7830 : if (s_pMap == NULL)
99 1188 : s_pMap = new OIdPropertyArrayMap();
100 7830 : ++s_nRefCount;
101 7830 : }
102 :
103 :
104 : template <class TYPE>
105 301732 : ::cppu::IPropertyArrayHelper* OIdPropertyArrayUsageHelper<TYPE>::getArrayHelper(sal_Int32 nId)
106 : {
107 : OSL_ENSURE(s_nRefCount, "OIdPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
108 301732 : ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
109 : // do we have the array already?
110 301732 : if (! (*s_pMap)[nId] )
111 : {
112 676 : (*s_pMap)[nId] = createArrayHelper(nId);
113 : OSL_ENSURE((*s_pMap)[nId], "OIdPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
114 : }
115 301732 : return (*s_pMap)[nId];
116 : }
117 : }
118 : #endif // INCLUDED_COMPHELPER_IDPROPARRAYHELPER_HXX
119 :
120 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|