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#pragma once
20
21#include <sal/config.h>
22
23#include <mutex>
25#include <cassert>
26#include <unordered_map>
27
28namespace comphelper
29{
30
31 typedef std::unordered_map< sal_Int32, ::cppu::IPropertyArrayHelper* > OIdPropertyArrayMap;
32 template <class TYPE>
34 {
35 public:
38 {
39 std::unique_lock aGuard(theMutex());
40 assert(s_nRefCount > 0 && "OIdPropertyArrayUsageHelper::~OIdPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
41 if (!--s_nRefCount)
42 {
43 // delete the element
44 for (auto const& elem : *s_pMap)
45 delete elem.second;
46 delete s_pMap;
47 s_pMap = nullptr;
48 }
49 }
50
55
56 protected:
63 virtual ::cppu::IPropertyArrayHelper* createArrayHelper(sal_Int32 nId) const = 0;
64 private:
65 static sal_Int32 s_nRefCount;
68 {
69 static std::mutex SINGLETON;
70 return SINGLETON;
71 }
72 };
73
74 template<class TYPE>
76
77 template<class TYPE>
79
80 template <class TYPE>
82 {
83 std::unique_lock aGuard(theMutex());
84 // create the map if necessary
85 if (!s_pMap)
86 s_pMap = new OIdPropertyArrayMap;
87 ++s_nRefCount;
88 }
89
90 template <class TYPE>
92 {
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)
98 {
99 rEntry = createArrayHelper(nId);
100 assert(rEntry && "OIdPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
101 }
102 return (*s_pMap)[nId];
103 }
104}
105
106/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::cppu::IPropertyArrayHelper * getArrayHelper(sal_Int32 nId)
call this in the getInfoHelper method of your derived class.
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...
std::unordered_map< sal_Int32, ::cppu::IPropertyArrayHelper * > OIdPropertyArrayMap
sal_Int16 nId
std::mutex mutex
Definition: random.cxx:41