Branch data 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 COMPHELPER_IDPROPERTYARRAYUSAGEHELPER_HXX
20 : : #define COMPHELPER_IDPROPERTYARRAYUSAGEHELPER_HXX
21 : :
22 : : #include <cppuhelper/component.hxx>
23 : : #include <osl/mutex.hxx>
24 : : #include <cppuhelper/interfacecontainer.hxx>
25 : : #include <osl/diagnose.h>
26 : : #include <comphelper/stl_types.hxx>
27 : : #include <rtl/instance.hxx>
28 : : #include <cppuhelper/propshlp.hxx>
29 : :
30 : : namespace cppu { class IPropertyArrayHelper; }
31 : :
32 : : namespace comphelper
33 : : {
34 : : //************************************************************
35 : : // OIdPropertyArrayUsageHelper
36 : : //************************************************************
37 : : template <typename TYPE> struct OIdPropertyArrayUsageHelperMutex
38 : : : public rtl::Static< ::osl::Mutex, OIdPropertyArrayUsageHelperMutex<TYPE> > {};
39 : :
40 : : typedef std::map< sal_Int32, ::cppu::IPropertyArrayHelper*, std::less< sal_Int32 > > OIdPropertyArrayMap;
41 : : template <class TYPE>
42 : : class OIdPropertyArrayUsageHelper
43 : : {
44 : : protected:
45 : : static sal_Int32 s_nRefCount;
46 : : static OIdPropertyArrayMap* s_pMap;
47 : :
48 : : public:
49 : : OIdPropertyArrayUsageHelper();
50 : 6740 : virtual ~OIdPropertyArrayUsageHelper()
51 : : {
52 [ + - ][ + - ]: 6740 : ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
[ + - ][ + - ]
53 : : OSL_ENSURE(s_nRefCount > 0, "OIdPropertyArrayUsageHelper::~OIdPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
54 [ + + ][ + + ]: 6740 : if (!--s_nRefCount)
55 : : {
56 : : // delete the element
57 [ + - ][ + - ]: 1320 : for (OIdPropertyArrayMap::iterator i = s_pMap->begin(); i != s_pMap->end(); ++i)
[ + + ][ # # ]
[ + - ][ - + ]
58 [ + - ][ + - ]: 472 : delete (*i).second;
[ + - ][ # # ]
[ # # ][ # # ]
59 [ + - ][ + - ]: 848 : delete s_pMap;
60 [ + - ][ - + ]: 6740 : s_pMap = NULL;
[ + - ][ - + ]
61 : : }
62 : 6740 : }
63 : :
64 : : /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
65 : : class, which is created if neccessary.
66 : : */
67 : : ::cppu::IPropertyArrayHelper* getArrayHelper(sal_Int32 nId);
68 : :
69 : : protected:
70 : : /** used to implement the creation of the array helper which is shared amongst all instances of the class.
71 : : This method needs to be implemented in derived classes.
72 : : <BR>
73 : : The method gets called with Mutex acquired.
74 : : <BR>
75 : : as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
76 : : assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
77 : : @return an pointer to the newly created array helper. Must not be NULL.
78 : : */
79 : : virtual ::cppu::IPropertyArrayHelper* createArrayHelper(sal_Int32 nId) const = 0;
80 : : };
81 : :
82 : : //------------------------------------------------------------------
83 : : template<class TYPE>
84 : : sal_Int32 OIdPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
85 : :
86 : : template<class TYPE>
87 : : OIdPropertyArrayMap* OIdPropertyArrayUsageHelper< TYPE >::s_pMap = NULL;
88 : :
89 : : //------------------------------------------------------------------
90 : : template <class TYPE>
91 : 6752 : OIdPropertyArrayUsageHelper<TYPE>::OIdPropertyArrayUsageHelper()
92 : : {
93 [ + - ][ + - ]: 6752 : ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
[ + - ][ + - ]
94 : : // create the map if necessary
95 [ + + ][ + + ]: 6752 : if (s_pMap == NULL)
96 [ + - ][ + - ]: 850 : s_pMap = new OIdPropertyArrayMap();
[ + - ][ + - ]
97 [ + - ][ + - ]: 6752 : ++s_nRefCount;
98 : 6752 : }
99 : :
100 : : //------------------------------------------------------------------
101 : : template <class TYPE>
102 : 227922 : ::cppu::IPropertyArrayHelper* OIdPropertyArrayUsageHelper<TYPE>::getArrayHelper(sal_Int32 nId)
103 : : {
104 : : OSL_ENSURE(s_nRefCount, "OIdPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
105 [ + - ][ + - ]: 227922 : ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
[ # # ][ # # ]
106 : : // do we have the array already?
107 [ + - ][ + + ]: 227922 : if (! (*s_pMap)[nId] )
[ # # ][ # # ]
108 : : {
109 [ + - ][ + - ]: 474 : (*s_pMap)[nId] = createArrayHelper(nId);
[ # # ][ # # ]
110 : : OSL_ENSURE((*s_pMap)[nId], "OIdPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
111 : : }
112 [ + - ][ + - ]: 227922 : return (*s_pMap)[nId];
[ # # ][ # # ]
113 : : }
114 : : }
115 : : #endif // COMPHELPER_IDPROPERTYARRAYUSAGEHELPER_HXX
116 : :
117 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|