update credits
[LibreOffice.git] / include / comphelper / proparrhlp.hxx
blob840213e88427af6c28e58012a7bd1f08f294bf31
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 _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
21 #define _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
23 #include <comphelper/stl_types.hxx>
24 #include <comphelper/propagg.hxx>
25 #include <cppuhelper/propshlp.hxx>
26 #include <osl/mutex.hxx>
27 #include <osl/diagnose.h>
28 #include <rtl/instance.hxx>
30 namespace cppu {
31 class IPropertyArrayHelper;
34 //... namespace comphelper ................................................
35 namespace comphelper
37 //.........................................................................
39 namespace staruno = ::com::sun::star::uno;
40 namespace starbeans = ::com::sun::star::beans;
43 //==================================================================
45 template <typename TYPE> struct OPropertyArrayUsageHelperMutex
46 : public rtl::Static< ::osl::Mutex, OPropertyArrayUsageHelperMutex<TYPE> > {};
49 template <class TYPE>
50 class OPropertyArrayUsageHelper
52 protected:
53 static sal_Int32 s_nRefCount;
54 static ::cppu::IPropertyArrayHelper* s_pProps;
56 public:
57 OPropertyArrayUsageHelper();
58 virtual ~OPropertyArrayUsageHelper()
59 { // ARGHHHHHHH ..... would like to implement this after the class
60 // definition (as we do with all other methods) but SUNPRO 5 compiler
61 // (linker) doesn't like this
62 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
63 OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
64 if (!--s_nRefCount)
66 delete s_pProps;
67 s_pProps = NULL;
71 /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
72 class, which is created if neccessary.
74 ::cppu::IPropertyArrayHelper* getArrayHelper();
76 protected:
77 /** used to implement the creation of the array helper which is shared amongst all instances of the class.
78 This method needs to be implemented in derived classes.
79 <BR>
80 The method gets called with Mutex acquired.
81 <BR>
82 as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
83 assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
84 @return an pointer to the newly created array helper. Must not be NULL.
86 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
89 //==================================================================
90 /** a OPropertyArrayUsageHelper which will create an OPropertyArrayAggregationHelper
92 template <class TYPE>
93 class OAggregationArrayUsageHelper: public OPropertyArrayUsageHelper<TYPE>
95 protected:
96 /** overwrite this in your derived class. initialize the two sequences with your and your aggregate's
97 properties.
98 <BR>
99 The method gets called with Mutex acquired.
100 @param _rProps out parameter to be filled with the property descriptions of your own class
101 @param _rAggregateProps out parameter to be filled with the properties of your aggregate.
103 virtual void fillProperties(
104 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rProps,
105 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rAggregateProps
106 ) const = 0;
108 /** creates an OPropertyArrayAggregationHelper filled with properties for which's initialization
109 fillProperties is called. getInfoService and getFirstAggregateId may be overwritten to determine
110 the additional parameters of the OPropertyArrayAggregationHelper.
112 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
114 /** the return value is used for the construction of the OPropertyArrayAggregationHelper.
115 Beware of the lifetime of the returned object, as it has to exist 'til the last instance
116 of this class dies.
118 virtual IPropertyInfoService* getInfoService() const { return NULL; }
120 /** the return value is used for the construction of the OPropertyArrayAggregationHelper.
122 virtual sal_Int32 getFirstAggregateId() const { return DEFAULT_AGGREGATE_PROPERTY_ID; }
125 //------------------------------------------------------------------
126 template<class TYPE>
127 sal_Int32 OPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
129 template<class TYPE>
130 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper< TYPE >::s_pProps = NULL;
132 //------------------------------------------------------------------
133 template <class TYPE>
134 OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
136 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
137 ++s_nRefCount;
140 //------------------------------------------------------------------
141 template <class TYPE>
142 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
144 OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
145 if (!s_pProps)
147 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
148 if (!s_pProps)
150 s_pProps = createArrayHelper();
151 OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
154 return s_pProps;
157 //------------------------------------------------------------------
158 template <class TYPE> inline
159 ::cppu::IPropertyArrayHelper* OAggregationArrayUsageHelper<TYPE>::createArrayHelper() const
161 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aProps;
162 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aAggregateProps;
163 fillProperties(aProps, aAggregateProps);
164 OSL_ENSURE(aProps.getLength(), "OAggregationArrayUsageHelper::createArrayHelper : fillProperties returned nonsense !");
165 return new OPropertyArrayAggregationHelper(aProps, aAggregateProps, getInfoService(), getFirstAggregateId());
168 //.........................................................................
170 //... namespace comphelper ................................................
172 #endif // _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */