Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / proparrhlp.hxx
blob114efda1734f198eab297f29b0dfd225a6bd9b46
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 .
20 #ifndef INCLUDED_COMPHELPER_PROPARRHLP_HXX
21 #define INCLUDED_COMPHELPER_PROPARRHLP_HXX
23 #include <comphelper/propagg.hxx>
24 #include <cppuhelper/propshlp.hxx>
25 #include <osl/mutex.hxx>
26 #include <osl/diagnose.h>
27 #include <rtl/instance.hxx>
29 namespace cppu {
30 class IPropertyArrayHelper;
33 namespace comphelper
36 template <typename TYPE> struct OPropertyArrayUsageHelperMutex
37 : public rtl::Static< ::osl::Mutex, OPropertyArrayUsageHelperMutex<TYPE> > {};
39 template <class TYPE>
40 class OPropertyArrayUsageHelper
42 protected:
43 static sal_Int32 s_nRefCount;
44 static ::cppu::IPropertyArrayHelper* s_pProps;
46 public:
47 OPropertyArrayUsageHelper();
48 virtual ~OPropertyArrayUsageHelper();
50 /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
51 class, which is created if necessary.
53 ::cppu::IPropertyArrayHelper* getArrayHelper();
55 protected:
56 /** used to implement the creation of the array helper which is shared amongst all instances of the class.
57 This method needs to be implemented in derived classes.
58 <BR>
59 The method gets called with Mutex acquired.
60 @return an pointer to the newly created array helper. Must not be NULL.
62 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
65 /** a OPropertyArrayUsageHelper which will create an OPropertyArrayAggregationHelper
67 template <class TYPE>
68 class OAggregationArrayUsageHelper: public OPropertyArrayUsageHelper<TYPE>
70 protected:
71 /** overwrite this in your derived class. initialize the two sequences with your and your aggregate's
72 properties.
73 <BR>
74 The method gets called with Mutex acquired.
75 @param _rProps out parameter to be filled with the property descriptions of your own class
76 @param _rAggregateProps out parameter to be filled with the properties of your aggregate.
78 virtual void fillProperties(
79 css::uno::Sequence< css::beans::Property >& /* [out] */ _rProps,
80 css::uno::Sequence< css::beans::Property >& /* [out] */ _rAggregateProps
81 ) const = 0;
83 /** creates an OPropertyArrayAggregationHelper filled with properties for which's initialization
84 fillProperties is called. getInfoService and getFirstAggregateId may be overwritten to determine
85 the additional parameters of the OPropertyArrayAggregationHelper.
87 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
90 template<class TYPE>
91 sal_Int32 OPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
93 template<class TYPE>
94 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper< TYPE >::s_pProps = nullptr;
96 template <class TYPE>
97 OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
99 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
100 ++s_nRefCount;
103 template <class TYPE>
104 OPropertyArrayUsageHelper<TYPE>::~OPropertyArrayUsageHelper()
106 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
107 OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
108 if (!--s_nRefCount)
110 delete s_pProps;
111 s_pProps = nullptr;
115 template <class TYPE>
116 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
118 OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
119 if (!s_pProps)
121 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
122 if (!s_pProps)
124 s_pProps = createArrayHelper();
125 OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
128 return s_pProps;
131 template <class TYPE> inline
132 ::cppu::IPropertyArrayHelper* OAggregationArrayUsageHelper<TYPE>::createArrayHelper() const
134 css::uno::Sequence< css::beans::Property > aProps;
135 css::uno::Sequence< css::beans::Property > aAggregateProps;
136 fillProperties(aProps, aAggregateProps);
137 OSL_ENSURE(aProps.getLength(), "OAggregationArrayUsageHelper::createArrayHelper : fillProperties returned nonsense !");
138 return new OPropertyArrayAggregationHelper(aProps, aAggregateProps, nullptr, DEFAULT_AGGREGATE_PROPERTY_ID);
143 #endif // INCLUDED_COMPHELPER_PROPARRHLP_HXX
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */