tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / include / comphelper / IdPropArrayHelper.hxx
blob8b70db849ed5143817f92d4d20742b9794be6701
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #pragma once
21 #include <sal/config.h>
23 #include <mutex>
24 #include <cppuhelper/propshlp.hxx>
25 #include <cassert>
26 #include <unordered_map>
28 namespace comphelper
31 typedef std::unordered_map< sal_Int32, ::cppu::IPropertyArrayHelper* > OIdPropertyArrayMap;
32 template <class TYPE>
33 class OIdPropertyArrayUsageHelper
35 public:
36 OIdPropertyArrayUsageHelper();
37 virtual ~OIdPropertyArrayUsageHelper()
39 std::unique_lock aGuard(theMutex());
40 assert(s_nRefCount > 0 && "OIdPropertyArrayUsageHelper::~OIdPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
41 if (!--s_nRefCount)
43 // delete the element
44 for (auto const& elem : *s_pMap)
45 delete elem.second;
46 delete s_pMap;
47 s_pMap = nullptr;
51 /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
52 class, which is created if necessary.
54 ::cppu::IPropertyArrayHelper* getArrayHelper(sal_Int32 nId);
56 protected:
57 /** used to implement the creation of the array helper which is shared amongst all instances of the class.
58 This method needs to be implemented in derived classes.
59 <BR>
60 The method gets called with Mutex acquired.
61 @return a pointer to the newly created array helper. Must not be NULL.
63 virtual ::cppu::IPropertyArrayHelper* createArrayHelper(sal_Int32 nId) const = 0;
64 private:
65 static sal_Int32 s_nRefCount;
66 static OIdPropertyArrayMap* s_pMap;
67 static std::mutex& theMutex()
69 static std::mutex SINGLETON;
70 return SINGLETON;
74 template<class TYPE>
75 sal_Int32 OIdPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
77 template<class TYPE>
78 OIdPropertyArrayMap* OIdPropertyArrayUsageHelper< TYPE >::s_pMap = nullptr;
80 template <class TYPE>
81 OIdPropertyArrayUsageHelper<TYPE>::OIdPropertyArrayUsageHelper()
83 std::unique_lock aGuard(theMutex());
84 // create the map if necessary
85 if (!s_pMap)
86 s_pMap = new OIdPropertyArrayMap;
87 ++s_nRefCount;
90 template <class TYPE>
91 ::cppu::IPropertyArrayHelper* OIdPropertyArrayUsageHelper<TYPE>::getArrayHelper(sal_Int32 nId)
93 assert(s_nRefCount && "OIdPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
94 std::unique_lock aGuard(theMutex());
95 // do we have the array already?
96 auto& rEntry = (*s_pMap)[nId];
97 if (!rEntry)
99 rEntry = createArrayHelper(nId);
100 assert(rEntry && "OIdPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
102 return (*s_pMap)[nId];
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */