Line data Source code
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 :
20 : #ifndef INCLUDED_COMPHELPER_PROPARRHLP_HXX
21 : #define INCLUDED_COMPHELPER_PROPARRHLP_HXX
22 :
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>
28 :
29 : namespace cppu {
30 : class IPropertyArrayHelper;
31 : }
32 :
33 : //... namespace comphelper ................................................
34 : namespace comphelper
35 : {
36 :
37 :
38 : namespace staruno = ::com::sun::star::uno;
39 : namespace starbeans = ::com::sun::star::beans;
40 :
41 :
42 :
43 :
44 : template <typename TYPE> struct OPropertyArrayUsageHelperMutex
45 : : public rtl::Static< ::osl::Mutex, OPropertyArrayUsageHelperMutex<TYPE> > {};
46 :
47 :
48 : template <class TYPE>
49 : class OPropertyArrayUsageHelper
50 : {
51 : protected:
52 : static sal_Int32 s_nRefCount;
53 : static ::cppu::IPropertyArrayHelper* s_pProps;
54 :
55 : public:
56 : OPropertyArrayUsageHelper();
57 0 : virtual ~OPropertyArrayUsageHelper()
58 : { // ARGHHHHHHH ..... would like to implement this after the class
59 : // definition (as we do with all other methods) but SUNPRO 5 compiler
60 : // (linker) doesn't like this
61 0 : ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
62 : OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
63 0 : if (!--s_nRefCount)
64 : {
65 0 : delete s_pProps;
66 0 : s_pProps = NULL;
67 0 : }
68 0 : }
69 :
70 : /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
71 : class, which is created if necessary.
72 : */
73 : ::cppu::IPropertyArrayHelper* getArrayHelper();
74 :
75 : protected:
76 : /** used to implement the creation of the array helper which is shared amongst all instances of the class.
77 : This method needs to be implemented in derived classes.
78 : <BR>
79 : The method gets called with Mutex acquired.
80 : <BR>
81 : as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
82 : assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
83 : @return an pointer to the newly created array helper. Must not be NULL.
84 : */
85 : virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
86 : };
87 :
88 :
89 : /** a OPropertyArrayUsageHelper which will create an OPropertyArrayAggregationHelper
90 : */
91 : template <class TYPE>
92 0 : class OAggregationArrayUsageHelper: public OPropertyArrayUsageHelper<TYPE>
93 : {
94 : protected:
95 : /** overwrite this in your derived class. initialize the two sequences with your and your aggregate's
96 : properties.
97 : <BR>
98 : The method gets called with Mutex acquired.
99 : @param _rProps out parameter to be filled with the property descriptions of your own class
100 : @param _rAggregateProps out parameter to be filled with the properties of your aggregate.
101 : */
102 : virtual void fillProperties(
103 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rProps,
104 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rAggregateProps
105 : ) const = 0;
106 :
107 : /** creates an OPropertyArrayAggregationHelper filled with properties for which's initialization
108 : fillProperties is called. getInfoService and getFirstAggregateId may be overwritten to determine
109 : the additional parameters of the OPropertyArrayAggregationHelper.
110 : */
111 : virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
112 :
113 : /** the return value is used for the construction of the OPropertyArrayAggregationHelper.
114 : Beware of the lifetime of the returned object, as it has to exist 'til the last instance
115 : of this class dies.
116 : */
117 0 : virtual IPropertyInfoService* getInfoService() const { return NULL; }
118 :
119 : /** the return value is used for the construction of the OPropertyArrayAggregationHelper.
120 : */
121 0 : virtual sal_Int32 getFirstAggregateId() const { return DEFAULT_AGGREGATE_PROPERTY_ID; }
122 : };
123 :
124 :
125 : template<class TYPE>
126 : sal_Int32 OPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
127 :
128 : template<class TYPE>
129 : ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper< TYPE >::s_pProps = NULL;
130 :
131 :
132 : template <class TYPE>
133 0 : OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
134 : {
135 0 : ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
136 0 : ++s_nRefCount;
137 0 : }
138 :
139 :
140 : template <class TYPE>
141 0 : ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
142 : {
143 : OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
144 0 : if (!s_pProps)
145 : {
146 0 : ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
147 0 : if (!s_pProps)
148 : {
149 0 : s_pProps = createArrayHelper();
150 : OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
151 0 : }
152 : }
153 0 : return s_pProps;
154 : }
155 :
156 :
157 : template <class TYPE> inline
158 0 : ::cppu::IPropertyArrayHelper* OAggregationArrayUsageHelper<TYPE>::createArrayHelper() const
159 : {
160 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aProps;
161 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aAggregateProps;
162 0 : fillProperties(aProps, aAggregateProps);
163 : OSL_ENSURE(aProps.getLength(), "OAggregationArrayUsageHelper::createArrayHelper : fillProperties returned nonsense !");
164 0 : return new OPropertyArrayAggregationHelper(aProps, aAggregateProps, getInfoService(), getFirstAggregateId());
165 : }
166 :
167 :
168 : }
169 : //... namespace comphelper ................................................
170 :
171 : #endif // INCLUDED_COMPHELPER_PROPARRHLP_HXX
172 :
173 :
174 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|