1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <sal/config.h>
24 #include <cppuhelper/propshlp.hxx>
26 #include <unordered_map>
31 typedef std::unordered_map
< sal_Int32
, ::cppu::IPropertyArrayHelper
* > OIdPropertyArrayMap
;
33 class OIdPropertyArrayUsageHelper
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 !");
44 for (auto const& elem
: *s_pMap
)
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
);
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.
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;
65 static sal_Int32 s_nRefCount
;
66 static OIdPropertyArrayMap
* s_pMap
;
67 static std::mutex
& theMutex()
69 static std::mutex SINGLETON
;
75 sal_Int32 OIdPropertyArrayUsageHelper
< TYPE
>::s_nRefCount
= 0;
78 OIdPropertyArrayMap
* OIdPropertyArrayUsageHelper
< TYPE
>::s_pMap
= nullptr;
81 OIdPropertyArrayUsageHelper
<TYPE
>::OIdPropertyArrayUsageHelper()
83 std::unique_lock
aGuard(theMutex());
84 // create the map if necessary
86 s_pMap
= new OIdPropertyArrayMap
;
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
];
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: */