LibreOffice Module comphelper (master)  1
IdPropArrayHelper.hxx
Go to the documentation of this file.
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 <osl/mutex.hxx>
27 #include <osl/diagnose.h>
28 #include <rtl/instance.hxx>
29 #include <cppuhelper/propshlp.hxx>
30 
31 namespace comphelper
32 {
33 
34  // OIdPropertyArrayUsageHelper
35 
36  template <typename TYPE> struct OIdPropertyArrayUsageHelperMutex
37  : public rtl::Static< ::osl::Mutex, OIdPropertyArrayUsageHelperMutex<TYPE> > {};
38 
39  typedef std::map< sal_Int32, ::cppu::IPropertyArrayHelper* > OIdPropertyArrayMap;
40  template <class TYPE>
42  {
43  protected:
44  static sal_Int32 s_nRefCount;
45  static OIdPropertyArrayMap* s_pMap;
46 
47  public:
50  {
51  ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
52  OSL_ENSURE(s_nRefCount > 0, "OIdPropertyArrayUsageHelper::~OIdPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
53  if (!--s_nRefCount)
54  {
55  // delete the element
56  for (auto const& elem : *s_pMap)
57  delete elem.second;
58  delete s_pMap;
59  s_pMap = nullptr;
60  }
61  }
62 
67 
68  protected:
75  virtual ::cppu::IPropertyArrayHelper* createArrayHelper(sal_Int32 nId) const = 0;
76  };
77 
78 
79  template<class TYPE>
80  sal_Int32 OIdPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
81 
82  template<class TYPE>
83  OIdPropertyArrayMap* OIdPropertyArrayUsageHelper< TYPE >::s_pMap = nullptr;
84 
85 
86  template <class TYPE>
88  {
89  ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
90  // create the map if necessary
91  if (s_pMap == nullptr)
92  s_pMap = new OIdPropertyArrayMap;
93  ++s_nRefCount;
94  }
95 
96 
97  template <class TYPE>
99  {
100  OSL_ENSURE(s_nRefCount, "OIdPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
101  ::osl::MutexGuard aGuard(OIdPropertyArrayUsageHelperMutex<TYPE>::get());
102  // do we have the array already?
103  auto& rEntry = (*s_pMap)[nId];
104  if (!rEntry)
105  {
106  rEntry = createArrayHelper(nId);
107  OSL_ENSURE((*s_pMap)[nId], "OIdPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
108  }
109  return (*s_pMap)[nId];
110  }
111 }
112 #endif // INCLUDED_COMPHELPER_IDPROPARRAYHELPER_HXX
113 
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::map< sal_Int32,::cppu::IPropertyArrayHelper * > OIdPropertyArrayMap
virtual ::cppu::IPropertyArrayHelper * createArrayHelper(sal_Int32 nId) const =0
used to implement the creation of the array helper which is shared amongst all instances of the class...
::cppu::IPropertyArrayHelper * getArrayHelper(sal_Int32 nId)
call this in the getInfoHelper method of your derived class.